#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);
}
views:
83answers:
2
+5
A:
Given the bad indentation, the only thing that jumps out at me is
for(;;;)
double turnrate, speed;
Since the for
isn't followed by a block in braces, its body is the next statement. It isn't a construct I've seen often, but I believe what happens is that the for
statement contains a definition of turnrate
and speed
, which are local to the for
statement.
David Thornley
2010-03-30 14:58:46
+1
A:
I've reformatted the sourcecode to a more readable version. Could someone with editing clearance replace the OP's source with this?
Using identation, the for loop without braces is clearly visvible as a cause of trouble. A short test (i didn't no how this would work) showed that a for(;;) - loop will loop forever, so the OP's Program will never do anything useful. It should probably be removed
#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);
}
}
sum1stolemyname
2010-03-30 15:08:25
Thanks - I edited as you suggested.
David Thornley
2010-03-30 15:15:29