Pololu Dual G2 High-Power Motor Driver 24v14

Hi everyone,
I purchased the Pololu Dual G2 High-Power Motor Driver 24v14 for Raspberry Pi and used the python examples were provided. Everything works fine….The one issue I’m having is everything is in max speed and runs for few seconds so my questions are
1- How to control the speed on this shield? Like reduce the speed to 50%
2- I defined left and right but it will run in circles and for obvious reasons of course. It will run for a long period of time. How to make it runs for short time?
I attached the manufacturer example and below is my right/left function… I would really appreciate any help

    def Right():
        for s in test_forward_speeds:
            motors.motor1.setSpeed(s)
            Motor_Controlling.raiseIfFault()
            timer_thread = multiprocessing.Process(target=Motor_Controlling.timer)
            timer_thread.start()
            if not timer_thread.is_alive():
                motors.forceStop()
                Motor_Controlling.forward()

and the motor example

from __future__ import print_function
import time
from dual_g2_hpmd_rpi import motors, MAX_SPEED

# Define a custom exception to raise if a fault is detected.
class DriverFault(Exception):
    def __init__(self, driver_num):
        self.driver_num = driver_num

def raiseIfFault():
    if motors.motor1.getFault():
        raise DriverFault(1)
    if motors.motor2.getFault():
        raise DriverFault(2)

# Set up sequences of motor speeds.
test_forward_speeds = list(range(0, MAX_SPEED, 1)) + \
  [MAX_SPEED] * 200 + list(range(MAX_SPEED, 0, -1)) + [0]  

test_reverse_speeds = list(range(0, -MAX_SPEED, -1)) + \
  [-MAX_SPEED] * 200 + list(range(-MAX_SPEED, 0, 1)) + [0]  

try:
    motors.setSpeeds(0, 0)

    print("Motor 1 forward")
    for s in test_forward_speeds:
        motors.motor1.setSpeed(s)
        raiseIfFault()
        time.sleep(0.002)

    print("Motor 1 reverse")
    for s in test_reverse_speeds:
        motors.motor1.setSpeed(s)
        raiseIfFault()
        time.sleep(0.002)

    # Disable the drivers for half a second.
    motors.disable()
    time.sleep(0.5)
    motors.enable()

    print("Motor 2 forward")
    for s in test_forward_speeds:
        motors.motor2.setSpeed(s)
        raiseIfFault()
        time.sleep(0.002)

    print("Motor 2 reverse")
    for s in test_reverse_speeds:
        motors.motor2.setSpeed(s)
        raiseIfFault()
        time.sleep(0.002)

except DriverFault as e:
    print("Driver %s fault!" % e.driver_num)

finally:
  # Stop the motors, even if there is an exception
  # or the user presses Ctrl+C to kill the process.
    motors.forceStop()

Motor_example.py (1.6 KB)

Hello, Aladin.

As described on the GitHub page for the Python library for the Pololu Dual G2 High-Power Motor Drivers for Raspberry Pi, motor speeds are represented as numbers between -480 and 480. You can specify the speeds using the following functions in that library:

motors.setSpeeds(m1_speed, m2_speed): Set speed and direction for both motor 1 and motor 2.
motors.motor1.setSpeed(speed): Set speed and direction for motor 1.
motors.motor2.setSpeed(speed): Set speed and direction for motor 2.

The example sets up some lists (sequences) of speeds and runs through them so that each motor ramps up from 0 to MAX_SPEED (480) and back down to 0, then in reverse from 0 to -480 and back to 0. To run the motors at a fixed speed, you would just need to call one of the above functions once.

I am not sure I understand your second question. It looks like your program is starting another thread inside the for loop (instead of delaying with time.sleep()), but it is not clear to me what that other thread is doing.

Brandon

1 Like