Difference between revisions of "Controlling the fakebot from code"

From Wiki for iCub and Friends
Jump to: navigation, search
 
(8 intermediate revisions by 2 users not shown)
Line 1: Line 1:
This example shows how to send commands to the fake robot.
+
== Basic position control ==
 +
* This example shows how to send commands to the fake robot.
  
 
   #include <yarp/os/Network.h>
 
   #include <yarp/os/Network.h>
Line 24: Line 25:
 
       //
 
       //
 
       Bottle& outBot1 = out.prepare();  // Get the object
 
       Bottle& outBot1 = out.prepare();  // Get the object
 +
      outBot1.clear();
 
       outBot1.addString("set"); // put "set" command in the bottle
 
       outBot1.addString("set"); // put "set" command in the bottle
 
       outBot1.addString("pos"); // put "pos" command in the bottle
 
       outBot1.addString("pos"); // put "pos" command in the bottle
Line 37: Line 39:
 
   }
 
   }
  
Another a bit more elaborated example:
 
  
#include <yarp/os/Network.h>
+
== Basic velocity control ==
#include <yarp/os/Port.h>
+
* Here's the message you need to do command the motors to velocities xVelocity and yVelocity:
#include <yarp/os/Bottle.h>
+
            Bottle& pos = out.prepare();
#include <yarp/os/BufferedPort.h>
+
            pos.clear();
#include <yarp/os/Time.h>
+
            pos.addVocab(VOCAB3('s','e','t'));
#include <yarp/os/Random.h>
+
            pos.addVocab(VOCAB4('v','m','o','s'));
//
+
            Bottle& coords = pos.addList();
using namespace yarp::os;
+
            coords.addDouble(xVelocity);
using namespace yarp;
+
            coords.addDouble(yVelocity);
//
+
            out.write();
int main() {
+
 
      // Initialize YARP - some OSes need network and time service initialization
+
== Random movement example ==
      Network::init();
+
* Another, a bit more elaborate, example:
      //
+
 
      // Create a port you will use to send commands to the bot
+
  #include <yarp/os/Network.h>
      BufferedPort<Bottle> out;
+
  #include <yarp/os/Port.h>
      //
+
  #include <yarp/os/Bottle.h>
      // Name the ports
+
  #include <yarp/os/BufferedPort.h>
      out.open("/nat/out");
+
  #include <yarp/os/Time.h>
      //
+
  #include <yarp/os/Random.h>
      // Connect the ports so that anything written from /out arrives to /in
+
  //
      Network::connect("/nat/out","/fakebot/motor/rpc:i");
+
  using namespace yarp::os;
      //
+
  using namespace yarp;
  int j=0;
+
  //
  int pos=0;
+
  int main() {
  while(true)
+
        // Initialize YARP - some OSes need network and time service initialization
  {
+
        Network::init();
Bottle& outBot1 = out.prepare();  // Get the object
+
        //
outBot1.clear();
+
        // Create a port you will use to send commands to the bot
outBot1.addString("set"); // put "set" command in the bottle
+
        BufferedPort<Bottle> out;
outBot1.addString("pos"); // put "pos" command in the bottle
+
        //
outBot1.addInt(j); //control joint 0
+
        // Name the ports
outBot1.addInt(pos); //move to 80
+
        out.open("/nat/out");
printf("Writing bottle 1 (%s)\n", outBot1.toString().c_str());
+
        //
out.write();                      // Now send it on its way
+
        // Connect the ports so that anything written from /out arrives to /in
//
+
        Network::connect("/nat/out","/fakebot/motor/rpc:i");
// change joint number
+
        //
if (j==0)
+
        int j=0;
j=1;
+
        int pos=0;
else
+
        while(true)
j=0;
+
        {
//
+
            Bottle& outBot1 = out.prepare();  // Get the object
// pick a random position
+
            outBot1.clear();
pos=100*(2*Random::uniform()-1);
+
            outBot1.addString("set"); // put "set" command in the bottle
// wait some time
+
            outBot1.addString("pos"); // put "pos" command in the bottle
Time::delay(3);
+
            outBot1.addInt(j); //control joint 0
  }
+
            outBot1.addInt(pos); //move to 80
  //
+
            printf("Writing bottle 1 (%s)\n",outBot1.toString().c_str());
  Network::fini(); // This is the reverse of Network::init()
+
            out.write();                      // Now send it on its way
      return 0;
+
            //
  }
+
            // change joint number
 +
            if (j==0)
 +
                j=1;
 +
            else
 +
                j=0;
 +
            //
 +
            // pick a random position
 +
            pos=100*(2*Random::uniform()-1);
 +
            // wait some time
 +
            Time::delay(3);
 +
          }
 +
          //
 +
          Network::fini(); // This is the reverse of Network::init()
 +
          return 0;
 +
    }

