While loop problems

brucelee8162's Avatar, Join Date: Aug 2007
Light Poster
Hi there. I'm really hoping someone can help me with this strange problem. I've asked elsewhere but got no answer. The problem is this:

cr8_set_speed(speed,0);
while((self->dist-startdist)<distance)
{
printf("%f\n",self->dist);
}
cr8_set_speed(0,0);

I'm programming a iRobot Create and the code above makes the robot move forward a certain distance and then it stops. The while loop exits when the printf statement is there, but as soon as I take it away, the while loop never finishes! Whats going on? I'm thinking its some sort of weird compiler error. Any ideas? I know this while loop condition is correct because it works when the printf statement is there.
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Just have self->dist into separate variable and let us know the values of each of the following
self->dist, startdist and the distance
0
DaWei's Avatar, Join Date: Dec 2006
Team Leader
Unless self->dist-startdist or distance is a volatile variable, being changed independently, you are doing nothing to alter the while condition. If it is a volatile variable, you need to specify that so that the compiler doesn't optimize away the read.
0
brucelee8162's Avatar, Join Date: Aug 2007
Light Poster
Hi thanks for your help everyone, I added the volatile word in front of all my struct variables, which are updated by a separate thread every 15ms. The drive_straight() function is working now, and so is the turn_right() function. But for some reason the turn_left() and reverse functions, which are basically copies of the other two are still not working! Any ideas? Here are the functions:
Code:
reverse():

startdist = self->dist;
    cr8_set_speed(-speed,0);
    while((startdist-self->dist)<distance)
    {printf("%f\n",(startdist-self->dist));
    }
    cr8_set_speed(0,0);

turn_left():

int16_t endAngle = self->angle - angle;
  int16_t tempangle = 0;
  if(endAngle <= 0)
  {
    endAngle = endAngle + 360;
  }

    cr8_set_speed(0, turnspeed);

    if(self->angle < endAngle)
    {
      while(self->angle < endAngle)
      { 
      }
    }
    while(self->angle > endAngle)
    {
    }
    cr8_set_speed(0,0);

Last edited by shabbir; 17Aug2007 at 18:33.. Reason: Code block - http://www.go4expert.com/forums/misc.php?do=bbcode#code
0
DaWei's Avatar, Join Date: Dec 2006
Team Leader
What language is this? It certainly isn't standard C.

At any rate, you have a printf statement in the reverse function, what is it telling you?

And please note that Shabbir has kindly provided code tags for you. It's rude to ask for free help if you can't spare the time to read the "Before you make a query" thread, in return.
0
brucelee8162's Avatar, Join Date: Aug 2007
Light Poster
Hi. I can't find the "before you make a query" thread and I don't know what code tags are so, sorry. The code I posted is standard C, the function you see there are ones that I have written. The printf function is telling me the distance the robot has travelled. The dist variable is static and the self->dist is the robots distance travelled total. The strange thing is that the printf outputs "0.000000" when it should be incrementing. I've tried this in the forward() function and this does indeed increment. Any ideas?
0
DaWei's Avatar, Join Date: Dec 2006
Team Leader
This is not standard C notation for a function:
Quote:
reverse():

startdist = self->dist;
cr8_set_speed(-speed,0);
while((startdist-self->dist)<distance)
{printf("%f\n",(startdist-self->dist));
}
cr8_set_speed(0,0);
The only use of a ':' in C is as a label identifier. A function (such as reverse ()) supposedly is) has to have the entire function blocked. Without braces, only the first line would constitute the body of the function. The snippet shown will not, in fact, compile without error in a C compiler.

It is impossible to judge the validity of your logic without knowing about the language you are using.

If you look at the very top right corner of this page, you will see 5 links. The 3rd one is the "before" link.
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Quote:
Originally Posted by brucelee8162
Hi. I can't find the "before you make a query" thread and I don't know what code tags are so, sorry.
The link is here and the it can also be found in the top right corner of every page of the forum.
0
brucelee8162's Avatar, Join Date: Aug 2007
Light Poster
Hi. I have read the before you make a query thread. Sorry that I confused you before, the "reverse():" notation was my own just to indicate that below was PART of my reverse function. So yes, it is standard C. I can post both of my forward and reverse functions if you want. Here they are:

Code: c
//Moves robot at specified speed for user specified distance
void drive_straight(float distance)
{
  float startdist = 0;
  if(distance != 0)
  {
    startdist = self->dist;
    cr8_set_speed(speed,0);
    while((self->dist-startdist)<distance)
    {printf("%f\n",(startdist-self->dist));
    }
    cr8_set_speed(0,0);
  }
  else
  {
    //user control, move forward at set speed until user stops
    if(self->stop == 0)
      cr8_set_speed(speed,0);
  }
}

//Moves robot at specified speed for user specified distance
void reverse(float distance)
{
  float startdist = 0;
  if(distance != 0)
  {
    startdist = self->dist;
    cr8_set_speed(-speed,0);
    while((startdist-self->dist)<distance)
    {printf("%f\n",(startdist-self->dist));
    }
    cr8_set_speed(0,0);
  }
  else
  {
    //user control, move forward at set speed until user stops
    if(self->stop == 0)
      cr8_set_speed(-speed,0);
  }
}

Ok. There you go. As you can see the functions are virtually the same apart from the speed argument. The first if statement in each function with get executed as I am passing drive_straight(0.1) and reverse(0.1) to them. The else part is for user control, so do not worry about that. Any ideas guys?
0
brucelee8162's Avatar, Join Date: Aug 2007
Light Poster
anyone? please, don't hate me cos I didn't read the before you post thread