
03-02-2004, 12:01
|
|
Registered User
no team
|
|
Join Date: Jan 2004
Location: USA
Posts: 8
|
|
|
Re: Encoder Counts/time period to PWM value
Quote:
|
Originally Posted by Jay Lundy
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.
|
This is the answer I needed. Thanks.
|