Go to Post you know you're going to meet someone important, so you put on your robotics team shirt. - NorviewsVeteran [more]
Home
Go Back   Chief Delphi > Technical > Programming
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Closed Thread
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 13-01-2007, 23:30
Kevin Watson's Avatar
Kevin Watson Kevin Watson is offline
La Caņada High School
FRC #2429
Team Role: Mentor
 
Join Date: Jan 2002
Rookie Year: 2001
Location: La Caņada, California
Posts: 1,335
Kevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond repute
PWM 13-16 Replacement Code

For those who are using interrupts and don't like the jittery PWM outputs on channels 13 through 16, I've written replacement code for IFI's Generate_Pwms() function that uses the built-in CCP hardware to generate super accurate PWM pulses. The code also allows you to set the neutral point and gain for each output channel, giving you complete control of your servos and motors. As an example, this code allows you to map the entire 0-255 PWM range into a small fraction of the normal travel of a servo arm, which would come in handy if you wanted to make a more accurate range calculation to the green light.

I'd like to do a quick beta test to find any bugs or documentation problems before posting it to my web page. If you're interested in helping out, you can download the code here: http://kevin.org/frc/frc_pwm.zip. If you find any problems with code or documentation, please leave a message here.

-Kevin

Edit: Changed link to point to released code.
__________________
Kevin Watson
Engineer at stealth-mode startup
http://kevin.org

Last edited by Kevin Watson : 16-01-2007 at 02:34.
  #2   Spotlight this post!  
Unread 14-01-2007, 00:57
Noah Kleinberg Noah Kleinberg is offline
Registered User
FRC #0395 (2TrainRobotics)
Team Role: Driver
 
Join Date: Jan 2006
Rookie Year: 2006
Location: New York
Posts: 196
Noah Kleinberg is a splendid one to beholdNoah Kleinberg is a splendid one to beholdNoah Kleinberg is a splendid one to beholdNoah Kleinberg is a splendid one to beholdNoah Kleinberg is a splendid one to beholdNoah Kleinberg is a splendid one to behold
Send a message via AIM to Noah Kleinberg
Re: PWM 13-16 Replacement Code Beta Test

Quote:
Originally Posted by Kevin Watson View Post
For those who are using interrupts and don't like the jittery PWM outputs on channels 13 through 16, I've written replacement code for IFI's Generate_Pwms() function that uses the built-in CCP hardware to generate super accurate PWM pulses. The code also allows you to set the neutral point and gain for each output channel, giving you complete control of your servos and motors. As an example, this code allows you to map the entire 0-255 PWM range into a small fraction of the normal travel of a servo arm, which would come in handy if you wanted to make a more accurate range calculation to the green light.

I'd like to do a quick beta test to find any bugs or documentation problems before posting it to my web page. If you're interested in helping out, you can download the code here: http://kevin.org/frc/frc_pwm_beta.zip. If you find any problems with code or documentation, please leave a message here.

-Kevin
Sounds realy useful, thanks Kevin! I'll try to test this out within a couple of days if I can.
  #3   Spotlight this post!  
Unread 14-01-2007, 19:44
bear24rw's Avatar
bear24rw bear24rw is offline
Team 11 Programming Captain
AKA: Max T
FRC #0011 (MORT)
Team Role: Programmer
 
Join Date: Sep 2005
Rookie Year: 2005
Location: Flanders, NJ
Posts: 385
bear24rw is a splendid one to beholdbear24rw is a splendid one to beholdbear24rw is a splendid one to beholdbear24rw is a splendid one to beholdbear24rw is a splendid one to beholdbear24rw is a splendid one to beholdbear24rw is a splendid one to behold
Send a message via AIM to bear24rw
Re: PWM 13-16 Replacement Code Beta Test

So how exactly would this effect the servos controlling the camera? Would it make them move smoother, more accurate?

Looks cool
  #4   Spotlight this post!  
Unread 14-01-2007, 20:31
Noah Kleinberg Noah Kleinberg is offline
Registered User
FRC #0395 (2TrainRobotics)
Team Role: Driver
 
Join Date: Jan 2006
Rookie Year: 2006
Location: New York
Posts: 196
Noah Kleinberg is a splendid one to beholdNoah Kleinberg is a splendid one to beholdNoah Kleinberg is a splendid one to beholdNoah Kleinberg is a splendid one to beholdNoah Kleinberg is a splendid one to beholdNoah Kleinberg is a splendid one to behold
Send a message via AIM to Noah Kleinberg
Re: PWM 13-16 Replacement Code Beta Test

Is there any reason that you couldn't use "int"s to store your motor speeds and have a greater range of values with the higher precision obtained by using the CCP (that is, instead of using the gain and center #defines)? I looked through the code and don't see why this wouldn't work, what i mean is something like this as a change to the PWM() function:

Code:
void PWM(unsigned int pwm_13, unsigned int pwm_14, unsigned int pwm_15, unsigned int pwm_16)
{
//	int temp_pwm_13;
//	int temp_pwm_14;
//	int temp_pwm_15;
//	int temp_pwm_16;

// .........
	// calculate the number of 100 ns timer ticks 
	// needed to match the desired PWM pulse width 
	temp_pwm_13 = (PWM_13_GAIN * ((int)pwm_13 - 127)) + PWM_13_CENTER;
	temp_pwm_14 = (PWM_14_GAIN * ((int)pwm_14 - 127)) + PWM_14_CENTER;
	temp_pwm_15 = (PWM_15_GAIN * ((int)pwm_15 - 127)) + PWM_15_CENTER;
	temp_pwm_16 = (PWM_16_GAIN * ((int)pwm_16 - 127)) + PWM_16_CENTER;

	// load the CCP compare registers
	CCPR2L = LOBYTE(pwm_13);
	CCPR2H = HIBYTE(pwm_13);

	CCPR3L = LOBYTE(pwm_14);
	CCPR3H = HIBYTE(pwm_14);

	CCPR4L = LOBYTE(pwm_15);
	CCPR4H = HIBYTE(pwm_15);

	CCPR5L = LOBYTE(pwm_16);
	CCPR5H = HIBYTE(pwm_16);

//..............
Also, could CCPR1L and CCPR1H (or 2-5) each be 255? I ask this because with the default GAIN and CENTER values, the maximum I believe is 21400 (that is for the two combined as one word), as opposed to 65535.
  #5   Spotlight this post!  
Unread 14-01-2007, 20:44
Kevin Watson's Avatar
Kevin Watson Kevin Watson is offline
La Caņada High School
FRC #2429
Team Role: Mentor
 
Join Date: Jan 2002
Rookie Year: 2001
Location: La Caņada, California
Posts: 1,335
Kevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond repute
Re: PWM 13-16 Replacement Code Beta Test

Quote:
Originally Posted by bear24rw View Post
So how exactly would this effect the servos controlling the camera? Would it make them move smoother, more accurate?

Looks cool
Other than getting rid of the spasms, the coolest benefit (at least in my mind) is to give you more accurate control over your camera tilt servo, which can be used to calculate the range to a green light. Instead of the 0-255 range moving the servo ~180 degrees from floor to ceiling, wouldn't having the 0-255 range move the servo only 30 degrees be more useful? You gain angular precision, which gets you better range estimates. While I don't think its FIRST legal, I've been experimenting with digital servos in my pan/tilt assembly with fairly spectacular results.

-Kevin
__________________
Kevin Watson
Engineer at stealth-mode startup
http://kevin.org
  #6   Spotlight this post!  
Unread 14-01-2007, 20:52
Kevin Watson's Avatar
Kevin Watson Kevin Watson is offline
La Caņada High School
FRC #2429
Team Role: Mentor
 
Join Date: Jan 2002
Rookie Year: 2001
Location: La Caņada, California
Posts: 1,335
Kevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond repute
Re: PWM 13-16 Replacement Code Beta Test

Quote:
Originally Posted by Noah Kleinberg View Post
Is there any reason that you couldn't use "int"s to store your motor speeds and have a greater range of values with the higher precision obtained by using the CCP (that is, instead of using the gain and center #defines)? I looked through the code and don't see why this wouldn't work, what i mean is something like this as a change to the PWM() function...
Yes, certainly! I didn't do it that way because I wanted backward compatibility with Generate_Pwms(). You could have any units you want, even degrees. Cool, eh?


Quote:
Originally Posted by Noah Kleinberg View Post
Also, could CCPR1L and CCPR1H (or 2-5) each be 255? I ask this because with the default GAIN and CENTER values, the maximum I believe is 21400 (that is for the two combined as one word), as opposed to 65535.
Sure, but having pulses as long as 6.5535 ms doesn't do you much good when controlling servos and victors, which want to see inputs ranging from about 1ms to 2ms.

-Kevin
__________________
Kevin Watson
Engineer at stealth-mode startup
http://kevin.org
  #7   Spotlight this post!  
Unread 14-01-2007, 22:23
robind robind is offline
Registered User
FRC #0675
 
Join Date: Dec 2006
Location: Rohnert Park
Posts: 31
robind is an unknown quantity at this point
Re: PWM 13-16 Replacement Code Beta Test

Looks great. I'll use it tomorrow and post back with results.
  #8   Spotlight this post!  
Unread 14-01-2007, 23:04
Mike Copioli's Avatar
Mike Copioli Mike Copioli is offline
You make it pretty We make it dance
no team (Retired(3539, 217))
Team Role: Engineer
 
Join Date: Jan 2006
Rookie Year: 2001
Location: Romeo
Posts: 454
Mike Copioli has a reputation beyond reputeMike Copioli has a reputation beyond reputeMike Copioli has a reputation beyond reputeMike Copioli has a reputation beyond reputeMike Copioli has a reputation beyond reputeMike Copioli has a reputation beyond reputeMike Copioli has a reputation beyond reputeMike Copioli has a reputation beyond reputeMike Copioli has a reputation beyond reputeMike Copioli has a reputation beyond reputeMike Copioli has a reputation beyond repute
Re: PWM 13-16 Replacement Code Beta Test

Hey Kevin? Does your code leave a pause between pulses sent from the CCP module? After looking at your code I did not see anything to indicate this. The reason I ask is because I have noticed that when continuous pulses are sent to a servo using the CCP module, the servo exhibits jitter. I have found that a 2ms delay or greater between pulses will stop this behavior. Have you encountered similar behavior?
__________________
Mike Copioli
CTRE Hardware Engineer
http://www.ctr-electronics.com

Team 3539 The Byting Bull Dogs
2013 Michigan State Champions
Team 217 The Thunder Chickens
2006 World Champions
2008 World Champions
2009 Michigan State Champions
  #9   Spotlight this post!  
Unread 14-01-2007, 23:38
Kevin Watson's Avatar
Kevin Watson Kevin Watson is offline
La Caņada High School
FRC #2429
Team Role: Mentor
 
Join Date: Jan 2002
Rookie Year: 2001
Location: La Caņada, California
Posts: 1,335
Kevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond repute
Re: PWM 13-16 Replacement Code Beta Test

Quote:
Originally Posted by Mike Copioli View Post
Hey Kevin? Does your code leave a pause between pulses sent from the CCP module? After looking at your code I did not see anything to indicate this. The reason I ask is because I have noticed that when continuous pulses are sent to a servo using the CCP module, the servo exhibits jitter. I have found that a 2ms delay or greater between pulses will stop this behavior. Have you encountered similar behavior?
Like Generate_Pwms(), my code only generates one pulse on each of the four outputs each time it's called. If called from Process_Data_From_Master_uP(), the update rate is ~38 Hz. Typical servos won't handle a rate much higher than this, but the Victors are okay to at least 100 Hz.

-Kevin
__________________
Kevin Watson
Engineer at stealth-mode startup
http://kevin.org
  #10   Spotlight this post!  
Unread 15-01-2007, 09:54
Mike Copioli's Avatar
Mike Copioli Mike Copioli is offline
You make it pretty We make it dance
no team (Retired(3539, 217))
Team Role: Engineer
 
Join Date: Jan 2006
Rookie Year: 2001
Location: Romeo
Posts: 454
Mike Copioli has a reputation beyond reputeMike Copioli has a reputation beyond reputeMike Copioli has a reputation beyond reputeMike Copioli has a reputation beyond reputeMike Copioli has a reputation beyond reputeMike Copioli has a reputation beyond reputeMike Copioli has a reputation beyond reputeMike Copioli has a reputation beyond reputeMike Copioli has a reputation beyond reputeMike Copioli has a reputation beyond reputeMike Copioli has a reputation beyond repute
Re: PWM 13-16 Replacement Code Beta Test

Quote:
Originally Posted by Kevin Watson View Post
Like Generate_Pwms(), my code only generates one pulse on each of the four outputs each time it's called. If called from Process_Data_From_Master_uP(), the update rate is ~38 Hz. Typical servos won't handle a rate much higher than this, but the Victors are okay to at least 100 Hz.

-Kevin
Ok, so does that mean you can not call it in the fast loop? If this is the case, is there an advantage to using PWM's 13-16? If the outputs can only be updated every 26.2 ms (38Hz) For example if I wanted to use 13 and 14 for camera servo control, to move 200 steps would take over 5 seconds if it moved in one step increments (200 * .0262s). I really would like to see the camera "snap" to a position. It seems that nasty little 26.2ms loop time keeps getting in the way.
__________________
Mike Copioli
CTRE Hardware Engineer
http://www.ctr-electronics.com

Team 3539 The Byting Bull Dogs
2013 Michigan State Champions
Team 217 The Thunder Chickens
2006 World Champions
2008 World Champions
2009 Michigan State Champions
  #11   Spotlight this post!  
Unread 15-01-2007, 11:12
kaszeta's Avatar
kaszeta kaszeta is offline
Registered User
FRC #0095 (Grasshoppers)
Team Role: Mentor
 
Join Date: Feb 2004
Rookie Year: 2002
Location: Lebanon, NH
Posts: 334
kaszeta is a glorious beacon of lightkaszeta is a glorious beacon of lightkaszeta is a glorious beacon of lightkaszeta is a glorious beacon of lightkaszeta is a glorious beacon of light
Re: PWM 13-16 Replacement Code Beta Test

Quote:
Originally Posted by Noah Kleinberg View Post
Is there any reason that you couldn't use "int"s to store your motor speeds and have a greater range of values with the higher precision obtained by using the CCP (that is, instead of using the gain and center #defines)?
You can do this. Last year I cobbled together a similar set of routines for controlling PWM13 and 14 for the camera with 10 bit precision instead of 8 bit precision. I'll see if I can still find the code (we ended up not using the camera in the end, so that code got removed since it wasn't needed).
  #12   Spotlight this post!  
