0

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()
user3577397
  • 453
  • 3
  • 12
  • 27
  • Is that your actual indentation? What you have written would never exit the `while True:` – quamrana Oct 12 '14 at 17:10
  • I corrected the indentation in my editor. But it still doesn't work. – user3577397 Oct 12 '14 at 17:12
  • So can you correct your question so that we see what your running program sees? – quamrana Oct 12 '14 at 17:13
  • I'll edit my question and put the code I changed it to. I got rid of while true: And now I'm just trying to do it with the center (getObstacle(1)) sensor. The robot moves, but it won't stop. – user3577397 Oct 12 '14 at 17:16
  • Looking at the Myro documentation it looks like `forward` takes two parameters. So what does `forward(0.5)` do? – quamrana Oct 12 '14 at 17:58
  • Yes, that's true. forward(0.5) specifies the speed. Generally, it will be forward(speed, time). So you specify the speed, 0 to 1, and the time in seconds, which could be any range. The thing is, I don't want to specify the second parameter because I want the robot to stop when it senses a wall, not stop when the amount of seconds I specified runs out, which is what the second parameter is. – user3577397 Oct 12 '14 at 18:10
  • So I think I understand your problem and your requirements. See my answer. – quamrana Oct 12 '14 at 21:11

2 Answers2

0

& 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()
Dica
  • 165
  • 2
  • Just tried this. Problem is, for some weird reason my obstacle sensor is no longer working. I have no idea what happened. When I write 'sensors' it shows 0 for my obstacle values. It worked yesterday... – user3577397 Oct 12 '14 at 17:01
0

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.

quamrana
  • 37,849
  • 12
  • 53
  • 71
  • I appreciate the help. Right now my robot is out of batteries, so they are recharging. But I tried it in Calico's simulation, and it doesn't stop near the wall. It could just be the simulation, but as of right now, it doesn't appear to work. Once my batteries are fully recharged I'll test again and report back. – user3577397 Oct 12 '14 at 21:29
  • So far I can only run a simulation, but it seems to still run into the wall in the simulation, and it goes very slow at 0.1, so I changed it to 0.5. Maybe because I changed the time in forward to 0.5 it's screwing the program up? The simulation isn't 100% accurate, so this code could work. I just need these batteries to charge.. I appreciate the help tho. – user3577397 Oct 12 '14 at 22:07