ROBOTC.net forums
http://www.robotc.net/forums/

Emergency cut off switch code
http://www.robotc.net/forums/viewtopic.php?f=11&t=4995
Page 1 of 1

Author:  OldManScoots [ Wed Oct 17, 2012 11:30 am ]
Post subject:  Emergency cut off switch code

We are working on a conveyor robot project. We programmed the conveyor to start motors when we press the bump switch. The motors stayed on until the Shaft Encoder reached "4950." This part worked! Now, we want to put an "Emergency cut-off" limit switch on it so when it gets touched, the motors stop. When the limit switch was pressed (indicated "1" in Debug Window), nothing happened!

Author:  magicode [ Wed Oct 17, 2012 11:38 am ]
Post subject:  Re: Emergency cut off switch code

Have you already tried to program this? If you are having problems with the code, please post it here, and maybe we can help. There are several ways to do this, but without looking at your code, it is very hard to help you.

Author:  OldManScoots [ Wed Oct 17, 2012 12:37 pm ]
Post subject:  Re: Emergency cut off switch code

task main()
{
while (SensorValue(limitSwitch) == 1)
stopMotor(mainMotor);

{


untilBump(bumpSwitch, 2);
startMotor(mainMotor, 45);
if (SensorValue(limitSwitch) == 1)
{
stopMotor(mainMotor);
}
untilEncoderCounts(4950, encoder);
stopMotor(mainMotor);
untilBump(bumpSwitch,2);
startMotor(mainMotor, -45);
untilEncoderCounts(-5000, encoder);
stopMotor(mainMotor);

} //Program begins, insert code within curly brace}

}

Author:  magicode [ Wed Oct 17, 2012 1:44 pm ]
Post subject:  Re: Emergency cut off switch code

I'm not too familiar with natural language, but after looking at the wiki, there doesn't seem to be a good way to do this with natural language functions (someone correct me if I'm wrong). The reason that the code you have now doesn't result in an e-stop is because you only check the state of the limit switch twice. IN the first while loop, if the switch is not pressed at the start of the program, it skips the loop. If it is pressed, it just turns off the motor while it is pressed, and then proceeds with the rest of the code. Even if the switch is pressed at the second time you poll it, your code does not check the state of the switch at any other point in the program, so it will not function as an e-stop.

Since you have multiple instructions being executed one after another, not in a while loop, a simple way to do this would be multitasking. Place this:

Code:
task eStop(){
  while(true){
    if(SensorValue(limitSwitch)){
      stopMotor(mainMotor);
      StopAllTasks(); //if you want to end the program
    }
    wait1MSec(50); // give time to other tasks
  }
}

right above the main task, and put this:
Code:
StartTask(eStop);

at the start of the main task.

Author:  OldManScoots [ Thu Oct 18, 2012 8:53 am ]
Post subject:  Re: Emergency cut off switch code

Is the Sensor Value for the limit switch == 0 or == 1?

Author:  JohnWatson [ Thu Oct 18, 2012 9:19 am ]
Post subject:  Re: Emergency cut off switch code

The sensor value for a switch depends on if it is open or closed. When it is open (not pressed) it returns a value of 0; when it is closed (pressed) it returns a value of 1. This is because the switch will physically close and complete a circuit that creates a logic high, or 1, value.

This is one instance where multitasking would be a good coding candidate. You have to be careful on when you use multitasking, though; although it is a very powerful tool in ROBOTC it is very easily abused if not implemented properly. To help with this, we have a guide one when to use multitasking you may want to look at: viewtopic.php?f=1&t=3342

Also, wikipedia has a very nice article that covers the basics of multitasking: http://en.wikipedia.org/wiki/Multitasking

Author:  magicode [ Thu Oct 18, 2012 10:53 am ]
Post subject:  Re: Emergency cut off switch code

OldManScoots wrote:
Is the Sensor Value for the limit switch == 0 or == 1?
Since, as John said, the switch returns 0 or 1 depending on whether it is open or closed, you don't need the == 1 or == 0. A value of 1 is 'True' and a value of 0 is 'False' so when you say "if(SensorValue(limitSwitch))", the code inside the 'if' will execute if the switch is pressed, and not execute if the switch is not pressed. You can use == 1 and == 0 if it makes things clearer for you, but it is not needed.

Page 1 of 1 All times are UTC - 5 hours [ DST ]
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/