|
|
|
![]() |
|
|||||||
|
||||||||
![]() |
| Thread Tools | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Encoder Counts/time period to PWM value
Hey guys, I have no clue how to do this. How do I convert the number of counts I receive from the encoder over a given time period to a corresponding PWM value?
Thanks |
|
#2
|
||||
|
||||
|
Re: Encoder Counts/time period to PWM value
Quote:
|
|
#3
|
||||
|
||||
|
Re: Encoder Counts/time period to PWM value
Quote:
-Kevin |
|
#4
|
|||
|
|||
|
Re: Encoder Counts/time period to PWM value
Quote:
|
|
#5
|
||||
|
||||
|
Re: Encoder Counts/time period to PWM value
Quote:
-Kevin |
|
#6
|
|||
|
|||
|
Re: Encoder Counts/time period to PWM value
Quote:
int PID(int request, int actual, int *lastActual, signed int *sum) In my code, request and actual will be PWM values from 0-255. The request PWM value will come from input from the joystick. The actual PWM value will come from the encoder. My question is, the encoder produces counts/time period. How do you get an actual PWM value from this. Or am I going about it all wrong? |
|
#7
|
||||
|
||||
|
Re: Encoder Counts/time period to PWM value
what you could try instead is if you wanted to use PID to make the PWMs function on a more linear scale, measure your top speed (when the PWM is set to 254) and then take any speed, divide it by the top speed, and multiply by 254 - this becomes your "actual" pwm. that way, the PID loop would result in the PWM control being more linear.
|
|
#8
|
|||
|
|||
|
Re: Encoder Counts/time period to PWM value
Quote:
|
|
#9
|
|||
|
|||
|
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. |
|
#10
|
|||
|
|||
|
Re: Encoder Counts/time period to PWM value
Quote:
|
|
#11
|
|||
|
|||
|
Re: Encoder Counts/time period to PWM value
Quote:
If you use the suggested equation(s) in this message, you will only be using the 'P' in PID. As in, the pwm01 is being changed only by a proportion of the error. You need to track the integral (sum) of the error, and the derivative (change/time) of the error and consider these in creating a PWM value to be sent to the motors. |
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Some EDURobotics Problems | squide | Robotics Education and Curriculum | 9 | 20-01-2004 10:27 |
| pwm 13-15 | wayne 05 | Programming | 2 | 04-10-2003 12:08 |
| PWM and burning out motors | patrickrd | Technical Discussion | 7 | 19-06-2003 15:30 |
| Loss of signal in PWM cables. | archiver | 2001 | 6 | 23-06-2002 23:29 |