Skip to main content

Catapult

Catapult Loading with Distance Sensor

Consider a catapult using a slip gear arrangement for shooting. To load the cata we need to run the motor to a hold position. To shoot the cata we need to run the motor a little more to get to the slip portion of the gear so it will shoot.

this can be accomplished by means of a distance sensor that can detect the position of the catapult.

We will prepare 2 functions for this. loadCata. and shootCata. they will be implemented as function callbacks so they will run as separate tasks when the controller button is pressed.

We also need to prepare a runCata function for use in hand loading, this function will runn the cata continuously. a controller button will toggle the cata on and off.

Code

First set up the devices, for this example just a Catapult motor, distance sensor and controller. Depending on your build the Catapult motor may need to be reversed.

// VEXcode device constructors
controller Controller1 = controller(primary);
motor Cat = motor(PORT1, ratio18_1, false);
distance Distance = distance(PORT2);
Functions to use
void loadCata() {
  int speed = 100;  // set speed as a variable to make it easy to change later
  float setDistance = 20.0;  // this is the distance that the is slightly larger than what the sensor reads when the cat is in load position
  Cat.spin(forward, speed, pct);
  while (Distance.objectDistance(mm) >= setDistance) {
    wait(10, msec);
  }
  Cat.stop(brake);
}
void shootCata() {
  int speed = 100;
  float setDistance = 20.0;
  Cat.spin(forward, speed, pct);
  while (Distance.objectDistance(mm) <= setDistance) {
    wait(10, msec);
  }
  Cat.stop(brake);
}
void runCat(int speed) { 
  Cat.spin(forward, speed, pct); 
  }
Implementation in driver control
void usercontrol(void) {
  // function call backs to put cata in load position or shoot
  Controller1.ButtonR1.pressed(loadCata);
  Controller1.ButtonR2.pressed(shootCata);
  bool running = false;
  while (1) {
    // toggle to run cat for hand loading
    if (Controller1.ButtonA.pressing()) {
      if (!running) {
        runCat(100);
        running = true;
      } else {
        runCat(0);
        running = false;
      }
    }
  }
}

Slow Moving Cata

if(catDown==true && Controller1.ButtonR1.pressing()){
  CatapultL.spin(forward, 20, pct);
      CatapultR.spin(forward, 20, pct);
}
else if(catDown==true){
    CatapultL.spin(forward, 0, pct);
      CatapultR.spin(forward, 0, pct);
}