Skip to content

Commit e910640

Browse files
committed
upgraded wander to search for a suitable ranger instead of hard-coded model name
1 parent 9f971ac commit e910640

File tree

1 file changed

+26
-7
lines changed

1 file changed

+26
-7
lines changed

examples/ctrl/wander.cc

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,34 @@ extern "C" int Init(Model *mod, CtrlArgs *)
4545

4646
robot->pos->Subscribe(); // starts the position updates
4747

48-
ModelRanger *first_laser = dynamic_cast<ModelRanger *>(robot->pos->GetChild("ranger:0")),
49-
*second_laser = dynamic_cast<ModelRanger *>(mod->GetChild("ranger:1"));
50-
if (!first_laser || !second_laser) {
51-
PRINT_ERR1("Wander controller requires 2 range sensors (%u given).",
52-
unsigned(bool(first_laser) + bool(second_laser)));
48+
// find a range finder
49+
50+
ModelRanger *laser = NULL;
51+
52+
printf( "\nWander ctrl for robot %s:\n", robot->pos->Token() );
53+
for( int i=0; i<16; i++ )
54+
{
55+
char name[32];
56+
snprintf( name, 32, "ranger:%d", i ); // generate sequence of model names
57+
58+
printf( " looking for a suitable ranger at \"%s:%s\" ... ", robot->pos->Token(), name );
59+
laser = dynamic_cast<ModelRanger *>(robot->pos->GetChild( name ));
60+
61+
if( laser && laser->GetSensors()[0].sample_count > 8 )
62+
{
63+
puts( "yes." );
64+
break;
65+
}
66+
67+
puts( "no." );
68+
}
69+
70+
if( !laser ) {
71+
PRINT_ERR(" Failed to find a ranger with more than 8 samples. Exit.");
5372
exit(2);
5473
}
55-
first_laser->Subscribe();
56-
robot->laser = second_laser;
74+
75+
robot->laser = laser;
5776
robot->laser->AddCallback(Model::CB_UPDATE, model_callback_t(LaserUpdate), robot);
5877
robot->laser->Subscribe(); // starts the ranger updates
5978

0 commit comments

Comments
 (0)