O.K., I’m going nuts trying to figure out what’s going on. I’ve got a case statement in the main loop (state 6 refers to the case in the sample output below). For awhile, things work fine, then I start getting haywire results, where a simple subtraction gives the wrong result. Here’s the code fragment:
case turning:
Serial.print("state: ");
Serial.print(state);
Serial.print("Waypoint: ");
Serial.print(waypointIndex);
Serial.print(" Location: ");
Serial.print(location.x);
Serial.print(" ");
Serial.print(location.y);
Serial.print(" heading: ");
Serial.print(heading);
Serial.print("target heading: ");
Serial.print(targetHeading);
Serial.print(" heading delta: ");
headingDelta = fabs(heading-targetHeading);
Serial.print(headingDelta);
Serial.print("small? ");
Serial.print((headingDelta<0.25));
Serial.print("\n");
if (headingDelta < 0.25) {
Stop();
state = travel;
}
else {
if (turn>=0) {
Left(255,255); // turn left
}
else {
Right(255,255); // turn right
}
}
break;
Here’s a piece of the output:
state: 6Waypoint: 2 Location: 111.98 18.12 heading: 0.95target heading: 1.57 heading delta: 0.62small? 0
state: 6Waypoint: 2 Location: 111.98 18.12 heading: 0.95target heading: 1.57 heading delta: 0.62small? 0
state: 6Waypoint: 2 Location: 111.98 18.12 heading: 1.35target heading: 1.57 heading delta: 0.22small? 1
state: 6Waypoint: 3 Location: 142.73 47.69 heading: 0.55target heading: 0.00 heading delta: 0.00small? 0
state: 6Waypoint: 3 Location: 142.73 47.69 heading: 0.55target heading: 0.00 heading delta: 0.00small? 0
state: 6Waypoint: 3 Location: 142.73 47.69 heading: 0.55target heading: 0.00 heading delta: 0.00small? 0
With the 4th output, suddenly my headingDelta prints as zero, when clearly it isn’t, and yet the logic check on whether or not it’s small says it’s not small!
I’m pulling my hair out. What’s going on? This is on a Romeo, which is an Arduino compatible ATmega328 board.
It looks like
targetHeadinghas becomeNaNor+/-Infand thatSerial.print(...)just outputs0.0when it encounters one of those values. Perhaps you are dividing by 0 somewhere when calculatingtargetHeading.