main.cc

Go to the documentation of this file.
00001 #include <iostream>
00002 #include <stdio.h>
00003 #include <libplayerc++/playerc++.h>
00004 #include <scorpion.h>
00005 #include <args.h>
00006 using namespace PlayerCc;
00007 
00008 int
00009 main(int argc, char *argv[])
00010 {
00011    // Parse input arguments
00012   parse_args(argc,argv);
00013    
00014    timespec move_sleep = { 5, 0 };
00015    timespec turn_sleep = { 3, 0 };
00016    timespec stop_sleep = { 10, 0 };
00017 
00018   // libplayerc++ throws a PlayerError exception when
00019   // it runs into trouble
00020    // The PlayerError is often more readable...
00021    try
00022    {
00023       PlayerClient robot(gHostname, gPort);
00024       Position2dProxy pp(&robot, gIndex);
00025       BumperProxy bp(&robot, gIndex);
00026 
00027       // Using pull datamode, so we only get newest data
00028       // Replacerule make old data being overwritten
00029       robot.SetDataMode(PLAYER_DATAMODE_PULL);
00030       robot.SetReplaceRule(true, PLAYER_MSGTYPE_DATA);
00031      
00032       // Speed and turn settings
00033       double turn_rate = 0.5;
00034       double move_speed = 0.10;
00035    
00036       
00037       /* Go into read-think-act loop */
00038       while (true) {
00039          // read from the proxies
00040          robot.Read();
00041    
00042          // Stop if bumpsensors not activated
00043          if (!bp.IsAnyBumped()) {
00044             printf("Nothing ... waiting for bump\n");
00045             pp.SetSpeed(0, 0);
00046          }
00047          
00048          // depending on the "bumpside" avoid the bump
00049          if (bp.IsBumped(SCORPION_BUMP_BNE) && bp.IsBumped(SCORPION_BUMP_BNW)) 
00050          { // both bumped
00051             printf("Both bumpsensors bumped \n");
00052             pp.SetSpeed(-move_speed, 0);              // go backwards
00053             nanosleep(&move_sleep, NULL);             
00054             nanosleep(&move_sleep, NULL);             
00055             pp.SetSpeed(0, 0);                              // stop
00056          } 
00057          else if (bp.IsBumped(SCORPION_BUMP_BNW))  
00058          {  //left bump
00059             printf("Left bump!\n");
00060             pp.SetSpeed(-move_speed, 0);              // go backwards
00061             nanosleep(&move_sleep, NULL);
00062             pp.SetSpeed(-move_speed, -turn_rate); // go backwards turning left 
00063             nanosleep(&turn_sleep, NULL);
00064             pp.SetSpeed(0, 0);                              // stop
00065          } 
00066          else if (bp.IsBumped(SCORPION_BUMP_BNE)) 
00067          { // right bump
00068             printf("Right bump!\n");
00069             pp.SetSpeed(-move_speed, 0);              // go backwards
00070             nanosleep(&move_sleep, NULL);             
00071             pp.SetSpeed(-move_speed, turn_rate);   // go backwards turning right
00072             nanosleep(&turn_sleep, NULL);
00073             pp.SetSpeed(0, 0);                              // stop
00074          }
00075       }
00076    }
00077     catch (PlayerCc::PlayerError e)
00078    {
00079     std::cerr << e << std::endl;
00080     return -1;
00081    }
00082 }

Generated on Sat Jan 13 03:11:09 2007 for ERSP Player driver by  doxygen 1.5.1