Inertial Sensor turning PROS help

Ive been trying to get a basic turning function for the inertial sensor here is what I have written.

void turnIMU(int left, int right, int value){
  imu_sensor.reset();
    while(fabs(imu_sensor.get_rotation() < value)){
        leftDrive.moveVoltage(left);
        rightDrive.moveVoltage(right);
        pros::delay(1);
    }
    leftDrive.moveVoltage(0);
    rightDrive.moveVoltage(0);
}
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
void test(){

  turnIMU(6000, -6000, 90);

}

The issue is that my robot doesnt move when I run the void

So a few things:

  1. You want to be comparing the floating point absolute value of the error to a constant.
    I’ll use 20 as the constant here just because
while( fabs (imu_sensor.get_rotation() - value) > 20 ) {
  // ...
}

Another thing, you should probably use a feedback loop, such as a PID loop for this. The reason is that where it lands will be unpredictable due to jerk, since you’ll be going fron your target velocity to 0 in the blink of an eye.

4 Likes

Extension: :smile:
How could you modify the solution which @DrewWHOOP provided, in order to account for a situation where your current position is -60 and your target is -90? How can you determine the optimum direction to turn which results in the shortest distance to the target?

2 Likes

Thank you for the help. I didn’t make a PID for this because I plan to use this for skills and only run my turns at a low speed so it doesn’t jerk