Python code controlling 4DC motors with mecanum wheels

Hello,

I am building a robot with 4 separate DC motors(from RoboShop-- RB-Dfr-444) with Raspberry Pi 3+, a Geekworm Full Function Motor Hat (TB-418460), a power relay HAT (12v) and using Python as the software.

This robot also has Mecanum wheels so therefore I want to control any combination of 1 to 4 motors’ movement via the software.

I have made this work successfully but have an intermittent lack of motion of motors 3 & 4 in some commands as indicated by the comments. Motors 1 & 2 work in all regimes. Motors 3 & 4 work in some lines of code and do not work in other lines. Code wording is exactly the same and all connections have be checked and are secure. I have replaced the motorHAT with a new unit. Still having same issues.

Could someone look at the file below (especially the comments) and see if any errors are spotted. This is definitely and programming software issue and not mechanical/electrical.

Thanks,
Gary

#This program test the mecanum wheels in all directions!

from Raspi_MotorHAT import Raspi_MotorHAT
import smbus
import time
import atexit

DEVICE_BUS = 1  **#this is a relay HAT to power motors**
DEVICE_ADDR = 0x10
bus = smbus.SMBus(DEVICE_BUS)
bus.write_byte_data(0x10, 1, 0xFF)

mh = Raspi_MotorHAT(addr=0x6f)  **#this is the motor controlling HAT**
m1 = mh.getMotor(1)
m2 = mh.getMotor(2)
m3 = mh.getMotor(3)
m4 = mh.getMotor(4)

def turn_off_motors():
        m1.run(Raspi_MotorHAT.RELEASE)
        m2.run(Raspi_MotorHAT.RELEASE)
        m3.run(Raspi_MotorHAT.RELEASE)
        m4.run(Raspi_MotorHAT.RELEASE)

atexit.register(turn_off_motors)

m1.setSpeed(50)
m2.setSpeed(50)
m3.setSpeed(50)
m4.setSpeed(50)

#straight forward
print("straight forward")
m1.run(Raspi_MotorHAT.FORWARD)  **#all wheels function normally on this command**
m2.run(Raspi_MotorHAT.FORWARD)
m3.run(Raspi_MotorHAT.FORWARD)
m4.run(Raspi_MotorHAT.FORWARD)
time.sleep(4)
turn_off_motors()
time.sleep(1)

#turn left
print("turn left")
time.sleep(2)
m2.run(Raspi_MotorHAT.FORWARD)  **# wheel functions normally on this command**
m4.run(Raspi_MotorHAT.FORWARD)  **# wheel does not function on this command**
time.sleep(2)
turn_off_motors()

#turn right
print("turn right")
time.sleep(2)
m1.run(Raspi_MotorHAT.FORWARD)  **# wheel functions normally on this command**
m3.run(Raspi_MotorHAT.FORWARD)  **# wheel does not function on this command**
time.sleep(2)
turn_off_motors()

#horizontal left
print("horizontal left")
time.sleep(2)
m1.run(Raspi_MotorHAT.BACKWARD)  **# wheel functions normally on this command**
m2.run(Raspi_MotorHAT.FORWARD)  **# wheel functions normally on this command**
m3.run(Raspi_MotorHAT.BACKWARD)  **# wheel does not function on this command**
m4.run(Raspi_MotorHAT.FORWARD)  **# wheel does not function on this command**
time.sleep(2)
turn_off_motors()

#spin left
print("spin left")
time.sleep(2)
m1.run(Raspi_MotorHAT.BACKWARD)  **# wheel functions normally on this command**
m2.run(Raspi_MotorHAT.FORWARD)  **# wheel functions normally on this command**
m3.run(Raspi_MotorHAT.FORWARD)  **# wheel does not function on this command**
m4.run(Raspi_MotorHAT.BACKWARD)  **# wheel does not function on this command**
time.sleep(2)
turn_off_motors()

#spin right
print("spin right")
time.sleep(4)
m1.run(Raspi_MotorHAT.FORWARD)  **# wheel functions normally on this command**
m2.run(Raspi_MotorHAT.BACKWARD)  **# wheel functions normally on this command**
m3.run(Raspi_MotorHAT.BACKWARD)  **# wheel does not function on this command**
m4.run(Raspi_MotorHAT.FORWARD)  **# wheel does not function on this command**
time.sleep(2)
turn_off_motors()

#horizontal right
print("horizontal right")
time.sleep(4)
m1.run(Raspi_MotorHAT.FORWARD)  **# wheel functions normally on this command**
m2.run(Raspi_MotorHAT.BACKWARD)  **# wheel functions normally on this command**
m3.run(Raspi_MotorHAT.FORWARD)  **# wheel does not function on this command**
m4.run(Raspi_MotorHAT.BACKWARD)  **# wheel does not function on this command**
time.sleep(2)
turn_off_motors()

#back left
print("1 & 3 back left")
time.sleep(4)
m1.run(Raspi_MotorHAT.BACKWARD)  **# wheel functions normally on this command**
m3.run(Raspi_MotorHAT.BACKWARD)  **# wheel does not function on this command**
time.sleep(2)
turn_off_motors()

#straight backwards
print("straight backwards")
time.sleep(2)
m1.run(Raspi_MotorHAT.BACKWARD)  **#all wheels function normally on this command**
m2.run(Raspi_MotorHAT.BACKWARD)
m3.run(Raspi_MotorHAT.BACKWARD)
m4.run(Raspi_MotorHAT.BACKWARD)
time.sleep(2)

turn_off_motors()
time.sleep(1)
bus.write_byte_data(0x10, 1, 0x00)

Hello @RoboNovice!

Seems to me that the problem is not the code, but that the motor controller is not powerful enough for your motors. Looking at the motor’s datasheet you can see that the stall current is 7A, so let us approximate the continuous current to 1.75A. The TB6612 chipset provides 1.2A per bridge (3A peak) with thermal shutdown protection. So when your motors need more torque to move they demand more current which the controller can’t provide. I guess this is happening with the back motors because the center of mass of the robot is not really in the center and this causes the forces not to be distributed evenly?

2 Likes

Thank you Geraldine and that makes sense but I’m not sure I follow the electrical math. Do you have a suggestion to resolve that issue? Or do you have a recommendation for a motor controller(s) that works with the Pi and handle the amperage for the four motors??

Thanks again for looking at this.

RoboNovice

1 Like

You could use two Dual H-bridge Motor Drivers, for example:

You can find more information here:

PDF File
Devantech MD22 Dual H-Bridge Motor Driver Datasheet

Web Site
Documentation
Raspberry Pi Example

1 Like

Thanks again, good solution

2 Likes