PWM Servo Controls

Help and questions on programming the codebug
Post Reply
Spanner
Posts: 1
Joined: Mon Oct 12, 2015 9:46 am

PWM Servo Controls

Post by Spanner » Mon Oct 12, 2015 9:56 am

Hi guys,

I'm having some difficulty with driving a servo, and thought you might be able to provide a bit of feedback on the code?

So, it's supposed to work like this. Press button A, and the servo goes to position 170 (the "bottom"). Press A again, and it goes to position 60 (the "top"). Continuing to press button A switches the servo between these two positions.

It works, for the most part. But if I leave the button unpressed for a short while (say, 10 seconds or longer), the whole thing becomes unresponsive. The servo does nothing until I've pressed A multiple times, or held it down for a while, and then it all comes back to life (until I leave things unpressed for a while again). It seems to work fine in the emulator, however.

Also, initially the code is set to switch the servo to 170 when it's first powered up, but this never seems to work.

Furthermore, even with the working code, if I add in a couple of other functions such as displaying a sprite, the servo control stops working.

If you guys would be good enough to take a look and let me know if I'm missing something (or maybe trying to do something the CodeBug can't handle), that's be very much appreciated.

Here's the code I've been using: http://www.codebug.org.uk/explore/codeb ... vo-driver/

Thanks in advance!

mikerr
Posts: 83
Joined: Fri Jul 31, 2015 10:37 am
Contact:

Re: PWM Servo Controls

Post by mikerr » Mon Oct 12, 2015 7:27 pm

couple of things that may help:

- you have no delay for the "off" part of the pulse, try putting a 19ms delay after the off signal:

Image
(also see graham's code http://codebugforum.co.uk/viewtopic.php?f=11&t=6)

- using "sleep until button changes" makes codebug go into a deep sleep with no display or output,
the servo will be powered down (not holding position) in that state.

- codebug goes to sleep by itself after 3 mins
to change this click the cog on the start icon,then select "sleep after" and "never".
Image
http://codebugforum.co.uk/viewtopic.php?f=11&t=23
Twitter @mikered codebug profile mikerr

pigraham
Posts: 65
Joined: Sat Aug 15, 2015 1:41 pm

Re: PWM Servo Controls

Post by pigraham » Tue Oct 13, 2015 7:56 am

You do need to keep sending the control pulse out to the servo. It doesn't have to be precise, but something around every 20 ms is normal.
If you expect to set a position then leave it alone for a long time (e.g. a door latch) then you should probably use a different approach.

You could try using a leg to switch power to the servo so that the servo is off in the idle state.

It will take some time to initialise control when you turn on and start sending control pulses, as you have found.

You will see that in my code I output a PWM pulse every time around the main loop, with a delay that gives about 20ms interval. That gives stable control. You can do other things from that loop even if they delay the PWM pulses a bit, but keep the pulses coming.

NattyAlway
Posts: 1
Joined: Mon Dec 07, 2015 5:48 pm

Re: PWM Servo Controls

Post by NattyAlway » Mon Dec 07, 2015 6:53 pm

I have gone through your problem details but i need some more information about your setup?
Can you please share all the details of all of your hardware and its specifications?
Also share your firmware?

Post Reply