Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*******************************************************************************
- * @mainpage
- * This is the program for the BCAMSC Robotics Team 2581P 2015 competition
- * robot.
- *
- * @details
- * This program is the primary file for the BCAMSC Robotics Team 2581P program
- * for the 2015-2016 school year.
- *
- * This program is based on a template by James Pearman in his "C on Vex"
- * library, V 1.00.
- *
- * @author James Pearman
- * @author Annelise Comai <anneliesecomai@gmail.com>
- * @author Derek Cheyne <>
- * @author Maycee McClure <maymcc01@gmail.com>
- * @author Ryan Hopkins <>
- *
- * @since 2015-05-19
- ******************************************************************************/
- #define MOT_INTAKE kVexMotor_10
- #define MOT_ELEVATOR kVexMotor_5
- #define MOT_NORTH_EAST kVexMotor_9
- #define MOT_NORTH_WEST kVexMotor_8
- #define MOT_SOUTH_EAST kVexMotor_3
- #define MOT_SOUTH_WEST kVexMotor_2
- #define MOT_FLY_ONE kVexMotor_4
- #define MOT_FLY_TWO kVexMotor_6
- #define MOT_FLY_THREE kVexMotor_1
- #define MOT_FLY_FOUR kVexMotor_7
- #include <stdlib.h>
- #include <math.h>
- #include <string.h>
- #include "ch.h" // needs for all ChibiOS programs
- #include "hal.h" // hardware abstraction layer header
- #include "vex.h" // vex library header
- //#include "apollo.h" // the library that includes the apollo debug window in screen
- //#include "smartmotor.h" // the library for smart motors
- #include "vexgyro.h"
- #include "robotc_glue.h"
- //Variables Used Almost Everywhere
- bool escapeTime(void) //Enables a kill switch for the robot, as long as !escapeTime is a condition in every function
- {
- if ((vexControllerGet(Btn7U) == 1) && //If all of these buttons are pressed
- (vexControllerGet(Btn7D) == 1) &&
- (vexControllerGet(Btn8D) == 1) &&
- (vexControllerGet(Btn5U) == 1) &&
- (vexControllerGet(Btn6U) == 1))
- {
- return true; //This function will return true
- }
- else
- {
- return false; //Else this will return false
- }
- }
- //Global variables used for controlling intake, elevator, and launcher during auton
- bool isLaunchOn= false;
- bool isIntakeOn= false;
- #include "DerekAuton.c" //Contains various functions with PID capabilities
- // Digi IO configuration
- static vexDigiCfg dConfig[kVexDigital_Num] = {
- { kVexDigital_1, kVexSensorQuadEncoder, kVexConfigQuadEnc1, kVexQuadEncoder_1 },
- { kVexDigital_2, kVexSensorQuadEncoder, kVexConfigQuadEnc2, kVexQuadEncoder_1 },
- { kVexDigital_3, kVexSensorQuadEncoder, kVexConfigQuadEnc1, kVexQuadEncoder_2 },
- { kVexDigital_4, kVexSensorQuadEncoder, kVexConfigQuadEnc2, kVexQuadEncoder_2 },
- { kVexDigital_5, kVexSensorQuadEncoder, kVexConfigQuadEnc1, kVexQuadEncoder_3 },
- { kVexDigital_6, kVexSensorQuadEncoder, kVexConfigQuadEnc2, kVexQuadEncoder_3 },
- { kVexDigital_7, kVexSensorQuadEncoder, kVexConfigQuadEnc1, kVexQuadEncoder_4 },
- { kVexDigital_8, kVexSensorQuadEncoder, kVexConfigQuadEnc2, kVexQuadEncoder_4 },
- { kVexDigital_9, kVexSensorQuadEncoder, kVexConfigQuadEnc1, kVexQuadEncoder_5 },
- { kVexDigital_10, kVexSensorQuadEncoder, kVexConfigQuadEnc2, kVexQuadEncoder_5 },
- { kVexDigital_11, kVexSensorDigitalInput, kVexConfigInput, 0 },
- { kVexDigital_12, kVexSensorDigitalInput, kVexConfigInput, 0 }
- };
- //Motor configuration
- static vexMotorCfg mConfig[kVexMotorNum] = {
- { MOT_NORTH_EAST, kVexMotor393T, kVexMotorNormal, kVexSensorIME, kImeChannel_1 },
- { MOT_NORTH_WEST, kVexMotor393T, kVexMotorNormal, kVexSensorIME, kImeChannel_2 },
- { MOT_SOUTH_EAST, kVexMotor393T, kVexMotorNormal, kVexSensorNone, 0 },
- { MOT_SOUTH_WEST, kVexMotor393T, kVexMotorNormal, kVexSensorNone, 0 },
- { MOT_FLY_ONE, kVexMotor393T, kVexMotorNormal, kVexSensorIME, kImeChannel_6 },
- { MOT_FLY_TWO, kVexMotor393T, kVexMotorNormal, kVexSensorNone, 0 },
- { MOT_FLY_THREE, kVexMotor393T, kVexMotorNormal, kVexSensorIME, kImeChannel_4 },
- { MOT_FLY_FOUR, kVexMotor393T, kVexMotorReversed, kVexSensorIME, kImeChannel_5 },
- { MOT_INTAKE, kVexMotor393T, kVexMotorNormal, kVexSensorNone, 0 },
- { MOT_ELEVATOR, kVexMotor393T, kVexMotorNormal, kVexSensorIME, kImeChannel_3}
- };
- //#include "automaticaiming.c"
- //#include "autonfunctions.c"
- void moveFunc(int ch1, int ch2, int ch4)
- {
- vexMotorSet(MOT_NORTH_EAST, -ch1 + ch2 + ch4);
- vexMotorSet(MOT_NORTH_WEST, ch1 + ch2 + ch4);
- vexMotorSet(MOT_SOUTH_EAST, -ch1 - ch2 + ch4);
- vexMotorSet(MOT_SOUTH_WEST, ch1 - ch2 + ch4);
- }
- void setIntake(int butnup, int butndown)
- {
- if( !(escapeTime()))
- {
- vexMotorSet(MOT_INTAKE, ((butnup - butndown) * 127) );
- vexMotorSet(MOT_ELEVATOR, ( (butnup - butndown) * 127) );
- }
- }
- void setMotors(int sped)
- {
- if(!escapeTime())
- {
- vexMotorSet(MOT_FLY_ONE, sped);
- vexMotorSet(MOT_FLY_TWO, sped);
- vexMotorSet(MOT_FLY_THREE, sped);
- vexMotorSet(MOT_FLY_FOUR, sped);
- }
- }
- void slowDown(void)
- {
- int speed = vexMotorGet(MOT_FLY_ONE);
- if(speed > 0)
- {
- speed -= 3;
- chThdSleepMilliseconds(100);
- setMotors(speed);
- }
- }
- /*
- void autonShootBalls(int time)
- {
- vexMotorSet(MOT_FLY_ONE, 127);
- vexMotorSet(MOT_FLY_TWO, 127);
- vexMotorSet(MOT_FLY_THREE, 127);
- vexMotorSet(MOT_FLY_FOUR, 127);
- chThdSleepMilliseconds(time);
- float speed = vexMotorGet(MOT_FLY_ONE);
- if(speed > 0)
- {
- speed -= 3;
- //chThdSleepMilliseconds(100);//
- vexMotorSet(MOT_FLY_ONE, speed);
- vexMotorSet(MOT_FLY_TWO, speed);
- vexMotorSet(MOT_FLY_THREE, speed);
- vexMotorSet(MOT_FLY_FOUR, speed);
- }
- }
- */
- void shootBall(int ballButton)
- {
- int speed = vexMotorGet(MOT_FLY_ONE);
- if(ballButton) //If button for launching the ball is pressed
- {
- vexMotorSet(MOT_FLY_ONE, 127);
- vexMotorSet(MOT_FLY_TWO, 127);
- vexMotorSet(MOT_FLY_THREE, 127);
- vexMotorSet(MOT_FLY_FOUR, 127);
- }
- else if(speed > 0)
- {
- speed -= 3;
- chThdSleepMilliseconds(100);
- vexMotorSet(MOT_FLY_ONE, speed);
- vexMotorSet(MOT_FLY_TWO, speed);
- vexMotorSet(MOT_FLY_THREE, speed);
- vexMotorSet(MOT_FLY_FOUR, speed);
- }
- }
- //Start motors
- //Set all of the motors to 127
- //slowDown();
- /*//Default State
- while(ballButton != 1) {
- slowDown();
- }
- */
- task vexLcdThread(void *arg)
- {
- (void)arg;
- vexTaskRegister("lcdout");
- bool shift = false;
- int print = 0;
- while(1)
- {
- /*
- float xCoord = findXPositionOnField();
- float yCoord = findYPositionOnField();
- */
- // gyro = (vexGyroGet() / 10);
- //This block sets the screen that is displayed
- if (vexLcdButtonGet(1) == kLcdButtonLeft)
- {
- if(!shift)
- {
- print += 1;
- }
- shift = true;
- }
- else if (vexLcdButtonGet(1) == kLcdButtonRight)
- {
- if(!shift)
- {
- print -= 1;
- }
- shift = true;
- }
- else
- {
- shift = false;
- }
- //Each level of print represents a screen that could be displayed.
- if (print == -1)
- {
- print = 0;
- }
- else if (print == 0)
- {
- vexLcdPrintf(1,1, "%s%d","North East", vexMotorPositionGet(MOT_NORTH_EAST) );
- vexLcdPrintf(1,0, "%s%d","North West", vexMotorPositionGet(MOT_NORTH_WEST) );
- }
- else if (print == 1)
- {
- vexLcdPrintf(1,1, "%s%d","Elevator", vexMotorPositionGet(MOT_ELEVATOR) );
- vexLcdPrintf(1,0, "%s%1.1d","Fly3", vexMotorPositionGet(MOT_FLY_THREE) );
- }
- else if (print == 2)
- {
- vexLcdPrintf(1,1, "%s%d","Fly4", vexMotorPositionGet(MOT_FLY_FOUR) );
- vexLcdPrintf(1,0, "%s%d","Fly1", vexMotorPositionGet(MOT_FLY_ONE) );
- }
- else if (print == 3)
- {
- // vexLcdPrintf(1,1, "%s%d","PT: ",vexAdcGet(0));
- vexLcdPrintf(1,1, "%s%d","FLY1SPED", vexMotorGet(MOT_FLY_ONE));
- vexLcdPrintf(1,0, "%s%d", "y", 0);
- }
- else if (print == 4)
- {
- print = 0;
- }
- chThdSleepMilliseconds(25);
- }
- return (msg_t)0;
- }
- task runIntake(void *arg)
- {
- (void)arg;
- vexTaskRegister("intake");
- while(1)
- {
- setIntake(vexControllerGet(Btn6U), vexControllerGet(Btn6D) );
- }
- return (msg_t)0;
- }
- task runLauncher(void *arg)
- {
- (void)arg;
- vexTaskRegister("launcher");
- while(1)
- {
- shootBall(vexControllerGet(Btn8U) );
- }
- return (msg_t)0;
- }
- task runSlowLauncher(void *arg) //used to launch at slower speed so motors can rest
- {
- (void)arg;
- vexTaskRegister("launcher");
- while(1)
- {
- int speed = vexMotorGet(MOT_FLY_ONE);
- if(vexControllerGet(Btn8L)) //If 8L button is pressed
- {
- vexMotorSet(MOT_FLY_ONE, 100);
- vexMotorSet(MOT_FLY_TWO, 100);
- vexMotorSet(MOT_FLY_THREE, 100);
- vexMotorSet(MOT_FLY_FOUR, 100);
- }
- else if(speed > 0)
- {
- speed -= 3;
- chThdSleepMilliseconds(100);
- vexMotorSet(MOT_FLY_ONE, speed);
- vexMotorSet(MOT_FLY_TWO, speed);
- vexMotorSet(MOT_FLY_THREE, speed);
- vexMotorSet(MOT_FLY_FOUR, speed);
- }
- }
- return (msg_t)0;
- }
- task driveTask(void *arg)
- {
- (void)arg;
- vexTaskRegister("drive");
- while(1)
- {
- int ch2, ch1, ch4;
- int deadZone = 10;
- if(abs(vexControllerGet(Ch2)) > deadZone)
- {
- ch2 = vexControllerGet(Ch2);
- }
- else ch2 = 0;
- if(abs(vexControllerGet(Ch1)) > 10)
- {
- ch1 = vexControllerGet(Ch1);
- }
- else ch1 = 0;
- if(abs(vexControllerGet(Ch4)) > deadZone)
- {
- ch4 = vexControllerGet(Ch4);
- }
- else ch4 = 0;
- vexMotorSet(MOT_NORTH_EAST, -ch2 + ch1 + ch4);
- vexMotorSet(MOT_NORTH_WEST, ch2 + ch1 + ch4);
- vexMotorSet(MOT_SOUTH_EAST, -ch2 - ch1 + ch4);
- vexMotorSet(MOT_SOUTH_WEST, ch2 - ch1 + ch4);
- /*
- vexMotorSet(MOT_NORTH_EAST, -vexControllerGet(Ch2) + vexControllerGet(Ch1) + vexControllerGet(Ch4));
- vexMotorSet(MOT_NORTH_WEST, vexControllerGet(Ch2) + vexControllerGet(Ch1) + vexControllerGet(Ch4));
- vexMotorSet(MOT_SOUTH_EAST, -vexControllerGet(Ch2) - vexControllerGet(Ch1) + vexControllerGet(Ch4));
- vexMotorSet(MOT_SOUTH_WEST, vexControllerGet(Ch2) - vexControllerGet(Ch1) + vexControllerGet(Ch4));
- */
- }
- }
- task autonRunIntake(void *arg)
- {
- (void)arg;
- vexTaskRegister("autonIntake");
- while(1)
- {
- //while(!escapeTime)
- //{
- if(isIntakeOn)
- {
- vexMotorSet(MOT_INTAKE, 127);
- vexMotorSet(MOT_ELEVATOR, 127);
- }
- else {
- vexMotorSet(MOT_INTAKE, 0);
- vexMotorSet(MOT_ELEVATOR, 0);
- }
- //}
- }
- }
- task autonShootBalls(void *arg)
- {
- (void)arg;
- vexTaskRegister("autonLauncher");
- while(1)
- {
- //while(!escapeTime)
- //{
- float speed = vexMotorGet(MOT_FLY_ONE);
- if(isLaunchOn == true) //If the global isLaunchOn variable is declared to be true within the function
- {
- vexMotorSet(MOT_FLY_ONE, 80); //Set all of the launch motors to 80 (can be changed later if needed)
- vexMotorSet(MOT_FLY_TWO, 80);
- vexMotorSet(MOT_FLY_THREE, 80);
- vexMotorSet(MOT_FLY_FOUR, 80);
- //chThdSleepMilliseconds(time);//
- }
- else if(speed > 0) //Else, slow down the launcher motors
- {
- speed -= 3;
- chThdSleepMilliseconds(100);
- vexMotorSet(MOT_FLY_ONE, speed);
- vexMotorSet(MOT_FLY_TWO, speed);
- vexMotorSet(MOT_FLY_THREE, speed);
- vexMotorSet(MOT_FLY_FOUR, speed);
- }
- //}
- /* else {
- vexMotorSet(MOT_FLY_ONE,0);
- vexMotorSet(MOT_FLY_TWO,0);
- vexMotorSet(MOT_FLY_THREE,0);
- vexMotorSet(MOT_FLY_FOUR,0);
- }
- */
- }
- }
- /**
- * @brief User setup
- * @details
- * The digital and motor ports can (should) be configured here.
- */
- void vexUserSetup()
- {
- vexDigitalConfigure(dConfig, DIG_CONFIG_SIZE( dConfig));
- vexMotorConfigure(mConfig, MOT_CONFIG_SIZE( mConfig));
- }
- /**
- * @brief User initialize
- * @details
- * This function is called after all setup is complete and communication has
- * been established with the master processor.
- * Start other tasks and initialize user variables here
- */
- void vexUserInit()
- {
- vexGyroInit(kVexAnalog_1);
- vexMotorPositionSet(MOT_SOUTH_WEST, 0);
- vexMotorPositionSet(MOT_SOUTH_EAST, 0);
- vexMotorPositionSet(MOT_NORTH_WEST, 0);
- vexMotorPositionSet(MOT_NORTH_EAST, 0);
- }
- /**
- * @brief Autonomous
- * @details
- * This thread is started when the autonomous period is started
- */
- msg_t vexAutonomous(void *arg)
- {
- (void)arg;
- // Must call this
- vexTaskRegister("auton");
- StartTask(vexLcdThread);
- StartTask(autonRunIntake);
- StartTask(autonShootBalls);
- return (msg_t)0;
- }
- /**
- * @brief Driver Control
- *
- * @details
- * This thread is started when the driver control period is started
- */
- msg_t vexOperator(void *arg)
- {
- (void)arg;
- // Must call this
- vexTaskRegister("operator");
- StartTask(vexLcdThread);
- StartTask(runIntake);
- //StartTask(runLauncher);
- StartTask(driveTask);
- //StartTask(runSlowLauncher);
- //StartTask(autonShootBalls);
- //StartTask(autonRunIntake);
- // Run until asked to terminate
- while(!chThdShouldTerminate())
- {
- if (vexControllerGet(Btn7L)) //Just here to test things
- {
- turnLeftNoPID(90);
- }
- if(vexControllerGet(Btn8D))
- {
- //////////This is code for an auton program focused on launching the four preloads (currently written for three). Place robot on starting tile directly facing goal with three balls in the intake, with the ball furthest in pressed up against the Moibus strip chain guard box (this auton works with no balls actually in elevator, just one at the bottom of the elevator, this can be changed later)
- //Warm Up Launcher
- isLaunchOn = true; //Turns on launcher (make sure that autonShootBalls task is started before trying this)
- chThdSleepMilliseconds(2500); //Time needed for launcher to get up to full speed
- vexMotorPositionSet(MOT_ELEVATOR, 0); //Resets encoder on elevator motor
- //Shooting the first ball
- isIntakeOn = true; //Turns on the intake and the elevator motors (make sure that autonRunIntake task is started before trying this)
- while(vexMotorPositionGet(MOT_ELEVATOR) < 800) //The elevator motor will run and the elevator encoder will count up to 800 (this is approx. number of elevator encoder counts needed to get ball at the bottom of the elevator shaft up the elevator and through launcher without the other balls coming up the elevator getting stuck on the bottom wheel of the launcher - this could be changed if we shoved a ball halfway up the elevator shaft as well)
- {
- chThdSleepMilliseconds(25); //Tells robot to keep doing what it's doing until encoder reaches its designated value
- }
- isIntakeOn = false; //Turns off the intake so that the launcher motors can get back up to speed after launching ball
- vexMotorPositionSet(MOT_ELEVATOR, 0); //Resets elevator encoder
- chThdSleepMilliseconds(3000); //Time needed for launcher motors to get back up to speed
- //Second ball
- isIntakeOn = true; //Turns intake back on again
- while(vexMotorPositionGet(MOT_ELEVATOR) < 800) //Brings single ball up elevator
- {
- chThdSleepMilliseconds(25);
- }
- isIntakeOn = false; //Turns off intake after ball is launched
- vexMotorPositionSet(MOT_ELEVATOR, 0); //Resets elevator encoder
- chThdSleepMilliseconds(2500); //Launcher motors get back up to speed
- //Third Ball
- isIntakeOn = true; //Turns on intake
- while(vexMotorPositionGet(MOT_ELEVATOR) < 800) //Brings single ball up elevator
- {
- chThdSleepMilliseconds(25);
- }
- //Stop everything
- isIntakeOn = false; //Stops intake
- isLaunchOn = false; //Stops launcher
- }
- if(vexControllerGet(Btn7U)) {
- //This program does the same thing as above (launches 3 preloads, same position as above), but with wait statements instead of encoder counts, making it a more unreliable and inefficient program.
- isLaunchOn = true; //starts launcher
- //I found out that this method below doesn't work - essentially, the vexMotorGet function thinks that when you set a motor to 80, the motor immediately is powered to 80, while the wheels (what we actually want to measure the speed of) have to take time to accelerate to their full speed. We could put an encoder on the wheel axle and write a function to measure exactly how fast that encoder increases per second to find the speed of the wheel later if we have time.
- /*while(vexMotorGet(MOT_FLY_ONE) < 80)
- { //once fly wheel motors are at full speed
- chThdSleepMilliseconds(25)
- }
- */
- chThdSleepMilliseconds(2500);
- isIntakeOn = true;
- chThdSleepMilliseconds(4000);
- isIntakeOn = false;
- chThdSleepMilliseconds(2500);
- isIntakeOn = true;
- chThdSleepMilliseconds(4000);
- isIntakeOn = false;
- isLaunchOn = false;
- /*
- chThdSleepMilliseconds(10000);
- isIntakeOn = false;
- isLaunchOn = false;
- */
- }
- //if (vexControllerGet(Btn7L))
- // {
- // moveForward(10);
- // }
- //findXPositionOnField();
- //findYPositionOnField();
- //xCoord = findXPositionOnField();
- // yCoord = findYPositionOnField();
- /*
- moveFunc( vexControllerGet(Ch1), vexControllerGet(Ch2), vexControllerGet(Ch3));
- setIntake( vexControllerGet(Btn5U), vexControllerGet(Btn5D) );
- shootBall( vexControllerGet(Btn8U) );
- */
- //Don't hog cpu
- vexSleep(25);
- }
- return (msg_t)0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement