Skip to content
Snippets Groups Projects
Commit 49c12fec authored by ft-Demo's avatar ft-Demo
Browse files

commit by robo pro coding

parents
No related branches found
No related tags found
No related merge requests found
{"name":"Buggy_Line_Follower_with_P_Controller","mode":"ADVANCED","uuid":"d5afebef-2128-72cb-331e-53289dcee298"}
\ No newline at end of file
This diff is collapsed.
from lib.controller import *
import time
from fischertechnik.controller.Motor import Motor
import math
from lib.camera import *
from numbers import Number
speed = None
index = None
kp = None
deviation = None
line_color = None
speed_alignment = None
last_deviation = None
change = None
speed_left = None
speed_right = None
def lines_callback(event):
global speed, index, kp, deviation, line_color, speed_alignment, last_deviation, change, speed_left, speed_right
index = 1
while index <= len(event.value):
line_color = event.value[int(index-1)].color.rgb
if line_color[0] < 100 and line_color[1] < 100 and line_color[2] < 100:
deviation = event.value[int(index-1)].position
break
index = (index if isinstance(index, Number) else 0) + 1
line_detector.add_detection_listener(lines_callback)
def log_data():
global speed, index, kp, deviation, line_color, speed_alignment, last_deviation, change, speed_left, speed_right
print(''.join([str(x) for x in [deviation, ', ', speed_alignment]]))
speed = 350
kp = 1.85
deviation = 0
last_deviation = deviation
TXT_O5_led.set_brightness(512)
while True:
if (deviation != 0):
break
time.sleep(0.010)
TXT_M1_encodermotor.set_speed(int(speed), Motor.CCW)
TXT_M2_encodermotor.set_speed(int(speed), Motor.CCW)
TXT_M1_encodermotor.start_sync(TXT_M2_encodermotor)
while True:
change = deviation - last_deviation
last_deviation = deviation
if change != 0:
speed_alignment = round(deviation * kp)
if speed_alignment > 0:
speed_left = speed
speed_right = speed - min([speed_alignment, speed])
elif speed_alignment < 0:
speed_left = speed + max([speed_alignment, -speed])
speed_right = speed
else:
speed_left = speed
speed_right = speed
TXT_M1_encodermotor.set_speed(int(speed_left), Motor.CCW)
TXT_M1_encodermotor.start_sync()
TXT_M2_encodermotor.set_speed(int(speed_right), Motor.CCW)
TXT_M2_encodermotor.start_sync()
log_data()
time.sleep(0.02)
# auto generated content from camera configuration
from lib.controller import *
import fischertechnik.factories as txt_factory
TXT_USB_1_camera.set_width(320)
TXT_USB_1_camera.set_height(240)
TXT_USB_1_camera.set_fps(15)
TXT_USB_1_camera.start()
line_detector = txt_factory.camera_factory.create_line_detector(0, 60, 320, 20, 30, 100, -100, 100, 3)
TXT_USB_1_camera.add_detector(line_detector)
<xml type="camera" verion="1" width="320" height="240" fps="15" version="2"><item id="2" class="LineDetector"><name>line_detector</name><min_line_width>30</min_line_width><max_line_width>100</max_line_width><start_range_value>-100</start_range_value><end_range_value>100</end_range_value><number_of_lines>3</number_of_lines><geometry><x>0</x><y>60</y><width>320</width><height>20</height></geometry></item></xml>
\ No newline at end of file
<xml xmlns="https://developers.google.com/blockly/xml" version="9">
<block type="txt4_controller" id="v}Wf^VN(H6-Q3:$II]KJ" x="86" y="69">
<mutation>
<arg type="Input" name="Input-0" value="-"/>
<arg type="Output" name="Output-0" value="O5"/>
<arg type="Output" name="Output-1" value="-"/>
<arg type="Motor" name="Motor-0" value="M1"/>
<arg type="Motor" name="Motor-1" value="M2"/>
<arg type="Motor" name="Motor-2" value="-"/>
<arg type="Servomotor" name="Servomotor-0" value="-"/>
<arg type="Counter" name="Counter-0" value="-"/>
<arg type="I2C" name="I2C-0" value="-"/>
<arg type="USB" name="USB-0" value="USB_1"/>
<arg type="USB" name="USB-1" value="-"/>
</mutation>
<field name="mode">0</field>
<field name="controller_name">TXT</field>
<field name="Input-0">-</field>
<field name="Output-0">O5</field>
<field name="Output-1">-</field>
<field name="Motor-0">M1</field>
<field name="Motor-1">M2</field>
<field name="Motor-2">-</field>
<field name="Servomotor-0">-</field>
<field name="Counter-0">-</field>
<field name="I2C-0">-</field>
<field name="USB-0">USB_1</field>
<field name="USB-1">-</field>
<value name="Output-0">
<block type="led" id="]Wq5=g5:oL3DUYw`fO#7"/>
</value>
<value name="Motor-0">
<block type="encodermotor" id="WpKi`VJ3XVuCOUuTc^=w"/>
</value>
<value name="Motor-1">
<block type="encodermotor" id="7E{.a)tBHsGHIm^uK-lc"/>
</value>
<value name="USB-0">
<block type="camera" id="?NXT}ke#b0b2ak*:{$?B"/>
</value>
</block>
</xml>
\ No newline at end of file
import fischertechnik.factories as txt_factory
txt_factory.init_controller_factory()
txt_factory.init_output_factory()
txt_factory.init_motor_factory()
txt_factory.init_usb_factory()
txt_factory.init_camera_factory()
TXT = txt_factory.controller_factory.create_graphical_controller()
TXT_O5_led = txt_factory.output_factory.create_led(TXT, 5)
TXT_M1_encodermotor = txt_factory.motor_factory.create_encodermotor(TXT, 1)
TXT_M2_encodermotor = txt_factory.motor_factory.create_encodermotor(TXT, 2)
TXT_USB_1_camera = txt_factory.usb_factory.create_camera(TXT, 1)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment