Need Bang Bang Code Help

I have been trying for so long to try and get my single fly wheel to shoot four balls in a very short period of time but I can not get it to speed back up fast enough. I have four motors on my fly wheel all high speed and right now I am trying to make a bang bang code to do what I want to do. I have also tried putting more then one wheel on my fly wheel but it is not helping enough. So I know I need code to do what I want to do but I just can not get it to work, can anyone help?

Moving to the general forum, the community can probably give you more feedback.

I’m not a programmer, but I have to question why you would use bang bang.

Because bang bang works :stuck_out_tongue:
At least, it worked well for my team.
I’m not the programmer, so I couldn’t tell you how to code it. Sorry. :confused:

If you are not getting the spin up time you want when using bang-bang, then I would look at the mechanical system of your flywheel. Try reducing friction making sure that all shafts and bearing blocks are aligned, and try placing some lubrication on the gears running your flywheel.

A PI loop with a very aggressive P serves the same purpose. We run a kp of 47 right now, so when error is 3, we are at full power. A PI loop allows for more accuracy around the actual target value which is why the Kent Denver teams use it.

Wait, your kP is 47? What units do you use for your flywheel velocity?

As for a bang-bang controller, you need to calculate the flywheel’s velocity (I’d also recommend filtering it), then compare it to a target value.

#define FLY_FLT_SIZE 10

task main() {

  static float flyVel[FLY_FLT_SIZE],
  int timeLast,

  while(true) {

    for(int i = FLY_FLT_SIZE - 1; i > 0; i--) {
      flyVel* = flyVel*;

    flyVel[0] = (SensorValue[flyEnc] - encLast) / (nSysTime / timeLast);
    encLast = SensorValue[flyEnc];
    timeLast = nSysTime;

    flyVelFlt = 0;
    for(int i = 0; i < FLY_FLT_SIZE; i++) {
      flyVelFlt += flyVel*;
    flyVelFlt = flyVelFlt / FLY_FLT_SIZE;

    if(flyVelFlt < target) {
    else {

Most of this code is actually for filtering the velocity, and if you find a large amount of noise, increase the value of FLY_FLT_SIZE (right now, it’s 10). If it’s too slow to respond, decrease it. As for the bang-bang controller, all you need to do is create a void function called setFly which takes motor power as an argument and sets the power of the flywheel motors to the argument’s value. You also need to find the correct value for the target variable.***

It’s just the encoder reading over 20 milliseconds running at 700 rpm. Full field at a value around 65 and short range at around 27 for reference. No filtering because we don’t need it.

Huh. We have an encoder on our middle axle in the flywheel gearbox, geared up 5:1 from turbo motors and down 1:3 from the flywheel, and it had significant noise before we added a 10-phase rolling average (the filtering code I posted above). The graph of velocity looks much prettier and smoother now, which is a plus. Is your noise just that little that you don’t need it, or did you decide that response time is more important than stability?

The noise was definitely noticeable, but not enough to outweigh the response time. We experimented with the rolling average but decided it wasn’t really worth it.

We use an ID loop (no proportional term) with TBH on the integral, and we have much smaller coefficients, so we found that our response time wasn’t significantly affected.
Back to the original question; the OP’s issue is most likely due to friction. Remove the motor axles from the motors, then give the flywheel a nice spin (like Wheel of Fortune, not a complete spin) and see how long it continues to turn. The time it spins is proportional to the mass and square of the radius of the wheel and inversely proportional to the amount of friction. Basically, as you make the flywheel larger or heavier, it will spin longer, and as friction increases, the wheel stops faster).