Difference between revisions of "Controlling the fakebot from code"

From Wiki for iCub and Friends
Jump to: navigation, search
Line 34: Line 34:
 
       Time::delay(3);
 
       Time::delay(3);
 
       Network::fini(); // This is the reverse of Network::init()
 
       Network::fini(); // This is the reverse of Network::init()
 +
      return 0;
 +
  }
 +
 +
Another a bit more elaborated 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;
 
       return 0;
 
   }
 
   }

Revision as of 13:37, 21 February 2007

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

Another a bit more elaborated example:

  1. include <yarp/os/Network.h>
  2. include <yarp/os/Port.h>
  3. include <yarp/os/Bottle.h>
  4. include <yarp/os/BufferedPort.h>
  5. include <yarp/os/Time.h>
  6. 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;
  }