Sorry I meant to start a new conversation
This is our team’s first flywheel velocity control attempt, and we do not have much experience with PID or bang bang code. We were wondering if anyone could take some time to review this code before we try it on our flywheel.
float barShotRPM = 2000 //Set RPM here for bar shots. Tune this!!!
void currentSpeed() { SensorValue[flywheelEncoder]*5 }
void flyMotorSpeed(int speed)
{
motor[port10] = speed;
motor[port1]= speed;
}
if(currentSpeed <= barShotRPM)
{
flyMotorSpeed(120);
}elseif(currentSpeed > barshotRPM)
{
flyMotorSpeed(90);
}
Flywheel Specs:
2 high speed motors
84:36, 60:12 compound ratio (1:11.666)
STRICTLY bar shots
encoder is mounted on the 36 and 60 compound gear axle
Please excuse any syntax errors at the moment, as I’m still a novice programmer, but do add any input. The logic behind setting the acceleration speed at 120 was to keep a decent acceleration time, while keeping the motor stress down. We’re not sure if the next part will work, but the thought process was if the motors slowed down then the flywheel would slow down too. As I saw above, going from 127 to 0 would stress the motors, so I made the flywheel stall speed at 90. Hopefully my explanation was clear, but ask if you need clarification. Once again my team and I would appreciate any feedback.
That is a good start. I’ll mention a few things to change.
To start off, it won’t stress the motors to increase from 90 to 127, so it’s okay to have 127 instead of 120 (I’m using 70 or so to 127 and haven’t have any problems).
In order to calculate speed, you have to take delta distance over delta time. jpearman has great code on Github Here’s a portion of it (slightly edited):
// Encoder
long encoder_counts; ///< current encoder count
long encoder_counts_last; ///< current encoder count
// velocity measurement
float motor_velocity; ///< current velocity in rpm
long nSysTime_last; ///< Time of last velocity calculation
long
FwMotorEncoderGet()
{
return( SensorValue[flywheelEncoder] );
}
void FwCalculateSpeed()
{
int delta_ms;
int delta_enc;
// Get current encoder value
encoder_counts = FwMotorEncoderGet();
// This is just used so we don't need to know how often we are called
// how many mS since we were last here
delta_ms = nSysTime - nSysTime_last;
nSysTime_last = nSysTime;
// Change in encoder count
delta_enc = (encoder_counts - encoder_counts_last);
// save last position
encoder_counts_last = encoder_counts;
// Calculate velocity in rpm
motor_velocity = (1000.0 / delta_ms) * delta_enc * 60.0 / 360;
}
Now, the hard part is done 
You would need something to set the target for the flywheel. Personally, I just do:
VelocitySet(Target, Predicted Motor Power)
to call it. It will look something like:
int predictedMotorPower;
int target;
void VelocitySet(int setTarget, int predicted)
{
target = setTarget;
predictedMotorPower = predicted;
}
You would call this with button presses from the joystick.
The rest is simple; basically, the full code (without definitions and stuff) is:
void VelocitySet()
/* ... */
task FwControlTask()
{
while(1)
{
FwCalculateSpeed();
if(currentSpeed <= target)
{
flyMotorSpeed(127);
}
else if(currentSpeed > target)
{
flyMotorSpeed(predictedMotorPower);
}
sleep(25); //You need this sleep here so that the Cortex can multitask between drive and flywheel
}
}
task main()
{
startTask(FwControlTask);
while(1)
{
if(vexRT//button you are using to start] == 1)
{
VelocitySet(barShotRPM,90);
}
else if(vexRT//button you are using to stop] == 1)
{
VelocitySet(0,0);
}
Driving Code //You need a sleep(whatever) here too
}
}
I think that’s all. If I’m missing anything, someone can add on.
EDIT: I messed up, so I’ll be editing for a bit.
EDIT2: I think I fixed the issue.
I used Jpearman’s code and it doesn’t seem to have any speed control. It was working before but now the flywheel doesn’t stop when I hit the 0 speed.