Reputation: 473
I'm using a scribbler robot and writing code in Python. I'm trying to get it to stop when it sees an obstacle
So I created variables for the left obstacle sensor, the center obstacle sensor and the right obstacle sensor
left = getObstacle(0)
center = getObstacle(1)
right = getObstacle(2)
Then an if statement
if (left < 6400 & center < 6400 & right < 6400):
forward(1,1)
else:
stop()
Basically the idea is if the sensors read less than 6400, it should move forward, otherwise, it should stop. When testing the scribbler with the senses
function, I noticed when I put the robot close to an object, it would read around 6400.
Here's what I have for the main()
code
def main():
while True:
left = getObstacle(0)
center = getObstacle(1)
right = getObstacle(2)
lir = getIR(0)
rir = getIR(1)
if (left < 6400 & center < 6400 & right < 6400):
forward(1,1)
else:
stop()
Why isn't my robot responding? The Python code doesn't show any errors when I put it into the shell, but nothing is happening with my robot.
EDIT:
Some code changes. So far the robot will move, but it won't stop. Are my if and else statements incorrect?
center = getObstacle(1)
def main():
if (center < 5400):
forward(0.5)
else:
stop()
Upvotes: 0
Views: 3100
Reputation: 39354
It sounds like you were close with your original code:
def main():
while True:
left = getObstacle(0)
center = getObstacle(1)
right = getObstacle(2)
#lir = getIR(0)
#rir = getIR(1)
if (left < 6400 and center < 6400 and right < 6400):
forward(1, 0.1)
else:
stop()
break
This loop both measures left
, center
and right
and performs the comparisons each time round the loop. I have modified the call to forward
to only move for one tenth of a second before making more measurements. Also when the condition is not satisfied both the robot and the loop are stopped.
btw it seems you don't use lir
and rir
.
Upvotes: 0
Reputation: 165
&
is the Bitwise operator
and
is the logical AND operator
So your condition should be :
if (left < 6400 and center < 6400 and right < 6400):
forward(1,1)
else:
stop()
Upvotes: 0