Hello! I am trying to write an on/off style line following robot. I have tried various logics but none seem to work. Here’s what I’ve tried so far; I would really appreciate any help debugging!
#this part is common between the two versions
threshold = 0.7
value_right = reflectance.get_right()
value_left = reflectance.get_left()
def is_over_line():
if value_right>threshold:
return True
else:
return False
Version 1:
def follow_line():
value_right = reflectance.get_right()
while True:
if value_right<threshold:
#robot on right side of line, so needs to turn left
drivetrain.set_speed(6,8)
elif value_right>threshold:
#robot on left side of line, so needs to turn right
drivetrain.set_speed(8,6)
else:
drivetrain.set_speed(6,6)
time.sleep(0.1)
print("right value = " + str(value_right))
print("left value = " + str(value_left))
Version 2:
def follow_line():
value_right = reflectance.get_right()
while True:
if is_over_line()==False:
drivetrain.set_speed(6,4)
else:
drivetrain.set_speed(4,6)
follow_line()
I strongly prefer your version 2, but there are two things that jump out:
Issue 1) In Python, the = operator assigns the value of the right-hand side to the variable (or variables) on the left-hand side. That variable then contains the value as it was at that moment. Until the variable is assigned a different value, it will hold the same value. If you want it to change over time, you need to assign to it over and over. Do you do that? (This issue affects both your Version 1 and your Version 2)
Issue 2) I’m going to guess that you don’t know about “scopes” and something called “variable hiding”, so I’m just going to give you the answer for this issue in Version 2:
You have to change is_over_line() so that it has a “parameter.” (change def is_over_line() to def is_over_line(value_right) ) and you have to change your Version 2 follow_line() function by changing the call is_over_line() to is_over_line(value_right) .
You’ll have to fix both issues to see a difference in behavior!
—
Once you’ve fixed Issues 1 and 2, you don’t need to define value_right and value_left in the # this part is common block. You actually should delete them from that section! The variables you declare there (threshold, value_right, and value_left) become global variables and your global value_right actually causes the “variable hiding” Issue 2.
In general, you should avoid global variables. threshold is okay as a global variable because it is a “constant” – a “variable” that is only assigned to once. Even better would be to use the “Pythonic” way to name constants: all capitals with underscores. So threshold could become THRESHOLD. The all-caps makes it easier to avoid the confusion that global values can easily cause.
PS: In addition to “what you’ve tried” (your V1 and V2 code), you should also say what you expected to happen and what you observed. Did the robot move at all? Do your print statements write to the console/log? Are you sure the program is running on the robot at all (can you turn on the LED? With robots, turning LEDs on and off is often a good way to see “did this code even execute?”)