I am programming a robot, and I was wondering if there was code to cause the robot to stop whatever it was doing if a bumper was activated. Right now I am using a while loop but the combination of the slow processor, fast motors and complex functions executed while moving cause a delay that has damaged the robot when the bumpers were activated. Is there a way to get it to break out of whatever the program is doing, even in the middle of a function and jump to the bumper activation code?

Hmm ... what about a return; statement in the middle of the function? Or you could use a break; statement to break out of a loop. For example,

void f(int x)
{
	 while (true)
	 {
		 move_forward(x);
		 if (bumper)
			 return;
	 }
	 return;
}

Make a separate thread that monitors the bumper. When the bumper is activated, it calls some routine to immediately shut down the motors, or whatever.

Do you have to poll the bumper switches, or is there an interrupt of some sort? If you have to poll, you could have the thread poll every n milliseconds (whatever works best) and then call Sleep(0) while you are waiting for that number of milliseconds to pass. You could say Sleep(n), but that isn't super accurate; maybe Sleep(n - fudgeFactor) and then Sleep(0) until you are ready to poll.

How do i make a separate thread run? I have never programmed in multiple threads, let alone on a 4mhz 16 bit processor. I am kinda fudging my way along here.

I found out about pthread.h to creat threads, i do not currently have a version of it that will work with my SourceBoost IDE, nor do i have any idea how to implement it. I do appreciate any help greatly.

Well, if threads aren't supported by your IDE or the OS that is running on the robot, you're in a world of hurt. You could build your own thread management, but that is pretty advanced.

Is the problem with the current method that you aren't checking frequently enough, or that the checks themselves are CPU intensive, thus taking time away from the other work to be done?

Do you have access to a timer that can quickly return the number of 'ticks' that have elapsed? If so maybe you could say "if elapsed-ticks > a quarter of a second then <check bumpers>", and liberally sprinkle that around your code.

it is teh checks are not often enough with the current while loop configuration. You are saying sprinkle code with if statements to check bumpers, then use goto's or returns to jump to other code?

it is teh checks are not often enough with the current while loop configuration. You are saying sprinkle code with if statements to check bumpers, then use goto's or returns to jump to other code?

Personaly I would have the bumpers send a signal back to the computer directly. the cpu you are using is far superiour to the days of ZX Spectrum based robots of my day, and they were programmed in BASIC.

the problem is that (a), i can't type and I know this so sorry people. (b) I do not know how to send directly to the CPU, i have to use a read_adc() function that contains the bumpers IO port and pin. I am working with a pre-fabricated (granted it is made by my school) board and IO pin configuration. I would think for the bumper to communicate directly with the CPU i need a separate thread running tha constantly checks the status of the pin. I do not think my IDE supports multiple threads, nor do I even know how to program those threads. If you can point me in a good direction i believe i can pick up on it. But let me know. Thanks again for your help people.

The polling idea is something like:

while (BumpersOk())
{
    if (!DecideWhatToDo()) break;
    if (!BumpersOk()) break;
    if (!DoWhatYouDecidedToDo()) break;
    if (!BumpersOk()) break;
}

and so on. Since this can slow things down, BumpersOk() might check to see if enough time has elapsed since the last check.

For a discussion on threads, check out http://jan.netcomp.monash.edu.au/OS/l8_2.html

Thanks alot people. I will try otu the code and let you know the results. Once again thanks so much.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.