View unanswered posts | View active topics It is currently Fri Apr 25, 2014 5:00 am






Reply to topic  [ 7 posts ] 
Emergency cut off switch code 
Author Message
Rookie

Joined: Wed Oct 17, 2012 11:15 am
Posts: 4
Post 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!


Wed Oct 17, 2012 11:30 am
Profile
Moderator
Moderator
User avatar

Joined: Tue Sep 14, 2010 9:19 pm
Posts: 496
Post 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.

_________________
sudo rm -rf /


Wed Oct 17, 2012 11:38 am
Profile
Rookie

Joined: Wed Oct 17, 2012 11:15 am
Posts: 4
Post 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}

}


Wed Oct 17, 2012 12:37 pm
Profile
Moderator
Moderator
User avatar

Joined: Tue Sep 14, 2010 9:19 pm
Posts: 496
Post 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.

_________________
sudo rm -rf /


Wed Oct 17, 2012 1:44 pm
Profile
Rookie

Joined: Wed Oct 17, 2012 11:15 am
Posts: 4
Post Re: Emergency cut off switch code
Is the Sensor Value for the limit switch == 0 or == 1?


Thu Oct 18, 2012 8:53 am
Profile
Site Admin
Site Admin

Joined: Thu May 24, 2012 12:15 pm
Posts: 537
Post 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

_________________
Check out our Blog! And our Facebook page!
Need help? Take a look at our Wiki and our Forums.

I just met you,
And this is crazy,
But here's my code now,
So fix it, maybe?
~ Carly Rae Jepsen parody


Thu Oct 18, 2012 9:19 am
Profile
Moderator
Moderator
User avatar

Joined: Tue Sep 14, 2010 9:19 pm
Posts: 496
Post 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.

_________________
sudo rm -rf /


Thu Oct 18, 2012 10:53 am
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 7 posts ] 

Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  



Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.