Shop Learn
Need help with a code for using cyberbot(microbit) Whiskers — Parallax Forums

Need help with a code for using cyberbot(microbit) Whiskers

HewedyoHewedyo Posts: 1
edited 2021-11-03 12:49 in Robotics

So I have this code currently but I need to figure out how to code to make a left turn if it continues to move forward for 5 seconds without hitting anything. The robot is going to be put angled towards the left side wall and it will need to make its way out of the door way which is also on the left side of the wall. I have tried a counter method like the one used in escaping corners tutorial and it didn't work out can anyone tell me how to do this last part?

from cyberbot import *
def forward():
    bot(18).servo_speed(-75)
    bot(19).servo_speed(75)


def backwards():
    bot(18).servo_speed(75)
    bot(19).servo_speed(-75)
    sleep(250)

def right():
    bot(18).servo_speed(0)
    bot(19).servo_speed(75)
    sleep(800)

def left():
    bot(18).servo_speed(-75)
    bot(19).servo_speed(0)
    sleep(800)

def stop():
    bot(18).servo_speed(0)
    bot(19).servo_speed(0)
    sleep(250)

counter = 0
while True:
    wl= bot(8).read_digital()
    wr= bot(10).read_digital()

    if( wl== 1 and wr ==0):
        counter = counter + 1
        if (counter > 5):
            stop()
            left()

    if wl==0 and wr==0:
        stop()
        backwards()
        right()
    elif wl== 1 and wr==0:
        stop()
        backwards()
        left()
    elif wl== 0 and wr==1:
        stop()
        backwards()
        right()
    else:
        forward()

Comments

  • ercoerco Posts: 20,031

    Hewedyo: OMG I'm surprised no one has responded. This is usually a very active forum, often it's a matter of an hour, not weeks to get a reply.

    I'm sorry I can't help since I don't have a Cyberbot to walk you through it. Perhaps I need to get one, because you are at least the second person I've seen asking for help here.

    I hope someone else chimes in here soon.

  • In the stop() routine, or just before you call stop() in each of the if and elif sections , do you need to reset the counter to 0 ? Otherwise you will always exceed the count of 5 in any condition.

    And maybe after the forward command you'd want to pause for 1 second ? Or perhaps that could be bad for responsiveness, but at least you might need to have some sort of clock reference that you use to determine when 5 sections has expired ? If the python libraries don't include some sort of timer, then you could add a short pause after each forward() call, and increase the counter to make it trigger at 5 seconds. The problem with 1 second (1000 millisecond) pauses is they hold up the program flow for too long, but you could use shorter pauses and increase the counter pro-rata.

    For example, add a pause of 100 milliseconds (which could be small enough to not interfere with the driving), and then multiply the counter by 10, ie. count to 50 instead of 5. That should get you the trigger around 5 seconds. It doesn't take account of the time spent calling the code, but should be in the ballpark for fine tuning.

    If that 100 milliseconds pause is still too long, reduce it to 10 milliseconds, and make the counter trigger at 500.

Sign In or Register to comment.