#include <iostream>
#include <libplayerc++/playerc++.h>
using namespace std;
int main(int argc, char *argv[])
{
using namespace PlayerCc;
PlayerClient robot("localhost");
BumperProxy bp(&robot,0);
Position2dProxy pp(&robot,0);
pp.SetMotorEnable(true);
for(;;)
double turnrate, speed;
double error;
bool wall;
motor_a_speed(0);
motor_c_speed(0);
while(1) {
front_bumper = SENSOR_2;
left_bumper = SENSOR_3;
if (front_bumper > 2) {
if (left_bumper < 3) {
motor_a_speed(5);
motor_c_speed(drive_speed);
motor_a_dir(fwd);
motor_c_dir(fwd);
}
else {
motor_a_speed(drive_speed);
motor_c_speed(5);
motor_a_dir(rev);
motor_c_dir(rev);
}
}
else {
motor_a_speed(drive_speed);
motor_c_speed(drive_speed);
motor_a_dir(brake);
motor_c_dir(brake);
mrest(100);
cputs("bump");
motor_a_dir(fwd);
motor_c_dir(rev);
msleep(450);
cputs("right");
motor_a_speed(10);
motor_a_dir(fwd);
motor_c_dir(fwd);
mrest(1300);
}
pp.SetSpeed(speed, turnrate);
}发布于 2010-03-30 22:58:47
考虑到不好的缩进,唯一让我吃惊的是
for(;;;)
double turnrate, speed;因为for后面没有大括号中的块,所以它的主体是下一条语句。这不是我经常看到的构造,但我相信发生的情况是for语句包含turnrate和speed的定义,它们是for语句的本地定义。
发布于 2010-03-30 23:08:25
我已经将源码重新格式化为更易读的版本。有编辑权限的人能用这个代替行动的线人吗?
使用标识,不带大括号的for循环显然是造成麻烦的原因。一个简单的测试(我不知道这是如何工作的)表明for(;;)循环将永远循环,因此OP的程序永远不会做任何有用的事情。它可能应该被删除
#include <iostream>
#include <libplayerc++/playerc++.h>
using namespace std;
int main(int argc, char *argv[])
{
using namespace PlayerCc;
PlayerClient robot("localhost");
BumperProxy bp(&robot,0);
Position2dProxy pp(&robot,0);
pp.SetMotorEnable(true);
for(;;)
double turnrate, speed;
double error;
bool wall;
motor_a_speed(0);
motor_c_speed(0);
while(1) {
front_bumper = SENSOR_2;
left_bumper = SENSOR_3;
if (front_bumper > 2) {
if (left_bumper < 3) {
motor_a_speed(5);
motor_c_speed(drive_speed);
motor_a_dir(fwd);
motor_c_dir(fwd);
}
else {
motor_a_speed(drive_speed);
motor_c_speed(5);
motor_a_dir(rev);
motor_c_dir(rev);
}
}
else {
motor_a_speed(drive_speed);
motor_c_speed(drive_speed);
motor_a_dir(brake);
motor_c_dir(brake);
mrest(100);
cputs("bump");
motor_a_dir(fwd);
motor_c_dir(rev);
msleep(450);
cputs("right");
motor_a_speed(10);
motor_a_dir(fwd);
motor_c_dir(fwd);
mrest(1300);
}
pp.SetSpeed(speed, turnrate);
}
}https://stackoverflow.com/questions/2546135
复制相似问题