2x7A motor controller: unreliable response to packet serial commands from RasPi
Posted: Mon Apr 15, 2019 10:48 am
I am using RoboClaw'sPython library to drive two 2x7A's from my Raspberry Pi. But, the motors' actual movement is flakey. I've condensed the problem down to a test case, with the code below. This test case just runs all motors forward for 2 seconds, stops for two seconds, runs them backward for two seconds, and repeats.
Running this code, the motors will usually respond correctly for a few commands, and then one of the motors will fail to respond to a command within 30 seconds or so. For example, the motors will be running, but not all of them will stop for the stop command. It's not always the same motor that fails to respond to a command -- it's unpredictable.
Note that the code shows 9600 baud communication, but I've tried it at the default 38400 and a few other speeds, with the same results. The initial setup is all based on the Packet Serial Bus Operation article.
Any help would be appreciated! Trying to stop my robot from running into walls ...
Running this code, the motors will usually respond correctly for a few commands, and then one of the motors will fail to respond to a command within 30 seconds or so. For example, the motors will be running, but not all of them will stop for the stop command. It's not always the same motor that fails to respond to a command -- it's unpredictable.
Note that the code shows 9600 baud communication, but I've tried it at the default 38400 and a few other speeds, with the same results. The initial setup is all based on the Packet Serial Bus Operation article.
Any help would be appreciated! Trying to stop my robot from running into walls ...
Code: Select all
from libs.roboclaw import Roboclaw
import time
class Motors(object):
# motor controller addresses
c1=128
c2=129
def __init__(self):
self.roboclaw = Roboclaw("/dev/ttyS0", 9600)
self.roboclaw.Open()
# Takes 4 values, representing speed for [front left, front right, back left, back right], and an optional acceleration arg.
# Each speed value should range from -128 (full reverse) to 128 (full forward). Pass 0 to stop the wheel
def move(self, fl, fr, bl, br, accel = 30000 ):
duty_factor = 259.9 # 128 will give you duty value of 32760, which is top speed forward
self.roboclaw.DutyAccelM1M2(self.c1, accel, int(fl * duty_factor), accel, int(fr * duty_factor))
self.roboclaw.DutyAccelM1M2(self.c2, accel, int(br * duty_factor), accel, int(bl * duty_factor))
if __name__ == '__main__':
m = Motors()
while True:
t = time.clock()
m.move(64,64,64,64)
print("call to m.move() took {:d} milliseconds".format(int((time.clock()-t)*1000)))
time.sleep(2)
t = time.clock()
m.move(0,0,0,0)
print("call to m.move() took {:d} milliseconds".format(int((time.clock() - t) * 1000)))
time.sleep(2)
t = time.clock()
m.move(-64,-64,-64,-64)
print("call to m.move() took {:d} milliseconds".format(int((time.clock() - t) * 1000)))
time.sleep(2)
t = time.clock()
m.move(0,0,0,0)
print("call to m.move() took {:d} milliseconds".format(int((time.clock() - t) * 1000)))
time.sleep(2)