Reputation: 75
I'm playing around with a BrickPi on the Raspberry Pi.
I'm using Python to control a 4 wheel drive robot. The default program allows you to control it in real time. But I tried to create a program that gives the robot a set route, ie move forwards 3 seconds then stop
by using the code:
def fwd():
BrickPi.MotorSpeed[fl] = speed
BrickPi.MotorSpeed[fr] = speed
BrickPi.MotorSpeed[bl] = -speed
BrickPi.MotorSpeed[br] = -speed
BrickPiUpdateValues()
def stop():
BrickPi.MotorSpeed[fl] = 0
BrickPi.MotorSpeed[fr] = 0
BrickPi.MotorSpeed[bl] = 0
BrickPi.MotorSpeed[br] = 0
BrickPiUpdateValues()
fwd()
time.sleep(4)
stop()
But it just revs up for like a second then instantly stops... I have the motors setup and assigned elsewhere in the code. And speed is set to 200.
The documentation for the library hasn't been helpful.
How do I make this work?
Upvotes: 3
Views: 631
Reputation: 161
But it just revs up for like a second then instantly stops... I have the motors setup and assigned elsewhere in the code. And speed is set to 200.
It looks like this should run at full blast, then stop. The BrickPi firmware has a safety feature that shuts down motors if it doesn't hear from the Raspberry Pi every few seconds. You might want to change the code to something like this:
fwd()
ot = time.time()
while(time.time() - ot < 4): #running while loop for 3 seconds
BrickPiUpdateValues() # Ask BrickPi to update values for sensors/motors
time.sleep(.1)
stop()
The loop on the fourth line calls the code every 100 ms (updates the BrickPi) and keeps the motors alive and running.
And you can see our code examples for running LEGO Mindstorms motors with the Raspberry Pi here.
Hope this helps!
Upvotes: 2