multiple.cc
Go to the documentation of this file.
00001 #include <libplayerc++/playerc++.h>
00002 #include <iostream>
00003 #include <assert.h>
00004 
00005 // For argument parsing
00006 #include "args.h"
00007 
00008 #define VSPEED 0.4 // meters per second
00009 #define WGAIN 1.0 // turn speed gain
00010 #define SAFE_DIST 0.3 // meters
00011 #define SAFE_ANGLE 0.4 // radians
00012 
00013 int main(int argc, char **argv)
00014 {
00015   // Parse command line options
00016   parse_args(argc,argv);
00017 
00018   // We throw exceptions on creation if we fail
00019   try
00020   {
00021     using namespace PlayerCc;
00022     using namespace std;
00023 
00024     // Create a player client object, using the variables assigned by the
00025     // call to parse_args()
00026     PlayerClient robot (gHostname, gPort);
00027     robot.SetDataMode( PLAYER_DATAMODE_PULL );
00028     robot.SetReplaceRule( true );
00029     
00030     Position2dProxy** ppp = new Position2dProxy*[gPop];
00031     SonarProxy**      spp = new SonarProxy*[gPop];
00032     Graphics2dProxy** gpp = new Graphics2dProxy*[gPop];
00033 
00034     for( uint i=0; i<gPop; i++ )
00035       {
00036         ppp[i] = new Position2dProxy( &robot , i );
00037         assert( ppp[i] );
00038 
00039         spp[i] = new SonarProxy( &robot, i );
00040         assert( spp[i] );
00041 
00042         gpp[i] = new Graphics2dProxy( &robot, i );
00043         assert( gpp[i] );
00044         
00045         spp[i]->RequestGeom();  // query the server for sonar
00046                                 // positions
00047       }
00048     
00049     while(1)
00050       {
00051         // blocks until new data comes from Player
00052         robot.Read();
00053 
00054         for( uint i=0; i<gPop; i++ )
00055           if( spp[i]->IsFresh() ) // only update if this proxy has some new data
00056             {
00057               spp[i]->NotFresh(); // mark the data as old
00058               
00059               // compute the vector sum of the sonar ranges           
00060               double dx=0, dy=0;
00061               
00062               int num_ranges = spp[i]->GetCount();
00063               for( int s=0; s<num_ranges; s++ )
00064                 {
00065                   player_pose3d_t spose = spp[i]->GetPose(s);
00066                   double srange = spp[i]->GetScan(s);
00067                   
00068                   dx += srange * cos( spose.pyaw );
00069                   dy += srange * sin( spose.pyaw );
00070                 }
00071 
00072               double resultant_angle = atan2( dy, dx );
00073               //double resultant_magnitude = hypot( dy, dx );
00074               
00075               double forward_speed = 0.0;
00076               double side_speed = 0.0;     
00077               double turn_speed = WGAIN * resultant_angle;
00078               
00079               int forward = num_ranges/2 -1 ;
00080               // if the front is clear, drive forwards
00081               if( (spp[i]->GetScan(forward-1) > SAFE_DIST) &&
00082                   (spp[i]->GetScan(forward) > SAFE_DIST) &&
00083                   (spp[i]->GetScan(forward+1) > SAFE_DIST) && 
00084                   (fabs( resultant_angle ) < SAFE_ANGLE) )
00085                 {
00086                   forward_speed = VSPEED;
00087                 }
00088 
00089               // send a command to the robot
00090               ppp[i]->SetSpeed( forward_speed, side_speed, turn_speed );
00091               
00092               // draw the resultant vector on the robot to show what it
00093               // is thinking          
00094               if( forward_speed > 0 )
00095                 gpp[i]->Color( 0,255,0,0 );
00096               else
00097                 gpp[i]->Color( 0,255,255,0 );
00098               
00099               player_point_2d_t pts[2];
00100               pts[0].px = 0;
00101               pts[0].py = 0;
00102               pts[1].px = dx;
00103               pts[1].py = dy;   
00104               
00105               gpp[i]->Clear();
00106               gpp[i]->DrawPolyline( pts, 2 );
00107                       
00108             }    
00109       }
00110   }
00111   catch (PlayerCc::PlayerError e)
00112     {
00113       std::cerr << e << std::endl;
00114       return -1;
00115     }
00116 }


stage
Author(s): Richard Vaughan , Brian Gerkey , Reed Hedges , Andrew Howard , Toby Collett , Pooya Karimian , Jeremy Asher , Alex Couture-Beil , Geoff Biggs , Rich Mattes , Abbas Sadat
autogenerated on Thu Aug 27 2015 15:20:57