Spin continuous servo control
AngeloQ
Posts: 10
I'm trying to get two continuous servos working with the sample SPIN code provided on the Parallax site. I'm new to the Propeller and SPIN, but I reviewed the manual and the counter app note, as well as whatever I could find online, but I can't seem to find the problem.
This is what the sample code looks like for one servo, and it works fine:
I added a second servo and I would like to control it from the same cog, so I tried the following:
When the code starts, both motors start turning at the same speed and in the same direction, but then it stops (after around 2s) and doesn't do anything else. It's as if it never gets into the second repeat loop. I thought maybe the extra power draw was causing problems so I looked for transients on a scope but didn't see anything.
I assume this is a code problem, or a lack of understanding on my part.
Any thoughts?
Thanks,
Angelo
This is what the sample code looks like for one servo, and it works fine:
CON _clkmode = xtal1 + pll16x ' System clock → 80 MHz _xinfreq = 5_000_000 PUB CenterServo | tInc, tc, tCtr, tCw, tCcw, t ctra[noparse][[/noparse]30..26] := %00100 ' Configure Counter A to NCO ctra[noparse][[/noparse]8..0] := 0 frqa := 1 dira[noparse][[/noparse]0]~~ tInc := clkfreq/1_000_000 ' 1 µs increment tC := tInc * 21_500 ' Low pulse tCtr := tInc * 1500 ' Center pulse = 1.5 ms tCw := tInc * 1300 ' Clockwise fast = 1.3 ms tCcw := tInc * 1700 ' Counter-Clockwise fast = 1.7 ms t := cnt ' Mark counter time repeat 100 ' Repeat PWM signal 100x phsa := -tCw ' Set up clockwise fast pulse t += tC ' Calculate next cycle repeat waitcnt(t) ' Wait for next cycle (20 ms) repeat 100 ' Repeat PWM signal 100x phsa := -tCtr ' Set up the center pulse t += (tC + 200) ' Calculate next cycle repeat waitcnt(t) ' Wait for next cycle (20 ms) repeat 100 ' Repeat PWM signal 100x phsa := -tCcw ' Set up counter-clockwise fast pulse t += (tC - 200) ' Calculate next cycle repeat waitcnt(t) ' Wait for next cycle (20 ms)
I added a second servo and I would like to control it from the same cog, so I tried the following:
CON _clkmode = xtal1 + pll16x ' System clock → 80 MHz _xinfreq = 5_000_000 PUB CenterServo | tInc, tc, tCtr, tCw, tCcw, t ctra[noparse][[/noparse]30..26] := %00100 ' Configure Counter A to NCO ctra[noparse][[/noparse]8..0] := 0 frqa := 1 dira[noparse][[/noparse]0]~~ ctrb[noparse][[/noparse]30..26] := %00100 ' Configure Counter B to NCO ctrb[noparse][[/noparse]8..0] := 1 frqb := 1 dira[noparse][[/noparse] 1 ]~~ ' spaces added for the sake of the forum posting (otherwise it comes out "dira~~") tInc := clkfreq/1_000_000 ' 1 µs increment tC := tInc * 21_500 ' Low pulse tCtr := tInc * 1500 ' Center pulse = 1.5 ms tCw := tInc * 1300 ' Clockwise fast = 1.3 ms tCcw := tInc * 1700 ' Counter-Clockwise fast = 1.7 ms t := cnt ' Mark counter time repeat 100 ' Repeat PWM signal 100x phsa := -tCw ' Set up clockwise fast pulse (motor A) phsb := -tCw ' Set up clockwise fast pulse (motor B) t += tC ' Calculate next cycle repeat waitcnt(t) ' Wait for next cycle (20 ms) repeat 100 ' Repeat PWM signal 100x phsa := -tCtr ' Set up the center pulse (motor A) phsb := -tCtr ' Set up the center pulse (motor B) t += (tC + 200) ' Calculate next cycle repeat waitcnt(t) ' Wait for next cycle (20 ms) repeat 100 ' Repeat PWM signal 100x phsa := -tCcw ' Set up counter-clockwise fast pulse (motor A) phsb := -tCcw ' Set up counter-clockwise fast pulse (motor B) t += (tC - 200) ' Calculate next cycle repeat waitcnt(t) ' Wait for next cycle (20 ms)
When the code starts, both motors start turning at the same speed and in the same direction, but then it stops (after around 2s) and doesn't do anything else. It's as if it never gets into the second repeat loop. I thought maybe the extra power draw was causing problems so I looked for transients on a scope but didn't see anything.
I assume this is a code problem, or a lack of understanding on my part.
Any thoughts?
Thanks,
Angelo
Comments
Rich H
Great thanks! That looks good - I will give it a try.
(I would still love to know what's wrong with what I have, even if I don't use it for servo control - but to clear up my understanding of these counters.)
Thanks again
You have a scope; does the prop just stop sending control pulses after 2s?
If I use only one motor (even on the second counter) it's OK. I modified it to stagger the shut-off of the two servos and then it would continue.
So I looked more carefully with the scope and saw a ~15ms dip in the 3.3V supply to the Propeller when both motors stop together. It only dips to about 2.9V, but that's enough to reset the micro. I changed to a different 3.3V regulator and that problem seems to have gone away.
I looked around from some sample schematics and notes on power management for servos in general, but I haven't seen any special considerations made. Usually it's just a normal 3.3V regulator and a 10uF cap. This is what I started with, and eventually ended up with 100uF which still didn't help. Eventually the new regulator fixed it, but I'm concerned that the design might still be susceptible to occasional resets.
Any suggestions or tips on designing boards where servos are used?
Thanks
May be more reliable to use an independent power rail with a higher current rating for them.
Cheers,
HarryE.
Rich H
The Propeller and servos are running off different rails, in the sense they had their own regulators, but both regulators are fed from the same power source. There are no other devices switching.
The app note for the continuous rotation servo (#900-00008) shows 140+/-50mA under no load, but there is no indication of max stall current.
This board is designed to fit on the BoeBot, and is normally taking power from the 4 AA batteries. I have two servos fed power through a 7805. I can run the servos directly from the batteries, but I'm worried someone (maybe even me [noparse]:)[/noparse] will plug in a high voltage power supply to the jack. I'm not sure how tolerant those servos are to high voltages on the supply line.
Maybe I'll use a zener.
Thanks