O.K。,我很想弄清楚发生了什么。我在主循环中有一个case语句(状态6指的是下面示例输出中的情况)。有一段时间,事情很好,然后我开始得到乱七八糟的结果,其中一个简单的减法给出了错误的结果。这是代码片段:
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;
这是输出的一部分:
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
随着第4个输出,突然我的标题Delta打印为零,当显然不是,但逻辑检查它是否小说它不小!
我把头发拉了出来。这是怎么回事?这是一个罗密欧,这是一个兼容Arduino的ATmega328主板。答案 0 :(得分:1)
看起来targetHeading
已成为NaN
或+/-Inf
,而Serial.print(...)
只在遇到其中一个值时输出0.0
。在计算targetHeading
时,也许你在某处除以0。