alphanerd132
Posts: 10
Joined: Sun Nov 04, 2018 1:19 am

controlling a servo.

Sun Nov 04, 2018 1:23 am

I am just starting out with python/pi, my question today is, how do you make a pin stop sending output? I am trying to use servos with the pi but even after I use a GPIO.cleanup() it still sends a signal to my servo. This normaly isn't an issue but i'm using high torque servos and it will rip the servo out of the mounting holes.

Idahowalker
Posts: 372
Joined: Wed Jan 03, 2018 5:43 pm

Re: Basic question

Sun Nov 04, 2018 2:06 am

If GPIO.cleanup is not working try, software, disconnecting from the servo torque engine, such as setting the oTorque = None and then sending a 0 to the pin the servo is connected to, before exiting.
Without knowing why you are deleting my postings, I will not know how...

alphanerd132
Posts: 10
Joined: Sun Nov 04, 2018 1:19 am

Re: Basic question

Sun Nov 04, 2018 8:38 am

Idahowalker wrote:
Sun Nov 04, 2018 2:06 am
If GPIO.cleanup is not working try, software, disconnecting from the servo torque engine, such as setting the oTorque = None and then sending a 0 to the pin the servo is connected to, before exiting.
How would you go about doing such? Would you just change the ".ChangeDutyCycle(0)" to 0 or is there a different function with GPIO called oTorque?

I tried just changing the duty cycle to 0 and it does the same thing, just sit's there strong and twitches. And another thought along the same lines, after I run my program for the first time on the pi, it says that pins are still in use and it never moves the servo again until I restart the pi.

Also, what would cause this strange random behavior when it's idle but still getting a frequency? it sometimes can move up to a half an inch on it's own. Could this be a damaged board with lead jumps or is it just the flaw of every pi?
Last edited by alphanerd132 on Sun Nov 04, 2018 9:11 am, edited 1 time in total.

User avatar
bensimmo
Posts: 3321
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: Basic question

Sun Nov 04, 2018 8:59 am

I wonder if this is a bug, is any error reported ?

There is a bug with buttons due to bugs in RPi.gpio that should be fixed soon. Possibly related
Unless you have moved across to another pin factory like pigpio?

Perhaps pop some code up to see if anyone can see an error, write a fix ?

User avatar
mahjongg
Forum Moderator
Forum Moderator
Posts: 11011
Joined: Sun Mar 11, 2012 12:19 am
Location: South Holland, The Netherlands

Re: controlling a servo.

Sun Nov 04, 2018 1:06 pm

to stop a pin set up for PWM as acting as a PWM output, (outputting a stream of pulses) revert the pin back to a normal GPIO. In other words, from a special function back to a normal GPIO.

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

Re: controlling a servo.

Sun Nov 04, 2018 1:59 pm

It's best if you post the complete script.

It sounds like you are using RPi.GPIO. That uses software PWM so it should certainly stop once the script is exited.

alphanerd132
Posts: 10
Joined: Sun Nov 04, 2018 1:19 am

Re: controlling a servo.

Sun Nov 04, 2018 6:07 pm

joan wrote:
Sun Nov 04, 2018 1:59 pm
It's best if you post the complete script.

It sounds like you are using RPi.GPIO. That uses software PWM so it should certainly stop once the script is exited.

Code: Select all

import RPi.GPIO as GPIO
import time

#GPIO.setwarnings(false) if you want, this will get rid of all warnings

servoPIN=14
aod = 0
GPIO.setmode(GPIO.BCM)
GPIO.setup(servoPIN, GPIO.OUT)

p = GPIO.PWM(servoPIN, 50) #GPIO 14 for PWM with 50Hz
p.start(2.50) # Initialization

try:
    if aod <= 5:
        aod += 1
        p.ChangeDutyCycle(aod)
        time.sleep(.5)
        print (aod)
except KeyboardInterrupt:
    aod = 0
    p.ChangeDutyCycle(aod)
    GPIO.cleanup()

And what it's prints is just 1, then stops counting and just sort of freaks out.

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

Re: controlling a servo.

Sun Nov 04, 2018 6:14 pm

I really dislike driving servos using dutycycle as the model.

The root problem is you need to default aod to 2.5 rather than zero. You are setting the dutycycle percentage to 1%. At 50 Hz that equates to a pulse width of 200µs which is way out of the safe servo range of 1000 to 2000. You can quickly destroy a servo by doing that.

alphanerd132
Posts: 10
Joined: Sun Nov 04, 2018 1:19 am

Re: controlling a servo.

Sun Nov 04, 2018 6:20 pm

joan wrote:
Sun Nov 04, 2018 6:14 pm
I really dislike driving servos using dutycycle as the model.

