Advertisement
Guest User

7686C Program IV

a guest
Feb 22nd, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.71 KB | None | 0 0
  1. #include <cmath>
  2.  
  3. using namespace vex;
  4.  
  5. #include "robot-config.h"
  6.  
  7. //Creates a competition object that allows access to Competition methods.
  8. competition Competition;
  9.  
  10. void sleep(double s){
  11.     task::sleep(s * 1000);
  12. }
  13.  
  14. enum DrivingMode { speed, precision };
  15. enum AimColor { red, blue };
  16.  
  17. void run(motor *Motor, double speed){
  18.     if(speed != 0){
  19.         Motor->spin(directionType::fwd, speed, velocityUnits::rpm);
  20.     } else {
  21.         Motor->stop(brakeType::coast);
  22.     }
  23. }
  24.  
  25. class Robot;
  26.  
  27. class VisionTracker {
  28.     public:
  29.     AimColor *aimColor;
  30.     vision::signature *blueflag, *redflag;
  31.     vision *VisionSensor;
  32.     long framesSinceFlag;
  33.     int screen_x_middle, current_x_middle, screen_y_middle, current_y_middle;
  34.    
  35.     VisionTracker(AimColor *robotAimColor){
  36.         aimColor = robotAimColor;
  37.        
  38.         blueflag = new vision::signature(2, -4229, -2759, -3494, 7523, 14313, 10918, 1.399999976158142, 0);
  39.         redflag = new vision::signature(1, 6843, 8403, 7622, -421, 395, -14, 3.9, 0);
  40.         VisionSensor = new vision(PORT7 /* 19 */, 50, *blueflag, *redflag);
  41.         framesSinceFlag = 0;
  42.  
  43.         screen_x_middle = 316 / 2;
  44.         screen_y_middle = 212 / 2;  
  45.     }
  46.    
  47.     void calculate(){
  48.         if(*aimColor == AimColor::blue){
  49.             VisionSensor->takeSnapshot(*blueflag);
  50.         } else if(*aimColor == AimColor::red){
  51.             VisionSensor->takeSnapshot(*redflag);
  52.         }
  53.        
  54.         if(VisionSensor->objectCount > 0){
  55.             current_x_middle = VisionSensor->largestObject.centerX;
  56.             current_y_middle = VisionSensor->largestObject.centerY;
  57.             framesSinceFlag = 0;
  58.         } else {
  59.             framesSinceFlag++;
  60.         }
  61.     }
  62.    
  63.     void display(controller *Con, int row){
  64.         Con->Screen.clearLine(3);
  65.         Con->Screen.setCursor(3, 1);
  66.         if(framesSinceFlag < 5){
  67.             Con->Screen.print("[%c] Y: %3d, X: %3d", *aimColor == AimColor::blue ?'B':'R', getY(), getX());
  68.             if(abs(getX()) < 8) Con->rumble("-");
  69.         } else {
  70.             Con->Screen.print("[%c] No flag", *aimColor == AimColor::blue ?'B':'R');
  71.         }
  72.     }
  73.    
  74.     int getX(){
  75.         return screen_x_middle - current_x_middle;
  76.     }
  77.    
  78.     int getY(){
  79.         return screen_y_middle - current_y_middle;
  80.     }
  81. };
  82.  
  83. const double autoSpeed = 125;
  84.  
  85. class Robot {
  86.     public:
  87.     brain *Brain;
  88.     controller *Con1;
  89.     motor *DriveL, *DriveR, *FlyWheelL, *FlyWheelR, *Feedbelt, *Kicker, *Intake;
  90.     VisionTracker *visionTracker;
  91.    
  92.     bool flywheel_running;
  93.     double flywheel_target_speed;
  94.     bool intake_running;
  95.     DrivingMode driving_mode;
  96.     AimColor aim_color;
  97.    
  98.     Robot(){
  99.         Brain = new brain();
  100.         Con1 = new controller(controllerType::primary);
  101.        
  102.         DriveL = new motor(PORT1, gearSetting::ratio18_1, false);
  103.         DriveR = new motor(PORT10, gearSetting::ratio18_1, true);
  104.         FlyWheelL = new motor(PORT4 /* 12 */, gearSetting::ratio18_1, false);
  105.         FlyWheelR = new motor(PORT6 /* 20 */ , gearSetting::ratio18_1, true);
  106.         Feedbelt = new motor(PORT5 /* 13 */, false); // Sharpie
  107.         Kicker = new motor(PORT9, true);
  108.         Intake = new motor(PORT11, false);
  109.        
  110.         DriveL->setStopping(brakeType::coast);
  111.         DriveR->setStopping(brakeType::coast);
  112.        
  113.         flywheel_running = false;
  114.         flywheel_target_speed = 70;
  115.         intake_running = false;
  116.         driving_mode = DrivingMode::speed;
  117.         aim_color = AimColor::red;
  118.        
  119.         visionTracker = new VisionTracker(&aim_color);
  120.     }
  121.    
  122.     double calc_rpm(double in){
  123.         return in / 127.0 * 200.0;
  124.     }
  125.    
  126.     void toggle_aim_color(){
  127.         aim_color = aim_color == AimColor::blue ? AimColor::red : AimColor::blue;
  128.     }
  129.    
  130.     void toggle_driving_mode(){
  131.         driving_mode = driving_mode == DrivingMode::speed ? DrivingMode::precision : DrivingMode::speed;
  132.     }
  133.    
  134.     void runDrive(double l, double r){
  135.         run(DriveL, l);
  136.         run(DriveR, r);
  137.     }
  138.    
  139.     void runDrive(double v){
  140.         runDrive(v,v);
  141.     }
  142.    
  143.     void runFly(double speed){
  144.         run(FlyWheelL, speed);
  145.         run(FlyWheelR, speed);
  146.     }
  147.    
  148.     void runBelt(double speed){
  149.         run(Feedbelt, speed);
  150.     }
  151.    
  152.     void moveKicker(bool up){
  153.         if(up){
  154.             Kicker->rotateTo(-0.3, rotationUnits::rev, 200, velocityUnits::rpm, false);
  155.         } else {
  156.             Kicker->rotateTo(0, rotationUnits::rev, 200, velocityUnits::rpm, false);
  157.         }
  158.     }
  159.    
  160.     void driveDistance(double distance_l, double distance_r, distanceUnits units, bool block){
  161.         double revolutions_multiplier = 0;
  162.        
  163.         if(units == distanceUnits::in){ // Inches
  164.             revolutions_multiplier = 1.0 / (4.0 * M_PI);
  165.         } else if(units == distanceUnits::cm){ // Centimeters
  166.             revolutions_multiplier = 1.0 / (4.0 * 2.54 * M_PI);
  167.         } else if(units == distanceUnits::mm){ // Millimeters
  168.             revolutions_multiplier = 1.0 / (4.0 * 25.4 * M_PI);
  169.         }
  170.        
  171.         DriveL->rotateFor(distance_l * revolutions_multiplier, rotationUnits::rev, autoSpeed, velocityUnits::rpm, false);
  172.         DriveR->rotateFor(distance_r * revolutions_multiplier, rotationUnits::rev, autoSpeed, velocityUnits::rpm, block);
  173.     }
  174.    
  175.     void driveDistance(double distance, distanceUnits units, bool block){
  176.         driveDistance(distance, distance, units, block);
  177.     }
  178.    
  179.     void driveAngle(double angle, bool block){
  180.         double distance_multiplier = 2 * M_PI * 5.9; // 5.9 inches is the radius of the turn circle
  181.         driveDistance(distance_multiplier * angle, -distance_multiplier * angle, distanceUnits::in, block);
  182.     }
  183.    
  184.     // drive distance rotation is 5.9 inches
  185.    
  186.     void preautonomous();
  187.     void autonomous();
  188.     void predrive();
  189.     void drive();
  190.     void feedback(long ticks);
  191. };
  192.  
  193. void Robot::preautonomous(){
  194.     Brain->Screen.setFont(fontType::mono60);
  195. }
  196.  
  197. void Robot::autonomous(){
  198.     //driveAngle(0.25, true);
  199.     /*
  200.     driveDistance(21, distanceUnits::in, true);
  201.     runFly(65);
  202.     sleep(3.0);
  203.     runBelt(-150);
  204.     sleep(1.0);
  205.     runFly(0);
  206.     runBelt(0);
  207.    
  208.     runDrive(175, 200);
  209.     sleep(0.75);
  210.     runDrive(0,0);
  211.    
  212.     driveDistance(-25, distanceUnits::in, true);
  213.    
  214.     driveAngle( -65.0 / 360.0, true);
  215.     //runDrive(100, -100);
  216.     //sleep(0.50);
  217.     //runDrive(0);
  218.    
  219.     sleep(0.1);// wait
  220.     runDrive(100);
  221.     sleep(1.0);
  222.     runDrive(0);
  223.     sleep(0.1); // wait
  224.     moveKicker(true);
  225.     sleep(0.5);
  226.     moveKicker(false);
  227.     */
  228.    
  229.    
  230.     // Other programs
  231.     /*MotorL.rotateFor(36/12.57, rotationUnits::rev, 150, velocityUnits::rpm, false);
  232.     MotorR.rotateFor(36/12.57, rotationUnits::rev, 150, velocityUnits::rpm, true);
  233.     MotorL.rotateFor(-12/12.57, rotationUnits::rev, 150, velocityUnits::rpm, false);
  234.     MotorR.rotateFor(-12/12.57, rotationUnits::rev, 150, velocityUnits::rpm, true);*/
  235.     /*run_fly(70);
  236.     sleep(5.0);
  237.     run_belt(-150);
  238.     sleep(1.0);
  239.     run_fly(0);
  240.     run_belt(0);*/
  241.    
  242.    
  243.     driveDistance(40, distanceUnits::in, true);
  244.     driveDistance(-30, distanceUnits::in, true);
  245.     driveAngle(50.0 / 360.0, true);
  246.     driveDistance(30, distanceUnits::in, true);
  247.     moveKicker(true);
  248.     sleep(0.5);
  249.     moveKicker(false);
  250.     driveDistance(-10, distanceUnits::in, true);
  251.    
  252. }
  253.  
  254. void Robot::predrive(){
  255.     // pass
  256. }
  257.  
  258. void Robot::drive(){
  259.     if(driving_mode == DrivingMode::precision){
  260.         double baseSpeed = calc_rpm(Con1->Axis3.value() / 4.0);
  261.         double bias = Con1->Axis4.value() / 4.0;
  262.         runDrive(baseSpeed + bias, baseSpeed - bias);
  263.     } else {
  264.         double left = calc_rpm(Con1->Axis3.value());
  265.         double right = calc_rpm(Con1->Axis2.value());
  266.         double sensitivity = ((left * right) < 1) ? 0.5 : 1.0;
  267.        
  268.         runDrive(left * sensitivity, right * sensitivity);
  269.     }
  270.    
  271.     moveKicker(Con1->ButtonR1.pressing());
  272.     runBelt((Con1->ButtonL1.pressing()*150) + (Con1->ButtonL2.pressing()*-150));
  273.    
  274.     if(Con1->ButtonR2.pressing()){
  275.         run(Intake, -200);
  276.     } else if(Con1->ButtonL2.pressing() || !intake_running){
  277.         run(Intake, 0);
  278.     } else {
  279.         run(Intake, 200);
  280.     }
  281.    
  282.     runFly(flywheel_running ? flywheel_target_speed : 0);
  283. }
  284.  
  285. void Robot::feedback(long ticks){
  286.     // Shows which color we are aiming for and flashes yellow if in precision driving mode.
  287.     if(driving_mode == DrivingMode::precision && ticks % 2 == 0){
  288.         Brain->Screen.clearScreen("#FF0");
  289.     } else if(aim_color == AimColor::blue){
  290.         Brain->Screen.clearScreen("#00F");
  291.     } else {
  292.         Brain->Screen.clearScreen("#F00");
  293.     }
  294.    
  295.     Con1->Screen.clearLine(2);
  296.     Con1->Screen.setCursor(2,1);
  297.     if(driving_mode == DrivingMode::precision){
  298.         Con1->Screen.print("Precision");
  299.     } else {
  300.         Con1->Screen.print("Speed");
  301.     }
  302.    
  303.     double average_speed = (FlyWheelL->velocity(velocityUnits::rpm) + FlyWheelR->velocity(velocityUnits::rpm)) / 2;
  304.     Con1->Screen.clearLine(1);
  305.     Con1->Screen.setCursor(1, 1);
  306.     Con1->Screen.print("T: %3.0f, M: %3.0f", flywheel_target_speed, average_speed); // T for target, M for measured
  307. }
  308.  
  309. Robot *robot = new Robot();          
  310.  
  311. void do_360(){
  312.     robot->runFly(70);
  313.     robot->driveAngle(0.98, true);
  314.     robot->runBelt(-150);
  315.     sleep(1.0);
  316.     robot->runBelt(0);
  317.     robot->runFly(0);
  318. }
  319.  
  320. void autonomous(){ robot->autonomous(); }
  321.  
  322. void drive(){    
  323.     // TODO: Fix issues with scope (robot pointer in lambda) [std::bind]
  324.     robot->Con1->ButtonA.pressed([]{ robot->flywheel_running = !robot->flywheel_running; });
  325.     robot->Con1->ButtonY.pressed([]{ robot->toggle_driving_mode(); });
  326.     robot->Con1->ButtonUp.pressed([]{ robot->flywheel_target_speed += 10; });
  327.     robot->Con1->ButtonLeft.pressed([]{ robot->flywheel_target_speed -= 10; });
  328.     robot->Con1->ButtonX.pressed([]{ robot->toggle_aim_color(); });
  329.     robot->Con1->ButtonRight.pressed([]{ robot->intake_running = !robot->intake_running; });
  330.  
  331.     long ticks = 0;
  332.     while (1) {  
  333.         robot->drive();
  334.        
  335.         if(robot->Con1->ButtonB.pressing()){
  336.             do_360();
  337.         }
  338.        
  339.         if(ticks % 5 == 0){
  340.             robot->feedback(ticks / 5);        
  341.             robot->visionTracker->calculate();
  342.             robot->visionTracker->display(robot->Con1, 3);
  343.         }
  344.  
  345.         sleep(1.0 / 50.0);
  346.  
  347.         ticks++;
  348.     }
  349. }
  350.  
  351. int main() {
  352.     robot->preautonomous();
  353.     robot->Brain->Screen.printAt(20,50,"Preauto");
  354.    
  355.     Competition.autonomous(autonomous);
  356.     Competition.drivercontrol(drive);
  357.  
  358.     while(1){
  359.         sleep(1);
  360.     }
  361. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement