jdorfsman
Posts: 14
Joined: Sun Jun 16, 2013 2:47 pm

Control dc motor using GPIO.PWM problem

Wed Oct 01, 2014 10:38 am

I'm trying to make a small remote controlled car with the raspberry pi and attached two small dc motors to it, using the L239D chip.
When testing without the remote control (which is a simple android app) the motors work good and the speed is changing.
But when I try with the remote control it doesn't work at all.
here is the function that receives the remote control params:

Code: Select all

    def customSpeed(direction,left, right):
    	right = int(right)
    	left = int(left)
    	if direction == "f":
    		GPIO.output(Motor1A, GPIO.HIGH)
    		GPIO.output(Motor1B, GPIO.LOW)
    		GPIO.output(Motor2A, GPIO.HIGH)
    		GPIO.output(Motor2B, GPIO.LOW)
    		motorR.ChangeDutyCycle(right)
    		motorL.ChangeDutyCycle(left)
    		print("Got to forward")
    	else:
    		GPIO.output(Motor1A, GPIO.LOW)
    		GPIO.output(Motor1B, GPIO.HIGH)
    		GPIO.output(Motor2A, GPIO.LOW)
    		GPIO.output(Motor2B, GPIO.HIGH)
    		motorR.ChangeDutyCycle(right)
    		motorL.ChangeDutyCycle(left)
    		print("Got to back")
    	ans = "Going "+ ("Forward" if direction == "f" else "Back") +". Left: "+str(left)+". Right: "+str(right)
    	print(ans)
The last print, prints the information correct, and when I try to do this methods "offline" they all work properly, but this function does not.
Any thoughts?

This are my methods for preparing the motors:

Code: Select all

    GPIO.setmode(GPIO.BOARD)
    Motor1A = 16
    Motor1B = 18
    Motor1E = 22
    
    Motor2A = 19
    Motor2B = 21
    Motor2E = 23
    
    GPIO.setup(Motor1A, GPIO.OUT)
    GPIO.setup(Motor1B, GPIO.OUT)
    GPIO.setup(Motor1E, GPIO.OUT)
    GPIO.setup(Motor2A, GPIO.OUT)
    GPIO.setup(Motor2B, GPIO.OUT)
    GPIO.setup(Motor2E, GPIO.OUT)
    
    motorR = GPIO.PWM(Motor1E,100)
    motorL = GPIO.PWM(Motor2E,100)
    motorR.start(0)
    motorL.start(0)
Is there any problem using the tcp connection with the GPIO at the same time?

User avatar
joan
Posts: 14935
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Control dc motor using GPIO.PWM problem

Wed Oct 01, 2014 11:05 am

Code snippets are rarely useful in diagnosing problems.

Would it be possible to post all the code (or at least all the relevant code)?

User avatar
B.Goode
Posts: 10356
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Control dc motor using GPIO.PWM problem

Wed Oct 01, 2014 5:50 pm

jdorfsman wrote: Is there any problem using the tcp connection with the GPIO at the same time?
Not in general. I do all my experimenting over a network using ssh, and have never had any issues with using GPIO connections that could be related to the network.

jdorfsman
Posts: 14
Joined: Sun Jun 16, 2013 2:47 pm

Re: Control dc motor using GPIO.PWM problem

Thu Oct 02, 2014 8:22 am

joan wrote:Code snippets are rarely useful in diagnosing problems.

Would it be possible to post all the code (or at least all the relevant code)?
Yes, here is the github link: https://github.com/c0rrupt3d/RPiCar

jdorfsman
Posts: 14
Joined: Sun Jun 16, 2013 2:47 pm

Re: Control dc motor using GPIO.PWM problem

Thu Oct 02, 2014 8:23 am

B.Goode wrote:
jdorfsman wrote: Is there any problem using the tcp connection with the GPIO at the same time?
Not in general. I do all my experimenting over a network using ssh, and have never had any issues with using GPIO connections that could be related to the network.
Then do you have any clue what could be the problem?
I have all the code in github, if you can look at it, it would be great: https://github.com/c0rrupt3d/RPiCar
Thanks!

User avatar
B.Goode
Posts: 10356
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Control dc motor using GPIO.PWM problem

Thu Oct 02, 2014 11:12 am

If you and your collaborators have written all that code then you clearly know far more about python and network programming than I do, so I have nothing to add. But I'm surprised you didn't test it during development to see whether it was working.

My own experiments have not been so sophisticated, and did not involve servers or protocol definitions. I simply used a 'game paddle' approach using a cluster of keys such as W A S Z to represent forward, left, right, back.

jdorfsman
Posts: 14
Joined: Sun Jun 16, 2013 2:47 pm

Re: Control dc motor using GPIO.PWM problem

Fri Oct 03, 2014 10:38 am

B.Goode wrote:If you and your collaborators have written all that code then you clearly know far more about python and network programming than I do, so I have nothing to add. But I'm surprised you didn't test it during development to see whether it was working.

My own experiments have not been so sophisticated, and did not involve servers or protocol definitions. I simply used a 'game paddle' approach using a cluster of keys such as W A S Z to represent forward, left, right, back.
That's the problem, we did test it, the function works offline but not when I try to send the same parameters via the network..

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: Control dc motor using GPIO.PWM problem

Fri Oct 03, 2014 10:40 am

Have you tried debugging this by printing out what commands are being received over the network compared with what you're expecting?
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

jdorfsman
Posts: 14
Joined: Sun Jun 16, 2013 2:47 pm

Re: Control dc motor using GPIO.PWM problem

Fri Oct 03, 2014 10:46 am

elParaguayo wrote:Have you tried debugging this by printing out what commands are being received over the network compared with what you're expecting?
Yes, found nothing strange or different.
Even tried to print all the commands and re send them locally and that worked!
:/

Ravenous
Posts: 1956
Joined: Fri Feb 24, 2012 1:01 pm
Location: UK

Re: Control dc motor using GPIO.PWM problem

Fri Oct 03, 2014 10:52 am

I notice in your motor controller you have very sinsibly put debug bits like:

Code: Select all

print("Going straigh with speed: "+str(speed))
Is this actually executing? Is the output appearing?

What I'm getting at, is either it isn't being called at all or it is being called but something else is interfering with the motor outputs immediately after that part executes... that might be a clue.

Return to “Python”