Trouble shooting a simple spin servo driver
Copper
Posts: 48
So I've run into a little trouble with my servo driver, and I'm sort of at a loss as to how I should proceed.
First off, you should know, I'm writing this driver as a learning exercise. I'm very new to programming. So while it may seem like a wasted effort, I would really like to continue developing this bit of code. I have downloaded and mulled over a number of servo drivers from the object exchange, but find most of it goes over my head. So I'm hoping building this driver from the ground up will help to expose me to all that needs to be addressed in a servo driver, and at least one way of doing it, so that I can begin to explore other drivers with some understanding.
I believe I have narrowed the problem down to some inter-relationship between multiple cog management and my signal timing.
Above, Is a trimmed up version of my driver (no ramping or serial port communication [neither of which probably work yet anyhow; we'll leave that for next time]).
When I run the code, the servo drives for 3sec at the given rate, but then there is a min+ delay before it starts again, it does start at the given rate and runs for 3sec, but I can't get it to start a third time (by changing the given parameters) or reduce the delay.
Hopefully something is glaringly wrong and easily explained, but whatever I've done wrong it's not apparent to me.
As always,
Thanks for your help.
First off, you should know, I'm writing this driver as a learning exercise. I'm very new to programming. So while it may seem like a wasted effort, I would really like to continue developing this bit of code. I have downloaded and mulled over a number of servo drivers from the object exchange, but find most of it goes over my head. So I'm hoping building this driver from the ground up will help to expose me to all that needs to be addressed in a servo driver, and at least one way of doing it, so that I can begin to explore other drivers with some understanding.
I believe I have narrowed the problem down to some inter-relationship between multiple cog management and my signal timing.
{{Basic Servo Stuff}}CON _clkmode = xtal1 + pll16x _xinfreq = 5_000_000 #0, For4, For3, For2, For1, stop, Rev1, Rev2, Rev3, Rev4 VAR long stackspace[100] word cog PUB Main Start_Servo(12, For2, 3000) waitcnt(clkfreq*3) Start_Servo(12, For4, 3000) waitcnt(clkfreq*3) Start_Servo(12, Rev2, 6000) waitcnt(clkfreq*6) PUB Start_Servo(servoPin, speed, duration) cog:=cognew(Run_servo(servoPin, speed, duration), @stackspace[0]) + 1 PUB Stop_Servo if cog cogstop(cog~ - 1) PUB Run_Servo(servoPin, speed, duration) | tInc, dInc, tc, tHa, t, run_time 'configure counterA ctra := (100 << 26 | servoPin) 'Counter A to NCO & Apin to servoPin frqa := 1 'increment phsa by 1 dira[servoPin]~~ 'set servoPin to output 'configure pwm signal tInc := clkfreq/1_000_000 'set tInc == 1 microsecond tC := tInc * 20_000 'determine pulse delay tHa := tInc * Speeds[speed] 'determine pulse width t := cnt 'mark counter time 'detirmine runtime dInc := clkfreq/1000 'set dInc == 1millisecond dInc == Duration Resolution run_time:=((duration*dInc)+t) 'set run_time 'begin sending signal repeat until t=>run_time 'start PWM signal phsa := -tHa 'send pulse for tHa seconds t += tC 'calculate end of cycle waitcnt(t) 'wait for next cycle DAT Speeds long 1000, 1200, 1450, 1500, 1515, 1530, 1580, 1830, 2000
Above, Is a trimmed up version of my driver (no ramping or serial port communication [neither of which probably work yet anyhow; we'll leave that for next time]).
When I run the code, the servo drives for 3sec at the given rate, but then there is a min+ delay before it starts again, it does start at the given rate and runs for 3sec, but I can't get it to start a third time (by changing the given parameters) or reduce the delay.
Hopefully something is glaringly wrong and easily explained, but whatever I've done wrong it's not apparent to me.
As always,
Thanks for your help.
Comments
and kuroneko was right about the waitcnt command. So I went about adding a method for Ramping the servo speed, and a debug method to report the actual "speed" of each pulse. But I've hit another roadblock.
I think the problem is in my ramping method
I'm sure there is some fundamental flaw in my approach here; of course Whatever advice you have would be greatly appreciated.
PS: given the delay between posts, should I start a new thread?
Only if you want to be annoying! It's considered poor forum etiquette to start multiple threads on the same topic. Assume that you are the problem; perhaps you haven't provided enough information or details (the root of specification is 'specific'). Maybe you've been given viable guidance and just cannot or will not grasp it at your current level of competence.
I think I pointed out that I and others have written viable servo drivers. I would suggest walking away from your project -- only for a moment -- to disect what others have done. Then go back. I use this trick on myself all the time. When I'm stuck, I turn to another technical bit of work to get my mind moving again. You may find, as I frequently do, that the time spent away allows you to see your project in a new light when you come back to it.
PS: I asked about new threads because I was un-sure of the existing forum etiquette.