armExample.cpp

Example program showing use of the P2 arm accessory.

Demonstrates how to connect with the Pioneer 2 controller and set up the P2Arm class, including the ARMpac handler. It simply queries and prints the status of the arm, moving it to a position, then exits.

00001 #include "Aria.h"
00002 
00011 int main(int argc, char **argv)
00012 {
00013   Aria::init();
00014   ArSimpleConnector con(&argc, argv);
00015   ArRobot robot;
00016   ArP2Arm arm;
00017 
00018 
00019   if(!Aria::parseArgs()) 
00020   {
00021     Aria::logOptions();
00022     Aria::shutdown();
00023     return 1;
00024   }
00025 
00026   ArLog::log(ArLog::Normal, "armExample: Connecting to the robot.");
00027   if(!con.connectRobot(&robot))
00028   {
00029     ArLog::log(ArLog::Terse, "armExample: Could not connect to the robot. Exiting.");
00030     Aria::shutdown();
00031     return 1;
00032   }
00033   robot.runAsync(true);
00034 
00035   // turn off sonar
00036   robot.comInt(28, 0);
00037 
00038   // Set up and initialize the arm
00039   arm.setRobot(&robot);
00040   if (arm.init() != ArP2Arm::SUCCESS)
00041   {
00042     ArLog::log(ArLog::Terse, "armExample: Error initializing the P2 Arm!");
00043     return 1;
00044   }
00045 
00046   // Print out some of the settings
00047   P2ArmJoint *joint;
00048   printf("Current joint info:\nJoint   Vel  Home  Center\n");
00049   for (int i=1; i<=ArP2Arm::NumJoints; i++)
00050   {
00051     joint = arm.getJoint(i);
00052     printf("  %2i:  %5i %5i   %5i\n", i, joint->myVel, joint->myHome, joint->myCenter);
00053   }
00054   printf("\n");
00055 
00056   // Put the arm to work
00057   printf("Powering on  (takes a couple seconds to stabilize)\n");
00058   arm.powerOn();
00059 
00060   // Request one status packet and print out the arm's status
00061   printf("Current arm status:\n");
00062   arm.requestStatus(ArP2Arm::StatusSingle);
00063   ArUtil::sleep(200);  // Give time to get the packet
00064   printf("Arm Status: ");
00065   if (arm.getStatus() & ArP2Arm::ArmGood)
00066     printf("Good=1 ");
00067   else
00068     printf("Good=0 ");
00069   if (arm.getStatus() & ArP2Arm::ArmInited)
00070     printf("Inited=1 ");
00071   else
00072     printf("Inited=0 ");
00073   if (arm.getStatus() & ArP2Arm::ArmPower)
00074     printf("Power=1 ");
00075   else
00076     printf("Power=0 ");
00077   if (arm.getStatus() & ArP2Arm::ArmHoming)
00078     printf("Homing=1 ");
00079   else
00080     printf("Homing=0 ");
00081   printf("\n\n");
00082 
00083   // Move the arm joints to specific positions
00084   printf("Moving Arm...\n");
00085   int deploy_offset[] = {0, -100, 10, 40, 80, -55, 20};
00086   for (int i=1; i<=ArP2Arm::NumJoints; i++)
00087   {
00088     joint = arm.getJoint(i);
00089     arm.moveToTicks(i, joint->myCenter + deploy_offset[i]);
00090   }
00091 
00092   // Wait for arm to achieve new position, printing joint positions and M for
00093   // moving, NM for not moving.
00094   arm.requestStatus(ArP2Arm::StatusContinuous);
00095   ArUtil::sleep(300); // wait a moment for arm status packet update with joints moving
00096   bool moving = true;
00097   while (moving)
00098   {
00099       moving = false;
00100       printf("Joints: ");
00101       for (int i=1; i<=ArP2Arm::NumJoints; i++)
00102       {
00103         printf("[%d] %.0f, ", i, arm.getJointPos(i));
00104         if (arm.getMoving(i))
00105         {
00106             printf("M;  ");
00107             moving = true;
00108           }
00109         else
00110         {
00111           printf("NM; ");
00112         }
00113         }
00114       printf("\r");
00115   }
00116   printf("\n\n");
00117 
00118   // Return arm to park, wait, and disconnect. (Though the arm will automatically park
00119   // on client disconnect)
00120   printf("Parking arm.\n");
00121   arm.park();
00122 
00123   // Wait 5 seconds or until arm shuts off
00124   for(int i = 5; (i > 0) && (arm.getStatus() & ArP2Arm::ArmPower); i--)
00125   {
00126     ArUtil::sleep(1000);
00127   }
00128 
00129   // Disconnect from robot, etc., and exit.
00130   printf("Shutting down ARIA and exiting.\n");
00131   Aria::shutdown();
00132 
00133   return(0);
00134 }
00135 

Generated on Fri Jul 31 12:36:37 2009 for Aria by  doxygen 1.4.7