Emergency Stop Button/Bumper

I am doing a project in my class and I am trying to code an emergency stop button does anyone know how to do that I am using Vex Coding Studios C++

Since you mention Vex Coding Studio, I assume, you have V5.

You could program a stop button functionality for one of the Controller buttons, but doing this with a bumper switch will give you more “industrial” look, especially if you put a large red “Emergency Stop” sticker on it. :slight_smile:

The following will provide you some reference on how to configure a 3-wire bumper or limit switches in V5 and then read their status, as well as how to stop the motors:

https://help.vexcodingstudio.com/#cpp/namespacevex/classvex_1_1bumper/pressing

The resulting code would look something like this:

while(true)  // this your main loop
{
  if( Bumper1.pressing() ) // Emergency Stop  
  {
     motor1.stop();
     motor2.stop();
     ...
  }
  else
  {
     motor1.spin(...);
     motor2.spin(...);
     ...
  }

  vex::task::sleep(20);
}
2 Likes

So I am using three 3-wire bumpers I want to use one as an Emergency Stop. The other two are for going up and down. Would the code be the same or different.

Well, in that case I assume you want to start and stop moving after the momentary button press of either of the buttons (and not only while the buttons are pressed).

Then the code would look like this:

int motorControl = 0;  // (0) - stop, (+1) - moveUp, (-1) - moveDown

while(true)  // this your main loop
{
   if(      buttonStop.pressing() ) motorControl = 0;
   else if( buttonDown.pressing() ) motorControl = -1;
   else if( buttonUp.pressing() )   motorControl = +1;

   if( motorControl == 0 )
   {
      motor1.stop();
   }
   else 
   { 
      motor1.spin( motorControl > 0 ? directionType::fwd  
                                   :  directionType::rev);
   }

  vex::task::sleep(20);
}

References: Ternary operation - Wikipedia
VCS API Reference

You also may want to add the limit switches at the top and the bottom of the lift to automatically stop the movement of the lift once it reaches its destination.

1 Like

Thank You it worked :ok_hand::ok_hand::ok_hand::ok_hand::ok_hand::ok_hand::ok_hand:

3 Likes

I’m curious. You specifically said “emergency stop.” Usually emergency stops are sorts of failsafes - things that shut everything down in the event something goes wrong. The code you were given and say works I would not consider an “emergency stop.” I would consider an “emergency stop” to not allow the motors to ever spin again unless reset. For example, in machine shops there are emergency kill switches on the power; if you hit them the power shuts off to the machines, and it doesn’t come back on until you manually reset it. The moment you release buttonStop here you can run the motors again. I would get rid of most of that code myself:

motor1.setVelocity(50, velocityUnits::pct);
bool motorControl = true;

while(motorControl)  // this your main loop
{
   if(      buttonStop.pressing() ) motorControl = false;
   if( buttonDown.pressing() )
      motor1.spin(directionType::fwd);
   else if buttonUp.pressing() )
      motor1.spin(directionType::rev);
   vex::task::sleep(20);
}

Or, shorter but less obvious:

motor1.setVelocity(50, velocityUnits::pct);

while(!buttonStop.pressing())  // this your main loop
{
    if( buttonDown.pressing() )
         motor1.spin(directionType::fwd);
    else if buttonUp.pressing() )
       motor1.spin(directionType::rev);
      vex::task::sleep(20);
 }
2 Likes

@callen, you make a good point and I, probably, dropped the ball with the first answer, because I was thinking mostly about getting enough links to the reference material on how to program V5 buttons to help OP.

However, given the 3 button constraint and that we don’t know what type of the mechanism OP is building and how it is supposed to be controlled, I would say the second answer could be correct.

One option could be to program the machine to go up or down only while “up” or “down” buttons are pressed. Then you wouldn’t need dedicated “stop” button for normal operation. But if you want an “Emergency Stop” button that another person could push independently, then your break out of the loop algorithm would be appropriate.

Alternatively, you could program “up” and “down” buttons to start the motor after momentary push and the “stop” button to turn motor off. That would be the type of machinery that I am used to (school workshop level) with two recessed buttons for “forward” and “reverse” that are hard to push by accident and a large red mushroom style button for “stop” that is very easy to push. Second algorithm would be appropriate for such setup.

The next level would be to use a special stop switch which you can either push or pull to break the circuit, but you would need to delicately reset it with a twist in the mid position to restore the power to the machine. I’ve seen several setups where it was used in addition to the regular three button for “fwd”, “rev”, and “stop” but didn’t have to work on such level myself.

1 Like

This is a sketch of what I am doing it is accurate to what I have built 20190426_142712.jpg - Google Drive

1 Like

If you wanted, you don’t have to use an emergency “STOP ALL” kind of function. I would just include a button that breaks whatever loop the lift is in (i.e. a return statement), and then reset all the V5 motors to zero when it is pressed.

I’m not entirely sure how that would work in code, though…

That is what I am trying to ask help on.

Ok so basically, after the “while” loop, you would have to initiate the reset function by writing code beneath it or using a void…

while(!button){
//whatever
}
//and then here you'd reverse motors until 0:

while(motorEncoderN > 0){
        //you could start a timer here too, if you need time to stop the code using the brain
        reverseMotorsAtSpeed(slow);
}
1 Like

For this type of lift is it, probably, best to include a couple of limit switches: one at the top and one at the bottom to automatically stop when the plate reaches the end positions.

It is better to reserve “Emergency Stop” to the truly abnormal situations and let automation handle normal situation.

Let say you have two limit switches: “topLimit” and “bottomLimit” that get pressed by the plate when it arrives to the end. Also, there are three bumper switches “Up”, “Down”, and “Emergency Stop”.

int motorControl = 0;  // (0) - stop, (+1) - moveUp, (-1) - moveDown

while(true)  // this your main loop
{
   if(      buttonStop.pressing() ) motorControl = 0;
   else if( buttonDown.pressing() ) motorControl = -1;
   else if(   buttonUp.pressing() ) motorControl = +1;

  if( motorControl>0 &&    topLimit.pressing() ||
      motorControl<0 && bottomLimit.pressing() ) motorControl = 0;

   if( motorControl == 0 )
   {
      motor1.stop();
   }
   else 
   { 
      motor1.spin( motorControl > 0 ? directionType::fwd  
                                    : directionType::rev);
   }

  vex::task::sleep(20);
}
2 Likes

Am I able to use multiple motors if the else statement just like in auton during a Competition because that is what I am trying to do but it is not working right.

Yes, there should be no problem using multiple motors. You just need to add a line with a call to .stop() for each motor when emergency stop button is pressed.