ptFront 变量链接到我的视差螺旋桨板上的 QT 电路。当光电晶体管看到白墙时,该值会降至 18000 以下,而当它看到白色以外的任何东西时,该值会增加到 20000-50000 以上。当 while 循环的参数无效时,它仍然会运行循环。我通过将 LED 电路连接到引脚 6 来确认这一点。它永远不会存在。那为什么不退出呢?我需要它能够与它识别为不是墙的物体进行交互。
#include "simpletools.h"
#include "abdrive360.h"
#include "ping.h"
int irLeft, irRight, ptFloor, ptFront; // IR variables
int main() {
high(9);
high(8);
low(26);
low(27);
low(6);
drive_setRampStep(12);
while (1) {
ptFront = rc_time(9, 1);
ptFloor = rc_time(8, 1);
freqout(11, 1, 38000); // Check left & right objects
irLeft = input(10);
freqout(1, 1, 38000);
irRight = input(2);
// Roaming and Obstacle Avoidance
if (irRight == 1 && irLeft == 1) { // No obstacles?
drive_rampStep(35, 35);
} else if (irLeft == 0 && irRight == 0 && ptFront > 18000) { // Left & right obstacles and it's not the wall?
print("oh robot!");
robotFight();
} else if ((irLeft==0||irRight==0)&& ptFront < 18000) { // Left & right obstacles and is the wall?
trajAdjust();
} else if (ptFloor <= 10000) { // In the pit?
pit();
}
}
}
void trajAdjust() {
while (ptFront <= 18000) {
// Implement trajectory adjustment logic here
// This could involve making a turn to avoid the obstacle (wall)
high(9);
high(6);
freqout(11, 1, 38000); // Check left & right objects
irLeft = input(10);
freqout(1, 1, 38000);
irRight = input(2);
ptFront = rc_time(9, 1); // Update ptFront inside the loop
print("ptFront= %d \n",ptFront);
if (irLeft == 0 && irRight == 1) {
// Only the left sensor detects an obstacle, turn right
drive_rampStep(35, -35);
print("left wall \n");
} else if (irLeft == 1 && irRight == 0) {
// Only the right sensor detects an obstacle, turn left
drive_rampStep(-35, 35);
print("right wall \n");
} else if (irLeft == 0 && irRight == 0) {
// Both sensors detect an obstacle
// Prioritize turning in one direction
drive_rampStep(35, -35); // Example adjustment, you may need to fine-tune
} else {
// No obstacle in front, or only one sensor detects an obstacle
// Continue straight or make other adjustments as needed
drive_rampStep(35, 35);
}
}
}
void pit() {
//pit logic
}
void robotFight() {
while (ptFront > 18000 && irRight == 0 && irLeft == 0) {
// Measure the distance using the ultrasonic sensor
int range = ping_cm(4);
// Print the distance to the terminal (optional)
print("Distance to target: %d cm\n", range);
// Define the desired range boundaries (5-8 cm)
int minRange = 5;
int maxRange = 8;
if (range > maxRange) {
drive_rampStep(35, 35);
} else if (range < minRange && range > 5) {
drive_rampStep(-35, -35);
} else if (range >= minRange && range <= maxRange) {
drive_rampStep(0, 0);
}
}
}
您的问题就在帖子的第一行,因为“ptFront 变量链接到我的视差螺旋桨板上的 QT 电路。”这不是真的,这个假设会让你偏离正轨。
变量
ptFront
并不特殊,在这里定义int irLeft, irRight, ptFloor, ptFront; // IR variables
(无初始化)ptFront = rc_time(9, 1);
ptFloor = rc_time(8, 1);
并在此循环更新
ptFront = rc_time(9, 1); // Update ptFront inside the loop
,在两个循环之一内,在其条件下评估此变量,并提供富有洞察力的注释。
这样做的另一个循环不包含其体内普通变量的必要更新,我假设它是永远不会退出的循环。
将评估变量的循环更新插入失败循环的主体内,您应该已设置完毕。