Simple Script or manual to control the motors

Hi,

there are excellent python scripts to control the robot. I'm not a python profi progammer, so the python sricts are not easy to read/unterstand for me. Is there a manual or a simple commented python script (simple start/stop the gear motors) available which helps to start own programming.
Many thx in advance

Peter

piborg's picture

Below is a very basic example of controlling motors from the ZeroBorg.

In the comments below forwards / backwards refers to the ZeroBorg output. The motors may run the opposite way depending on the wiring.

The maxPower calculation is not strictly necessary, however running the motors over their intended voltage is not recommended.

# Load the ZeroBorg library
import ZeroBorg

# Load the standard time library
import time

# Setup the ZeroBorg so it is ready to use
ZB = ZeroBorg.ZeroBorg()
ZB.Init()

# Reset the EPO flag to enable motors
ZB.ResetEpo()

# Determine the maximum power output
# In this case 6V motors from a "9V" rechargeable
maxPower = 6.0 / 8.4

# Run motor 1 forwards at full power
# for 5 seconds, then stop motor 1
ZB.SetMotor1(1.0 * maxPower)
time.sleep(5.0)
ZB.SetMotor1(0)

# Run motor 2 forwards at half power
# for 5 seconds, then stop motor 2
ZB.SetMotor2(0.5 * maxPower)
time.sleep(5.0)
ZB.SetMotor2(0)

# Run motors 3 and 4 backwards at 3/4 power
# for ten seconds, then stop all motors
ZB.SetMotor3(-0.75 * maxPower)
ZB.SetMotor4(-0.75 * maxPower)
time.sleep(10.0)
ZB.MotorsOff()

# Run all motors forwards at half power
# for ten seconds, then stop all motors
ZB.SetMotors(0.5 * maxPower)
time.sleep(10.0)
ZB.MotorsOff()

many thx souds cool ..

Hi
Just trying this and its doesn't work.

Code is below. It just starts waits 10 seconds and returns to prompt. Any Ideas?

Thanks

Nic

import ZeroBorg
import time

ZB = ZeroBorg.ZeroBorg()
ZB.Init()

ZB.ResetEpo()

maxPower = 6.0 / 8.4

ZB.SetMotors(0.5 * maxPower)
time.sleep(10.0)
ZB.MotorsOff()

piborg's picture

There are a few different reasons that could explain why the motor(s) are not moving:

  1. Each motor needs to be wired so that it is connected on both the + and - of the same output, e.g. M1+ and M1- terminals
  2. You need a battery or power supply connected to both the V+ and GND terminals on the ZeroBorg
  3. It is possible that the motor needs more power in order to move
  4. If the EPO jumper is removed it will prevent any motor output
  5. If the SW jumper is removed it will disconnect the ZeroBorg from the battery

We can help narrow the problem down, it is probably something simple to fix :)

  1. Do you see any messages shown on the screen when you run the script?
  2. What motors and battery are you using with the ZeroBorg?

Hi

I have another program that works fine though, using a controller.
The robot moves well, using that. It has I'm using this kit - https://www.core-tec.co.uk/tiny-4wd/

It is currently plugged into mains supply but I will be using a 9V battery.

No error message comes up, it shows that the board has been detected, waits 10 secs, then ends.

Thanks

Nic

piborg's picture

Can you give us a link to the code you are using which works?

My best guess is that the other code is doing something which is needed to make the robot move.

Hi its your pre-loaded code for the zeroborg. (ZBjoystick.py) I have made some small tweaks but it is basically the same.

piborg's picture

You mentioned above that the robot is only powered by a mains supply at the moment.

Do you mean that the Raspberry Pi is being powered by a USB mains charger and no other power is supplied to the ZeroBorg itself?

Hi, it the Zeroborg is connected to the Pi Via the GPIO pins. So is powered that way, the red light comes on on the Zeroborg. As I said before if I run the ZBjoysick program it works fine, it is also plugged in at the moment via USB still, but I have had it running on a 9 Volt battery as well.
What version of python should it being run in ?
Thanks

piborg's picture

While the ZeroBorg logic does get power from the GPIO (hence the LED flash), the motors will only take power from the battery connection. You will need a battery attached to the ZeroBorg for the motors to turn.

If you can try this code example with the battery attached it should behave the same way as the zbJoystick.py example when driving forwards at full speed:

import ZeroBorg
import time

ZB = ZeroBorg.ZeroBorg()
ZB.Init()
ZB.ResetEpo()

maxPower = 6.0 / 8.4

driveLeft = 1.0
driveRight = 1.0

ZB.SetMotor1(-driveLeft * maxPower)
ZB.SetMotor2(-driveLeft * maxPower)
ZB.SetMotor3(driveRight * maxPower)
ZB.SetMotor4(driveRight * maxPower)

time.sleep(10.0)

ZB.MotorsOff()

If your tweaks modified the driveLeft / driveRight lines above you should make the same changes.

Our examples are tested with Python 2, which is the version loaded by default on the Raspberry Pi when you run python from a terminal.

Hi

Thank for this I'll try it tomorrow.

Why does the basic code not work then?

But also the when i'm running the normal zbJoystick.py file, the robot moves fine when attached to mains power. (I was testing it) I have nothing attached to the zeroborg and I have been running it with a power pack attached fine. So I don't understand "the motors will only take power from the battery connection. You will need a battery attached to the ZeroBorg for the motors to turn."

Thanks again for all your help.

Nic

Hi

Just tested this. Sorry been busy.
Tried the above, with a 9V battery attached, not on mains power, and it behaves in the same way.

Wheels turn for about a second, then after 10 seconds the script ends.

The following message is displayed.

Loading ZeroBorg on Bus 1, address 40
Found ZeroBorg at 40
ZeroBorg loading on bus 1

Then after 10 seconds the prompt returns.

Thanks

Nic

piborg's picture

It sounds like the problem is that the motors are being turned off outside of the script rather than an issue with the script itself.

Having had another look at the zbJoystick.py script it enables the failsafe on the ZeroBorg which turns the motors off unless it receives updates from the Pi at least twice a second.

To fix the problem you can simply turn the failsafe off in the script. Try this version:

import ZeroBorg
import time
 
ZB = ZeroBorg.ZeroBorg()
ZB.Init()
ZB.SetCommsFailsafe(False)
ZB.ResetEpo()
 
maxPower = 6.0 / 8.4
 
driveLeft = 1.0
driveRight = 1.0
 
ZB.SetMotor1(-driveLeft * maxPower)
ZB.SetMotor2(-driveLeft * maxPower)
ZB.SetMotor3(driveRight * maxPower)
ZB.SetMotor4(driveRight * maxPower)
 
time.sleep(10.0)
 
ZB.MotorsOff()

If that works you should be able to add this line after the ZB.Init() in your original code and get it to work as well:

ZB.SetCommsFailsafe(False)

Thanks that script you have show does work.

It doesn't quite work with my more complex script but it works well enough.
It will run forward but won't stop, you have to EPO reset to get it to stop.

I will post my full code later to see, though.

Thanks

Nic

Subscribe to Comments for "Simple Script or manual to control the motors"