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;
  }
}

I'll see if I can answer some your question here, Like you I am trying to learn player stage, but I have a pretty extensive programming background and that has helped a lot.

1. i am not really sure with that one, I'd have to look it up in the API more closely.

2. the GetCount() method gets the number of LaserProxy objects and with that the for loop can interate through each LaserProxy object.

3. It is not exactly an array but the sonar sensor placements.

http://rubyplayer.rubyforge.org/autodoc/classes/SonarProxy.html

check out that link for some more detailed information in regards to this.

4. See answer 3.

I might also suggest looking the player/stage API on sourceforge, I am sure that can be some help as well.

http://playerstage.sourceforge.net/

Hope that helps a little

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;
  }
}

Oh and the Arrow designator "->" is for referencing a method or property of an object, much like the "." in Java or C#. It can also be used to denote a pointer. Incase you are wondering as well when you see the "&" infront of a variable name that is telling the compiler to reference the memory address of that variable.

Oh and the Arrow designator "->" is for referencing a method or property of an object, much like the "." in Java or C#. It can also be used to denote a pointer. Incase you are wondering as well when you see the "&" infront of a variable name that is telling the compiler to reference the memory address of that variable.

I don't know player/stage so I can't really address the OP's questions re: that. But I thought I should clarify something concerning operators.

Whenever you see the keyword "new" as in:

if (gUseLaser)
      lp = new LaserProxy (&robot, gIndex);
    else
      sp = new SonarProxy (&robot, gIndex);

a pointer is being created. In C++ there are 2 operators that are used to access an object's member methods and properties. Which one you use depends on what is on the "left-hand" side of the operator.

The first is the "dot" operator '.'. It is used when accessing the method or property via an actual object.

The second is the "arrow" operator '->' that is used when accessing the method or property via a pointer to an object.

The code that you (the OP) posted uses pointers. As a result, '->' is required to be used.

'&' is another pointer-related operator. Pointers work on memory addresses rather than actual values that are relevant to the program. This operator represents a request for the memory address of a particular value or object, not the actual value or object itself. It's like telling the program "My house is at 'XYZ Whatever St.'" as opposed to physically moving your house to a new location.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.