Update 'Follow_the_line/Programs /Follow_line_intensity_reflected_light.py'

This commit is contained in:
Tanja Sukal 2024-09-02 14:50:14 +00:00
parent 5451bfe835
commit 8dd0490191

View File

@ -1,61 +1,61 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
from time import sleep from time import sleep
import ev3dev2 as ev3 import ev3dev2 as ev3
from ev3dev2.motor import Motor, SpeedPercent, OUTPUT_A, OUTPUT_B, OUTPUT_C, OUTPUT_D from ev3dev2.motor import Motor, SpeedPercent, OUTPUT_A, OUTPUT_B, OUTPUT_C, OUTPUT_D
from ev3dev2.sensor import INPUT_1,INPUT_2,INPUT_3,INPUT_4 from ev3dev2.sensor import INPUT_1,INPUT_2,INPUT_3,INPUT_4
from ev3dev2.sensor.lego import ColorSensor from ev3dev2.sensor.lego import ColorSensor
from ev3dev2.led import Leds from ev3dev2.led import Leds
# Define all inputs and outputs of the Robot # Define all inputs and outputs of the Robot
# Sensors are inputs # Sensors are inputs
cs_right = ColorSensor(INPUT_4) cs_right = ColorSensor(INPUT_4)
cs_left = ColorSensor(INPUT_2) cs_left = ColorSensor(INPUT_2)
# Motors are outputs # Motors are outputs
m_left = Motor(OUTPUT_A) m_left = Motor(OUTPUT_A)
m_right = Motor(OUTPUT_D) m_right = Motor(OUTPUT_D)
leds = Leds() leds = Leds()
# The motor is initialized to run counter-clockwise (gegen den Uhrzeigersinn) # The motor is initialized to run counter-clockwise (gegen den Uhrzeigersinn)
m_right.polarity = 'inversed' # use 'normal' to initialize it to run clockwise (im Uhrzeigersinn) m_right.polarity = 'inversed' # use 'normal' to initialize it to run clockwise (im Uhrzeigersinn)
m_left.polarity = 'inversed' m_left.polarity = 'inversed'
# Defined inputs for the given experiment / enviroment # Defined inputs for the given experiment / enviroment
Line = 3 # min. reflection meassured of line Line = 2 # min. reflection meassured of line
Floor = 35 # max. reflection meassured of floor Floor = 30 # max. reflection meassured of floor
max_speed_percent = 40 # maximum wanted speed percentage (highest possible value 100%) max_speed_percent = 45 # maximum wanted speed percentage (highest possible value 100%)
# calculate range # calculate range
range = (Floor - Line) range = (Floor - Line)
# while-loop: the robot drives as long as the programm is not stopped # while-loop: the robot drives as long as the programm is not stopped
while True: while True:
# the detected intensity of the two colorsensors are measured # the detected intensity of the two colorsensors are measured
cs_r_intensity = cs_right.reflected_light_intensity cs_r_intensity = cs_right.reflected_light_intensity
cs_l_intensity = cs_left.reflected_light_intensity cs_l_intensity = cs_left.reflected_light_intensity
# calculate percentage of intensity from given range # calculate percentage of intensity from given range
cs_r_percent = (cs_r_intensity - Line) / range cs_r_percent = (cs_r_intensity - Line) / range
cs_l_percent = (cs_l_intensity - Line) / range cs_l_percent = (cs_l_intensity - Line) / range
# calculation percantage of driving speed, depending on maximum speed percentage # calculation percantage of driving speed, depending on maximum speed percentage
m_l_speed_percent= max_speed_percent*cs_l_percent m_l_speed_percent= max_speed_percent*cs_l_percent
m_r_speed_percent= max_speed_percent*cs_r_percent m_r_speed_percent= max_speed_percent*cs_r_percent
# make sure percentage is not higher than possible (100%) and signal it through lights # make sure percentage is not higher than possible (100%) and signal it through lights
if m_l_speed_percent > 100: if m_l_speed_percent > 100:
m_l_speed_percent = 0 m_l_speed_percent = 0
leds.set_color("LEFT", "AMBER") leds.set_color("LEFT", "AMBER")
else: else:
leds.set_color("LEFT", "GREEN") leds.set_color("LEFT", "GREEN")
if m_r_speed_percent > 100: if m_r_speed_percent > 100:
m_r_speed_percent = 0 m_r_speed_percent = 0
leds.set_color("RIGHT", "AMBER") leds.set_color("RIGHT", "AMBER")
else: else:
leds.set_color("RIGHT", "GREEN") leds.set_color("RIGHT", "GREEN")
# the motors drive with the calculated speed # the motors drive with the calculated speed
m_left.on(SpeedPercent(m_l_speed_percent)) m_left.on(SpeedPercent(m_l_speed_percent))
m_right.on(SpeedPercent(m_r_speed_percent)) m_right.on(SpeedPercent(m_r_speed_percent))
sleep(0.3) sleep(0.3)