Unread 15-01-2007, 12:55
Kevin Watson's Avatar
Kevin Watson Kevin Watson is offline
La Caņada High School
FRC #2429
Team Role: Mentor
 
Join Date: Jan 2002
Rookie Year: 2001
Location: La Caņada, California
Posts: 1,335
Kevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond repute
Re: PWM 13-16 Replacement Code Beta Test

Quote:
Originally Posted by Mike Copioli View Post
Ok, so does that mean you can not call it in the fast loop? If this is the case, is there an advantage to using PWM's 13-16?
Yes, you could call it in the fast loop to control your Victors at a higher rate for applications like position control (which generally works better at rates greater than 38 Hz). You'll need to synchronize the updates to a timer or some sensor like a gyro.

I've already enumerated some other advantages above.

Quote:
Originally Posted by Mike Copioli View Post
If the outputs can only be updated every 26.2 ms (38Hz) For example if I wanted to use 13 and 14 for camera servo control, to move 200 steps would take over 5 seconds if it moved in one step increments (200 * .0262s). I really would like to see the camera "snap" to a position. It seems that nasty little 26.2ms loop time keeps getting in the way.
Your searching algorithm needs tuning. The camera has a fairly wide field of view, so I think you could use a much larger step. I would step in 30 degree increments, wait a bit for the camera to send a t-packet (or two) and then either start tracking, or step another 30 degrees... If you're using my code, these are built-in parameters you can tweak.

For grins, I used digital servos (not FRC legal) with my code and after modifying tracking.c a bit, I can find a green light and start tracking in somewhere between one and two seconds, without the software having any a priori knowledge of where the light is located. I suspect nearly the same level of performance can be had with the HS322HD servos.

-Kevin
__________________
Kevin Watson
Engineer at stealth-mode startup
http://kevin.org
  #13   Spotlight this post!  
Unread 15-01-2007, 15:53
Mike Bortfeldt Mike Bortfeldt is offline
Registered User
FRC #1126 (& 1511)
Team Role: Mentor
 
Join Date: Oct 2004
Rookie Year: 2004
Location: Rochester, NY
Posts: 119
Mike Bortfeldt has much to be proud ofMike Bortfeldt has much to be proud ofMike Bortfeldt has much to be proud ofMike Bortfeldt has much to be proud ofMike Bortfeldt has much to be proud ofMike Bortfeldt has much to be proud ofMike Bortfeldt has much to be proud ofMike Bortfeldt has much to be proud of
Re: PWM 13-16 Replacement Code Beta Test

Kevin,

I would expect that the code should work fine. We (1126) used my version throughout the 2006 season without any issues. Your increase in gain from 40 to 50 is probably a good idea, as we found one of the Victors did not quite go to maximum at the high end (2 msec) without recalibration. The additional 25% on range should easily eliminate this potential problem. You may want to think about modifying your "temp_pwm_xx" calculation so it doesn't require the signed int multiplication. This adds a fair amount of execution time to the routine (relative to how quick it could execute, not to overall processor usage which is extremely minor). It also requires the use of the MATH_DATA section so if someone ever wanted to use the routine from within an interrupt (why? I don't know, but I never rule anything out), they would need to save that data section. I do like the addition of the CENTER & GAIN options it allows for much more flexibility.

