Vex thread working in usercontrol but not auton

Here’s my code! I have a PID code below that runs on a task:

float kp = 0.07;
float ki = 0;
float kd = 0.11;
int targetFlywheelSpeedrpm = 600; // put desired rpm flywheel speed here
int flywheelVelocityrpm;
int error = targetFlywheelSpeedrpm - flywheelVelocityrpm;
int lastError = 0;
int totalError = 0;
int integralActiveZone = 15;
float finalAdjustment = error * kp;
int flywheelPowerVolts;
int autonPIDFlywheel() {
while (1) {
  if (wantToShoot) {
   flywheelVelocityrpm = (Flywheel1_Motor.velocity(rpm) + Flywheel2_Motor.velocity(rpm)) / -2;
   error = targetFlywheelSpeedrpm - flywheelVelocityrpm;
   printf("here!\n");
   if (error <= 1) { // 0-5 noise rpm?
     readyToShoot = true;
     Controller1.rumble("-");
   }
   else {
     readyToShoot = false;
   }
   flywheelPowerVolts = ((error * kp) + (totalError * ki) + ((error - lastError) * kd));
   Flywheel1_Motor.spin(reverse, flywheelPowerVolts, volt);
   Flywheel2_Motor.spin(reverse, flywheelPowerVolts, volt);
   lastError = error;
   totalError += error;
  }
  else {
   Flywheel1_Motor.stop(coast);
   Flywheel2_Motor.stop(coast);
  }
  this_thread::sleep_for(20);
}
return 0;
}
void firstAuton() {
setVelocityFIR(100);
if (readyToShoot) {
   // flywheel should be spinning rn
   wait(4, sec);
   Intake_Motor.spin(reverse);
   wait(0.3, sec);
   Intake_Motor.stop();
   wait(3, sec);
   Intake_Motor.spin(reverse);
}
Intake_Motor.stop();
wantToShoot = false;
readyToShoot = false;
}
void autonomous(void) {
thread pidtask1(autonPIDFlywheel);
wantToShoot = true;
enableDrivePD = true;
firstAuton();
}

The task is not even running or responding to the thread. How do I fix this? However, when I run the thread code in driver control, it works just fine.

how did you determine that ?

I think the thread probably runs, but some of the logic involving readyToShoot (which is probably false, you don’t show that variable) is probably wrong

2 Likes

Drivercontrol works in almost the same way. readyToShoot is declared early without bool declaration and declared false in preauton function. In the auton code, the flywheel should be spinning either way as we set wantToShoot to true so it should run…

We tried debugging and printing out whenever flywheel pid runs, which usually works but nothing responded or outputted to the terminal.

  if (Controller1.ButtonL1.pressing() || Controller1.ButtonL2.pressing()) { // manual intake
    wantToShoot = true;
    if (readyToShoot) {
      Intake_Motor.spin(fwd);
    }
  }
  else {
    wantToShoot = false;
    readyToShoot = false;
    Flywheel1_Motor.stop(coast);
    Flywheel1_Motor.stop(coast);
    Intake_Motor.stop(coast);
  }

so this will prepare the thread to start but not actually start it yet (most probably).

wantToShoot = true;
enableDrivePD = true;

these are set true, you said readyToShoot will be false.
then you call

void firstAuton() {
setVelocityFIR(100);
if (readyToShoot) {
   // flywheel should be spinning rn
   wait(4, sec);
   Intake_Motor.spin(reverse);
   wait(0.3, sec);
   Intake_Motor.stop();
   wait(3, sec);
   Intake_Motor.spin(reverse);
}
Intake_Motor.stop();
wantToShoot = false;
readyToShoot = false;
}

which will pretty much just exit as readyToShoot is false, so these will be set false again.

wantToShoot = false;
readyToShoot = false

The autonPIDFlywheel will then run at some point when the scheduler allows (perhaps 1mS later) and it will find wantToShoot as false and will just send stop to the flywheel.

3 Likes

Thank you lord and savior jpearman! The solution has worked!

We would like to also address some issues that previously somewhat did appear with user control and auton… that being the flywheel randomly brakes after speeding up to max rpm and returns back the flywheel spinning. The current code shown above with no alterations or other additions. The code in drivetrain used to work smoothly, but now there seems to be the issue. Plus the fact that the flywheel keeps spinning after we tell it to coast and want to stop and ready variables are set to false

This is our team on a different account, I’d like to clarify that:

Our flywheel stops and starts, effectively grinding against each other

No problems occur when running flywheel normally

We have our competition in a few minutes help :sob: