I got stuck with a NaN error in this section of code:
void Robot::updatePos(int msTime){
if(vel_left==vel_right){
pos_x-=vel_right*msTime*0.001*sin(orientation);
pos_y-=vel_right*msTime*0.001*cos(orientation);
}
else{
int sign=1;
if(vel_left<vel_right) sign=-1;
float centre_x, centre_y;
float right_rad=width/(vel_left/vel_right-1);
centre_x=pos_x-cos(orientation)*(right_rad+width/2);
centre_y=pos_y-sin(orientation)*(right_rad+width/2);
cout << "centre" << centre_x << "right_rad" << right_rad << endl;
orientation+=sign*vel_right*msTime/right_rad;
pos_x=centre_x+cos(orientation)*(right_rad+width/2);
pos_y=centre_y+sin(orientation)*(right_rad+width/2);
}
while(orientation>M_PI) orientation-=2*M_PI;
while(orientation<-M_PI) orientation+=2*M_PI;
cout << "pos_x: " << pos_x << " pos_y: " << pos_y <<
" orientation: " << orientation << endl;
}
all the class variables are floats. Do you have any idea what might be causing this error?
EDIT: sorry, should have specified that. The function runs in a loop) I get NaN for the following variables centre_x(in the first pass through loop ok then nan), pos_x, centre_y(in the first pass through loop ok then nan), pos_y, orientation. right_rad=0. Clearly the problem is in the 'else' section.
OK, narrowed down to the line: float right_rad=width/(vel_left/vel_right-1); for some reason this turns out to be 0.
Problem solved. Thanks a lot guys.