# Velocity Control Help

I built a 6 motor turbo drive with 4 motor turbo flywheel and a two stage intake. But I was having problems with velocity control so I was wondering if you had any samples of bang bang code that I could see and any advice on methods to tune it.

I have not had much experience with bang-bang code but I have seen several different ways on how to do it. I would think that the best way to make it would be to make it like this:

``````while(true)
{
if(FLYWHEEL != target)
{
flywheel motors = 127;
}

if(FLYWHEEL = target)
{
flywheel motors = target speed;
}
}
``````

basically what this does is that if your flywheel is under your desired target than the motors run at full power until they reach the target speed, at which point they should stabilize.
NOTE: THIS IS NOT REAL CODE, this code will NOT WORK, I just gave you a basic idea of how bang-bang works, you will need to make your own code. Anyway, hope this helps.

``````
while(true) {
if(velocity <= target)
setFly(127);
else
setFly(0);
}

``````

If you really wanted, you could add a middle ground, which would use a motor power about the same as the stable motor power of a PID or TBH loop. That would look like this:

``````
while(true) {
if(velocity < target - thresh)
setFly(127);
else if(velocity > target + thresh)
setFly(0);
else
setFly(stablePwr);
}

``````

This code won’t work on its own; the variables need to be defined and the velocity calculation is missing. However, it will work if a velocity calculation is added and the variables and setFly() function are defined.
This is what setFly would look like for a 2 motor flywheel; if you change the names and add motors as necessary, you don’t need to change anything else.

``````
void setFly(int pwr) {
motor[fly1] =
motor[fly2] =
pwr;
}

``````

I like to use functions for this because it requires fewer changes to the code if we add or remove motors.