single.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 POP 100 // population size
00009 #define VSPEED 0.4 // meters per second
00010 #define WGAIN 1.0 // turn speed gain
00011 #define SAFE_DIST 0.3 // meters
00012 #define SAFE_ANGLE 0.4 // radians
00013 
00014 int main(int argc, char **argv)
00015 {
00016   // Parse command line options
00017   parse_args(argc,argv);
00018 
00019   // We throw exceptions on creation if we fail
00020   try
00021   {
00022     using namespace PlayerCc;
00023     using namespace std;
00024 
00025     // Create a player client object, using the variables assigned by the
00026     // call to parse_args()
00027     PlayerClient robot (gHostname, gPort);
00028     robot.SetDataMode( PLAYER_DATAMODE_PULL );
00029     robot.SetReplaceRule( true );
00030     
00031     Position2dProxy pp( &robot, gIndex );
00032     SonarProxy      sp( &robot, gIndex );
00033     Graphics2dProxy gp( &robot, gIndex );
00034 
00035     sp.RequestGeom();   // query the server for sonar positions
00036         
00037     while(1)
00038       {
00039         // blocks until new data comes from Player
00040         robot.Read();
00041         
00042         for( int i=0; i<POP; i++ )
00043           {
00044             // compute the vector sum of the sonar ranges             
00045             double dx=0, dy=0;
00046             
00047             int num_ranges = sp.GetCount();
00048             for( int s=0; s<num_ranges; s++ )
00049               {
00050                 player_pose3d_t spose = sp.GetPose(s);
00051                 double srange = sp.GetScan(s);
00052                 
00053                 dx += srange * cos( spose.pyaw );
00054                 dy += srange * sin( spose.pyaw );
00055               }
00056             
00057             // compute the direction of the resultant vector 
00058             double resultant_angle = atan2( dy, dx );
00059             //double resultant_magnitude = hypot( dy, dx );
00060             
00061             double forward_speed = 0.0;
00062             double side_speed = 0.0;       
00063             double turn_speed = WGAIN * resultant_angle;
00064                     
00065             // find the index of the forward-pointing sensor
00066             int forward = num_ranges/2 -1 ;
00067             
00068             // if the front is clear, drive forwards
00069             if( (sp.GetScan(forward-1) > SAFE_DIST) &&
00070                 (sp.GetScan(forward) > SAFE_DIST) &&
00071                 (sp.GetScan(forward+1) > SAFE_DIST) && 
00072                 (fabs( resultant_angle ) < SAFE_ANGLE) )
00073               {
00074                 forward_speed = VSPEED;
00075               }
00076         
00077             // send a command to the robot's position device
00078             pp.SetSpeed( forward_speed, side_speed, turn_speed );
00079             
00080             // draw the resultant vector on the robot to show what it
00081             // is thinking
00082             if( forward_speed > 0 )           
00083               gp.Color( 0,255,0,0 );
00084             else
00085               gp.Color( 0,255,255,0 );
00086 
00087             player_point_2d_t pts[2];
00088             pts[0].px = 0;
00089             pts[0].py = 0;
00090             pts[1].px = dx;
00091             pts[1].py = dy;     
00092             
00093             gp.Clear();
00094             gp.DrawPolyline( pts, 2 );
00095             
00096           }    
00097       }
00098   }
00099   catch (PlayerCc::PlayerError e)
00100     {
00101       std::cerr << e << std::endl;
00102       return -1;
00103     }
00104 }


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