Help converting code from PWM to PSC
bulkhead
Posts: 405
I have recently decided to move all servo controlling tasks to a PSC I just got, and I would like some input on the easiest possible way to do this. I already have written a fairly large program that controls servos using 6 PWM objects that looks something like this:
{
//declarations
public static PWM rightDrive = new PWM(CPU.pin15,159,2304);
public static PWM leftDrive = new PWM(CPU.pin12,157,2304);
public static PWM dumper = new PWM(CPU.pin11,248,2304);
public static PWM leftArm = new PWM(CPU.pin9,270,2304);
public static PWM rightArm = new PWM(CPU.pin10,065,2304);
public static PWM arm = new PWM(CPU.pin8,170,2304);
//sample useage as in various methods
dumper.update(100,2304);
arm.update(242,2304);
//etc
}
After looking at how other's use their PSC and javelin, I thought about creating a new "Servo" class with a method "update(int x, int y)". That way, all I need to do is change the declarations (class Servo instead of class PWM), and all my method calls would still do the correct tasks. If it could be done this way, I'd rather not go through my whole program and change the (probably around 50) "update" method calls to some other method call(s).
My question is, regarding the PSC, what needs to be done to communicate serially to the PSC? I understand that a Uart needs to be created on a pin, and that instructions sent through that to change servo positions, but I don't really know how to code this. More specifically, how would a "Servo" class like I described look, and how would I use that class with my main program? Like what new lines of code do I need to add in the main program (other than changing the declarations from PWM to Servo)? Also, how do I download both the main program and Servo class onto the Javelin?
Thanks.
{
//declarations
public static PWM rightDrive = new PWM(CPU.pin15,159,2304);
public static PWM leftDrive = new PWM(CPU.pin12,157,2304);
public static PWM dumper = new PWM(CPU.pin11,248,2304);
public static PWM leftArm = new PWM(CPU.pin9,270,2304);
public static PWM rightArm = new PWM(CPU.pin10,065,2304);
public static PWM arm = new PWM(CPU.pin8,170,2304);
//sample useage as in various methods
dumper.update(100,2304);
arm.update(242,2304);
//etc
}
After looking at how other's use their PSC and javelin, I thought about creating a new "Servo" class with a method "update(int x, int y)". That way, all I need to do is change the declarations (class Servo instead of class PWM), and all my method calls would still do the correct tasks. If it could be done this way, I'd rather not go through my whole program and change the (probably around 50) "update" method calls to some other method call(s).
My question is, regarding the PSC, what needs to be done to communicate serially to the PSC? I understand that a Uart needs to be created on a pin, and that instructions sent through that to change servo positions, but I don't really know how to code this. More specifically, how would a "Servo" class like I described look, and how would I use that class with my main program? Like what new lines of code do I need to add in the main program (other than changing the declarations from PWM to Servo)? Also, how do I download both the main program and Servo class onto the Javelin?
Thanks.
Comments
http://forums.parallax.com/showthread.php?p=544652
Near the last post you find a link to my psc class.
regards peter
is that psc operates with position values 250-1250 and I believe these
correspond to duty cycle values 250-1250, where 750 equals 50% dutycycle.
So to convert, you need to convert your high and low times to
duty values, and if hightime == lowtime than position = 750.
Not so easy.
regards peter
position = (hightime*1500)/(lowtime+hightime)
but you must consider overflow
for hightime = 2304 and lowtime=270
the exact position = 1343
Javelin calculation position = ((hightime*10)/(lowtime+hightime))*150
= (23040/2574)*150 = 8*150 = 1200
If you round 23040/2574 by calculating (23040+(2574/2))/2574 = 9
you get position = 9*150 = ·1350
So rounding gets you close but position resolution is 150.
The formule for rounding
position = (((hightime*10) + ((lowtime+hightime)/2))/(lowtime+hightime))*150
regards peter