View unanswered posts | View active topics It is currently Thu Oct 23, 2014 4:34 pm






Reply to topic  [ 3 posts ] 
problem with wait time 
Author Message
Rookie

Joined: Sun Mar 18, 2007 6:06 pm
Posts: 2
Post problem with wait time
I am trying to make a robot which handles a complex living-room-floor-type environment intelligently. It's my first program in robotc and my first program of any kind in many years (not that I was every more than a rank beginner, as you can see from the clunky code).

The program works well when I step through it, but gets tied up when it's actually running. I am pretty sure this is because I don't have sufficient wait times after certain motor actions. However, I am getting an error when I try to compile after adding the necessary wait time. It's quite weird. Am I doing something stupid wrong? or is there a limit on the amount of nesting, or wait times used, or something similar? or is it a weird bug? grateful for any help. Here is the code

Code:
const tSensors sonarSensor          = (tSensors) S1;   //sensorSONAR9V      //*!!!!*//
//*!!CLICK to edit 'wizard' created sensor & motor configuration.                !!*//
//this is an attempt to create a robot that navigates our living room intelligently
//the robot has two rear motors (A and B) and a front wheel (C) that turns, it's basically a tricyle.
//the sonar is on the same axis as the front wheel so "turning left" and "looking left" are the same physical action
//i should probably synch the motors.  I also want to add a stall detection thing, but i need to solve the wait time problem
//before i do anything more complicated
//the program depends on starting the front wheel/sonar facing forward.

int look_left;
int look_ahead; //not using this now
int look_right;
int rotation_speed = -20;
int turn_power = 30;
int default_power = 75;
int decision;
int rotation_angle = 45;
int rotation_direction;
int wait_time = 700;
int stop_distance = 20;
int bailout_distance = 50;
int backup_rotations = 2*360;
task main()
{
while(true)
{
motor[motorA] = -default_power;
motor[motorB] = -default_power;
if(SensorValue(sonarSensor) < stop_distance)
   {
   motor[motorA]=0;
   motor[motorB]=0;
   nMotorEncoderTarget[motorC] = rotation_angle;
   motor(motorC) = rotation_speed;
   wait1Msec(wait_time);
   look_left = SensorRaw(sonarSensor);
   nMotorEncoderTarget[motorC] = rotation_angle;
   motor(motorC) = -rotation_speed;
   wait1Msec(wait_time);
   look_ahead = SensorRaw(sonarSensor);
   nMotorEncoderTarget[motorC] = rotation_angle;
   motor(motorC) = -rotation_speed;
   wait1Msec(wait_time);
   look_right = SensorRaw(sonarSensor);
   nMotorEncoderTarget[motorC] = rotation_angle;
   motor(motorC) = rotation_speed;
   wait1Msec[wait_time];//this is the one causing problems.
   //The error message is "**Error**:Procedure 'wait1Msec' is invalid syntax at this point"
  if(look_left>look_right)
   {
      decision=look_left;
      rotation_direction = 1;
   }
   else
   {
      decision = look_right;
      rotation_direction = -1;
   }
   if(decision > bailout_distance)
   {
      nMotorEncoderTarget[motorC] = rotation_angle;
      motor(motorC)=rotation_speed*rotation_direction;
      wait1Msec(wait_time*2);
      motor[motorA] = -turn_power;
    motor[motorB] = -turn_power;
    wait1Msec(2000);
    motor[motorA] = 0;
    motor[motorB] = 0;
    nMotorEncoderTarget[motorC] = rotation_angle;
    motor(motorC)=-rotation_speed*rotation_direction;
    wait1Msec(wait_time);
   }
   else //this is a bailout routine for if it gets stuck in a corner.  The random variable tries to make sure it doesn't get stuck
   //in a loop
   {
   int chance = random(2) - 1;
   nMotorEncoderTarget[motorA] = backup_rotations;
   nMotorEncoderTarget[motorB] = backup_rotations;
   nMotorEncoderTarget[motorC] = rotation_angle;
   motor(motorC)=rotation_speed*chance;
   motor[motorA]=default_power;
   motor[motorB]=default_power;
   nMotorEncoderTarget[motorC] = rotation_angle;
   motor(motorC)=-rotation_speed*chance;

   }
 }
}
}

Also while I'm at it, a couple of really basic questions:

If I use a while loop, and at some point the condition is not fulfilled, so the code leaves the while loop, and then at some future point the condition is fulfilled again, will the while loop "kick in" automatically or once you fail to meet the condition once, you only go back into the while loop if the code returns to the while condition statement? A specific example may clarify: I run both motors forward in a while loop as long as there is no obstacle closer than 20 cm. If there is a close obstacle, i back up, indefinitely. Clearly the while condition will be satisfied again. Is it monitored during the backup process or only if the code ends and you return to the begining where the while loop is stated?

Second question: obviously there is a lot of unnecessary repetitive stuff in the code above that could be simplified with either void or #define. Looking at the thread about simplifying into functions seems to suggest to have a list of voids in a functions.c or whatever, which makes sense. But in that case when do you use #define? Is there a good primer for those sort of basic programing questions?

Thanks again.


Wed Mar 21, 2007 9:54 pm
Profile
Rookie

Joined: Thu Mar 01, 2007 12:49 pm
Posts: 11
Post 
Quote:
wait1Msec[wait_time];//this is the one causing problems.

try:
wait1Msec(wait_time);

Quote:
f I use a while loop, and at some point the condition is not fulfilled, so the code leaves the while loop, and then at some future point the condition is fulfilled again, will the while loop "kick in" automatically

I'm not sure I understood the question.
If you want to come back inside a while loop also after you went out from it, you should include this while inside another while

while(true){
....
while(SensorValue(sonarSensor) > stop_distance){
....
}
//this lines will be executed only if sonarSensor < stop_distance
}
//now the program will come back to the firs while
}


Quote:
Second question: obviously there is a lot of unnecessary repetitive stuff in the code above that could be simplified with either void or #define.

yes, there are. I would suggest you to try, first of all, to make functions INSIDE the program - then you should try to implement tasks - at this point, try to make you C function to call at the beginning of any program. There are good samples for all of these in RobotC.
Furthermore, I think you should try to implement the PID control for motors speed. The robot will run really better after. There are also better ways to use MotorEncoderTarget[] . Try to look in this forum, there are lot of examples.

Anyway, enjoy your NXT and let us know if now it is working!


Thu Mar 22, 2007 5:19 am
Profile
Rookie

Joined: Sun Mar 18, 2007 6:06 pm
Posts: 2
Post 
duh! thanks on the wait time I knew it was something stupid. And thanks for the while answer...I guess I was trying to ask, is the while look some sort of continuous monitoring even after the first time the condition is not fulfilled, I think your answer means that it's not. I guess if you want to do monitoring it's through a separate task running concurrently. Lots to learn still, but having fun. Figuring out the PID is definitely right up there on the list. Thanks!


Thu Mar 22, 2007 6:56 am
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 3 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.