The root problem is you need to default aod to 2.5 rather than zero. You are setting the dutycycle percentage to 1%. At 50 Hz that equates to a pulse width of 200µs which is way out of the safe servo range of 1000 to 2000. You can quickly destroy a servo by doing that.
OHH okay. I can defiantly see that. So what would you suggest for driving servos? But that also doesn't explain, why does the code cycle once then just sit there? And I applied that fix, but it still counts once, then stops the program and just freaks out

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

Re: controlling a servo.

Sun Nov 04, 2018 6:38 pm

I'll run the code on a Pi later on to see what it actually is doing.

The best way to drive one or two servos on the Pi is with the PWM hardware (GPIO 12/13 or GPIO 18/19). To do that you need to find software to put those GPIO in PWM mode etc.

The second best way is to use hardware timed PWM such as provided by my pigpio or servoblaster amongst others. That isn't as fine grained as full hardware PWM as it only provides about 5µs of position resolution (by default). An advantage is that all GPIO may be driven with hardware timed PWM, so 28 servos on the 40 pin Pis.

This ignores the hardware boards based on the PCA9685 which provide about 5µs resolution for 16 servos.

To get a feel for hardware timed PWM you could try my pigpio.

sudo pigpiod

pigs s 14 1000
pigs s 14 1100
pigs s 14 2000

etc. etc.

alphanerd132
Posts: 10
Joined: Sun Nov 04, 2018 1:19 am

Re: controlling a servo.

Sun Nov 04, 2018 6:53 pm

joan wrote:
Sun Nov 04, 2018 6:38 pm
I'll run the code on a Pi later on to see what it actually is doing.

The best way to drive one or two servos on the Pi is with the PWM hardware (GPIO 12/13 or GPIO 18/19). To do that you need to find software to put those GPIO in PWM mode etc.

The second best way is to use hardware timed PWM such as provided by my pigpio or servoblaster amongst others. That isn't as fine grained as full hardware PWM as it only provides about 5µs of position resolution (by default). An advantage is that all GPIO may be driven with hardware timed PWM, so 28 servos on the 40 pin Pis.

This ignores the hardware boards based on the PCA9685 which provide about 5µs resolution for 16 servos.

To get a feel for hardware timed PWM you could try my pigpio.

sudo pigpiod

pigs s 14 1000
pigs s 14 1100
pigs s 14 2000

etc. etc.
Interesting. Ill have to do research into that to see exactly how to use that. It is interesting though. I did that command that you suggested, and now my servo is freaking out like crazy doing about 200 degrees of movement randomly and occasionally vibrating. And oddly enough, it caused my mouse to stop working as well..

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

Re: controlling a servo.

Sun Nov 04, 2018 7:04 pm

Okay, a couple of things wrong with the code.

Code: Select all

import RPi.GPIO as GPIO
import time

GPIO.setwarnings(False) # if you want, this will get rid of all warnings

servoPIN=14
aod = 2.5
GPIO.setmode(GPIO.BCM)
GPIO.setup(servoPIN, GPIO.OUT)

p = GPIO.PWM(servoPIN, 50) #GPIO 14 for PWM with 50Hz
p.start(2.50) # Initialization
time.sleep(5)
try:
    while aod <= 5:
        aod += 1
        p.ChangeDutyCycle(aod)
        time.sleep(.5)
        print (aod)
except KeyboardInterrupt:
    aod = 0
    p.ChangeDutyCycle(aod)
    GPIO.cleanup()
Basically the if should be a while.

It still doesn't produce any pulses on my Pi though. Had me stumped for a few seconds until I realised GPIO 14 is serial TXD and I have serial enabled on my Pi. If you have the same problem change the output to another GPIO (16 say).

alphanerd132
Posts: 10
Joined: Sun Nov 04, 2018 1:19 am

Re: controlling a servo.

Sun Nov 04, 2018 7:14 pm

joan wrote:
Sun Nov 04, 2018 7:04 pm
Okay, a couple of things wrong with the code.

Code: Select all

import RPi.GPIO as GPIO
import time

GPIO.setwarnings(False) # if you want, this will get rid of all warnings

servoPIN=14
aod = 2.5
GPIO.setmode(GPIO.BCM)
GPIO.setup(servoPIN, GPIO.OUT)

p = GPIO.PWM(servoPIN, 50) #GPIO 14 for PWM with 50Hz
p.start(2.50) # Initialization
time.sleep(5)
try:
    while aod <= 5:
        aod += 1
        p.ChangeDutyCycle(aod)
        time.sleep(.5)
        print (aod)
except KeyboardInterrupt:
    aod = 0
    p.ChangeDutyCycle(aod)
    GPIO.cleanup()
Basically the if should be a while.

