SHARE
TWEET

FIRST Robotics Code 2011

erks Sep 7th, 2011 501 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "WPILib.h"
  2. #include "dashboard.h"
  3. #include "math.h"
  4. /*change list:
  5.  * 1. Increased acquire/spit out speed
  6.  * 2. Release Button 4-28-11
  7.  * 3. Improved arm extend/shift code 4-28-11
  8.  * 4. Fixed claw in manual mode, preset now overrides manual 4-29-11
  9.  * 5. Increased encoder distance from 210->214
  10.  * 6. Fixed Release Button 4-29-11
  11.  * 7. Added dual mode release button 4-29-11
  12.  */
  13.  
  14. /*TODO list:
  15.  * 1. Dual tube auto
  16.  */
  17.  
  18. /********************* FOR XBOX 360 CONTROLLER****************************
  19. BUTTON NUMBERS
  20. ButtonA = 1
  21. ButtonB = 2
  22. ButtonX = 3
  23. ButtonY = 4
  24. LeftBumper = 5
  25. RightBumper = 6
  26. Select = 7
  27. Start = 8
  28. LeftClick = 9
  29. RightClick = 10
  30.  
  31. THUMBSTICK AXIS'
  32. LeftXAxis = 1 top is -1, rest is 0 , bottom is 1
  33. LeftYAxis = 2
  34. TriggerAxis = 3
  35.  
  36. Both triggers are in this.
  37. Right Trigger is [-1,0]
  38. Left Trigger is [0,1]
  39. Return value = Right Trigger + Left Trigger
  40.  
  41. RightXAxis = 4
  42. RightYAxis = 5
  43. ************************************************************************/
  44.  
  45. class RobotDemo : public SimpleRobot
  46. {
  47.         RobotDrive *myRobot; // robot drive system
  48.         Victor *leftDrive;
  49.         Victor *rightDrive;
  50.         Jaguar *armJags;
  51.         Jaguar *rightArmJag;
  52.         Jaguar *topClawJag;
  53.         Jaguar *bottomClawJag;
  54.         Servo *leftDeployServo;
  55.         Servo *rightDeployServo;
  56.        
  57.         //GetRawAxis returns from -1 to 1, -1 is top
  58.         Joystick *xboxStickManip;
  59.         Joystick *leftStick;
  60.         Joystick *rightStick;
  61.        
  62.         Compressor *compressor1;
  63.        
  64.         Solenoid *shiftHigh;
  65.         Solenoid *shiftLow;
  66.         Solenoid *minibotDeployer;
  67.         Solenoid *armExtend;
  68.         Solenoid *armRetract;
  69.         Solenoid *minibotHold;
  70.        
  71.         DigitalInput *lightLeft; //MAKE SURE THE LIGHT SENSOR RIBBON IS WIRED CORRECTLY
  72.         DigitalInput *lightMid; //PORTS 1,2,3 MUST BE OCCUPIED
  73.         DigitalInput *lightRight;
  74.         DigitalInput *autoSwitch;
  75.        
  76.         DigitalOutput *redLEDs;
  77.         DigitalOutput *blueLEDs;
  78.        
  79.         //DigitalInput *minibotDeploymentBarSwitch; // Activates when the switch is depressed, deployment bar is touching pole
  80.         DigitalInput *highLimitSwitch;
  81.         DigitalInput *lowLimitSwitch;
  82.        
  83.         Gyro *gyro1;
  84.        
  85.         //Ultrasonic *ultrasonic1;
  86.        
  87.         Encoder *rightEncoder;
  88.        
  89.         Timer *robotTimer;
  90.        
  91.         AnalogChannel *pot1;
  92.        
  93.         //presets for arm angle, calibrated by pot values
  94.         #define ARM_ANGLE_MIN (1000-560)
  95.         #define ARM_ANGLE_MAX (1000-430)
  96.  
  97.         #define ARM_ANGLE_FLOOR (1000-550)
  98.         #define ARM_ANGLE_HOME_LOW (1000-560)
  99.         #define ARM_ANGLE_FEED (1000-500)
  100.         #define ARM_ANGLE_MID (1000-470)
  101.         #define ARM_ANGLE_MID_DROP (1000-502)
  102.         #define AUTO_RELEASE_THRESHOLD (1000-450)//the value where the auto release chooses between high release and mid release
  103.         #define ARM_ANGLE_HIGH (1000-428)
  104.         #define ARM_ANGLE_HIGH_DROP (1000-460)//drops the arm while spinning durning auto
  105.  
  106.        
  107.         bool firstCycle;
  108.         int previousError;
  109.         int LEDState;
  110.        
  111. public:
  112.         RobotDemo(void)
  113.         {
  114.                 /* DIGITAL IO PORTS
  115.                 1       lightLeft
  116.                 2       lightMid
  117.                 3       lightRight
  118.                 4       --(ultrasonic)
  119.                 5       --(ultrasonic)
  120.                 6       redLEDs
  121.                 7       blueLEDs
  122.                 8       Encoder B Channel
  123.                 9       Encoder A Channel
  124.                 10      --(minibotDeploymentBarSwitch)
  125.                 11      highLimitSwitch
  126.                 12      compressor pressor switch
  127.                 13      autoSwitch
  128.                 14      lowLimitSwitch
  129.                 */
  130.                
  131.                 leftDrive = new Victor(1);
  132.                 rightDrive = new Victor(2);
  133.                 myRobot = new RobotDrive(leftDrive, rightDrive);
  134.                 armJags = new Jaguar(3);
  135.                 topClawJag = new Jaguar(5);
  136.                 bottomClawJag = new Jaguar(6);
  137.                 leftDeployServo = new Servo(7);
  138.                 rightDeployServo = new Servo(8);
  139.                 leftDeployServo->Set(0.3); //to reset, must restart robot. Disable won't reset.
  140.                 rightDeployServo->Set(0.7);
  141.                
  142.                 AxisCamera::GetInstance();
  143.                
  144.                 xboxStickManip = new Joystick(1);
  145.                 leftStick = new Joystick(2);
  146.                 rightStick = new Joystick(3);
  147.                
  148.                 compressor1 = new Compressor(12,1);
  149.                 compressor1->Start();
  150.                
  151.                 shiftHigh = new Solenoid(1);
  152.                 shiftLow = new Solenoid(2);
  153.                 minibotDeployer = new Solenoid(3);
  154.                 armExtend = new Solenoid (4);
  155.                 armRetract = new Solenoid (5);
  156.                 minibotHold = new Solenoid (6);
  157.                 minibotHold->Set(true);
  158.                 minibotDeployer->Set(false);
  159.                
  160.                 lightLeft = new DigitalInput(1);
  161.                 lightMid = new DigitalInput(2);
  162.                 lightRight = new DigitalInput(3);
  163.                
  164.                 redLEDs = new DigitalOutput(6);
  165.                 blueLEDs = new DigitalOutput(7);
  166.                
  167.                 //minibotDeploymentBarSwitch = new DigitalInput(10);
  168.                 highLimitSwitch = new DigitalInput(11);
  169.                 autoSwitch = new DigitalInput(13);//0 is to the side, 1 is towards robot
  170.                 lowLimitSwitch = new DigitalInput(14);
  171.                
  172.                 gyro1 = new Gyro(1);
  173.                
  174.                 //ultrasonic1 = new Ultrasonic(4, 5, Ultrasonic::kInches); //Digital ping out on 4, echo in on 5
  175.                 //ultrasonic1->SetAutomaticMode(true);
  176.  
  177.                 rightEncoder = new Encoder(9, 8, true, Encoder::k4X);
  178.                 rightEncoder->Start();
  179.                
  180.                                                                                                                 //set how many inches traveled per tick
  181.                 rightEncoder->SetDistancePerPulse(0.0349);              //this number should be the circumference of the
  182.                                                                                                                 //wheel divided by 360
  183.                
  184.                 robotTimer = new Timer();
  185.                
  186.                 SmartDashboard::init();
  187.                
  188.                 pot1 = new AnalogChannel(2);
  189.                
  190.                 firstCycle = true;
  191.                 previousError = 0; //do not apply d during first cycle through position arm
  192.                 LEDState = 2;
  193.                
  194.                 GetWatchdog().SetExpiration(0.1);
  195.         }
  196.  
  197.         ~RobotDemo(void)
  198.         {
  199.                 delete myRobot;
  200.                 delete armJags;
  201.                 delete topClawJag;
  202.                 delete bottomClawJag;
  203.                 delete leftDeployServo;
  204.                 delete rightDeployServo;
  205.                
  206.                 delete xboxStickManip;
  207.                 delete leftStick;
  208.                 delete rightStick;
  209.                
  210.                 delete compressor1;
  211.                
  212.                 delete shiftHigh;
  213.                 delete shiftLow;
  214.                 delete minibotDeployer;
  215.                 delete armExtend;
  216.                 delete armRetract;
  217.                 delete minibotHold;
  218.                
  219.                 delete autoSwitch;
  220.                
  221.                 delete gyro1;
  222.                
  223.                 //delete ultrasonic1;
  224.                
  225.                 delete rightEncoder;
  226.                
  227.                 delete robotTimer;
  228.                
  229.                 delete pot1;
  230.         }
  231.  
  232.         void Autonomous(void)
  233.         {
  234.                 GetWatchdog().SetEnabled(false);
  235.                
  236.                 double timeElapsed = 0.0;
  237.                
  238.                 robotTimer->Reset();
  239.                 robotTimer->Start();
  240.                
  241.                 rightEncoder->Reset();
  242.                
  243.                 gyro1->Reset();
  244.                
  245.  
  246.                 ExtendArm(false);
  247.                
  248.                 if (autoSwitch->Get()==1)//set to 1, gyro drive
  249.                 {
  250.                         while(IsAutonomous())
  251.                         {
  252.                                 timeElapsed = robotTimer->Get();
  253.  
  254.                                 Shift(0);
  255.                                
  256.                                 if(((int)(timeElapsed))%2==0)
  257.                                         SetLEDs(2);
  258.                                 else
  259.                                         SetLEDs(1);
  260.                                                                
  261.                                 if(timeElapsed < 8.0)
  262.                                 {      
  263.                                         PositionArm(ARM_ANGLE_HIGH);
  264.                                         ExtendArm(true);
  265.                                         GyroDrive(0.0, 0.5, 214);
  266.                                 }
  267.                                 else if (timeElapsed > 8.0 && timeElapsed <8.5)
  268.                                 {
  269.                                         PositionArm(ARM_ANGLE_HIGH);
  270.                                         myRobot->Drive(0.0, 0.0);
  271.                                 }
  272.                                
  273.                                 if(timeElapsed > 8.0 && timeElapsed < 11.0)
  274.                                 {
  275.                                         PositionArm(ARM_ANGLE_HIGH_DROP);
  276.                                         SpinClaw(-1, 0.7);//tilt tube forward
  277.                                 }
  278.                                 else if(timeElapsed > 11.0 && timeElapsed < 11.5)
  279.                                 {      
  280.                                         PositionArm(ARM_ANGLE_HIGH_DROP);
  281.                                         SpinClaw(0.0, 0.0);
  282.                                 }
  283.                                
  284.                                 if(timeElapsed > 9.0)
  285.                                         ExtendArm(false);
  286.                                
  287.                                 if(timeElapsed > 11.0 && timeElapsed < 13.0)
  288.                                 {
  289.                                         PositionArm(ARM_ANGLE_MID);
  290.                                         myRobot->Drive(-0.5, 0);
  291.                                 }
  292.                                 else if (timeElapsed > 12.0)
  293.                                 {
  294.                                         PositionArm(ARM_ANGLE_MID);
  295.                                         myRobot->Drive(0.0, 0.0);
  296.                                 }
  297.  
  298.                         }
  299.                 }      
  300.                 else
  301.                 {
  302.                         while(IsAutonomous())
  303.                         {
  304.                                 timeElapsed = robotTimer->Get();
  305.  
  306.                                 Shift(0);
  307.                                                        
  308.                                 if(((int)(timeElapsed))%2==0)
  309.                                         SetLEDs(2);
  310.                                 else
  311.                                         SetLEDs(1);
  312.                                                                                        
  313.                                 if(timeElapsed < 8.0)
  314.                                 {      
  315.                                         PositionArm(ARM_ANGLE_HIGH);
  316.                                         ExtendArm(true);
  317.                                         LineFollow(false, 0.5, 214);
  318.                                 }
  319.                                 else if (timeElapsed > 8.0 && timeElapsed <8.5)
  320.                                 {
  321.                                         PositionArm(ARM_ANGLE_HIGH);
  322.                                         myRobot->Drive(0.0, 0.0);
  323.                                 }
  324.                                                        
  325.                                 if(timeElapsed > 8.0 && timeElapsed < 11.0)
  326.                                 {
  327.                                         PositionArm(ARM_ANGLE_HIGH_DROP);
  328.                                         SpinClaw(-1, 0.7);//tilt tube forward
  329.                                 }
  330.                                 else if(timeElapsed > 11.0 && timeElapsed < 11.5)
  331.                                 {      
  332.                                         PositionArm(ARM_ANGLE_HIGH_DROP);
  333.                                         SpinClaw(0.0, 0.0);
  334.                                 }
  335.                                                        
  336.                                 if(timeElapsed > 9.0)
  337.                                         ExtendArm(false);
  338.                                        
  339.                                 if(timeElapsed > 11.0 && timeElapsed < 13.0)
  340.                                 {
  341.                                         PositionArm(ARM_ANGLE_MID);
  342.                                         myRobot->Drive(-0.5, 0);
  343.                                 }
  344.                                 else if (timeElapsed > 12.0)
  345.                                 {
  346.                                         PositionArm(ARM_ANGLE_MID);
  347.                                         myRobot->Drive(0.0, 0.0);
  348.                                 }
  349.  
  350.                         }      
  351.                 }
  352.         }
  353.                 /* Dual tube mode
  354.                  *
  355.                         while(IsAutonomous())
  356.                         {      
  357.                                 timeElapsed = robotTimer->Get();
  358.  
  359.                                 Shift(0);
  360.                                
  361.                                 if(((int)(timeElapsed))%2==0)
  362.                                         SetLEDs(2);
  363.                                 else
  364.                                         SetLEDs(1);
  365.                                                                
  366.                                 if((timeElapsed > 0.0) && (timeElapsed < 2.95))
  367.                                 {
  368.                                         PositionArm(ARM_ANGLE_HIGH);
  369.                                         ExtendArm(true);
  370.                                         //Shift(1);
  371.                                         GyroDrive(0.0, 1.0, 214);
  372.                                 }      
  373.                                 else if ((timeElapsed > 2.95) && (timeElapsed < 3.0))
  374.                                 {
  375.                                         rightEncoder->Reset();
  376.                                         myRobot->TankDrive(0.0, 0.0);
  377.                                 }
  378.                                
  379.                                 if((timeElapsed > 3.0) && (timeElapsed < 6.95))
  380.                                 {
  381.                                         SpinClaw(-1, 1);
  382.                                         ExtendArm(false);
  383.                                         PositionArm(ARM_ANGLE_MID);
  384.                                         if(timeElapsed > 3.5)//wait 0.5 sec before backing up
  385.                                         {
  386.                                                 PositionArm(ARM_ANGLE_FLOOR);
  387.                                                 GyroDrive(0.0, -1.0, 237);
  388.                                         }
  389.                                 }
  390.                                 else if ((timeElapsed > 6.95) && (timeElapsed < 7.0))
  391.                                 {
  392.                                         SpinClaw(0.0, 0.0);
  393.                                         rightEncoder->Reset();
  394.                                         myRobot->TankDrive(0.0, 0.0);
  395.                                 }
  396.  
  397.                                 if((timeElapsed > 7.0) && (timeElapsed < 7.95))
  398.                                 {
  399.                                         //Shift(0);
  400.                                         GyroTurn(-30.0, 1.0);
  401.                                 }
  402.                                 else if ((timeElapsed > 7.95) && (timeElapsed < 8.0))
  403.                                 {
  404.                                         //Shift(1);
  405.                                         rightEncoder->Reset();
  406.                                         myRobot->TankDrive(0.0, 0.0);
  407.                                 }
  408.                                
  409.                                 if((timeElapsed > 8.0) && (timeElapsed < 9.95))
  410.                                 {
  411.                                         PositionArm(ARM_ANGLE_FLOOR);
  412.                                         ExtendArm(true);
  413.                                         //Shift(1);
  414.                                         LineFollow(true, 1.0, 25);
  415.                                         SpinClaw(0.7, -0.7);
  416.                                 }      
  417.                                 else if ((timeElapsed > 9.95) && (timeElapsed < 10.0))
  418.                                 {
  419.                                         rightEncoder->Reset();
  420.                                         myRobot->TankDrive(0.0, 0.0);
  421.                                         SpinClaw(0.0, -0.0);
  422.                                 }
  423.                                
  424.                                 if((timeElapsed > 10.0) && (timeElapsed < 12.95))
  425.                                 {
  426.                                         PositionArm(ARM_ANGLE_HIGH);
  427.                                         ExtendArm(true);
  428.                                         //Shift(1);
  429.                                         LineFollow(true, 1.0, 215);
  430.                                 }      
  431.                                 else if ((timeElapsed > 12.95) && (timeElapsed < 13.0))
  432.                                 {
  433.                                         rightEncoder->Reset();
  434.                                         myRobot->TankDrive(0.0, 0.0);
  435.                                 }
  436.                                
  437.                                 if((timeElapsed > 13.0) && (timeElapsed < 14.95))
  438.                                 {
  439.                                         SpinClaw(-1, 1);
  440.                                         ExtendArm(false);
  441.                                         PositionArm(ARM_ANGLE_MID);
  442.                                         if(timeElapsed > 13.5)//wait 0.5 sec before backing up
  443.                                         {
  444.                                                 PositionArm(ARM_ANGLE_MID);
  445.                                                 GyroDrive(0.0, -1.0, 50);
  446.                                         }
  447.                                 }
  448.                                 else if ((timeElapsed > 14.95) && (timeElapsed < 15.0))
  449.                                 {
  450.                                         SpinClaw(0.0, 0.0);
  451.                                         rightEncoder->Reset();
  452.                                         myRobot->TankDrive(0.0, 0.0);
  453.                                 }
  454.                         }
  455.                                 */
  456.                
  457.         void GyroDrive(float desiredDriveAngle, float speed, int desiredDistance)//todo:fix for teleop
  458.         {
  459.                 double encoderInchesTraveled = fabs(rightEncoder->GetDistance());//absolute value distance
  460.                 float myAngle = gyro1->GetAngle();
  461.                 //normalizes angle from gyro to [-180,180) with zero as straight ahead
  462.                 while(myAngle >= 180)
  463.                 {
  464.                         GetWatchdog().Feed();
  465.                         myAngle = myAngle - 360;
  466.                 }
  467.                 while(myAngle < -180)
  468.                 {
  469.                         GetWatchdog().Feed();
  470.                         myAngle = myAngle + 360;
  471.                 }
  472.                
  473.                 float my_speed = 0.0;
  474.                 float turn = 0.0;
  475.                
  476.                 if(speed > 0)
  477.                         turn = -((myAngle + desiredDriveAngle) / 30.0); //proportionally adjust turn. As the robot gets more off 0, the greater the turn will be
  478.                 else
  479.                         turn = (myAngle + desiredDriveAngle) / 30.0; //proportionally adjust turn. As the robot gets more off 0, the greater the turn will be
  480.                        
  481.                 if (encoderInchesTraveled < desiredDistance)
  482.                         my_speed = speed;
  483.                 else
  484.                         my_speed = 0.0;
  485.                
  486.                 myRobot->Drive(my_speed, turn);
  487.         }
  488.  
  489. //probably should use a postive speed here
  490.         void LineFollow(bool lookForLine, float speed, int desiredDistance)
  491.         {
  492.                 //flips 0 to 1, so 0 means the sensor does not detect a line
  493.                 int lightLeftValue = (int)(!((bool)lightLeft->Get()));
  494.                 int lightMidValue = (int)(!((bool)lightMid->Get()));
  495.                 int lightRightValue = (int)(!((bool)lightRight->Get()));
  496.                
  497.                 //double ultrasonicInchesFromWall = ultrasonic1->GetRangeInches();
  498.                 double encoderInchesTraveled = rightEncoder->GetDistance();
  499.  
  500.                 float my_speed = 0.0;
  501.                 float turn = 0.0;
  502.                
  503.                 if (encoderInchesTraveled<desiredDistance)
  504.                         my_speed = speed;
  505.                 else
  506.                         my_speed = 0.0;
  507.                
  508.                 if((lightLeftValue == 1) && (lightMidValue == 1) && (lightRightValue == 1)) // sensors detect everything, maybe at end?
  509.                 {
  510.                         my_speed = (0.0);
  511.                         turn = (0.0);
  512.                 }
  513.                 else if((lightLeftValue == 0) && (lightMidValue == 0) && (lightRightValue == 0)) // sensors detect nothing, maybe passed end?
  514.                 {
  515.                         if(!lookForLine)//if not looking for line, set speed to 0 if sensors detect nothing
  516.                         {
  517.                                 my_speed = (0.0);
  518.                                 turn = (0.0);
  519.                         }
  520.                 }
  521.                 else if ((lightLeftValue == 0) && (lightMidValue == 1) && (lightRightValue == 0)) // ideal condition
  522.                         turn = 0.0;
  523.                 else if ((lightLeftValue == 1) && (lightMidValue == 0) && (lightRightValue == 0))// far left, so turn right
  524.                         turn = -0.8;
  525.                 else if ((lightLeftValue == 0) && (lightMidValue == 0) && (lightRightValue == 1))// far right, so turn left
  526.                         turn = 0.8;
  527.                 else if ((lightLeftValue == 1) && (lightMidValue == 1) && (lightRightValue == 0))// near left, so turn right
  528.                         turn = -0.8;
  529.                 else if ((lightLeftValue == 0) && (lightMidValue == 1) && (lightRightValue == 1))// near right, so turn left
  530.                         turn = 0.8;
  531.                 else if((lightLeftValue == 1) && (lightMidValue == 0) && (lightRightValue == 1)) // should happen at branch, but unreliable
  532.                         turn = -0.8; // in a perfect world, turns right at branch
  533.                 else turn = 0.0; //should never reach this
  534.                
  535.                        
  536.                 myRobot->Drive(my_speed, turn);
  537.                                
  538.                
  539.         }
  540.                
  541.         void Shift(bool gear)
  542.         {
  543.                 if (gear)
  544.                 {
  545.                         shiftHigh->Set(true);
  546.                         shiftLow->Set(false);
  547.                 }
  548.                 else
  549.                 {
  550.                         shiftHigh->Set(false);
  551.                         shiftLow->Set(true);
  552.                 }
  553.         }
  554.        
  555.         void PositionArm(INT16 desiredArmAngle)
  556.         {
  557.                
  558.                 double proportionalControl = 0.0;
  559.                 double derivativeControl = 0.0;
  560.                 double combinedControl = 0.0;
  561.                
  562.                 int currentAngle = 1000-(pot1->GetValue());
  563.                 int currentError = desiredArmAngle - currentAngle;
  564.                 if(firstCycle)
  565.                 {
  566.                         previousError = currentError;
  567.                         firstCycle = false;
  568.                 }
  569.                 int deltaError = currentError - previousError;
  570.                 previousError = currentError;
  571.                
  572.                 proportionalControl = currentError * 0.02;
  573.                 derivativeControl = deltaError * 0.01; //todo: fix this.
  574.                 combinedControl = proportionalControl + derivativeControl;
  575.                
  576.                 MoveArm(combinedControl);
  577.                
  578.         }
  579.         void MoveArm(float speed) //negative speed is lift up arm
  580.         {
  581.                 /*
  582.                 int currentAngle = 1000-(pot1->GetValue());
  583.                 if(currentAngle > ARM_ANGLE_MAX) //todo:make sure this works, not in the opposite way
  584.                         armJags->Set(-0.1);
  585.                 if(currentAngle < ARM_ANGLE_MIN)
  586.                         armJags->Set(0.1);
  587.                 */
  588.                 float my_speed = speed;
  589.                
  590.                 if(speed > 0.7)
  591.                         my_speed = 0.7;
  592.                 if(speed < -0.7)
  593.                         my_speed = -0.7;
  594.                
  595.                 //Prevent the arm from overshooting, mainly for manual arm control, which does not slow down on its own
  596.                 //if((1000-(pot1->GetValue()) > ARM_ANGLE_MID+20) && (speed > 0.3))
  597.                         //my_speed = 0.3;
  598.                
  599.                 if(highLimitSwitch->Get() && (speed > 0))
  600.                         my_speed = 0;
  601.                 if(lowLimitSwitch->Get() && (speed < 0))
  602.                         my_speed = 0;
  603.                
  604.                 armJags->Set(my_speed);
  605.                
  606.         }
  607.         void ExtendArm(bool extend)
  608.         {
  609.                 if (extend)
  610.                 {
  611.                         armExtend->Set(true);
  612.                         armRetract->Set(false);
  613.                 }
  614.                 else
  615.                 {
  616.                         armExtend->Set(false);
  617.                         armRetract->Set(true);
  618.                 }
  619.         }
  620.         void SpinClaw(float topSpeed, float bottomSpeed)//squared for more sensitive low end
  621.         {
  622.                 if (topSpeed > 0)
  623.                         topClawJag->Set(topSpeed*topSpeed);
  624.                 else
  625.                         topClawJag->Set(-topSpeed*topSpeed);
  626.                 if (bottomSpeed > 0)
  627.                         bottomClawJag->Set(bottomSpeed*bottomSpeed);
  628.                 else
  629.                         bottomClawJag->Set(-bottomSpeed*bottomSpeed);
  630.         }
  631.         //robot turns to desired position with a deadband of 2 degrees in each direction
  632.         void GyroTurn (float desiredTurnAngle, float turnSpeed)
  633.         {
  634.                 float myAngle = gyro1->GetAngle();
  635.                 //normalizes angle from gyro to [-180,180) with zero as straight ahead
  636.                 while(myAngle >= 180)
  637.                 {
  638.                         GetWatchdog().Feed();
  639.                         myAngle = myAngle - 360;
  640.                 }
  641.                 while(myAngle < -180)
  642.                 {
  643.                         GetWatchdog().Feed();
  644.                         myAngle = myAngle + 360;
  645.                 }
  646.                 if(myAngle < (desiredTurnAngle - 2))// if robot is too far left, turn right a bit
  647.                 {
  648.                         myRobot->TankDrive(turnSpeed, -turnSpeed); //(right,left)
  649.                 }
  650.                 if(myAngle > (desiredTurnAngle + 2))// if robot is too far right, turn left a bit
  651.                 {
  652.                         myRobot->TankDrive(-turnSpeed, turnSpeed); //(right,left)
  653.                 }
  654.         }
  655.  
  656.         void SetLEDs(int state)//0 is off, 1 is red, 2 is blue, 3 is both on.
  657.         {//hmm red and blue may be reversed
  658.                 if(state==0)
  659.                 {
  660.                         redLEDs->Set(0);
  661.                         blueLEDs->Set(0);
  662.                 }
  663.                 else if(state==1)
  664.                 {
  665.                         redLEDs->Set(1);
  666.                         blueLEDs->Set(0);
  667.                 }
  668.                 else if(state==2)
  669.                 {
  670.                         redLEDs->Set(0);
  671.                         blueLEDs->Set(1);
  672.                 }
  673.                 else
  674.                 {
  675.                         redLEDs->Set(1);
  676.                         blueLEDs->Set(1);
  677.                 }
  678.         }
  679.  
  680.        
  681.         void OperatorControl(void)
  682.         {
  683.                
  684.                 robotTimer->Stop();
  685.                 rightEncoder->Reset();
  686.                 myRobot->TankDrive(0.0, 0.0);
  687.                
  688.                 bool minibotStage1Complete = false;// stage 1 (drop) has to be done before stage 2 (push)
  689.                
  690.                 Shift(0);
  691.                 ExtendArm(false);
  692.                 MoveArm(0.0);
  693.                
  694.                 bool previousLEDToggle = false;
  695.                 bool currentLEDToggle = false;
  696.                
  697.                 bool previousShiftToggle = false;
  698.                 bool currentShiftToggle = false;
  699.                
  700.                 bool previousArmExtendToggle = false;
  701.                 bool currentArmExtendToggle = false;
  702.  
  703.                
  704.                 LEDState = 1;
  705.                 bool ShiftState = false;
  706.                 bool ArmExtendState = false;
  707.                
  708.                 GetWatchdog().SetEnabled(true);
  709.                
  710.                 while (IsOperatorControl())
  711.                 {
  712.                         GetWatchdog().Feed();
  713.                        
  714.                         float leftStickYAxis = leftStick->GetY();                                                       //Tank Drive
  715.                         float rightStickYAxis = rightStick->GetY();                                                     //Tank Drive
  716.                         bool rightStickButton1 = rightStick->GetRawButton(1);                           //Shifts Robot
  717.                         bool rightStickButton10 = rightStick->GetRawButton(10);                         //Button 10+11 = Activate deployment servos
  718.                         bool rightStickButton11 = rightStick->GetRawButton(11);
  719.                         bool leftStickButton9 = leftStick->GetRawButton(9);                                     //Backup Deploy Minibot
  720.                         bool rightStickButton8 = rightStick->GetRawButton(8);
  721.                        
  722.                         float manipRightYAxis = xboxStickManip->GetRawAxis(5);                          //Manual Arm Up/Down
  723.                         float manipTriggerAxis = xboxStickManip->GetRawAxis(3);                         //Rotate Tube Up/Down
  724.                         float manipLeftYAxis = xboxStickManip->GetRawAxis(2);                           //Cycle LEDs
  725.                         bool manipLeftBumper = xboxStickManip->GetRawButton(5);                         //Acquire Tube
  726.                         bool manipRightBumper = xboxStickManip->GetRawButton(6);                        //Spit Out Tube
  727.                         bool manipYButton = xboxStickManip->GetRawButton(4);                            //High Preset
  728.                         bool manipBButton = xboxStickManip->GetRawButton(2);                            //Mid Preset
  729.                         bool manipAButton = xboxStickManip->GetRawButton(1);                            //Floor Preset
  730.                         bool manipXButton = xboxStickManip->GetRawButton(3);                            //Home/Low Preset
  731.                         bool manipLeftClick = xboxStickManip->GetRawButton(9);                          //Feed preset = L click
  732.                         bool manipStartButton = xboxStickManip->GetRawButton(8);                        //Start + Select deploys minibot
  733.                         bool manipSelectButton = xboxStickManip->GetRawButton(7);
  734.                         bool manipRightClick = xboxStickManip->GetRawButton(10);                        //Manual Arm Extend
  735.  
  736.                        
  737.                         //SmartDashboard::Log(ultrasonic1->GetRangeInches(), "Ultrasonic1 inches");
  738.                        
  739.                         //SmartDashboard::Log(rightEncoder->GetDistance(), "Encoder Inches Traveled");
  740.                        
  741.                         //SmartDashboard::Log(pot1->GetValue(), "pot1 Value");//must not be neg, read true value to calibrate
  742.                        
  743.                         //SmartDashboard::Log(gyro1->GetAngle(), "gyro1 Angle");
  744.                        
  745.                         //int lightLeftValue = (int)(!((bool)lightLeft->Get()));
  746.                         //int lightMidValue = (int)(!((bool)lightMid->Get()));
  747.                         //int lightRightValue = (int)(!((bool)lightRight->Get()));
  748.                         //SmartDashboard::Log(lightLeftValue, "Left Light");
  749.                         //SmartDashboard::Log(lightMidValue, "Middle Light");
  750.                         //SmartDashboard::Log(lightRightValue, "Right Light");
  751.                        
  752.                         //SmartDashboard::Log(leftDeployServo->Get(), "Left Servo Position");
  753.                         //SmartDashboard::Log(rightDeployServo->Get(), "Right Servo Position");
  754.  
  755.                         //int autoVal = autoSwitch->Get();
  756.                         //SmartDashboard::Log(autoVal, "Autoswitch");
  757.                        
  758.                         /****************DRIVE CODE STARTS HERE***************/
  759.                         myRobot->TankDrive(-leftStickYAxis, -rightStickYAxis);
  760.                        
  761.                        
  762.                         //MINIBOT DEPLOY STAGE 1
  763.                         if (rightStickButton10 && rightStickButton11)
  764.                         {
  765.                                
  766.                                 leftDeployServo->Set(0.7); //to reset, must restart robot. Disable won't reset.
  767.                                 rightDeployServo->Set(0.3);
  768.                                 minibotStage1Complete = true;
  769.                         }
  770.                        
  771.                         //MINIBOT DEPLOY STAGE 2 BACKUP
  772.                         if(leftStickButton9 && rightStickButton8 && minibotStage1Complete)
  773.                         {
  774.                                 minibotHold->Set(false);
  775.                                 minibotDeployer->Set(true);
  776.                         }
  777.                        
  778.                         Shift(ShiftState); //shifts back to previous gear when line follow/gyro routine ends
  779.  
  780.                         //Shift code, keeps track of shift button state and actual gear state
  781.                         if(rightStickButton1)
  782.                                 currentShiftToggle = true;
  783.                         else
  784.                                 currentShiftToggle = false;
  785.                        
  786.                         if(currentShiftToggle && (previousShiftToggle != currentShiftToggle))
  787.                                 ShiftState=!ShiftState;
  788.                        
  789.                         previousShiftToggle = currentShiftToggle;
  790.                         Shift(ShiftState);
  791.                        
  792.                         /****************MANIPULATOR CODE STARTS HERE***************/
  793.                         //CYCLE LEDS
  794.                         if(manipLeftYAxis < -0.6 || manipLeftYAxis > 0.6)
  795.                                 currentLEDToggle = true;
  796.                         else
  797.                                 currentLEDToggle = false;
  798.                         //LEDState will only advance if thumbstick moves from 0 to an extreme, not from an extreme to 0,
  799.                         //nor when the thumbstick is held. This prevents rapid LED cycling.
  800.                         if(currentLEDToggle && (previousLEDToggle != currentLEDToggle))
  801.                         {
  802.                                 if (LEDState > 3)
  803.                                         LEDState = 0;
  804.                                 else
  805.                                         LEDState++;
  806.                         }
  807.                         previousLEDToggle = currentLEDToggle;
  808.                        
  809.                         //if(minibotDeploymentBarSwitch->Get()==1)
  810.                                 //SetLEDs(2);
  811.                         //else
  812.                                 SetLEDs(LEDState);
  813.                                
  814.                        
  815.                         // ARM ANGLE PRESETS
  816.                         if(manipXButton)
  817.                         {
  818.                                 if(1000-(pot1->GetValue()) > AUTO_RELEASE_THRESHOLD) //high release
  819.                                 {
  820.                                         ArmExtendState = false;
  821.                                         PositionArm(ARM_ANGLE_HIGH_DROP);
  822.                                         SpinClaw(-1.0, 0.7);
  823.                                 }
  824.                                 else //mid release
  825.                                 {
  826.                                         ArmExtendState = false;
  827.                                         PositionArm(ARM_ANGLE_MID_DROP);
  828.                                         SpinClaw(-1.0, 0.7);   
  829.                                 }
  830.                         }
  831.                         else if(manipAButton)
  832.                         {
  833.                                 ArmExtendState = true;
  834.                                 PositionArm(ARM_ANGLE_FLOOR);
  835.                         }
  836.                         else if(manipYButton)
  837.                         {
  838.                                 ArmExtendState = true;
  839.                                 PositionArm(ARM_ANGLE_HIGH);
  840.                         }
  841.                         else if(manipBButton)
  842.                         {
  843.                                 ArmExtendState = false;
  844.                                 PositionArm(ARM_ANGLE_MID);
  845.                         }
  846.                         else if(manipLeftClick)
  847.                         {
  848.                                 ArmExtendState = false;
  849.                                 PositionArm(ARM_ANGLE_FEED);
  850.                         }
  851.                         else if(manipRightYAxis>=0.1 || manipRightYAxis<=-0.1)
  852.                                 MoveArm(-manipRightYAxis); //manual arm control
  853.                         else
  854.                                 MoveArm(0.0);
  855.                        
  856.                         ExtendArm(ArmExtendState);
  857.                        
  858.                         //Arm extend code, keeps track of extend button state and actual extend state
  859.                         if(manipRightClick)
  860.                                 currentArmExtendToggle = true;
  861.                         else
  862.                                 currentArmExtendToggle = false;
  863.                        
  864.                         if(currentArmExtendToggle && (previousArmExtendToggle != currentArmExtendToggle))
  865.                                 ArmExtendState=!ArmExtendState;
  866.                        
  867.                         previousArmExtendToggle = currentArmExtendToggle;
  868.                         ExtendArm(ArmExtendState);
  869.                        
  870.                         //MINIBOT DEPLOY STAGE 2
  871.                         if(manipStartButton && manipSelectButton && minibotStage1Complete)
  872.                         {
  873.                                 minibotHold->Set(false);
  874.                                 minibotDeployer->Set(true);
  875.                         }
  876.                        
  877.                         // MANUAL CLAW BEGINS HERE
  878.                         if (manipXButton)//if the release preset is activated, skip the manual claw section
  879.                                 ;//do nothing
  880.                         else if(manipLeftBumper) //Left Bumper aquires tube
  881.                                 SpinClaw(0.85, -0.85);
  882.                         else if(manipRightBumper)
  883.                                 SpinClaw(-0.85, 0.85);
  884.                         else SpinClaw(-manipTriggerAxis, -manipTriggerAxis); // Left Trigger rotates tube up, Right rotates down
  885.                        
  886.                        
  887.                 }
  888.  
  889.         }
  890.  
  891. };
  892.  
  893. START_ROBOT_CLASS(RobotDemo);
RAW Paste Data
Top