View Single Post
  #9   Spotlight this post!  
Unread 02-02-2004, 23:37
Jay Lundy Jay Lundy is offline
Programmer/Driver 2001-2004
FRC #0254 (The Cheesy Poofs)
Team Role: Alumni
 
Join Date: Jun 2001
Rookie Year: 2001
Location: Berkeley, CA
Posts: 320
Jay Lundy is a name known to allJay Lundy is a name known to allJay Lundy is a name known to allJay Lundy is a name known to allJay Lundy is a name known to allJay Lundy is a name known to all
Re: Encoder Counts/time period to PWM value

Correct me if I'm wrong (which is entirely possible) but I think this is the idea of the PID loop.

First of all, it is impossible to do a straight conversion between target encoder counts/second to pwm output, since there are always going to be external forces acting on your robot to either speed it up or slow it down.

You need to compare actual encoder counts/sec with a target encoder counts/sec, and use that to adjust your pwm output. Something like this.

pwmOut += (targetEncoderCps - actualEncoderCps) * P_CONSTANT

I would convert your joystick input to a target encoder counts/sec. So if your top speed is 10 rev/sec and you have an encoder with 100 counts/rev (for the sake of simplicity I know encoders don't come like that) then at top speed you would be getting 1000 counts/sec. So I would do:

targetEncoderCps = (p1_y - 127) / 127 * 1000

First step is to center the joystick at 0, then convert it to the range [-1,1], then convert it to the range [-1000,1000]. Of course, if you are dealing with integer math (highly recommended), then do this:

targetEncoderCps = ((long)p1_y - 127) * 1000 / 127

The cast is necessary to make p1_y signed and to make sure *1000 does not overflow.