Go to the documentation of this file.00001 #include <libplayerc++/playerc++.h>
00002 #include <iostream>
00003 #include <assert.h>
00004
00005
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
00017 parse_args(argc,argv);
00018
00019
00020 try
00021 {
00022 using namespace PlayerCc;
00023 using namespace std;
00024
00025
00026
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();
00036
00037 while(1)
00038 {
00039
00040 robot.Read();
00041
00042 for( int i=0; i<POP; i++ )
00043 {
00044
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
00058 double resultant_angle = atan2( dy, dx );
00059
00060
00061 double forward_speed = 0.0;
00062 double side_speed = 0.0;
00063 double turn_speed = WGAIN * resultant_angle;
00064
00065
00066 int forward = num_ranges/2 -1 ;
00067
00068
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
00078 pp.SetSpeed( forward_speed, side_speed, turn_speed );
00079
00080
00081
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 }