View Single Post
  #4   Spotlight this post!  
Unread 22-07-2012, 09:47
apalrd's Avatar
apalrd apalrd is offline
More Torque!
AKA: Andrew Palardy (Most people call me Palardy)
VRC #3333
Team Role: College Student
 
Join Date: Mar 2009
Rookie Year: 2009
Location: Auburn Hills, MI
Posts: 1,347
apalrd has a reputation beyond reputeapalrd has a reputation beyond reputeapalrd has a reputation beyond reputeapalrd has a reputation beyond reputeapalrd has a reputation beyond reputeapalrd has a reputation beyond reputeapalrd has a reputation beyond reputeapalrd has a reputation beyond reputeapalrd has a reputation beyond reputeapalrd has a reputation beyond reputeapalrd has a reputation beyond repute
Re: Help with Speed control using Cortex and US Digital Encoders

Sorry I couldn't reply sooner, I was at the IRI.

-The gear ratio from the transmission output to the wheel is the only ratio you need. If you direct-drive the wheel with the same shaft as the encoder, or have a 1:1 chain or gear ratio between the encoder's shaft and the wheel, you don't need to include that ratio in the calculation. If you have any chain reduction between the wheel and the encoder, you will need to include it. The ratio between the wheel and encoder is NOT the same ratio as between the wheel and motors.

-The RobotC code would look something like this:
Code:
int setpoint = 9999; //This is the ticks/iteration you are trying to achieve
int integrator = 0;   //This is the integrator value
int enc_last = 0;     //This is the last encoder value
int enc_cur = 0;     //This is the current encoder value
int vel_cur = 0;      //This is the current velocity
int vel_error = 0;    //This is the error in velocity
int pwm_out = 0;   //This is the motor output value
float ki = 0.01;      //This is the gain constant - You MUST tune this value

task main()
{

while(1) {
//Read the sensor into a cache var
enc_cur = SensorValue[MyEncoder];//You need to name the encoder in the sensors window
vel_cur = enc_cur - enc_last;
enc_last = enc_cur;//Store current encoder value for next iteration

//Calculate the error
vel_error = setpoint - vel_cur;

//Add to the integrator
integrator += (vel_error * ki);

//Cap the integrator to +-127
if(integrator > 127) integrator = 127;
if(integrator < -127) integrator = -127;

//Set the integrator to the motor
pwm_out = integrator
motors[1] = pwm_out;//I might have the syntax slightly off for the motors, check RobotC help

Wait1Msec(20);
}//Loop

}//End of task main
-These calculations are for one wheel. If you have two encoders (one per side), you should do two sets of calculations, one for each side. Make sure to re-name the variables to accomodate this. You can use the same task and loop.

-You should add more code so it dosen't just run forward always (possibly a state-machine for the drive action?)

-The distance per count would be:
(wheel radius)*(2)*(pi)
(number of counts)

-The counts/iteration the program needs would be:
(desired m/sec) * (1/50) * (1/dist per count)
The 1/50 is the scaling for the time (as the loop is running at 50hz)
The 1/dist per count is the counts per distance
__________________
Kettering University - Computer Engineering
Kettering Motorsports
Williams International - Commercial Engines - Controls and Accessories
FRC 33 - The Killer Bees - 2009-2012 Student, 2013-2014 Advisor
VEX IQ 3333 - The Bumble Bees - 2014+ Mentor

"Sometimes, the elegant implementation is a function. Not a method. Not a class. Not a framework. Just a function." ~ John Carmack