eStop help

Can someone tell me if I used “estop” Correctly and if the program will run.

#pragma config(Sensor, in1, estopsensor, sensorReflection)
#pragma config(Sensor, dgtl1, bumpSwitchstart, sensorTouch)
#pragma config(Sensor, dgtl2, bumpSwitchstop, sensorTouch)
#pragma config(Sensor, dgtl3, limitSwitchdrop, sensorTouch)
#pragma config(Sensor, dgtl4, limitSwitchup, sensorTouch)
#pragma config(Motor, port1, craneMotor, tmotorVex393_HBridge, openLoop)
#pragma config(Motor, port9, craneServo, tmotorServoStandard, openLoop)
//!!Code automatically generated by ROBOTC configuration wizard !!//

task estop()
{
	while(true)
	{
		if(SensorValue(estopsensor) <500)
		{
			stopAllTasks();
		}
		wait1Msec(10);
	}
}

task main()
{
	startTask(estop);
	while(true)
	{
		if(SensorValue(bumpSwitchstart)==1)
		{
			setServo(craneServo, 90);
		}
		if(SensorValue(bumpSwitchstop)==1)
		{
			setServo(craneServo, -90);
		}
		if(SensorValue(limitSwitchdrop)==1)
		{
			startMotor(craneMotor, 1);
			wait(18);
			stopMotor(craneMotor);
		}
		if(SensorValue(limitSwitchup)==1)
		{
			startMotor(craneMotor, -1);
			wait(18);
			stopMotor(craneMotor);
		}
	}
}

This looks like something that can be in your main function. You really shouldn’t be using tasks for something this simple.

thats how i was taught by my teacher

You should always avoid using multitasking when possible. Instead of

why not

void estop() {
     if (SensorValue[estopsensor] < 500) {
          stopAllTasks();
     }
}

task main() {
     while (true) {
          estop();
     }
}
1 Like

Here’s the thing, he hasnt taught us all this yet. He kinda gave us the problem and told us to figure it out.

Problem 4: Crane and Cable Winding Mechanism

A construction contractor needs your team to design a crane that can rotate 90 degrees and lower a basket 18 inches for supplies to be moved around the job site. The crane should be run with a series of buttons and switches that can activate the rotation, lowering, and raising of the basket. The device must also be able to be started and stopped (emergency) by using a switch.

So I don’t really know what you mean by estop but I think just what you had in main was good but what I don’t get is why can’t you just put the stop within the while loop. like for example (I don’t know what the estop sensor is but lets just pretend its a button) all you have to do is just say if the value == 1 or <500 I guess and then break; that should end your while loop so

if(SensorValue(estopsensor) < 500)
{
     break;
}

I have to disagree. An emergency stop must be able to activate instantly at any point, even and especially during the operation of other tasks. An E-Stop button MUST be in a separate task.

1 Like

Your point about an emergency stop’s requirements does not correlate with your point about it being in a separate task.

Tasks are not threads!
Tasks do not run in the background while you have other code executing. Lower-priority tasks only run during higher-priority tasks’ waits/delays.

Which means, even if you use a task, it would be effectively no different from doing what @WChen20 suggested.


Side note: This is why I agree with @WChen20 that multitasking should be avoided when possible. Most people don’t understand how multitasking actually works, resulting in an overuse of tasks and code that I would generally consider lazy.

You are making the assumption that they are incapable of using threads, priority, and waits correctly.

But in general, an E-Stop should not be a Task or a Function, it should be a completely independent electrical system. That is unfortunately not an option in VEX. I would love to see V6 include some safety equipment. Deadman switch on the controller, e-stops, a separate safety circuit. Its not necessary for robots with such low power, but it would be a good introduction to industrial equipment.

By the point you have set up multitasking properly, you might as well have just not used tasks at all.

Though yes, if VEX allowed for a discrete system (or even true multithreading), I would agree with that approach.

So I do or don’t have to make any changes.

You should take the information given to you, both here, from your teacher, and potentially from other sources, and formulate your own educated decision.

There is not one single right answer; you have to choose what you are comfortable with and what fits your use case best. And asking others to simply give you an answer is not going to help you learn.

1 Like

yea, but hardly anyone ever changes task priority in ROBOTC, so even if a task does not sleep or yield the ROBOTC VM will allow other tasks to run if it’s been started with default priority.

3 Likes

what does that mean?

All it means is that your approach to using eStop is perfectly valid. The eStop task will stop the main task and all motors when you detect whatever analog sensor you are using going below the threshold you have set. Using a check for that sensor in the main loop would be ok, but as soon as the program becomes more complex and you possibly have other tests and while loops, it will not work so well, that’s why putting it in another task is really the correct approach. You can read about ROBOTC tasks in detail here (it gets complicated)

and you can see an old implementation of eStop in this project

Open source robot

https://github.com/jpearman/OSR_1/blob/master/OpenSourceBot_simple.c

that one was a bit verbose, the eStop task did not want to stop itself and there is no easy way to do that in ROBOTC except for testing each know task as I did there.

2 Likes

thanks for the help everyone, I figured it out and my engineering teacher was very amazed at what i put together for my team considering the level that he is teaching us at.

Thanks again!

2 Likes