Line follower proportional py v2: Difference between revisions

From wikiluntti
(Created page with "<languages /> <translate> == Introduction == === Robot === The idea and principle works for almost any robot thought this is tested using Asimov. === Sensors === The color...")
 
(Marked this version for translation)
 
(2 intermediate revisions by the same user not shown)
Line 3: Line 3:
<translate>
<translate>


== Introduction ==
== Introduction == <!--T:1-->


=== Robot ===
=== Robot === <!--T:2-->
 
<!--T:3-->
The idea and principle works for almost any robot thought this is tested using Asimov.
The idea and principle works for almost any robot thought this is tested using Asimov.


=== Sensors ===
=== Sensors === <!--T:4-->
 
<!--T:5-->
The color sensor in reflected light intensity mode is used.
The color sensor in reflected light intensity mode is used.
The sensor convention is
The sensor convention is
Line 16: Line 20:
#port 4 = infrared or ultrasonic
#port 4 = infrared or ultrasonic


== An Illuminating Example ==
== An Illuminating Example == <!--T:6-->
 
<!--T:7-->
<youtube>q8XD_El4DEI</youtube>
<youtube>q8XD_El4DEI</youtube>


== Theory ==
== Theory == <!--T:8-->
 
<!--T:9-->
The proportional line follower actually follows the other side of the line. The turning radius is calculated using the minimum, maximum and current color sensor readings. Also, a proportional coefficient (P) is introduced. It should be noted that the values of the steering function needs be between -100 and +100. Thus, we employ Python Max and Min function.
The proportional line follower actually follows the other side of the line. The turning radius is calculated using the minimum, maximum and current color sensor readings. Also, a proportional coefficient (P) is introduced. It should be noted that the values of the steering function needs be between -100 and +100. Thus, we employ Python Max and Min function.


== An Example Code ==
== An Example Code == <!--T:10-->
 
<!--T:11-->
<syntaxhighlight lang="python">
<syntaxhighlight lang="python">
#!/usr/bin/env python3
#!/usr/bin/env python3
# https://sites.google.com/site/ev3devpython/
# https://sites.google.com/site/ev3devpython/


<!--T:12-->
#Sensor port convention:
#Sensor port convention:
#port 3 = color
#port 3 = color
Line 33: Line 44:
#30 is Min
#30 is Min


<!--T:13-->
from ev3dev2.sensor.lego import ColorSensor
from ev3dev2.sensor.lego import ColorSensor
from ev3dev2.motor import MoveSteering, OUTPUT_B, OUTPUT_C
from ev3dev2.motor import MoveSteering, OUTPUT_B, OUTPUT_C
Line 38: Line 50:
import os
import os


<!--T:14-->
os.system('setfont Lat15-TerminusBold32x16')  
os.system('setfont Lat15-TerminusBold32x16')  


<!--T:15-->
steer_pair = MoveSteering(OUTPUT_B, OUTPUT_C)
steer_pair = MoveSteering(OUTPUT_B, OUTPUT_C)
steer_pair.on(steering=0, speed=10)
steer_pair.on(steering=0, speed=10)


<!--T:16-->
cl = ColorSensor()  
cl = ColorSensor()  
clMax = 84
clMax = 84
Line 49: Line 64:
P = 2.0
P = 2.0


<!--T:17-->
clN = clAve
clN = clAve
steering = 0
steering = 0


<!--T:18-->
while True:
while True:
     clN = cl.reflected_light_intensity
     clN = cl.reflected_light_intensity
Line 62: Line 79:
     steer_pair.on(steering=steering, speed=20)
     steer_pair.on(steering=steering, speed=20)


<!--T:19-->
steer_pair.off()
steer_pair.off()
sleep(5)
sleep(5)
Line 67: Line 85:
<youtube>6j8KsGABPdU</youtube>
<youtube>6j8KsGABPdU</youtube>


== Exercises ==
== Exercises == <!--T:20-->
 
