Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "robot-config.h"
- #include <cmath>
- /*---------------------------------------------------------------------------*/
- /* */
- /* Description: Competition template for VCS VEX V5 */
- /* */
- /*---------------------------------------------------------------------------*/
- //Creates a competition object that allows access to Competition methods.
- vex::competition Competition;
- /*---------------------------------------------------------------------------*/
- /* Pre-Autonomous Functions */
- /* */
- /* You may want to perform some actions before the competition starts. */
- /* Do them in the following function. You must return from this function */
- /* or the autonomous and usercontrol tasks will not be started. This */
- /* function is only called once after the cortex has been powered on and */
- /* not every time that the robot is disabled. */
- /*---------------------------------------------------------------------------*/
- int count;
- int press;
- int SL = 15.1/2; //left right distance from tracking center to tracking wheels.
- int SR = 15.1/2;
- int SS; //forward backward distance from tracking center to back tracking wheel.
- float PreviousAbsTheta; // absolute orientation from previous loop.
- float NewAbsTheta; // new absolute orientation.
- float DeltaTheta; // difference between thetas of each loop.
- float LeftEncoderRotation, RightEncoderRotation,StrafeEncoderRotation, SrotPrev, LrotPrev, RrotPrev;
- float DLcurrent, DRcurrent, DScurrent, DLprevious, DRprevious, DSprevious, DeltaLeftInches , DeltaRightInches , DeltaStrafeInches ; // change in each encoder value of each tracking wheel since last cycle as inches
- float DLr, DRr,DSr ; //change in each encoder value as degrees
- double localOffsetX, localOffsetY;
- float GlobalTheta; //global theta, ultimate orientation of the robot.
- float AvgTheta; // average orientation.
- float xl,yl; //cartesian coordinates for this loop
- float x, y;
- double r; //this is just the hypotenuse made by xl and xy
- double q; // q=r^
- float LIMIT;
- int PositionTracking(void)
- {
- {
- LeftEncoderRotation = LeftFront.rotation(rotationUnits::deg); //collecting current encoder values
- RightEncoderRotation = RightFront.rotation(rotationUnits::deg);
- //StrafeEncoderRotation = StrafeTr.rotation(rotationUnits::deg);
- DLr = LeftEncoderRotation - LrotPrev;
- DRr = RightEncoderRotation - RrotPrev; //Difference between encoder values in rotationUnits degrees
- //DSr = StrafeEncoderRotation - SrotPrev; //since last loop
- DLcurrent = (DLr/360) * (12.959);
- DRcurrent = (DRr/360) * (12.959); //The difference between encoder values since last loop
- //DScurrent = (DSr/360) * (12.959); // converted to inches.
- DeltaLeftInches = DLcurrent - DLprevious;
- DeltaRightInches = DRcurrent - DRprevious; //Distance in inches each wheel has turned since last loop.
- //DeltaStrafeInches = DScurrent - DSprevious;
- NewAbsTheta = PreviousAbsTheta + (DLr-DRr)/15.1; //orientation of robot in this loop.
- DeltaTheta = NewAbsTheta-PreviousAbsTheta; //difference between orientation of robot between this loop and last loop.
- DScurrent = (DeltaTheta/360) * (47.43);
- DeltaStrafeInches = DScurrent - DSprevious;
- GlobalTheta = GlobalTheta+NewAbsTheta; //updating global orientation of robot.
- if(DeltaTheta==0)
- {localOffsetX = DeltaStrafeInches;
- localOffsetY = DeltaRightInches;
- }
- if(DeltaTheta!=0)
- {
- localOffsetX = 2*sin(GlobalTheta/2)+DeltaStrafeInches/DeltaTheta +SR ;
- localOffsetY = 2*sin(GlobalTheta/2)+DeltaRightInches/DeltaTheta + SR;
- }
- AvgTheta = PreviousAbsTheta + DeltaTheta/2; // average orientation of robot in maneuver.
- q = (((localOffsetX)*(localOffsetX))+((localOffsetY)*(localOffsetY)));
- r = sqrt(q); //calculate the length of hypotenuse made by xl and yl.
- xl = r * (cos(DeltaTheta-AvgTheta) ); //Here we convert the cartesian coordinates to polar coordinates,
- yl = r * (sin(DeltaTheta-AvgTheta) ); //then rotate those polar coordinates by our Tm. Then we can convert back to cartesian coordinates
- x = x + xl; //updating global xy coordinates
- y = y + yl;
- DLprevious = DLcurrent;
- DRprevious = DRcurrent; //Reassigning current distance each wheel has traveled
- DSprevious = DScurrent; //in inches as past values for the next loop.
- LrotPrev = LeftEncoderRotation;
- RrotPrev = RightEncoderRotation; //Reassigning current encoder values of each wheel as
- SrotPrev = StrafeEncoderRotation; //past value for the next loop.
- Brain.Screen.clearScreen();
- Brain.Screen.printAt(5,10,"X = %f",x);
- Brain.Screen.printAt(5,40,"Y = %f",y);
- Brain.Screen.printAt(5,70,"GlobalTheta = %f",GlobalTheta);
- task::sleep(20);
- }
- return(0);
- }
- double circumference = 12.959; // circumference of wheel in inches
- double rotateDegree;
- double turningCircumference = 33.725;
- void moveForInch(double target,double speed) {
- rotateDegree = (360*target)/circumference ;
- LeftFront.setVelocity(speed,vex::velocityUnits::rpm);
- RightFront.setVelocity(speed,vex::velocityUnits::rpm);
- LeftBack.setVelocity(speed,vex::velocityUnits::rpm);
- RightBack.setVelocity(speed,vex::velocityUnits::rpm);
- LeftFront.rotateFor(rotateDegree, vex::rotationUnits::deg, false);
- LeftBack.rotateFor(rotateDegree, vex::rotationUnits::deg, false);
- RightFront.rotateFor(rotateDegree, vex::rotationUnits::deg,false);
- RightBack.rotateFor(rotateDegree, vex::rotationUnits::deg);
- }
- void driveTrainStop(void){
- LeftFront.stop(brakeType::hold);
- RightFront.stop(brakeType::hold);
- LeftBack.stop(brakeType::hold);
- RightBack.stop(brakeType::hold);
- }
- void turnForDegrees(double degrees){
- double rotateinch = ((degrees/360)*(turningCircumference)) ;
- double leftRotateDegree = ((-1)*(360*rotateinch)/circumference );
- double rotateDegree = ((360*rotateinch)/circumference);
- LeftFront.setVelocity(100,vex::velocityUnits::rpm);
- RightFront.setVelocity(100,vex::velocityUnits::rpm);
- LeftBack.setVelocity(100,vex::velocityUnits::rpm);
- RightBack.setVelocity(100,vex::velocityUnits::rpm);
- LeftFront.startRotateFor(leftRotateDegree, vex::rotationUnits::deg);
- LeftBack.startRotateFor(leftRotateDegree, vex::rotationUnits::deg);
- RightFront.startRotateFor(rotateDegree, vex::rotationUnits::deg);
- RightBack.rotateFor(rotateDegree, vex::rotationUnits::deg);
- }
- int leftRotateDegree;
- void turnForInch(double target)
- {
- rotateDegree = (360*target)/circumference ;
- leftRotateDegree = ((-1)*(360*target)/circumference);
- LeftFront.rotateFor(leftRotateDegree, vex::rotationUnits::deg, false);
- LeftBack.rotateFor(leftRotateDegree, vex::rotationUnits::deg, false);
- RightBack.rotateFor(rotateDegree, vex::rotationUnits::deg,false);
- RightFront.rotateFor(rotateDegree, vex::rotationUnits::deg);
- }
- void moveLeftForInch(float target) {
- rotateDegree = (360*target)/circumference ;
- LeftFront.rotateFor(rotateDegree, vex::rotationUnits::deg, false);
- LeftBack.rotateFor(rotateDegree, vex::rotationUnits::deg); }
- void moveRightForInch(float target)
- {
- rotateDegree = (360*target)/circumference ;
- RightFront.rotateFor(rotateDegree, vex::rotationUnits::deg, false);
- RightBack.rotateFor(rotateDegree, vex::rotationUnits::deg);
- }
- void speed(double target)
- {
- LeftFront.setVelocity(target,vex::velocityUnits::rpm);
- RightFront.setVelocity(target,vex::velocityUnits::rpm);
- LeftBack.setVelocity(target,vex::velocityUnits::rpm);
- RightBack.setVelocity(target,vex::velocityUnits::rpm);
- }
- void autonLiftTo(double height)
- {
- Lift.rotateTo(height,rotationUnits::deg);
- Lift.stop(brakeType::hold);
- }
- void pre_auton( void ) {
- // All activities that occur before the competition starts
- // Example: clearing encoders, setting servo positions, ...
- count = 0;
- press = 0;
- LeftFront.setStopping(brakeType::brake);
- RightFront.setStopping(brakeType::brake);
- RightBack.setStopping(brakeType::brake);
- LeftBack.setStopping(brakeType::brake);
- }
- /*---------------------------------------------------------------------------*/
- /* */
- /* Autonomous Task */
- /* */
- /* This task is used to control your robot during the autonomous phase of */
- /* a VEX Competition. */
- /* */
- /* You must modify the code to add your own robot specific commands here. */
- /*---------------------------------------------------------------------------*/
- int Delay;
- void autonomous( void )
- {
- //sideChange
- std::string side = "blue";
- int number = 1;
- if (side == "red")
- {
- number = 1;
- }
- else
- {
- number = -1;
- }
- MGL.spin(vex::directionType::rev, 100, vex::velocityUnits::pct);
- vex::task::sleep(750);
- MGL.spin(vex::directionType::fwd, 100, vex::velocityUnits::pct);
- vex::task::sleep(750);
- MGL.stop(brakeType::coast);
- Lift.spin(vex::directionType::fwd, 100, vex::velocityUnits::pct);
- vex::task::sleep(750);
- Lift.spin(vex::directionType::rev, 100, vex::velocityUnits::pct);
- vex::task::sleep(750);
- Lift.stop(brakeType::coast);
- /*
- IntakeL.spin(vex::directionType::rev, 100, vex::velocityUnits::pct);
- IntakeR.spin(vex::directionType::rev, 100, vex::velocityUnits::pct);
- moveForInch(-48,100); //get first cubes
- IntakeL.stop(brakeType::coast);
- IntakeR.stop(brakeType::coast);
- vex::task::sleep(100);
- moveForInch(35.5,100);//move back
- vex::task::sleep(100);
- turnForDegrees(-65); //angle
- vex::task::sleep(100);
- moveForInch(24,100); //move back
- vex::task::sleep(100);
- turnForDegrees(74); //straighten
- vex::task::sleep(100);
- moveForInch(4,25); //push into wall
- vex::task::sleep(100);
- IntakeL.spin(vex::directionType::rev, 100, vex::velocityUnits::pct);
- IntakeR.spin(vex::directionType::rev, 100, vex::velocityUnits::pct);
- */
- IntakeL.spin(vex::directionType::rev, 100, vex::velocityUnits::pct);
- IntakeR.spin(vex::directionType::rev, 100, vex::velocityUnits::pct);
- moveForInch(-34,80); //drive into second cube stack
- vex::task::sleep(100);
- IntakeL.spin(vex::directionType::rev, 25, vex::velocityUnits::pct);
- IntakeR.spin(vex::directionType::rev, 25, vex::velocityUnits::pct);
- moveForInch(3,80); //back up
- vex::task::sleep(100);
- IntakeL.spin(vex::directionType::rev, 50, vex::velocityUnits::pct);
- IntakeR.spin(vex::directionType::rev, 50, vex::velocityUnits::pct);
- turnForDegrees(157*number); //turn to zone
- Controller1.Screen.clearScreen();
- vex::task::sleep(100);
- moveForInch(-30,80); //move into zone
- vex::task::sleep(100);
- while (MGL_LIMIT.pressing() == false)
- {
- Controller1.Screen.print("Hello");
- MGL.spin(vex::directionType::rev, 15, vex::velocityUnits::pct);
- IntakeL.spin(vex::directionType::fwd, 100, vex::velocityUnits::pct);
- IntakeR.spin(vex::directionType::fwd, 100, vex::velocityUnits::pct);
- }
- MGL.stop(brakeType::hold);
- IntakeL.spin(vex::directionType::fwd, 20, vex::velocityUnits::pct);
- IntakeR.spin(vex::directionType::fwd, 20, vex::velocityUnits::pct);
- task::sleep(200);
- moveForInch(15,50);
- }
- int DriveTrain(void){
- //Set the left and right motor to spin forward using the controller Axis values as the velocity value.
- LeftFront.spin(vex::directionType::rev, Controller1.Axis3.value(), vex::velocityUnits::pct);
- LeftBack.spin(vex::directionType::rev, Controller1.Axis3.value(), vex::velocityUnits::pct);
- RightBack.spin(vex::directionType::rev, Controller1.Axis2.value(), vex::velocityUnits::pct);
- RightFront.spin(vex::directionType::rev, Controller1.Axis2.value(), vex::velocityUnits::pct);
- if(Controller1.ButtonRight.pressing()) { //If button up is pressed...
- LeftFront.spin(vex::directionType::fwd, 100, vex::velocityUnits::pct);
- LeftBack.spin(vex::directionType::rev, 100, vex::velocityUnits::pct);
- RightBack.spin(vex::directionType::fwd, 100, vex::velocityUnits::pct);
- RightFront.spin(vex::directionType::rev, 100, vex::velocityUnits::pct);
- }
- return(0);
- }
- int Arm(void){
- //Arm Control
- if(Controller1.ButtonX.pressing()) { //If button up is pressed...
- //...Spin the arm motor forward.
- Lift.spin(vex::directionType::fwd, 100, vex::velocityUnits::pct);
- }
- else if(Controller1.ButtonB.pressing()) { //If the down button is pressed...
- //...Spin the arm motor backward.
- Lift.spin(vex::directionType::rev, 100, vex::velocityUnits::pct);
- }
- else { //If the the up or down button is not pressed...
- //...Stop the arm motor.
- Lift.stop(vex::brakeType::hold);
- }
- return(0);
- }
- int Intake(void){
- if(Controller1.ButtonL2.pressing())
- {
- IntakeL.spin(vex::directionType::rev, 100, vex::velocityUnits::pct);
- IntakeR.spin(vex::directionType::rev, 100, vex::velocityUnits::pct);
- }
- else if(Controller1.ButtonL1.pressing())
- {
- IntakeL.spin(vex::directionType::fwd, 100, vex::velocityUnits::pct);
- IntakeR.spin(vex::directionType::fwd, 100, vex::velocityUnits::pct);
- }
- else
- {
- IntakeL.stop(brakeType::coast);
- IntakeR.stop(brakeType::coast);
- }
- return(0);
- }
- int MobileGoal(void)
- {
- if(Controller1.ButtonA.pressing()) { //If button up is pressed...
- MGL.spin(vex::directionType::fwd, 100, vex::velocityUnits::pct);
- }
- else if(Controller1.ButtonR1.pressing())
- {
- MGL.spin(vex::directionType::rev, 25, vex::velocityUnits::pct);
- }
- else if(Controller1.ButtonR2.pressing())
- {
- MGL.stop(brakeType::hold);
- }
- else
- {
- MGL.stop(brakeType::coast);
- }
- return(0);
- }
- /*----------------------------------------------------------------------------*/
- /* */
- /* User Control Task */
- /* */
- /* This task is used to control your robot during the user control phase of */
- /* a VEX Competition. */
- /* */
- /* You must modify the code to add your own robot specific commands here. */
- /*----------------------------------------------------------------------------*/
- void usercontrol( void )
- {
- while (1){
- Brain.Screen.clearScreen();
- DriveTrain();
- Intake();
- Arm();
- MobileGoal();
- PositionTracking();
- vex::task::sleep(20); //Sleep the task for a short amount of time to prevent wasted resources.
- }
- }
- //
- // Main will set up the competition functions and callbacks.
- //
- int main() {
- //Run the pre-autonomous function.
- pre_auton();
- //Set up callbacks for autonomous and driver control periods.
- Competition.autonomous( autonomous );
- Competition.drivercontrol( usercontrol );
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement