Upload files to 'Follow_the_line/Programs '

This commit is contained in:
Tanja Sukal 2023-08-08 11:35:01 +00:00
parent dd6c4121eb
commit 6182df3fb6
3 changed files with 145 additions and 0 deletions

View File

@ -0,0 +1,63 @@
#!/usr/bin/env python3
# Version 1.0 from 02/08/23
from time import sleep
import ev3dev2 as ev3
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.lego import ColorSensor
from ev3dev2.led import Leds
# Define all inputs and outputs of the Robot
# Sensors are inputs
cs_right = ColorSensor(INPUT_4)
cs_left = ColorSensor(INPUT_2)
# Motors are outputs
m_left = Motor(OUTPUT_B)
m_right = Motor(OUTPUT_C)
leds = Leds()
# 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_left.polarity = 'inversed'
# Defined inputs for the given experiment / enviroment
line_color = 1 # Color of the given line is black
speed_percent_alone = 60 # speed percentage when only one motor rotates (highest possible value 100%)
speed_percent_both = 30 # speed percentage when both motors rotate (should be less than speed_percent_alone)
# while-loop: the robot drives as long as the programm is not stopped
while True:
# if-functions: for different actions when the Colorsensors detect the defined line-color
# after every function is a sleep set because it is enough to give the motors signals every 100 ms
# when the colorsensors detect black the function: .color is equal to 1 (one)
if cs_right.color == line_color and cs_left.color != line_color: #if the right coloursensor detects the defined line-color, the robot drives faster to the right
leds.set_color("LEFT", "GREEN") # the left led lights green
leds.set_color("RIGHT", "AMBER") # the right led (side which detected black) lights amber
m_left.on(SpeedPercent(speed_percent_alone))
m_right.on(SpeedPercent(0))
sleep(0.1) # the sleep function is given in seconds (0.1 s = 100 milliseconds)
elif cs_left.color == line_color and cs_right.color != line_color : #if the left coloursensor detects the defined line-color, the robot drives faster to the left
leds.set_color("LEFT", "AMBER") # the left led (side which detected black) lights amber
leds.set_color("RIGHT", "GREEN") # the right led lights green
m_left.on(SpeedPercent(0))
m_right.on(SpeedPercent(speed_percent_alone))
sleep(0.1)
elif cs_left.color == line_color and cs_right.color == line_color : #if both coloursensors detect the defined line-color, the robot stops because it has no direction
m_right.stop()
m_left.stop()
leds.set_color("LEFT", "RED") # both leds light red
leds.set_color("RIGHT", "RED")
sleep(0.1)
else: #if no coloursensor detects the defined line-color, the robot drives straight ahead
leds.set_color("LEFT", "GREEN") # both leds light green
leds.set_color("RIGHT", "GREEN")
m_right.on(SpeedPercent(speed_percent_both))
m_left.on(SpeedPercent(speed_percent_both))
sleep(0.1)

View File

@ -0,0 +1,61 @@
#!/usr/bin/env python3
from time import sleep
import ev3dev2 as ev3
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.lego import ColorSensor
from ev3dev2.led import Leds
# Define all inputs and outputs of the Robot
# Sensors are inputs
cs_right = ColorSensor(INPUT_4)
cs_left = ColorSensor(INPUT_2)
# Motors are outputs
m_left = Motor(OUTPUT_B)
m_right = Motor(OUTPUT_C)
leds = Leds()
# 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_left.polarity = 'inversed'
# Defined inputs for the given experiment / enviroment
Line = 1 # min. reflection meassured of line
Floor = 30 # max. reflection meassured of floor
max_speed_percent = 50 # maximum wanted speed percentage (highest possible value 100%)
# calculate range
range = (Floor - Line)
# while-loop: the robot drives as long as the programm is not stopped
while True:
# the detected intensity of the two colorsensors are measured
cs_r_intensity = cs_right.reflected_light_intensity
cs_l_intensity = cs_left.reflected_light_intensity
# calculate percentage of intensity from given range
cs_r_percent = (cs_r_intensity - Line) / range
cs_l_percent = (cs_l_intensity - Line) / range
# calculation percantage of driving speed, depending on maximum speed percentage
m_l_speed_percent= max_speed_percent*cs_l_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
if m_l_speed_percent > 100:
m_l_speed_percent = 0
leds.set_color("LEFT", "AMBER")
else:
leds.set_color("LEFT", "GREEN")
if m_r_speed_percent > 100:
m_r_speed_percent = 0
leds.set_color("RIGHT", "AMBER")
else:
leds.set_color("RIGHT", "GREEN")
# the motors drive with the calculated speed
m_left.on(SpeedPercent(m_l_speed_percent))
m_right.on(SpeedPercent(m_r_speed_percent))
sleep(0.3)

View File

@ -0,0 +1,21 @@
#!/usr/bin/env python3
# Version 1.0 from 02/08/23
from time import sleep
import ev3dev2 as ev3
from ev3dev2.sensor import INPUT_1,INPUT_2,INPUT_3,INPUT_4
from ev3dev2.sensor.lego import ColorSensor
# Define the sensor inputs of the robot
cs_right = ColorSensor(INPUT_4)
cs_left = ColorSensor(INPUT_2)
while True:
# printing the values of the right colour sensor
print('Right-Sensor: ','Intensity=', cs_right.reflected_light_intensity,' Color=',cs_right.color)
# printing the values of the left colour sensor
print('Left-Sensor: ','Intensity=', cs_left.reflected_light_intensity,' Color=',cs_left.color)
sleep(1) #the values are printed every second