Pololu Robotics & Electronics
Menu
My account Comments or questions? About Pololu Contact Ordering information Distributors

Pololu Forum

Best practice for turning the balboa? Should I use the encoder or drive_ticks?


#1

Hello

The following code runs fine…

@app.route("/drive_test")
def drive_test():
    # The following is a VL6180X sensor attached via the I2C bus
    tof_distance = tof_sensor.get_distance()
    # while no object is detected 
    while (tof_distance > 254):
        # drive forwards
        drive(-10,-10)
        # and take another reading
        tof_distance = tof_sensor.get_distance()
    #if you detect an object, stop
    drive(0,0)
    #then turn slowly, for 0.5 seconds
    drive(10,-10)
    # pirouettes forever! Never executes the following sleep function
    time.sleep(0.5)
    drive(-10,-10)
    tof_distance = tof_sensor.get_distance()

Until the time.sleep(0.5). I’m trying to make the Balboa turn through 90 degrees to the left or right but there’s no time argument available to pass to the balancer.drive(int, int) call to tell the motors to spin for a fixed length of time before stopping.

Is there a better way to do this? Should I use short for loops to adjust the speed of the motor to get it to turn in an orderly and measurable way?

Many thanks


#2

Could you try adding some debugging output by printing some short messages before and after time.sleep(0.5)? That should help narrow down where the program is getting stuck.

Kevin


#3

Hi again Kevin,

Many thanks for the suggestion. I added the following basic logging:

import logging

@app.route("/drive_test")
def drive_test():
    tof_distance = tof_sensor.get_distance()
    while (tof_distance > 254):
        drive(-10,-10)
        tof_distance = tof_sensor.get_distance()
    logging.debug("Detected object")
    drive(0,0)
    logging.debug("Just stopped")
    drive(10,-10)
    logging.debug("Turning round through 10,-10")
    time.sleep(0.2)
    logging.debug("Just slept for 0.2 seconds")
    drive(-10,-10)
    logging.debug("Driving forwards on -10,-10")
    tof_distance = tof_sensor.get_distance()
    logging.debug("Taking VL6180X sensor reading")

and the output looks like this:

(snipped)
2019-02-09 09:14:51,343 192.168.1.137 - - [09/Feb/2019 09:14:51] "GET /status.json HTTP/1.1" 200 -
2019-02-09 09:14:51,614 192.168.1.137 - - [09/Feb/2019 09:14:51] "GET /status.json HTTP/1.1" 200 -
2019-02-09 09:14:55,415 Detected object
2019-02-09 09:14:55,416 Just stopped
2019-02-09 09:14:55,419 Turning round through 10,-10
2019-02-09 09:14:55,504 192.168.1.137 - - [09/Feb/2019 09:14:55] "GET /drive_test HTTP/1.1" 500 -
2019-02-09 09:14:55,629 192.168.1.137 - - [09/Feb/2019 09:14:55] "GET /status.json HTTP/1.1" 200 -
2019-02-09 09:14:55,634 127.0.0.1 - - [09/Feb/2019 09:14:55] "GET /heartbeat/1 HTTP/1.1" 200 -
2019-02-09 09:14:55,652 127.0.0.1 - - [09/Feb/2019 09:14:55] "GET /heartbeat/0 HTTP/1.1" 200 -
2019-02-09 09:14:55,885 192.168.1.137 - - [09/Feb/2019 09:14:55] "GET /status.json HTTP/1.1" 200 -
2019-02-09 09:14:56,147 192.168.1.137 - - [09/Feb/2019 09:14:56] "GET /status.json HTTP/1.1" 200 -
2019-02-09 09:14:56,417 192.168.1.137 - - [09/Feb/2019 09:14:56] "GET /status.json HTTP/1.1" 200 -
2019-02-09 09:14:56,686 192.168.1.137 - - [09/Feb/2019 09:14:56] "GET /status.json HTTP/1.1" 200 -
2019-02-09 09:14:56,696 127.0.0.1 - - [09/Feb/2019 09:14:56] "GET /heartbeat/1 HTTP/1.1" 200 -
2019-02-09 09:14:56,719 127.0.0.1 - - [09/Feb/2019 09:14:56] "GET /heartbeat/0 HTTP/1.1" 200 -
2019-02-09 09:14:56,956 192.168.1.137 - - [09/Feb/2019 09:14:56] "GET /status.json HTTP/1.1" 200 -
2019-02-09 09:14:57,226 192.168.1.137 - - [09/Feb/2019 09:14:57] "GET /status.json HTTP/1.1" 200 -
2019-02-09 09:14:57,504 192.168.1.137 - - [09/Feb/2019 09:14:57] "GET /status.json HTTP/1.1" 200 -
2019-02-09 09:14:57,771 192.168.1.137 - - [09/Feb/2019 09:14:57] "GET /status.json HTTP/1.1" 200 -
(snipped)

So it doesn’t really tell us much I think.

The only way I can see of breaking out of the loop is if Balboa32U4.motors in BalboaRPiSlaveDemo.ino has a milliseconds argument to run the motors for a fixed length of time. It doesn’t look like that code is available to modify though.


#4

I’m not really sure what’s going on to cause your program to get stuck on the time.sleep(); I’ll try to look into it later today or sometime this week.

There isn’t a way to pass a duration to the slave program because all the commands are designed to immediately take effect (e.g. set the motor speed), and there’s no mechanism for either delaying an action or scheduling an action for a future time (with the exception of some buzzer functionality). Doing the wait on the Python side seems like the better way to do it to me, as long as we can figure out how to make it work correctly.

Kevin


#5

I tried running some test programs in a virtual machine yesterday and was not able to reproduce your problem; the drive_test() function executed fine, other than causing Flask to raise an error afterward because it doesn’t return a valid response. I can try it on an actual Raspberry Pi, but in the meantime, could you also post all of your source files so I can look at the complete code you are using?


#6

Ah, okay. Many thanks - that’s good news because it points to an error in my code.

I’ll update the forked repository I’m using shortly.