Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   While loop problems (http://www.go4expert.com/forums/while-loop-problems-t5905/)

brucelee8162 17Aug2007 13:38

While loop problems
 
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.

shabbir 17Aug2007 14:20

Re: While loop problems
 
Just have self->dist into separate variable and let us know the values of each of the following
self->dist, startdist and the distance

DaWei 17Aug2007 15:17

Re: While loop problems
 
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.

brucelee8162 17Aug2007 17:00

Re: While loop problems
 
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);


DaWei 17Aug2007 19:25

Re: While loop problems
 
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.

brucelee8162 17Aug2007 20:40

Re: While loop problems
 
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?

DaWei 18Aug2007 00:31

Re: While loop problems
 
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.

shabbir 18Aug2007 07:33

Re: While loop problems
 
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.

brucelee8162 18Aug2007 22:49

Re: While loop problems
 
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?

brucelee8162 20Aug2007 17:05

Re: While loop problems
 
anyone? please, don't hate me cos I didn't read the before you post thread :(


All times are GMT +5.5. The time now is 19:14.