<!--T:21-->
1. It is difficult to debug the robot as it is silent. Let it say if it is in dark or light area. Use [https://python-ev3dev.readthedocs.io/en/ev3dev-stretch/sensors.html#color-sensor Sound.speak('White').wait()] command or e.g. sound.beep() command. Sound is imported using import Sound command.
1. It is difficult to debug the robot as it is silent. Let it say if it is in dark or light area. Use [https://python-ev3dev.readthedocs.io/en/ev3dev-stretch/sensors.html#color-sensor Sound.speak('White').wait()] command or e.g. sound.beep() command. Sound is imported using import Sound command.


<!--T:22-->
2. Make the robot move faster. Note that you need to change the parameters according to your line to follow (and robot). Generally, it is advised to change only one value at time. Time your original time and try to make it half.
2. Make the robot move faster. Note that you need to change the parameters according to your line to follow (and robot). Generally, it is advised to change only one value at time. Time your original time and try to make it half.


<!--T:23-->
3. Let the robot use the other side of the line.
3. Let the robot use the other side of the line.


<!--T:24-->
4. Now the while loop is forever. Make the robot stop when the right has turned 3.4 revolutions. See [https://sites.google.com/site/ev3devpython/learn_ev3_python/using-motors ev3 Python] for help.
4. Now the while loop is forever. Make the robot stop when the right has turned 3.4 revolutions. See [https://sites.google.com/site/ev3devpython/learn_ev3_python/using-motors ev3 Python] for help.


<!--T:25-->
5. Let the robot end when it encounters a silver tape (highly shiny).
5. Let the robot end when it encounters a silver tape (highly shiny).


<!--T:26-->
6. The Rescue is robot game such that it needs to follow the dashed line. So, make a line with a segment missing, but make your robot to still overlap the missing segment and follow the line on the other side of the missing segment.
6. The Rescue is robot game such that it needs to follow the dashed line. So, make a line with a segment missing, but make your robot to still overlap the missing segment and follow the line on the other side of the missing segment.






<!--T:27-->
[[Category: Python v2]]
[[Category: Python v2]]
[[Category: Line follower]]
[[Category: Line follower]]
[[Category: Color sensor py v2]]
[[Category: Color sensor py v2]]
[[Category: Asimov]]
[[Category: Asimov]]
== About == <!--T:28-->
<!--T:29-->
This course is supported by [https://meet-and-code.org/ Meet and Code]. The course is made in collaboration with [http://www.fllsuomi.org/ Robotiikka- ja tiedekasvatus ry].
[[File:MeetAndcodeLogo.png|thumb]]


</translate>
</translate>
[[Meet_and_Code_2020_II:_Python | Meet and Code II: Python]]

Latest revision as of 16:48, 23 September 2020

<languages />

<translate>

Introduction

Robot

The idea and principle works for almost any robot thought this is tested using Asimov.

Sensors

The color sensor in reflected light intensity mode is used. The sensor convention is

  1. port 1 = touch,
  2. port 2 = gyro,
  3. port 3 = color,
  4. port 4 = infrared or ultrasonic

An Illuminating Example

Theory

The proportional line follower actually follows the other side of the line. The turning radius is calculated using the minimum, maximum and current color sensor readings. Also, a proportional coefficient (P) is introduced. It should be noted that the values of the steering function needs be between -100 and +100. Thus, we employ Python Max and Min function.

An Example Code

#!/usr/bin/env python3
# https://sites.google.com/site/ev3devpython/

<!--T:12-->
#Sensor port convention:
#port 3 = color
#port 1 = touch, port 2 = gyro, port 3 = color, port 4 = infrared or ultrasonic.
#84 is Max
#30 is Min

<!--T:13-->
from ev3dev2.sensor.lego import ColorSensor
from ev3dev2.motor import MoveSteering, OUTPUT_B, OUTPUT_C
from time import sleep
import os

<!--T:14-->
os.system('setfont Lat15-TerminusBold32x16') 

<!--T:15-->
steer_pair = MoveSteering(OUTPUT_B, OUTPUT_C)
steer_pair.on(steering=0, speed=10)

<!--T:16-->
cl = ColorSensor() 
clMax = 84
clMin = 30
clAve = (clMax + clMin)/2
P = 2.0

<!--T:17-->
clN = clAve
steering = 0

<!--T:18-->
while True:
    clN = cl.reflected_light_intensity
    #print( clN )
    #print( clAve )
    steering = P*( clN - clAve )
    steering = min(steering, 100)
    steering = max(steering, -100)
    print( steering )
    steer_pair.on(steering=steering, speed=20)

<!--T:19-->
steer_pair.off()
sleep(5)

Exercises

1. It is difficult to debug the robot as it is silent. Let it say if it is in dark or light area. Use Sound.speak('White').wait() command or e.g. sound.beep() command. Sound is imported using import Sound command.

2. Make the robot move faster. Note that you need to change the parameters according to your line to follow (and robot). Generally, it is advised to change only one value at time. Time your original time and try to make it half.

3. Let the robot use the other side of the line.

4. Now the while loop is forever. Make the robot stop when the right has turned 3.4 revolutions. See ev3 Python for help.

5. Let the robot end when it encounters a silver tape (highly shiny).

6. The Rescue is robot game such that it needs to follow the dashed line. So, make a line with a segment missing, but make your robot to still overlap the missing segment and follow the line on the other side of the missing segment.

About

This course is supported by Meet and Code. The course is made in collaboration with Robotiikka- ja tiedekasvatus ry.

</translate>

Meet and Code II: Python