Using GoPiGo Encoders in Python


#1

Hi,

I am new to programming the GoPiGo and need help. I started with the following code.

from gopigo import *

enc_tgt(1,1,72)
fwd()

This did exactly what I was expecting it to do, move forward by four revolutions of the wheels (except for slippage, I need to add ballast). So far, so good.

Then I thought I’d execute a right turn so I added…

enc_tgt(1,1,18)
right_rot()

I knew this wouldn’t be a 90 degree turn, but I was expecting the robot to move forward again by four revolutions and then rotate right by one revolution of the wheel. The GoPiGo didn’t move forward at all. It only rotated right the one revolution of the wheels.

Obviously the program is not giving the robot time to move forward before sending the right_rot() command. What I need help with is how to make it finish going forward the prescribed distance before turning right.

I do not have any sensors yet so my goal is to program the robot so I am trying for now to program it to drive a path taped on the floor.

Thanks.


#2

Hi @jswann5726,
So when you wrote the code like this:

enc_tgt(1,1,72)
fwd()
enc_tgt(1,1,18)
right_rot()

What happened was that, encoder target was set to 72, then the forward command was issued then encoder target was set to 18 and then rotate right command was issued. There wasn’t any delay for forward command to run and the program jumped straight to the right_rot command.

If you have a look at this example, you can use something similar to keep checking the status if the encoder target was hit and if it was hit, then move to the next encoder target.

Here is some more documentation on the encoder targeting function.

And for the ballast, a lot of people mount the battery pack on the top plate of the GoPiGo rather than at the back.


#3

Thank you! That was just the push I needed. Here’s what I coded:

from gopigo import *
from time import sleep

enc_tgt(1,1,72)
fwd()

while read_enc_status():
    sleep(0.1)

enc_tgt(1,1,16)
right_rot()

This successfully moved the GoPiGo forward by one revolution of the wheels, stopped it, and then rotated it to the right a half turn. (I fiddled with the third parameter second enc_tgt statement until it did an about face.) Now my mind is racing with all kinds of possibilities.

On the slippage, my tape dispenser normally rides on top of the GoPiGo for ballast. Even with that there is a little bit of slippage on the tile floor in the classroom where I teach. This was much worse on the hardwood floor at my home where I am tinkering over the weekend. As my students and I get into programming this to travel a predefined course I’m trying to determine if I should find some sort of mat to drive on or if we should just set the speed slower so that it doesn’t spin out so easily.


#4

@jswann5726: Great to hear that it’s working for you now. To reduce the slip, you should decrease the speed of the GoPiGo by a bit. A mat might help but we haven’t tried that before. What would definitely help is adding a rubberband on the wheels to improve traction.


#5

Also, to reduce slip: place the batteries on top of the canopy, rather than on the back of the GoPiGo. And as @karan points out, it helps to place some rubber bands on wheels if they’re slipping.