It still doesn't produce any pulses on my Pi though. Had me stumped for a few seconds until I realised GPIO 14 is serial TXD and I have serial enabled on my Pi. If you have the same problem change the output to another GPIO (16 say).
Good thought! I don't have it enabled but I swapped it anyway because it may have been causing some weird stuff. But sadly no, it's still doing 1 good run then random junk after it. Not as crazy as the console commands did, but still crazy.

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

Re: controlling a servo.

Sun Nov 04, 2018 7:17 pm

Are you sure it's a servo? Sounds more like a stepper motor. Can you link to the specs?

alphanerd132
Posts: 10
Joined: Sun Nov 04, 2018 1:19 am

Re: controlling a servo.

Sun Nov 04, 2018 7:21 pm

joan wrote:
Sun Nov 04, 2018 7:17 pm
Are you sure it's a servo? Sounds more like a stepper motor. Can you link to the specs?
https://www.amazon.com/gp/product/B073M ... UTF8&psc=1

I doubt it's a stepper motor based on the sheer torque of the thing. I can't move it by hand when it's turned on.

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

Re: controlling a servo.

Sun Nov 04, 2018 7:40 pm

Certainly a servo. It's digital which means it can probably accept pulse rates higher than 50Hz, but it should still accept 50Hz. I'm at a loss to know why it's misbehaving.

alphanerd132
Posts: 10
Joined: Sun Nov 04, 2018 1:19 am

Re: controlling a servo.

Mon Nov 05, 2018 5:47 am

joan wrote:
Sun Nov 04, 2018 7:40 pm
Certainly a servo. It's digital which means it can probably accept pulse rates higher than 50Hz, but it should still accept 50Hz. I'm at a loss to know why it's misbehaving.
Okay. I did fix the code with while and it fixed the issue for it not responding. But the question is why? I thought IF and WHILE for the most part where interchangeable. Also, it still does the add twitchy thing when it's set to just 1 position and sitting there. If I had an oscilloscope I could check it. Do you think it could be the power source i'm using? I'm just using a cheap 2.5 amp usb charger intended for phones. Also a note, I tried a different pi with the same cables/power supply, still had the issue.

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

Re: controlling a servo.

Mon Nov 05, 2018 7:36 am

When you use software PWM it will twitch.

One thing which would cause erratic behaviour is not having the Pi ground and servo ground connected.

alphanerd132
Posts: 10
Joined: Sun Nov 04, 2018 1:19 am

Re: controlling a servo.

Mon Nov 05, 2018 8:48 am

joan wrote:
Mon Nov 05, 2018 7:36 am
When you use software PWM it will twitch.

One thing which would cause erratic behaviour is not having the Pi ground and servo ground connected.
OHH I can see that, I don't have central grounding at my place.

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

Re: controlling a servo.

Mon Nov 05, 2018 8:58 am

The servo and Pi need a common ground reference so they can agree on the GPIO level. So you have to connect one of the ground pins on the expansion header (e.g. pin 6) to the servo ground. The most convenient place might be the ground at the servo power supply end. If you were using a battery to power the servo the same applies - you would connect a Pi ground to the -ve battery terminal.

A while introduces a block of code which will be executed while its condition is true (so 0 or more times). An if introduces a block of code which will be executed once if its condition is true (so 0 or 1 times). I may have just confused the issue for you. Have a look at one of online Python tutorials. It is important to grasp the difference as this is a construct common to most programming languages.

alphanerd132
Posts: 10
Joined: Sun Nov 04, 2018 1:19 am

Re: controlling a servo.

Mon Nov 05, 2018 9:14 am

joan wrote:
Mon Nov 05, 2018 8:58 am
The servo and Pi need a common ground reference so they can agree on the GPIO level. So you have to connect one of the ground pins on the expansion header (e.g. pin 6) to the servo ground. The most convenient place might be the ground at the servo power supply end. If you were using a battery to power the servo the same applies - you would connect a Pi ground to the -ve battery terminal.

A while introduces a block of code which will be executed while its condition is true (so 0 or more times). An if introduces a block of code which will be executed once if its condition is true (so 0 or 1 times). I may have just confused the issue for you. Have a look at one of online Python tutorials. It is important to grasp the difference as this is a construct common to most programming languages.
Ya I have watched all the way through a couple but none have actualy specified that IF only runs once and only once.

User avatar
asaenzc
Posts: 3
Joined: Fri Apr 07, 2017 7:35 am

Re: controlling a servo.

Fri Nov 09, 2018 11:00 am

Joan, do you need Hard RealTime behavior on you servo motor control in a RPi3? Minimum Latencies should be achieved (being more precising, with minimum Jitter: as less microseconds as possible because, we are trying to control whith EtherCAT servo-drives). Thanks in advance!!!

Return to “Python”