I don't understand what the following functions does in the code random walk ( sample programes in the player/stage library)
could someone please be kind enough to help me understanding it
1. in the following one why dont we just use 0 whats the need to use gindex?
if (gUseLaser)
lp = new LaserProxy (&robot, gIndex);
else
sp = new SonarProxy (&robot, gIndex);
2. in this one what does "lp->GetCount ()" mean? what is it suppose to do and what the arrow used for? am not familiar with C++ that much.
if (gUseLaser)
{
obs = false;
for (uint i = 0; i < lp->GetCount (); i++)
{
if((*lp)[i] < minfrontdistance)
obs = true;
}
}
3. the following one sp->GetScan (2) why do they have numbers as 2,3,4,5? Is it for an array?
else
{
obs = ((sp->GetScan (2) < minfrontdistance) || // 0?
(sp->GetScan (3) < minfrontdistance) ||
(sp->GetScan (4) < minfrontdistance) || // 0?
(sp->GetScan (5) < minfrontdistance) );
}
4. Same with the below one. why is everything added and why do they have different numbers for GetScaen as 1,15,7,8
if(sp->GetScan (1) + sp->GetScan (15) < sp->GetScan (7) + sp->GetScan (8))
newturnrate = -turnrate;
else
newturnrate = turnrate;
}
}
avoidcount--;
}
Thank you so so much.
#include <libplayerc++/playerc++.h>
#include <iostream>
using namespace PlayerCc;
#include "args.h"
double minfrontdistance = 0.750;
double speed = 0.200;
double avoidspeed = 0; // -150;
double turnrate = DTOR(40);
int main(int argc, char** argv)
{
int randint;
int randcount = 0;
int avoidcount = 0;
bool obs = false;
parse_args(argc,argv);
LaserProxy *lp = NULL;
SonarProxy *sp = NULL;
// we throw exceptions on creation if we fail
try
{
PlayerClient robot(gHostname, gPort);
Position2dProxy pp(&robot, gIndex);
if (gUseLaser)
lp = new LaserProxy (&robot, gIndex);
else
sp = new SonarProxy (&robot, gIndex);
std::cout << robot << std::endl;
pp.SetMotorEnable (true);
// go into read-think-act loop
double newturnrate=0.0f, newspeed=0.0f;
for(;;)
{
robot.Read();
/* See if there is an obstacle in front */
if (gUseLaser)
{
obs = false;
for (uint i = 0; i < lp->GetCount (); i++)
{
if((*lp)[i] < minfrontdistance)
obs = true;
}
}
else
{
obs = ((sp->GetScan (2) < minfrontdistance) || // 0?
(sp->GetScan (3) < minfrontdistance) ||
(sp->GetScan (4) < minfrontdistance) || // 0?
(sp->GetScan (5) < minfrontdistance) );
}
if(obs || avoidcount || pp.GetStall ())
{
newspeed = avoidspeed;
/* once we start avoiding, continue avoiding for 2 seconds */
/* (we run at about 10Hz, so 20 loop iterations is about 2 sec) */
if(!avoidcount)
{
avoidcount = 15;
randcount = 0;
if(gUseLaser)
{
if(lp->MinLeft () < lp->MinRight ())
newturnrate = -turnrate;
else
newturnrate = turnrate;
}
else
{
if(sp->GetScan (1) + sp->GetScan (15) < sp->GetScan (7) + sp->GetScan (8))
newturnrate = -turnrate;
else
newturnrate = turnrate;
}
}
avoidcount--;
}
else
{
avoidcount = 0;
newspeed = speed;
/* update turnrate every 3 seconds */
if(!randcount)
{
/* make random int tween -20 and 20 */
//randint = (1+(int)(40.0*rand()/(RAND_MAX+1.0))) - 20;
randint = rand() % 41 - 20;
newturnrate = dtor(randint);
randcount = 20;
}
randcount--;
}
// write commands to robot
pp.SetSpeed(newspeed, newturnrate);
}
}
catch (PlayerCc::PlayerError e)
{
std::cerr << e << std::endl;
return -1;
}
}