I’m trying to write an implementation of TBH for my flywheel, however it is not working right. It never reaches the correct speed it is supposed to be at, and I determined this was because the RPM calculation was incorrect. Also, the current and former position variables are always the same value as each other. Could someone inspect the algorithm to help figure out why the RPM calculation always returns “0”.
int fRPM = 0;
int Ferror = 0;
float trueRPM = 0;
int position = 0;
int prevposition = 0;
int Fpreverror =0;
int tbh = 0;
float speed = 0;
task TBHFlywheel()
{
float gain = .00005;
SensorValue[flywheel] = 0;
bool firstcross = true;
while(true)
{
position = SensorValue[flywheel];
trueRPM = (position - prevposition)/360 / 50 * 1000 * 60 * 35;
Ferror = fRPM - trueRPM;
wait1Msec(10);
prevposition = position;
speed = speed + (Ferror *gain);
if((Ferror>0)!= (Fpreverror>0))
{
speed = .5*(speed +tbh);
tbh = speed;
Fpreverror = Ferror;
//firstcross = false;
}
if(speed > 120)
{
speed = 120;
}
if(speed <0)
{
speed = 0;
}
wait1Msec(40);
if(fRPM == 0)
{
firstcross = true;
}
}
}
fRPM is the value set in driver control to control the speed. The Quad-Encoder does work and counts positive, and is directly connected to a high strength 393.