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.
|
Go4Expert Founder
|
![]() |
| 17Aug2007,14:20 | #2 |
|
Just have self->dist into separate variable and let us know the values of each of the following
self->dist, startdist and the distance |
|
Team Leader
|
![]() |
| 17Aug2007,15:17 | #3 |
|
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.
|
|
Light Poster
|
|
| 17Aug2007,17:00 | #4 |
|
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 |
|
Team Leader
|
![]() |
| 17Aug2007,19:25 | #5 |
|
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. |
|
Light Poster
|
|
| 17Aug2007,20:40 | #6 |
|
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?
|
|
Team Leader
|
![]() |
| 18Aug2007,00:31 | #7 |
|
This is not standard C notation for a function:
Quote:
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. |
|
Go4Expert Founder
|
![]() |
| 18Aug2007,07:33 | #8 |
|
Quote:
Originally Posted by brucelee8162 |
|
Light Poster
|
|
| 18Aug2007,22:49 | #9 |
|
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
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? |
|
Light Poster
|
|
| 20Aug2007,17:05 | #10 |
|
anyone? please, don't hate me cos I didn't read the before you post thread
|


