I received my GoPiGo a few days ago, assembled it and started playing around, lots of fun!
I was a bit annoyed though: couldn’t get the robot to go straight forward, with or without PID, and regardless of having tried many trim settings.
I wrote the following program that seems to make it go straight, thought I’d post it here and see if others find it useful / have solved it in a different way / improve on it:
#!/usr/bin/env python
#############################################################################################################
# Rafi: Trying to move forward while reading the encoders and correcting for bias
##############################################################################################################
from gopigo import * #Has the basic functions for controlling the GoPiGo Robot
import sys #Used for closing the running program
import signal
def signalHandler(signum, frame):
if signum in [signal.SIGINT, signal.SIGTERM]:
global stop_now
stop_now = True
stop_now = False
signal.signal(signal.SIGINT, signalHandler)
signal.signal(signal.SIGTERM, signalHandler)
lenc = enc_read(0)
renc = enc_read(1)
speed = 100
left_speed = speed #start with left speed the same as right speed, adapt as needed
trim = 0.0
set_speed(speed)
time.sleep(0.1)
motor_fwd()
while not stop_now:
time.sleep(0.1)
nlenc = enc_read(0)
nrenc = enc_read(1)
print('left: ' + str(nlenc - lenc) + ' right: ' + str(nrenc - renc))
if (nlenc != -1) and (lenc != -1) and (nrenc != -1) and (renc != -1): # all reading are valid
if (nrenc > renc): # right wheel moved at all?
trim = float(nlenc - lenc) / (nrenc - renc)
else:
trim = 100 # sort of infinite
print ('trim: ' + str(trim))
# start simple: if left is faster, make it a bit slower. Not sensitive to how much faster
if (trim < 0.95):
left_speed += 10
if (left_speed > speed * 2):
left_speed = speed * 2
set_left_speed(left_speed)
print('increasing left_speed to ' + str(left_speed))
elif (trim > 1.05):
left_speed -= 10
if (left_speed < speed / 2):
left_speed = speed / 2
set_left_speed(left_speed)
print('decreasing left_speed to ' + str(left_speed))
lenc = nlenc
renc = nrenc
print('stop')
stop()
Hey rafit,
The code looks awesome. We had something similar sitting in the firmware and controlling the motors but it did not work well for a lot of folks. It would be great if you could add a before and after video of how your GoPiGo works with and without the code.
We’ll be happy to add this to the Github repo so that other people can use it too.
Hey Rafi,
Thanks a lot for sharing the new code and the video, it does look like the performance of the GoPiGo is much better compared to the normal code and it is even better since it runs on the software so other people can tinker with it too easily.
We’ll try the new code and would definitely like to merge it with the GoPiGo repository do that other people can use it too.