Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "robot-config.h"
- #include <cmath>
- vex::competition Competition;
- void driveforward(int inches, bool speed){
- bool autonomous_running = true;
- int mastermotorpower;
- int slavemotorpower;
- FrontLeftMotor.resetRotation();
- FrontRightMotor.resetRotation();
- float Kp1 = 0.85;
- float Kp2 = 0.35;
- if (speed == true){
- while (autonomous_running == true && std::abs(FrontLeftMotor.rotation(vex::rotationUnits::deg)) < inches * 28){
- mastermotorpower = ((inches*28) - std::abs(FrontLeftMotor.rotation(vex::rotationUnits::deg))) * Kp1;
- if (mastermotorpower > 170){
- mastermotorpower = 170;
- } else if (mastermotorpower < 50){
- mastermotorpower = 50;
- }
- slavemotorpower = mastermotorpower + ((std::abs(FrontLeftMotor.rotation(vex::rotationUnits::deg)) - std::abs(FrontLeftMotor.rotation(vex::rotationUnits::deg))));
- FrontLeftMotor.spin(vex::directionType::fwd,mastermotorpower, vex::velocityUnits::rpm);
- FrontRightMotor.spin(vex::directionType::fwd,slavemotorpower, vex::velocityUnits::rpm);
- BackLeftMotor.spin(vex::directionType::fwd,mastermotorpower, vex::velocityUnits::rpm);
- BackRightMotor.spin(vex::directionType::fwd,slavemotorpower, vex::velocityUnits::rpm);
- vex::task::sleep(5);
- }
- FrontLeftMotor.stop(vex::brakeType::brake);
- FrontRightMotor.stop(vex::brakeType::brake);
- BackLeftMotor.stop(vex::brakeType::brake);
- BackRightMotor.stop(vex::brakeType::brake);
- } else {
- while(autonomous_running == true && std::abs(FrontLeftMotor.rotation(vex::rotationUnits::deg))< inches * 28){
- mastermotorpower = ((inches*28) - std::abs(FrontLeftMotor.rotation(vex::rotationUnits::deg)))* Kp2;
- if (mastermotorpower > 100){
- mastermotorpower = 100;
- } else if (mastermotorpower < 30){
- mastermotorpower = 30;
- }
- slavemotorpower = mastermotorpower + ((std::abs(FrontLeftMotor.rotation(vex::rotationUnits::deg))- std::abs(FrontRightMotor.rotation(vex::rotationUnits::deg))));
- FrontLeftMotor.spin(vex::directionType::fwd,mastermotorpower, vex::velocityUnits::rpm);
- BackLeftMotor.spin(vex::directionType::fwd,mastermotorpower, vex::velocityUnits::rpm);
- FrontRightMotor.spin(vex::directionType::fwd,slavemotorpower, vex::velocityUnits::rpm);
- BackRightMotor.spin(vex::directionType::fwd,slavemotorpower, vex::velocityUnits::rpm);
- vex::task::sleep(5);
- }
- FrontLeftMotor.stop(vex::brakeType::coast);
- BackLeftMotor.stop(vex::brakeType::coast);
- FrontRightMotor.stop(vex::brakeType::coast);
- BackRightMotor.stop(vex::brakeType::coast);
- }
- }
- void drivebackwards(int inches, bool speed){
- bool autonomous_running = true;
- int mastermotorpower;
- int slavemotorpower;
- FrontLeftMotor.resetRotation();
- FrontRightMotor.resetRotation();
- float Kp1 = 0.65;
- float Kp2 = 0.4;
- if (speed == true){
- while (autonomous_running == true && std::abs(FrontLeftMotor.rotation(vex::rotationUnits::deg)) < inches * 28){
- mastermotorpower = ((inches*28) - std::abs(FrontLeftMotor.rotation(vex::rotationUnits::deg))) * Kp1;
- if (mastermotorpower > 150){
- mastermotorpower = 150;
- } else if (mastermotorpower < 50){
- mastermotorpower = 50;
- }
- slavemotorpower = mastermotorpower + ((std::abs(FrontLeftMotor.rotation(vex::rotationUnits::deg)) - std::abs(FrontLeftMotor.rotation(vex::rotationUnits::deg))));
- FrontLeftMotor.spin(vex::directionType::fwd,mastermotorpower * -1, vex::velocityUnits::rpm);
- FrontRightMotor.spin(vex::directionType::fwd,slavemotorpower * -1, vex::velocityUnits::rpm);
- BackLeftMotor.spin(vex::directionType::fwd,mastermotorpower * -1, vex::velocityUnits::rpm);
- BackRightMotor.spin(vex::directionType::fwd,slavemotorpower * -1, vex::velocityUnits::rpm);
- vex::task::sleep(5);
- }
- FrontLeftMotor.stop(vex::brakeType::brake);
- FrontRightMotor.stop(vex::brakeType::brake);
- BackLeftMotor.stop(vex::brakeType::brake);
- BackRightMotor.stop(vex::brakeType::brake);
- vex::task::sleep(25);
- FrontLeftMotor.stop(vex::brakeType::coast);
- BackLeftMotor.stop(vex::brakeType::coast);
- FrontRightMotor.stop(vex::brakeType::coast);
- BackRightMotor.stop(vex::brakeType::coast);
- } else {
- while(autonomous_running == true && std::abs(FrontLeftMotor.rotation(vex::rotationUnits::deg))< inches * 28){
- mastermotorpower = ((inches*28) - std::abs(FrontLeftMotor.rotation(vex::rotationUnits::deg)))* Kp2;
- if (mastermotorpower > 100){
- mastermotorpower = 100;
- } else if (mastermotorpower < 30){
- mastermotorpower = 30;
- }
- slavemotorpower = mastermotorpower + ((std::abs(FrontLeftMotor.rotation(vex::rotationUnits::deg))- std::abs(FrontRightMotor.rotation(vex::rotationUnits::deg))));
- FrontLeftMotor.spin(vex::directionType::fwd,mastermotorpower * -1, vex::velocityUnits::rpm);
- BackLeftMotor.spin(vex::directionType::fwd,mastermotorpower * -1, vex::velocityUnits::rpm);
- FrontRightMotor.spin(vex::directionType::fwd,slavemotorpower * -1, vex::velocityUnits::rpm);
- BackRightMotor.spin(vex::directionType::fwd,slavemotorpower * -1, vex::velocityUnits::rpm);
- vex::task::sleep(5);
- }
- FrontLeftMotor.stop(vex::brakeType::brake);
- BackLeftMotor.stop(vex::brakeType::brake);
- FrontRightMotor.stop(vex::brakeType::brake);
- BackRightMotor.stop(vex::brakeType::brake);
- vex::task::sleep(25);
- FrontLeftMotor.stop(vex::brakeType::coast);
- BackLeftMotor.stop(vex::brakeType::coast);
- FrontRightMotor.stop(vex::brakeType::coast);
- BackRightMotor.stop(vex::brakeType::coast);
- }
- }
- void turnleft(float turn){
- bool autonomous_running = true;
- int turn_power;
- int error;
- float Kp1 = 0.65;
- double encoder_turning_proportional = 3.2;
- TrackingWheel.resetRotation();
- while (autonomous_running == true && std::abs(TrackingWheel.rotation(vex::rotationUnits::deg)) / encoder_turning_proportional < turn){
- vex::task::sleep(5);
- error = turn - std::abs((TrackingWheel.rotation(vex::rotationUnits::deg) / encoder_turning_proportional));
- turn_power = (error * Kp1) * 2;
- if (turn_power > 110){
- turn_power = 110;
- }
- else if (turn_power < 30){
- turn_power = 20;
- }
- FrontLeftMotor.spin(vex::directionType::fwd,turn_power * -1,vex::velocityUnits::rpm);
- BackLeftMotor.spin(vex::directionType::fwd,turn_power * -1,vex::velocityUnits::rpm);
- FrontRightMotor.spin(vex::directionType::fwd,turn_power ,vex::velocityUnits::rpm);
- BackRightMotor.spin(vex::directionType::fwd,turn_power ,vex::velocityUnits::rpm);
- }
- FrontLeftMotor.stop(vex::brakeType::brake);
- BackLeftMotor.stop(vex::brakeType::brake);
- FrontRightMotor.stop(vex::brakeType::brake);
- BackRightMotor.stop(vex::brakeType::brake);
- vex::task::sleep(20);
- FrontLeftMotor.stop(vex::brakeType::coast);
- BackLeftMotor.stop(vex::brakeType::coast);
- FrontRightMotor.stop(vex::brakeType::coast);
- BackRightMotor.stop(vex::brakeType::coast);
- }
- void turnright(float turn){
- bool autonomous_running = true;
- int turn_power;
- int error;
- float Kp1 = 0.55;
- double encoder_turning_proportional = 3.2;
- TrackingWheel.resetRotation();
- while (autonomous_running == true && std::abs(TrackingWheel.rotation(vex::rotationUnits::deg)) < turn * encoder_turning_proportional){
- vex::task::sleep(5);
- error = turn - std::abs((TrackingWheel.rotation(vex::rotationUnits::deg) / encoder_turning_proportional));
- turn_power = (error * Kp1) * 2;
- if (turn_power > 100){
- turn_power = 100;
- }
- else if (turn_power < 20){
- turn_power = 20;
- }
- FrontLeftMotor.spin(vex::directionType::fwd,turn_power,vex::velocityUnits::rpm);
- BackLeftMotor.spin(vex::directionType::fwd,turn_power,vex::velocityUnits::rpm);
- FrontRightMotor.spin(vex::directionType::fwd,turn_power * -1,vex::velocityUnits::rpm);
- BackRightMotor.spin(vex::directionType::fwd,turn_power * -1,vex::velocityUnits::rpm);
- }
- FrontRightMotor.stop(vex::brakeType::brake);
- BackLeftMotor.stop(vex::brakeType::brake);
- FrontRightMotor.stop(vex::brakeType::brake);
- BackRightMotor.stop(vex::brakeType::brake);
- vex::task::sleep(20);
- BackLeftMotor.stop(vex::brakeType::coast);
- BackRightMotor.stop(vex::brakeType::coast);
- FrontRightMotor.stop(vex::brakeType::coast);
- BackRightMotor.stop(vex::brakeType::coast);
- }
- void pre_auton( void ) {
- FrontLeftMotor.resetRotation();
- FrontRightMotor.resetRotation();
- BackLeftMotor.resetRotation();
- BackRightMotor.resetRotation();
- }
- void autonomous( void ) {
- Flipper.rotateTo(-50,vex::rotationUnits::deg,85,vex::velocityUnits::pct);
- BallIntake.spin(directionType::rev, 100, velocityUnits::pct);
- vex::task::sleep(25);
- FlyWheel.spin(directionType::fwd, 100, velocityUnits::pct);
- vex::task::sleep(25);
- driveforward(44,true);
- vex::task::sleep(250);
- drivebackwards(3,true);
- vex::task::sleep(250);
- turnright(9);
- vex::task::sleep(100);
- BallIntake.stop(brakeType::coast);
- drivebackwards(40,true);
- vex::task::sleep(25);
- Flipper.rotateTo(-50,vex::rotationUnits::deg,85,vex::velocityUnits::pct);
- turnleft(56);
- vex::task::sleep(100);
- driveforward(7,false);
- FrontLeftMotor.stop(vex::brakeType::hold);
- FrontRightMotor.stop(vex::brakeType::hold);
- BackLeftMotor.stop(vex::brakeType::hold);
- BackRightMotor.stop(vex::brakeType::hold);
- BallIntake.startRotateFor(-1500,vex::rotationUnits::deg,100,vex::velocityUnits::pct);
- Indexer.rotateFor(1500,vex::rotationUnits::deg,100,vex::velocityUnits::pct);
- Flipper.startRotateTo(20,vex::rotationUnits::deg,90,vex::velocityUnits::pct);
- BallIntake.startRotateFor(-1500,vex::rotationUnits::deg,100,vex::velocityUnits::pct);
- Indexer.startRotateFor(1500,vex::rotationUnits::deg,100,vex::velocityUnits::pct);
- vex::task::sleep(5);
- Indexer.startRotateFor(800,vex::rotationUnits::deg,100,vex::velocityUnits::pct);
- BallIntake.rotateFor(-800,vex::rotationUnits::deg,100,vex::velocityUnits::pct);
- vex::task::sleep(15);
- Flipper.rotateTo(-50,vex::rotationUnits::deg,85,vex::velocityUnits::pct);
- FrontLeftMotor.stop(vex::brakeType::coast);
- FrontRightMotor.stop(vex::brakeType::coast);
- BackLeftMotor.stop(vex::brakeType::coast);
- BackRightMotor.stop(vex::brakeType::coast);
- driveforward(3,false);
- Flipper.rotateTo(-275,vex::rotationUnits::deg,45,vex::velocityUnits::pct);
- BallIntake.spin(directionType::rev, 100, velocityUnits::pct);
- drivebackwards(8,true);
- vex::task::sleep(400);
- Flipper.rotateTo(-50,vex::rotationUnits::deg,85,vex::velocityUnits::pct);
- vex::task::sleep(150);
- drivebackwards(2,true);
- vex::task::sleep(300);
- turnleft(47);
- vex::task::sleep(300);
- drivebackwards(5,true);
- vex::task::sleep(300);
- FrontLeftMotor.stop(vex::brakeType::hold);
- FrontRightMotor.stop(vex::brakeType::hold);
- BackLeftMotor.stop(vex::brakeType::hold);
- BackRightMotor.stop(vex::brakeType::hold);
- BallIntake.startRotateFor(-1500,vex::rotationUnits::deg,100,vex::velocityUnits::pct);
- Indexer.rotateFor(1500,vex::rotationUnits::deg,100,vex::velocityUnits::pct);
- Flipper.startRotateTo(20,vex::rotationUnits::deg,90,vex::velocityUnits::pct);
- BallIntake.startRotateFor(-1500,vex::rotationUnits::deg,100,vex::velocityUnits::pct);
- Indexer.startRotateFor(1500,vex::rotationUnits::deg,100,vex::velocityUnits::pct);
- vex::task::sleep(5);
- Indexer.startRotateFor(800,vex::rotationUnits::deg,100,vex::velocityUnits::pct);
- BallIntake.rotateFor(-800,vex::rotationUnits::deg,100,vex::velocityUnits::pct);
- vex::task::sleep(15);
- Flipper.rotateTo(-50,vex::rotationUnits::deg,85,vex::velocityUnits::pct);
- FrontLeftMotor.stop(vex::brakeType::coast);
- FrontRightMotor.stop(vex::brakeType::coast);
- BackLeftMotor.stop(vex::brakeType::coast);
- BackRightMotor.stop(vex::brakeType::coast);
- driveforward(44,true);
- Flipper.rotateTo(-180,vex::rotationUnits::deg,85,vex::velocityUnits::pct);
- FrontLeftMotor.stop(vex::brakeType::coast);
- BackLeftMotor.stop(vex::brakeType::coast);
- FrontRightMotor.stop(vex::brakeType::coast);
- BackRightMotor.stop(vex::brakeType::coast);
- }
- void usercontrol( void ) {
- int FlyWheelPCT = 100;
- int BallIntakePCT = 100;
- int IndexerPCT = 100;
- int FlipperPCT = 70;
- while (true) {
- if(Controller1.ButtonUp.pressing()) {
- FrontLeftMotor.stop(vex::brakeType::hold);
- FrontRightMotor.stop(vex::brakeType::hold);
- BackLeftMotor.stop(vex::brakeType::hold);
- BackRightMotor.stop(vex::brakeType::hold);
- }
- else if(Controller1.ButtonDown.pressing()) {
- FrontLeftMotor.stop(vex::brakeType::coast);
- FrontRightMotor.stop(vex::brakeType::coast);
- BackLeftMotor.stop(vex::brakeType::coast);
- BackRightMotor.stop(vex::brakeType::coast);
- }
- else{
- FrontLeftMotor.spin(directionType::fwd, (Controller1.Axis3.value() + Controller1.Axis4.value()), velocityUnits::pct); //(Axis3+Axis4)/1.5;
- FrontRightMotor.spin(directionType::fwd, (Controller1.Axis3.value() - Controller1.Axis4.value()), velocityUnits::pct);//(Axis3-Axis4)/1.5;
- BackLeftMotor.spin(directionType::fwd, (Controller1.Axis3.value() + Controller1.Axis4.value()), velocityUnits::pct); //(Axis3+Axis4)/1.5;
- BackRightMotor.spin(directionType::fwd, (Controller1.Axis3.value() - Controller1.Axis4.value()), velocityUnits::pct);//(Axis3-Axis4)/1.5;
- }
- if(Controller1.ButtonR1.pressing()) {
- BallIntake.spin(directionType::fwd, BallIntakePCT, velocityUnits::pct);
- }
- else if(Controller1.ButtonR2.pressing()) {
- BallIntake.spin(directionType::rev, BallIntakePCT, velocityUnits::pct);
- }
- else if(Controller1.ButtonL2.pressing()) {
- FrontLeftMotor.stop(vex::brakeType::hold);
- FrontRightMotor.stop(vex::brakeType::hold);
- BackLeftMotor.stop(vex::brakeType::hold);
- BackRightMotor.stop(vex::brakeType::hold);
- BallIntake.startRotateFor(-1500,vex::rotationUnits::deg,100,vex::velocityUnits::pct);
- Indexer.rotateFor(1500,vex::rotationUnits::deg,100,vex::velocityUnits::pct);
- Flipper.startRotateTo(20,vex::rotationUnits::deg,90,vex::velocityUnits::pct);
- BallIntake.startRotateFor(-1500,vex::rotationUnits::deg,100,vex::velocityUnits::pct);
- Indexer.startRotateFor(1500,vex::rotationUnits::deg,100,vex::velocityUnits::pct);
- vex::task::sleep(5);
- Indexer.startRotateFor(800,vex::rotationUnits::deg,100,vex::velocityUnits::pct);
- BallIntake.rotateFor(-800,vex::rotationUnits::deg,100,vex::velocityUnits::pct);
- vex::task::sleep(15);
- Flipper.rotateTo(-50,vex::rotationUnits::deg,85,vex::velocityUnits::pct);
- FrontLeftMotor.stop(vex::brakeType::coast);
- FrontRightMotor.stop(vex::brakeType::coast);
- BackLeftMotor.stop(vex::brakeType::coast);
- BackRightMotor.stop(vex::brakeType::coast);
- }
- else if(Controller1.ButtonY.pressing()) {
- Indexer.startRotateFor(160,vex::rotationUnits::deg,100,vex::velocityUnits::pct);
- BallIntake.startRotateFor(-160,vex::rotationUnits::deg,100,vex::velocityUnits::pct);
- Flipper.rotateTo(-50,vex::rotationUnits::deg,85,vex::velocityUnits::pct);
- vex::task::sleep(50);
- }
- else if(Controller1.ButtonLeft.pressing()) {
- Flipper.rotateTo(15,vex::rotationUnits::deg,90,vex::velocityUnits::pct);
- Indexer.startRotateFor(1100,vex::rotationUnits::deg,100,vex::velocityUnits::pct);
- BallIntake.rotateFor(-1100,vex::rotationUnits::deg,100,vex::velocityUnits::pct);
- Flipper.rotateTo(50,vex::rotationUnits::deg,90,vex::velocityUnits::pct);
- vex::task::sleep(50);
- }
- else {
- BallIntake.stop(brakeType::brake);
- }
- if(Controller1.ButtonL1.pressing()) {
- FlyWheel.spin(directionType::fwd, FlyWheelPCT, velocityUnits::pct);
- }
- else {
- FlyWheel.stop(brakeType::coast);
- }
- if(Controller1.ButtonX.pressing()) {
- Indexer.spin(directionType::fwd, IndexerPCT, velocityUnits::pct);
- BallIntake.spin(directionType::rev, BallIntakePCT, velocityUnits::pct);
- }
- else {
- Indexer.stop(brakeType::coast);
- }
- if(Controller1.ButtonA.pressing()) {
- Flipper.spin(directionType::rev, FlipperPCT, velocityUnits::pct);
- }
- else if(Controller1.ButtonB.pressing()) {
- Flipper.spin(directionType::fwd, FlipperPCT, velocityUnits::pct);
- }
- else {
- Flipper.stop(brakeType::hold);
- }
- task::sleep(20);
- }
- }
- int main() {
- pre_auton();
- Competition.autonomous( autonomous );
- Competition.drivercontrol( usercontrol );
- while(1) {
- vex::task::sleep(100);//Sleep the task for a short amount of time to prevent wasted resources.
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement