Difference between revisions of "Controlling the fakebot from your own code"

From Wiki for iCub and Friends
Jump to: navigation, search
(//)
 
 
Line 1: Line 1:
You need to create a port to send commands to the fake bot.
+
This example shows how to send commands to the fake robot.
  
Have a look at this example.
+
  int main() {
 
+
      // Initialize YARP - some OSes need network and time service initialization
int main() {
+
      Network::init();
    // Initialize YARP - some OSes need network and time service initialization
+
      //
    Network::init();
+
      // Create a port you will use to send commands to the bot
 
+
      Port out;
    // Create a port you will use to send commands to the bot
+
      //
    Port out;
+
      // Name the ports
 
+
      out.open("/USER_NAME/out");
    // Name the ports
+
      //
    out.open("/USER_NAME/out");
+
      // Connect the ports so that anything written from /out arrives to /in
 
+
      Network::connect("/USERNAME/out","/fakebot/motor/rpc:i");
    // Connect the ports so that anything written from /out arrives to /in
+
      //
    Network::connect("/USERNAME/out","/fakebot/motor/rpc:i");
+
      Bottle& outBot1 = out.prepare();  // Get the object
 
+
      outBot1.addString("set"); // put "set" command in the bottle
    Bottle& outBot1 = out.prepare();  // Get the object
+
      outBot1.addInt(0); //control joint 0
    outBot1.addString("set"); // put "set" command in the bottle
+
      outBot2.addInt(80); //move to 80
    outBot1.addInt(0); //control joint 0
+
      printf("Writing bottle 1 (%s)\n", outBot1.toString().c_str());
    outBot2.addInt(80); //move to 80
+
      out.write();                      // Now send it on its way
    printf("Writing bottle 1 (%s)\n", outBot1.toString().c_str());
+
      //
    out.write();                      // Now send it on its way
+
      Network::fini(); // This is the reverse of Network::init()
 
+
      return 0;
    Network::fini(); // This is the reverse of Network::init()
+
  }
    return 0;
 
}
 

Latest revision as of 12:54, 21 February 2007

This example shows how to send commands to the fake robot.

  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
     Port out;
     //
     // Name the ports
     out.open("/USER_NAME/out");
     //
     // Connect the ports so that anything written from /out arrives to /in
     Network::connect("/USERNAME/out","/fakebot/motor/rpc:i");
     //
     Bottle& outBot1 = out.prepare();   // Get the object
     outBot1.addString("set"); // put "set" command in the bottle
     outBot1.addInt(0); //control joint 0
     outBot2.addInt(80); //move to 80
     printf("Writing bottle 1 (%s)\n", outBot1.toString().c_str());
     out.write();                       // Now send it on its way
     //
     Network::fini(); // This is the reverse of Network::init()
     return 0;
  }