I get following error when the robot is running with debugger,
pORGRAM SLOT: 0, TASK ID: MAIN[0]
ERROR AT pc: sub approach_room+0x006D
task pc: sub behave+00x0000
task state: ‘exception’
exception type: ‘stack overflow[9]’
Exceptions exists at somewhere marked with ERROR
Part of my program: (some of less important parts are cut)
task main()
{
mode = sleeping;
//waiting to be awakened
while(true)
{
setup(); //pre set the robot
while(mode == sleeping)
{
behave(1);
if(SensorValue[Confirm] == 1) //call the robot to be ready to work
{
mode = waiting;
}
}
behave(0);
wait1Msec(4000);
SensorValue[LED1] = 1;
read();
//rebuild();
wait1Msec(4000);
while(mode == waiting)
{
behave(2);
if(SensorValue[Confirm] == 1)
mode = working;
}
motor[left] = 127;
motor[right] = (-1)*st_forward;
wait1Msec(100); //calibrate , from p1 to p2
if(size_data1 > 0)//need to deliver documents to pigeon hole
{
//get to starting point of pigeon hole, need to configure time
navigate_to_box();
}
if(size_data2 > 0)//need to deliver documents to staff's room(s)
{
navigate_to_room();
}
}
return;
}
//now robot's head is pinting towards the door, cw is rotation after delivery
void approach_room(bool cw, int room_num)
{
int up = 0; //knocking arm moves upward
bool knocking = false;
bool deliver = true; // take document to receiver
/*ClearTimer(T1);
while(time1[T1] < 2400) // need to calibrate 2400
{
motor[port2] = 80;
motor[port3] = -80;
//behave();
}*/
//moving to door
while(SensorValue[head_bumper1] == 0 && SensorValue[head_bumper2] == 0)
{
motor[left] = 127;
motor[right] = (-1)*st_forward;
//behave(); //make sound
}
//door is closed
if(SensorValue[head_bumper1] == 1 || SensorValue[head_bumper2] == 1)
{
//motor[port5] = 10;
//behave();
knocking = true;
}
//door reached and keep knocking
if(knocking)
{
//keep knocking if kocking arm can't contact lower switch and timer hasn't expired (50secs)
ClearTimer(T2);
while(SensorValue[down_switch] == 0)// && time1[T2] < 50000)
{
//stop moving
motor[left] = 0;
motor[right] = 0;
//start knocking
while(SensorValue[knock_bumper] == 0)
{
motor[arm] = 100;
//StartTask(ack_noise);
if(SensorValue[down_switch] == 1)
break;
}
//StopTask(1);
behave(0); <---ERROR
//wait1Msec(4000);
//knock onto door
if(SensorValue[knock_bumper] == 1)
up = 1;
//move upward to original position
while (up == 1)
{
motor[arm] = -100;
//behave(4);
if(SensorValue[up_switch] == 1)
{
up = 0;
}
}
}
//behave(0);
if(SensorValue[down_switch] == 0)// && time1[T2] > 50000)
{
deliver = false;
}
//door is opened
if (SensorValue[down_switch] == 1)
{
up = 1;
}
//door is opened
while (up == 1)
{
motor[arm] = -100;
if(SensorValue[up_switch] == 1)
{
up = 0;
}
}
}
//someone open door
if(deliver)
{ -----same ERROR here too
communicate(0,true,room_num); //send signal to acknowledge that appropriate column of boxes or room is reached //and send numbers
communicate(1,true,room_num); //receive signal that the task is finished
communicate(2,true,room_num);
communicate(1,true,room_num);
}
//move backward
motor[left] = (-1)*st_backward;
motor[right] = 127;
wait1Msec(500); //calibrate
if(cw)
change_posture(true);
else
change_posture(false);
return;
}
void navigate_to_room()
{
bool A = false;
bool B = false;
bool C = false;
bool D = false;
int sum_room_A = 0;
int sum_room_B = 0;
int sum_room_C = 0;
int sum_room_D = 0;
//check whether robot should go to particulat area or not
for(int i = 0; i < 2; i++)
{
if(database2*)
{
A = true;
sum_room_A += 1;
}
}
for(int i = 2; i < 8; i++)
{
if(database2*)
{
B = true;
sum_room_B += 1;
}
}
for(int i = 8; i < 10; i++)
{
if(database2*)
{
C = true;
sum_room_C += 1;
}
}
for(int i = 10; i < 16; i++)
{
if(database2*)
{
D = true;
sum_room_D += 1;
}
}
if(A || B)
{
nav_AB(A, B, sum_room_A, sum_room_B);
}
if(C || D)
{
nav_CD(C, D, sum_room_C, sum_room_D);
}
if(posture == 4)
{
change_posture(true);
}
motor[left] = 127;
motor[right] = (-1)*st_forward;
wait1Msec(100);//cali
}
void nav_AB(bool A, bool B, int numA, int numB)
{
int off_dis = 0;
if (posture == South)
{
change_posture(true);
}
else if(posture == North)
{
change_posture(false);
}
//get to P4
while(SensorValue(head_bumper1) == 0 && SensorValue(head_bumper2) == 0)
{
motor[left] = 127;
motor[right] = (-1)*st_forward;
}
//move a bit backward
motor[left] = (-1)*st_backward;
motor[right] = 127;
wait1Msec(500); //calibrate
if(A)
{
change_posture(true);
int distanceA] = {0,0}; //calibrate, get data by actual pre-test
int i = 0;
while(numA != 0)
{
if(database2*)
{
motor[left] = 127;
motor[right] = -127;
wait1Msec(distanceA* - off_dis);
off_dis = distanceA*;
change_posture(false);
approach_room(true,i);
numA--;
}
i++;
}
//move back to P4
change_posture(false);
change_posture(false);
motor[left] = 127;
motor[right] = (-1)*st_forward;
wait1Msec(off_dis);
off_dis = 0;
}
if(B)
{
if(posture == West)
{
change_posture(false);
}
int distanceB] = {0,0,0,0,0,0};
int i = 2;
while(numB != 0)
{
if(database2*)
{
motor[left] = 127;
motor[right] = -127;
wait1Msec(distanceB* - off_dis);
off_dis = distanceB*;
change_posture(true);
approach_room(false,i);
numB--;
}
i++;
}
//move back to P4
change_posture(false);
change_posture(false);
motor[left] = 127;
motor[right] = (-1)*st_forward;
wait1Msec(off_dis);
off_dis = 0;
}
if(posture == South)
{
change_posture(false);
}
else if(posture == North)
{
change_posture(true);
}
//move back to P2
while(SensorValue(head_bumper1) == 0 && SensorValue(head_bumper2) == 0)
{
motor[left] = 127;
motor[right] = (-1)*st_forward;
}
//move a bit backward
motor[left] = 127;
motor[right] = (-1)*st_backward;
wait1Msec(500); //calibrate
//point to east at p2 at last
}
void nav_CD(bool C, bool D, int numC, int numD)
{
}
void communicate(int status, bool room, int num)
{
//send signal for arrival of column or room
if(status == 0)
{
SensorValue[Sent] = 1;
wait1Msec(400);
}
else if(status == 1) //receive signal for completion of a task
{
/*while(SensorValue[Received] == 0)
{
//behave();
}*/
//behave(3); //!!!!!!!!!!!
//wait1Msec(3000);
}
else if(status == 2) // send signal as well as data of room no, box no
{
if(room)
{
SensorValue[Sent] = 1;
wait1Msec(300);
SensorValue[Sent] = 0;
wait1Msec(300);
if(num < 8)
SensorValue[Data1] = 0;
if(num > 7 && num < 16)
SensorValue[Data1] = 1;
SensorValue[Sent] = 1;
wait1Msec(300);
SensorValue[Sent] = 0;
wait1Msec(300);
if(num < 8)
{
SensorValue[Data1] = num%2;
SensorValue[Data2] = (num/2)%2;
SensorValue[Data3] = num/4;
}
else if(num > 7 && num < 16)
{
SensorValue[Data1] = (num-8)%2;
SensorValue[Data2] = ((num-8)/2)%2;
SensorValue[Data3] = (num-8)/4;
}
SensorValue[Sent] = 1;
wait1Msec(200);
SensorValue[Data1] = 0;
SensorValue[Data2] = 0;
SensorValue[Data3] = 0;
}
else //box
{
SensorValue[Sent] = 1;
wait1Msec(300);
SensorValue[Sent] = 0;
wait1Msec(300);
//column number
SensorValue[Data1] = (num+1)%2;
SensorValue[Data2] = ((num+1)/2)%2;
SensorValue[Data3] = (num+1)/4;
SensorValue[Sent] = 1;
wait1Msec(300);
SensorValue[Sent] = 0;
wait1Msec(300);
for(int i = 1; i < 7; i++)
{
if(database1[num*7 + i])
{
SensorValue[Sent] = 1;
wait1Msec(300);
SensorValue[Data1] = i%2;
SensorValue[Data2] = (i/2)%2;
SensorValue[Data3] = i/4;
SensorValue[Sent] = 0;
wait1Msec(300);
}
}
SensorValue[Sent] = 1;
wait1Msec(500);
SensorValue[Data1] = 0;
SensorValue[Data2] = 0;
SensorValue[Data3] = 0;
}
}
SensorValue[Sent] = 0;
return;
}
//how the robot behaves in response to different conditions
void behave(int style)
{
//tail, LEDs, sound
if(style == 0)
{
motor[tail] = 0;
}
if(style == 1)
{
motor[tail] = 50;
wait1Msec(200);
motor[tail] = -50;
wait1Msec(400);
motor[tail] = 50;
wait1Msec(200);
sound1();
}
else if(style == 2)
{
SensorValue[LED1] = 1;
wait1Msec(200);
SensorValue[LED1] = 0;
wait1Msec(200);
sound2();
}
else if(style == 3)
{
SensorValue[LED1] = 1;
SensorValue[LED2] = 1;
wait1Msec(200);
SensorValue[LED1] = 0;
SensorValue[LED2] = 0;
wait1Msec(200);
sound3();
}
else if(style == 4)
{
sound4();
motor[tail] = -80;
wait1Msec(400);
}
else if(style == 5)
{
}
else if(style == 6)
{
}
else if(style == 7)
{
}
}