Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- float previousError;
- int integral;
- float turnPID(int angle){
- const float Kp = 3;
- const float Ki = 0.0;
- const float Kd = 0.0;
- float error;
- int derivative;
- int motorSpeed;
- error = (angle) - avg(Gyro2.value(vex::rotationUnits::deg), Gyro1.value(vex::rotationUnits::deg)); //calculate proportional error
- integral += error; //calculate integral of error
- derivative = error - previousError; //calculate derivative of drive movement
- previousError = error; //current error for next loop
- if (error > .5) { integral = 0; }
- motorSpeed = (Kp*error) + (Ki*integral) + (Kd*derivative); //setting power level for motors
- if (motorSpeed > 50) { motorSpeed = 50; }
- else if (motorSpeed < -50) { motorSpeed = -50; }
- return -motorSpeed;
- }
- void autonTurn(int angle, int time){
- int timeout = time;
- while (timeout > 0){
- frontLeftDrive.spin(vex::directionType::rev,turnPID(angle),vex::velocityUnits::rpm);
- midLeftDrive.spin(vex::directionType::rev,turnPID(angle),vex::velocityUnits::rpm);
- backLeftDrive.spin(vex::directionType::rev,turnPID(angle),vex::velocityUnits::rpm);
- frontRightDrive.spin(vex::directionType::rev,turnPID(angle),vex::velocityUnits::rpm);
- midRightDrive.spin(vex::directionType::rev,turnPID(angle),vex::velocityUnits::rpm);
- backRightDrive.spin(vex::directionType::rev,turnPID(angle),vex::velocityUnits::rpm);
- /*
- frontLeftDrive.startRotateFor(-rev(dist),vex::rotationUnits::rev,150,vex::velocityUnits::rpm);
- midLeftDrive.startRotateFor(-rev(dist),vex::rotationUnits::rev,150,vex::velocityUnits::rpm);
- backLeftDrive.startRotateFor(-rev(dist),vex::rotationUnits::rev,150,vex::velocityUnits::rpm);
- frontRightDrive.startRotateFor(-rev(dist),vex::rotationUnits::rev,150,vex::velocityUnits::rpm);
- midRightDrive.startRotateFor(-rev(dist),vex::rotationUnits::rev,150,vex::velocityUnits::rpm);
- backRightDrive.startRotateFor(-rev(dist),vex::rotationUnits::rev,150,vex::velocityUnits::rpm);
- */
- vex::task::sleep(10);
- timeout -= 10;
- }
- frontLeftDrive.stop();
- midLeftDrive.stop();
- backLeftDrive.stop();
- frontRightDrive.stop();
- midRightDrive.stop();
- backRightDrive.stop();
- }
- //to call turn function in auton
- autonTurn(90, 2500); //turn 90 degrees counterclockwise and run PID for 1.2 sec
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement