Velocity Help

I have a Velocity Controller that runs fine but i do not know how to include changes in power. It requires 32 on full power and 48 on low power. I use RobotC 4.x in Natural Language PLTW. If anyone can help

A velocity controller should ignore the effects of the battery. Do you mind posting your code?

#pragma config(Sensor, dgtl1, FlyWheel, sensorQuadEncoder)
#pragma config(Sensor, dgtl3, Ball, sensorTouch)
#pragma config(Motor, port1, Intake, tmotorVex393HighSpeed_HBridge, openLoop, reversed)
#pragma config(Motor, port2, FrontRightWheel, tmotorVex393HighSpeed_MC29, openLoop, reversed, driveRight)
#pragma config(Motor, port3, BackRightWheel, tmotorVex393HighSpeed_MC29, openLoop, reversed, driveRight)
#pragma config(Motor, port4, FrontRightTransmission, tmotorVex393_MC29, openLoop)
#pragma config(Motor, port5, BackRightTransmission, tmotorVex393_MC29, openLoop)
#pragma config(Motor, port6, BackLeftTransmission, tmotorVex393_MC29, openLoop, reversed)
#pragma config(Motor, port7, FrontLeftTransmission, tmotorVex393_MC29, openLoop, reversed)
#pragma config(Motor, port8, BackLeftWheel, tmotorVex393HighSpeed_MC29, openLoop, driveLeft)
#pragma config(Motor, port9, FrontLeftWheel, tmotorVex393HighSpeed_MC29, openLoop, driveLeft)
#pragma config(Motor, port10, Lifter, tmotorVex393HighSpeed_HBridge, openLoop)
#pragma platform(VEX)
#pragma competitionControl(Competition)
#pragma autonomousDuration(15)
#pragma userControlDuration(105)
#include “Vex_Competition_Includes.c”
int speed;
int target;
int bar = 36;
int half = 127;
int x;
int y;
void pre_auton()
{
}
task autonomous()
{
}

task usercontrol()
{
while (true)
{
motor[FrontRightWheel] = vexRT[Ch2];
motor[BackRightWheel] = vexRT[Ch2];
motor[FrontLeftWheel] = vexRT[Ch3];
motor[BackLeftWheel] = vexRT[Ch3];
SensorValue[FlyWheel] = 0;
wait(0.02);
speed = SensorValue[FlyWheel];
if (speed >= target)
{
x=1;
y=1;
}
if (speed < target && SensorValue[Ball] == 1)
{
x=127;
y=0;
}
else if (speed < target)
{
x=127;
}
if(vexRT[Btn6U] == 1)
{
motor[FrontRightTransmission] = -127;
motor[BackRightTransmission] = -127;
motor[FrontLeftTransmission] = -127;
motor[BackLeftTransmission] = -127;
}
else if(vexRT[Btn6D] == 1)
{
motor[FrontRightTransmission] = 127;
motor[BackRightTransmission] = 127;
motor[FrontLeftTransmission] = 127;
motor[BackLeftTransmission] = 127;
}
else if(vexRT[Btn8U] == 1)
{
target=6;
motor[FrontRightTransmission] = barx;
motor[BackRightTransmission] = bar
x;
motor[FrontLeftTransmission] = barx;
motor[BackLeftTransmission] = bar
x;
}
else if(vexRT[Btn8D] == 1)
{
target=1;
motor[FrontRightTransmission] = halfx;
motor[BackRightTransmission] = half
x;
motor[FrontLeftTransmission] = halfx;
motor[BackLeftTransmission] = half
x;
}
else
{
motor[FrontRightTransmission] = 0;
motor[BackRightTransmission] = 0;
motor[FrontLeftTransmission] = 0;
motor[BackLeftTransmission] = 0;
}
if(vexRT[Btn5U] == 1)
{
motor[Intake] = 127;
motor[Lifter] = 127*y;
}
else if(vexRT[Btn5D] == 1)
{
motor[Intake] = -127;
motor[Lifter] = -127;
}
else
{
motor[Intake] = 0;
motor[Lifter] = 0;
}
}
}

Half court is not tuned yet but the Bar is for middle power

So this is just a bang bang controller? I don’t have very much experience with bang bang controllers on flywheels, primarily because it seems to be really inaccurate (although other teams have gotten it to work). I think the drastic change from 36 to 127 might be throwing it off. Try changing it from 127 to a lower number and see what happens.

It works fine but when i plug in a fresh battery it requires less power to shoot than when the battery is almost dead. I was wondering if there is any way to let the robot control the speed based on the voltage.

I am not sure what exactly you are doing, but you seem to be measuring the quad encoder sensor for 20 milliseconds. You reset to 0 and then 20ms later get a new check.

SensorValue[FlyWheel] = 0;
wait(0.02);
speed = SensorValue[FlyWheel];

You then don’t initialize target to a value so until you press Button 8 up or down the value of target could be anything (but most likely 0). But once you press that button 8, you are looking for 20ms speeds of 1 or 6 clicks per that time interval.

You loop around again, and the initial flywheel multiplier x be set if your speed is above or below target. If above target, the flywheel will pretty much go to 127 since barx or halfx will be waaay over 127 when button 8 is pressed. Otherwise set to the values of bar or half.

So this code takes a while to figure out that you set the controls to full or half speed with a kicker to full power if the instant velocity is too low.

What you want to do is have a control loop like take back half to adjust the motor power little by little every loop around. Waiting 20 milliseconds may get some noise in the velocity. So there is no smoothing of the values. And there is no slight adjustments to the speed, just full power or this set power. So as the battery is drained, a PWM signal of 36 will be weaker and weaker.

You need to adjust the power value more gradually. You give these big spikes to full power versus adding one or two more PWM power to the motor.

https://vexforum.com/t/flywheel-velocity-control/29892/1

This is the one you want to read 10 times:
https://vexforum.com/t/flywheel-velocity-control-revisited/30480/1

https://vexforum.com/t/tbh-tuning/30908/1

Yeah, this is what I meant, I just worded it really badly… You might also want to look at a PI controller. A quick search for that (vex forum pid) on google will get you plenty of results.

Would it work with a ratchet and pawl system.

A ratchet and pawl would affect the system minimally.

TBH looks good and i would love to try it out but I do not know how to tune it.

https://vexforum.com/t/tbh-tuning/30908/1