Pololu Dual G2 High Powered MC for raspberry pi 18v22

Hi all,
I have the following Pololu motor controller shield for the raspberry pi.
Its the Pololu Dual G2 High-Power Motor Driver for Raspberry Pi https://shop.pimoroni.com/collections/raspberry-pi/products/pololu-dual-g2-high-power-motor-driver-for-raspberry-pi> 18v22

I have it working using the python library and running the example.py .

Although this is a good start I want to integrate the shield into a project that is web based and uses cgi files to control the motors. The cgi files contain individual GPIO calls to 4 GPIO pins to control the motors. The command for forward is

#!/bin/bash
gpio -g write 10 0
gpio -g write 17 1
gpio -g write 27 1
gpio -g write 22 0

I would also like to control the motor driver directly without the library by writing to the GPIO pins directly. I thought this code may work but so far now action.

Rather than rewriting all the web base code can you tell me how to achieve my goal as I am going around in circles
I will most appreciate some help on this.

regards
Max

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BOARD)
GPIO.setup(18,GPIO.OUT)   // GPIO24   M1DIR
GPIO.setup(22,GPIO.OUT)   // GPIO25   M2DIR
GPIO.setup(32,GPIO.OUT)   // GPIO12   M1PWM
GPIO.setup(33,GPIO.OUT)   // GPIO13   M2PWM
         
GPIO.output(18,False)
GPIO.output(22,False)
GPIO.output(32,False)
GPIO.output(33,False)
time.sleep(5)

# by selection True and False I should be able to turn on and off the motors
GPIO.output(18,False)
GPIO.output(22,False)
GPIO.output(32,True)
GPIO.output(33,True)
time.sleep(5) 
           
GPIO.output(18,False)
GPIO.output(22,False)
GPIO.output(32,False)
GPIO.output(33,False)   

GPIO.cleanup()

Hello, Max.

You should also make sure the motor drivers are enabled by tying their nSLEEP pins high, which you can do by adding this at the top of your code:

GPIO.setup(15,GPIO.OUT)   // GPIO22   M1nSLEEP
GPIO.setup(16,GPIO.OUT)   // GPIO23   M2nSLEEP
GPIO.output(15,True)
GPIO.output(16,True)

I am not sure if there are any other issues, but could you try that and see if it helps?

-Jon

I will give it a go and report back

I did not get a notification of your recent reply. Do I need to turn that function on somewhere or do I need to keep checking for an reply

anyway thanks for your suggestion
Max

Hey Super Hero it worked. Ok so now I can get on and have some fun this this project
thanks
Max

I am glad things are working!

Yes, by default forum users should be getting email notifications when their posts get replies. You might try checking your spam folder or white list "inbox@pololu.com", which is the address the forum notifications come from.

-Jon

Ok now I would like to make an improvement.

I have copied parts of the code into a number of CGI scripts for forward backward, left, right etc.
the code for forward is

gpio -g write 24 0
gpio -g write 25 1
gpio -g write 12 1
gpio -g write 13 1
gpio -g write 22 1
gpio -g write 23 1

I also added the code required to set up the pins into etc/rc.local

gpio -g mode 23 out
gpio -g mode 24 out
gpio -g mode 25 out
gpio -g mode 12 out
gpio -g mode 13 out
gpio -g mode 22 out

It is all working well.

However when I call the cgi scripts as the pin is controlled by a true or false the motor when triggered comes on at 100%.

I would like to use the PWM functionality that is built into the motor driver shield.
I know the raspberry pi has 1 pwm pin which is physical pin 12 and GPIO 18

Can you give me an idea how to achieve this with wiringPi.

best regards
Max

You should be able to run man gpio on your Raspberry Pi to see a list of options for the gpio utility. It looks like you can use the mode subcommand to change the pin to PWM mode, and then use pwm to set the duty cycle. If that works, you should also try using pwmr to get a PWM frequency of 20 kHz. You might find this video useful, too.

-Jon

Unfortunatey something is not working can you take a look

in the file etc/rc.local I have the following
image

this code sets the modes

in the cgi folder for the forward command, I have the following

image

I have tried to follow you instructions however I may have gone off course.

regards
Max

Can you describe how your code is not working? Can you verify that you are actually running your code in /etc/rc.local after making your edits? (Note that you will probably have to reboot.) Also, do you have access to an oscilloscope?

-Jon

Ok

Every time I change the code in rc.local I do reboot

First I run the non pwm versions of the cgi files and all is fine. The motors spin. I have a number of cgi files for forward backward etc and all work as desired

when I change the code in rc.local and in the cgi files and run the programs all I get is a very slight click. but no spinning. May be I need to also configure some other aspect like the frequency
hope this helps

So long as your connections have remained the same, it does sound like there is an issue with generating PWM signals. I recommend using an oscilloscope to look at the output of those PWM pins to see what is happening there.

-Jon