Programming a servo in SPIN
rlmjdime
Posts: 2
Hello all. I am very new to programming and seem to hit a wall every time I try and write a program. I'm playing around with a servo, and trying to get it to stop at 0 and 180 degrees. I used the example programs in the propeller tool, and was able to get it right. I don't really understand everything that the object files were doing, so I'm trying to recreate using my own code. Maybe my problems stem from not fully understanding the pulse widths, but my own code runs smooth to 0 degrees and then begins to step to 180.
I'm trying to gain an understanding of using multiple cogs, so that is why I implemented the PUB LED. But after adding this my program does nothing. The aim of the code was to have the servo to and fro the extremes, and light the led when it gets there. I actually got the idea for the program from one of the exercises in the propeller education kit.
I've been bouncing around with this for a bit, so any direction and insight is most appreciated.
{{Servomotor driver}} CON _clkmode = xtal1 + pll16x _xinfreq = 5_000_000 VAR long stack[30] long count 'variable to establish start angle long count1 'variable to establish end angle PUB Main cognew(Moveservo(5), @stack[0]) 'cog to move the servo cognew(LED, @stack[20]) 'cog to flash the led PUB Moveservo (pin) dira[5] := 1 'pin 5 set to ouput repeat 'repeat the cycle count1 := (18 * 1111 + 50000)/1000 'set the variable to 18 degrees outa[pin]~~ 'set pin to high waitcnt(clkfreq/100000*count1 + cnt) 'use count 1 and create pulse for 18 degrees outa[pin]~ 'set pin to low waitcnt(clkfreq/200*2 + cnt) 'wait 20 ms count := (162 * 1111 + 50000)/1000 'set the count variable to 162 degrees outa[pin]~~ 'set pin high waitcnt(clkfreq/100000*count + cnt) 'use count and create pulse for 162 degrees outa[pin]~ 'pin low waitcnt(clkfreq/200*2 + cnt) 'wait 20 ms PUB LED dira[0] := 1 'pin 0 set to output repeat 'repeat cycle if outa[5]~ 'if pin 5 low outa[0] := 1 ' led on else outa[0] := 0 'else led off
I'm trying to gain an understanding of using multiple cogs, so that is why I implemented the PUB LED. But after adding this my program does nothing. The aim of the code was to have the servo to and fro the extremes, and light the led when it gets there. I actually got the idea for the program from one of the exercises in the propeller education kit.
I've been bouncing around with this for a bit, so any direction and insight is most appreciated.
Comments
Here's one example of code that would move the motor from one position to another with a pause after each movement.
I because I like to show off, whenever I see mention of using a Propeller to control a servo, I feel the need to remind people of my 32 servo demo. I had seen it mentioned many times that the Propeller could drive up to 32 servos but I hadn't see it done (probably because it's kind of a lot of work without a real useful purpose (other than to show off the fact I have 32 servos)).
It's very possible to add a LED to the servo code. There are many ways to do it. If you want to use another cog, you need to keep a few rules in mind. One rule is don't set pin states from more than one cog. Another is to make sure you have enough stack space.
The easiest way to see what went wrong is to post the code.
BTW, are you using a Propeller board or a DIP chip on a breadboard? If you are using a board, which one?
Note: this is great for very simple control of one or two servos. When you want to control multiple servos and add features like ramping, it's best to use a servo object -- many of us have written them so you have a lot of good choices.