Mike
  #14   Spotlight this post!  
Unread 15-01-2007, 16:03
AdamHeard's Avatar
AdamHeard AdamHeard is offline
Lead Mentor
FRC #0973 (Greybots)
Team Role: Mentor
 
Join Date: Oct 2004
Rookie Year: 2004
Location: Atascadero
Posts: 5,526
AdamHeard has a reputation beyond reputeAdamHeard has a reputation beyond reputeAdamHeard has a reputation beyond reputeAdamHeard has a reputation beyond reputeAdamHeard has a reputation beyond reputeAdamHeard has a reputation beyond reputeAdamHeard has a reputation beyond reputeAdamHeard has a reputation beyond reputeAdamHeard has a reputation beyond reputeAdamHeard has a reputation beyond reputeAdamHeard has a reputation beyond repute
Send a message via AIM to AdamHeard
Re: PWM 13-16 Replacement Code Beta Test

So... is the legality of this confirmed?

-Adam
  #15   Spotlight this post!  
Unread 15-01-2007, 16:39
Kevin Watson's Avatar
Kevin Watson Kevin Watson is offline
La Caņada High School
FRC #2429
Team Role: Mentor
 
Join Date: Jan 2002
Rookie Year: 2001
Location: La Caņada, California
Posts: 1,335
Kevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond repute
Re: PWM 13-16 Replacement Code Beta Test

Quote:
Originally Posted by Mike Bortfeldt View Post
Kevin,

I would expect that the code should work fine. We (1126) used my version throughout the 2006 season without any issues. Your increase in gain from 40 to 50 is probably a good idea, as we found one of the Victors did not quite go to maximum at the high end (2 msec) without recalibration. The additional 25% on range should easily eliminate this potential problem. You may want to think about modifying your "temp_pwm_xx" calculation so it doesn't require the signed int multiplication. This adds a fair amount of execution time to the routine (relative to how quick it could execute, not to overall processor usage which is extremely minor). It also requires the use of the MATH_DATA section so if someone ever wanted to use the routine from within an interrupt (why? I don't know, but I never rule anything out), they would need to save that data section. I do like the addition of the CENTER & GAIN options it allows for much more flexibility.

Mike
Thanks. To be compatible with Generate_Pwms() and the Victors, I needed to use a 5 us step. Servos seem to like a narrower range of about 1.1 ms to 1.9 ms (gain = 32).

I used the signed int so that I don't have to worry about underflow when the subtraction takes place. I'll have another look to see if I can move the terms around to get rid of the int.

-Kevin
__________________
Kevin Watson
Engineer at stealth-mode startup
http://kevin.org
Closed Thread


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Vex start up code run PWM outputs without Rx intellec7 Programming 3 05-05-2006 23:29
Pwm 11, 12 and CMU code Chris Bright Programming 3 01-02-2005 19:34
Victor Replacement JamesCH95 Electrical 4 15-12-2004 20:12
Replacement Parts archiver 1999 0 23-06-2002 22:01
replacement gears joni_m Technical Discussion 1 22-01-2002 18:01


All times are GMT -5. The time now is 02:08.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi