# Behavior Based Robotic

I was reading this book which talks about behavior base robotics. Since it uses IC for programming at some point it uses a function called startprocess(). After looking through the web I could not find how this function could translate in plain C. Or how to implement a simple two behavior in C

So here is two simple behavior, with it's arbitration system. I actually understand very well how this can work. The on thing that does not make sense is how would I start the behavior in C

//Cruise Behavior
int cru_trans = 0; //Translational rotation velocity
int cru_rot = 0; //Rotational velocity
int cru_act = 0; //Cruise active flag
int cru_def_vel = 100; //Default speed

void cruise(void){
while(1){
cru_act =1; //Cruise Always wants control
cru_trans = cru_def_vel; //Standard Velocity
cru_rot = 0; //Don't rotate
}
}

int av_trans = 0; //avoid translational velocity
int av_rot = 0; // avoid rotational velocity
int av_act = 0; // avoid active flag
int av_def_trans = 100; // avoid default translational velocity
int av_def_rot = 50; //avoid default rotational velocity

void avoid(void){
int ir_hit = 0; //Local variable for obstacle detection

while(1){
ir_hit = ir_detect(); //ir_detect returns which ir sensor sees something
if(ir_hit == 0){ //No detection
av_act = 0;
}
else if(ir_hit == 3){ //Detection Both side
av_act = 1;
av_rot = av_def_rot; //Arbitrary rotate left
av_trans = 0;
}
else{
av_act = 1; //Avoid wants control
av_trans = 0; //Don't move forward
av_rot = av_def_rot; //Rotate away
}
}
}

int mot_trans = 0;
int mot_rot = 0;

void motors(){
while(1){
drive(mot_trans,mot_rot);
}

void arbitrate(){
while(1){
if(av_act){
mot_trans = av_trans;
mot_rot = av_rot;
}
else if(cru_act){
mot_trans = cru_trans;
mot_rot = cru_rot;
}
else{
mot_trans = 0;
mot_rot = 0;
}
}
}

void main(){
start_process(avoid());
start_process(cruise());
start_process(motors());
start_process(arbitrate());
}

• MCUs start wherever their reset vector points. – Ignacio Vazquez-Abrams Oct 10 '13 at 22:40
• Unless you expect all of us to buy the book, you should explain what this function does and why you are having difficulty coding it. Your last question, about implementing "a simple two behavior in C" makes no sense to me. – Joe Hass Oct 10 '13 at 22:56
• @JoeHass, I added code and tried to clarify my question – Hawk_08 Oct 10 '13 at 23:32
• Sounds like threads to me. – Ignacio Vazquez-Abrams Oct 10 '13 at 23:34
• With the code this does not look like it needs to be closed. – Kortuk Oct 11 '13 at 3:45

The function start_process isn't part of any standard C library that I can think of. I think it's just being used to illustrate the creation of a thread which varies between Linux, Windows etc. On an embedded microcontroller it's normally better to use a state machine and/or interrupts to ensure code is excecuted on a timely basis.

In this case I'd remove the while (1) from inside each function so they each perform a single iteration and then move the looping to the main function like this:

void main() {
ir_hit = 0;
while (1) {
avoid();
cruise();
motors();
arbitrate();
}
}


Note that doing this means each function will be called from the start on each interation so I've moved the initialization ofir_hit to the start of main so that it only gets performed once and it would need to be changed to a global variable.

Also be careful when using this method of any function calls that will block execution for an extended time. For example if drive had some sort of long delay while driving the motor it might require using a timer to keep track of when the motor should be switched on and off. In general this looks like a pseudocode example that will require quite a bit of extra work to actually implement.

• I know start_process is part the interactive C (IC) library. And I quite agree with you on being careful with code that could go in an infinite loop. That was the point of my question ton figure out a way to manage all the infinite loops – Hawk_08 Oct 11 '13 at 3:23
• @Hawk_08 They are using some form of RTOS most likely. This is like an OS on your IC, it is more then like, it is an OS on your IC. with start process you are telling it that you have 4 separate programs to run, it will, just like on your computer, let those 4 functions share time by switching between them. At the simplest it is just using a timer interrupt and every time it goes off going to the next function, at the most complex it can be a very intelligent OS that is what devices like the Mars lander use. – Kortuk Oct 11 '13 at 3:47

The IC library you are using supports multitasking.

The start_process() function creates a task. Tasks are independent paths of execution which don't return (in this case the functions with the while(1) loops). A round-robin scheduler runs tasks for a default of 5 "ticks" each before switching to the next one. A single tick is the time it takes (default 1 ms) for a counter (controlled by the library) to produce an interrupt.

With a round-robin scheduler all the tasks run for an equal amount of time. This is in contrast to a normal superloop implementation in which the tasks run for as long as it takes them to. For example if your robot's decision making process takes 1 second, the rest of the tasks (for example movement) will have to wait that long, whereas with this multitasking setup it will take a multiple of 5 ticks, i.e. 5ms.

There are ways to create, destroy and do various other things to the created tasks. You should read the multitasking documentation found here.