Help with bumper in a function

Hi guys,

Helping my 7th graders out, we’re trying to get a bump switch to work with their drive train, but I cant seem to figure it out:

void BumpB(directionType direction){

if(Bumper.pressing()){
robotDrive.stop(brake);
}
else{
robotDrive.drive(direction);
}

This was my first attempt, bu it sent back a memory error

void BumpB(directionType direction){

if(Bumper.pressing()){
robotDrive.stop(brake);
}
else{
robotDrive.drive(forward);
}

This was my 2nd one but it wont stop the drive train, it just keeps running, not sure how else to do it.

Sorry for the newb questions.

We’re trying to make it so when the bumper hits a mobile goal, it stops and continues on with the code so their clamp gets it. Right now not sure how to make the code stop.

void BumpB(directionType direction){ while(1){ if(Bumper.pressing()){ robotDrive.stop(brake); } else{ robotDrive.drive(forward); } }

I tried this too but it just kept going or gave me a error when I changed the else robotdrive from Forward to direction

Need some more context, where does “BumpB” get called from ? perhaps post all the code inside code tags (use 3 backticks (```) on their own line above and below your code. Be sure there are no spaces on the lines with the backticks, or this will not work.)

1 Like

Heres the whole code:

/*----------------------------------------------------------------------------*/
/*                                                                            */
/*    Module:       main.cpp                                                  */
/*    Author:       VEX                                                       */
/*    Created:      Thu Sep 26 2019                                           */
/*    Description:  Competition Template                                      */
/*                                                                            */
/*----------------------------------------------------------------------------*/

// ---- START VEXCODE CONFIGURED DEVICES ----
// Robot Configuration:
// [Name]               [Type]        [Port(s)]
// Frontleft            motor         1               
// leftarm              motor         3               
// Controller1          controller                    
// Inertial21           inertial      21              
// rightarm             motor         8               
// Backleft             motor         2               
// Backright            motor         9               
// Frontright           motor         10              
// Piston               digital_out   A               
// ClampArm             motor         4               
// Bumper               bumper        B               
// ---- END VEXCODE CONFIGURED DEVICES ----

#include "vex.h"

using namespace vex;

// A global instance of competition
competition Competition;

motor_group LeftSide(Frontleft, Backleft);
motor_group RightSide(Frontright, Backright);
motor_group arms(rightarm, leftarm);
smartdrive robotDrive(LeftSide, RightSide, Inertial21, 12.56, 16, 16, distanceUnits::in);
digital_out piston = Piston;

void DrivinG(int distance, int speed, directionType direction){
  robotDrive.driveFor(direction, distance, distanceUnits::in, speed, velocityUnits::pct);
}

void Arm(int degrees, int speed){
  arms.rotateFor(degrees, rotationUnits::deg, speed, velocityUnits::pct);
}

void pn(int value){
  piston.set(!piston.value());

}

void turning(int angle, int speed, turnType turn){
  robotDrive.turnFor(turn, angle, deg);
}

void Clamp(int degrees, int speed){
  ClampArm.rotateFor(degrees, rotationUnits::deg, speed, velocityUnits::pct);
}

void BumpB(int speed, directionType direction){
  //if bumper is not being pressed, hence the !
while(!Bumper.pressing()){
  robotDrive.driveFor(direction, speed, velocityUnits::pct);
}
  robotDrive.stop(brake);
}


int armsSpeed = 90;
int leftSpeed = 90;
int rightSpeed = 90;
int clampspeed = 100;
// define your global instances of motors and other devices here

/*---------------------------------------------------------------------------*/
/*                          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 V5 has been powered on and        */
/*  not every time that the robot is disabled.                               */
/*---------------------------------------------------------------------------*/

void pre_auton(void) {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();
  robotDrive.setTurnConstant(2.5);
  robotDrive.setTurnThreshold(2.0);
  Inertial21.calibrate();
  robotDrive.setDriveVelocity(100, pct);
  robotDrive.setTurnVelocity(100, pct);
  robotDrive.setStopping(coast);
  arms.setStopping(hold);
  arms.resetRotation();
  ClampArm.resetRotation();
}
  
  // All activities that occur before the competition starts
  // Example: clearing encoders, setting servo positions, ...
/*---------------------------------------------------------------------------*/
/*                                                                           */
/*                              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.   
*/
/*---------------------------------------------------------------------------*/

void autonomous(void) {
Arm(65, 80);
BumpB(100);
pn(true);
DrivinG(20, 60, reverse);
wait(2, seconds);
turning(90, 53, left);
DrivinG(60, 65, fwd);
wait(2, seconds);
turning(45, 63, right);
DrivinG(35, 65, fwd);
pn(false);
DrivinG(10, 60, reverse);
wait(2, seconds);
turning(90, 53, left);
DrivinG(20, 60, fwd);
pn(true);
wait(2, seconds);
turning(90, 53, left);
DrivinG(40, 60, fwd);
wait(2, seconds);
turning(90, 53, left);
DrivinG(50, 60, fwd);
Arm(95, 80);
wait(2, seconds);
turning(90, 63, right);
DrivinG(10, 60, fwd);
pn(false);

  // ..........................................................................
}

/*---------------------------------------------------------------------------*/
/*                                                                           */
/*                              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) {

 
  
  // User control code here, inside the loop
  while (1) {
    
    // This is the main execution loop for the user control program.
    // Each time through the loop your program should update motor + servo
    // values based on feedback from the joysticks.
    LeftSide.spin(vex::directionType::fwd, (Controller1 .Axis1.value()) + Controller1.Axis3.value(), vex::velocityUnits::pct);
    RightSide.spin(vex::directionType::fwd, (-Controller1 .Axis1.value()) + Controller1.Axis3.value(), vex::velocityUnits::pct);
    // ........................................................................
    // Insert user code here. This is where you use the joystick values to
    // update your motors, etc.
    // ........................................................................
    if(Controller1.ButtonR1.pressing()){
         arms.spin(vex::directionType::fwd, armsSpeed, vex::velocityUnits::pct);
    }
    else if(Controller1.ButtonR2.pressing()){
    
   arms.spin(vex::directionType::rev, armsSpeed, vex::velocityUnits::pct);
    }
    else{
         arms.stop(hold);
    }
    if(Controller1.ButtonUp.pressing()){
         ClampArm.spin(vex::directionType::fwd, armsSpeed, vex::velocityUnits::pct);
    }
    else if(Controller1.ButtonDown.pressing()){
    
   ClampArm.spin(vex::directionType::rev, armsSpeed, vex::velocityUnits::pct);
    }
    else{
         ClampArm.stop(hold);
    }

    if (Controller1.ButtonL1.pressing()){

      piston.set(true);
    }
    else {
      piston.set(false);
    }
    wait(20, msec); // Sleep the task for a short amount of time to
                    // prevent wasted resources.
  }
}

//
// Main will set up the competition functions and callbacks.
//
int main() {
  // Set up callbacks for autonomous and driver control periods.
  Competition.autonomous(autonomous);
  Competition.drivercontrol(usercontrol);

  // Run the pre-autonomous function.
  pre_auton();

  // Prevent main from exiting with an infinite loop.
  while (true) {
    wait(100, msec);
  }
}

edit: code tags fixed by moderators

robotDrive.driveFor will block and needs a distance to drive for.

but something like this should work.

void BumpB(int speed, directionType direction){
  //if bumper is not being pressed, hence the !
  while(!Bumper.pressing()){
    robotDrive.drive(direction, speed, velocityUnits::pct);
    this_thread::sleep_for(20);
  }
  
  robotDrive.stop(brake);
}
3 Likes

Thank you for your help, now I just need to learn how to do something with distance and optical sensors haha.

Thanks again.