Latest revision as of 20:18, 27 February 2007

Basic position control

  • This example shows how to send commands to the fake robot.
 #include <yarp/os/Network.h>
 #include <yarp/os/Port.h>
 #include <yarp/os/Bottle.h>
 #include <yarp/os/BufferedPort.h>
 #include <yarp/os/Time.h>
 //
 using namespace yarp::os;
 using namespace yarp;
 //
 int main() {
     // Initialize YARP - some OSes need network and time service initialization
      Network::init();
     //
     // Create a port you will use to send commands to the bot
     BufferedPort<Bottle> out;
     //
     // Name the ports
     out.open("/nat/out");
     //
     // Connect the ports so that anything written from /out arrives to /in
     Network::connect("/nat/out","/fakebot/motor/rpc:i");
     //
     Bottle& outBot1 = out.prepare();   // Get the object
     outBot1.clear();
     outBot1.addString("set"); // put "set" command in the bottle
     outBot1.addString("pos"); // put "pos" command in the bottle
     outBot1.addInt(0); //control joint 0
     outBot1.addInt(-80); //move to 80
     printf("Writing bottle 1 (%s)\n", outBot1.toString().c_str());
     out.write();                       // Now send it on its way
     //
     //
     Time::delay(3);
     Network::fini(); // This is the reverse of Network::init()
     return 0;
  }


Basic velocity control

  • Here's the message you need to do command the motors to velocities xVelocity and yVelocity:
           Bottle& pos = out.prepare();
           pos.clear();
           pos.addVocab(VOCAB3('s','e','t'));
           pos.addVocab(VOCAB4('v','m','o','s'));
           Bottle& coords = pos.addList();
           coords.addDouble(xVelocity);
           coords.addDouble(yVelocity);
           out.write();

Random movement example

  • Another, a bit more elaborate, example:
  #include <yarp/os/Network.h>
  #include <yarp/os/Port.h>
  #include <yarp/os/Bottle.h>
  #include <yarp/os/BufferedPort.h>
  #include <yarp/os/Time.h>
  #include <yarp/os/Random.h>
  //
  using namespace yarp::os;
  using namespace yarp;
  //
  int main() {
        // Initialize YARP - some OSes need network and time service initialization
        Network::init();
        //
        // Create a port you will use to send commands to the bot
        BufferedPort<Bottle> out;
        //
        // Name the ports
        out.open("/nat/out");
        //
        // Connect the ports so that anything written from /out arrives to /in
        Network::connect("/nat/out","/fakebot/motor/rpc:i");
        //
        int j=0;
        int pos=0;
        while(true)
        {
           Bottle& outBot1 = out.prepare();   // Get the object
           outBot1.clear();
           outBot1.addString("set"); // put "set" command in the bottle
           outBot1.addString("pos"); // put "pos" command in the bottle
           outBot1.addInt(j); //control joint 0
           outBot1.addInt(pos); //move to 80
           printf("Writing bottle 1 (%s)\n",outBot1.toString().c_str());
           out.write();                       // Now send it on its way
           //
           // change joint number
           if (j==0)
               j=1;
           else
               j=0;
           //
           // pick a random position
           pos=100*(2*Random::uniform()-1);
           // wait some time
           Time::delay(3);
         }
         //
         Network::fini(); // This is the reverse of Network::init()
         return 0;
   }