Update 'Follow_the_line/Programs /Follow_line_color_recognition.py'

This commit is contained in:
Tanja Sukal 2023-08-08 11:53:24 +00:00
parent 30d2c09500
commit 8dacac01b4

View File

@ -1,63 +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)
#!/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_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)