00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #include "Aria.h"
00027
00041
00042
00043
00044
00045
00046
00047 class ConnHandler
00048 {
00049 public:
00050
00051 ConnHandler(ArRobot *robot);
00052
00053 ~ConnHandler(void) {}
00054
00055 void connected(void);
00056
00057 void connFail(void);
00058
00059 void disconnected(void);
00060 protected:
00061
00062 ArRobot *myRobot;
00063
00064 ArFunctorC<ConnHandler> myConnectedCB;
00065 ArFunctorC<ConnHandler> myConnFailCB;
00066 ArFunctorC<ConnHandler> myDisconnectedCB;
00067 };
00068
00069
00070
00071
00072
00073 ConnHandler::ConnHandler(ArRobot *robot) :
00074 myConnectedCB(this, &ConnHandler::connected),
00075 myConnFailCB(this, &ConnHandler::connFail),
00076 myDisconnectedCB(this, &ConnHandler::disconnected)
00077
00078 {
00079
00080 myRobot = robot;
00081
00082
00083 myRobot->addConnectCB(&myConnectedCB, ArListPos::FIRST);
00084 myRobot->addFailedConnectCB(&myConnFailCB, ArListPos::FIRST);
00085 myRobot->addDisconnectNormallyCB(&myDisconnectedCB, ArListPos::FIRST);
00086 myRobot->addDisconnectOnErrorCB(&myDisconnectedCB, ArListPos::FIRST);
00087 }
00088
00089
00090 void ConnHandler::connFail(void)
00091 {
00092 printf("Failed to connect.\n");
00093 myRobot->stopRunning();
00094 Aria::shutdown();
00095 return;
00096 }
00097
00098
00099 void ConnHandler::connected(void)
00100 {
00101 printf("Connected\n");
00102 myRobot->comInt(ArCommands::SONAR, 0);
00103 myRobot->comInt(ArCommands::ENABLE, 1);
00104 myRobot->comInt(ArCommands::SOUNDTOG, 0);
00105 }
00106
00107
00108 void ConnHandler::disconnected(void)
00109 {
00110 printf("Lost connection\n");
00111 exit(0);
00112 }
00113
00114
00115
00116 int main(int argc, char **argv)
00117 {
00118 std::string str;
00119 int ret;
00120 ArTime start;
00121
00122
00123 ArTcpConnection con;
00124
00125 ArRobot robot;
00126
00127 ConnHandler ch(&robot);
00128
00129
00130 Aria::init(Aria::SIGHANDLE_THREAD);
00131
00132
00133 if ((ret = con.open()) != 0)
00134 {
00135 str = con.getOpenMessage(ret);
00136 printf("Open failed: %s\n", str.c_str());
00137 Aria::shutdown();
00138 return 1;
00139 }
00140
00141
00142 robot.setDeviceConnection(&con);
00143
00144 if (!robot.blockingConnect())
00145 {
00146
00147
00148 printf(
00149 "asyncConnect failed because robot is not running in its own thread.\n");
00150 Aria::shutdown();
00151 return 1;
00152 }
00153
00154 robot.runAsync(false);
00155
00156
00157
00158
00159 printf("Setting rot velocity to 100 deg/sec then sleeping 3 seconds\n");
00160 robot.lock();
00161 robot.setRotVel(100);
00162 robot.unlock();
00163 ArUtil::sleep(3*1000);
00164 printf("Stopping\n");
00165 robot.lock();
00166 robot.setRotVel(0);
00167 robot.unlock();
00168 ArUtil::sleep(200);
00169
00170 printf("Telling the robot to go 300 mm on left wheel and 100 mm on right wheel for 5 seconds\n");
00171 robot.lock();
00172 robot.setVel2(300, 100);
00173 robot.unlock();
00174 start.setToNow();
00175 while (1)
00176 {
00177 robot.lock();
00178 if (start.mSecSince() > 5000)
00179 {
00180 robot.unlock();
00181 break;
00182 }
00183 robot.unlock();
00184 ArUtil::sleep(50);
00185 }
00186
00187 printf("Telling the robot to move forwards one meter, then sleeping 5 seconds\n");
00188 robot.lock();
00189 robot.move(1000);
00190 robot.unlock();
00191 start.setToNow();
00192 while (1)
00193 {
00194 robot.lock();
00195 if (robot.isMoveDone())
00196 {
00197 printf("Finished distance\n");
00198 robot.unlock();
00199 break;
00200 }
00201 if (start.mSecSince() > 5000)
00202 {
00203 printf("Distance timed out\n");
00204 robot.unlock();
00205 break;
00206 }
00207 robot.unlock();
00208 ArUtil::sleep(50);
00209 }
00210 printf("Telling the robot to move backwards one meter, then sleeping 5 seconds\n");
00211 robot.lock();
00212 robot.move(-1000);
00213 robot.unlock();
00214 start.setToNow();
00215 while (1)
00216 {
00217 robot.lock();
00218 if (robot.isMoveDone())
00219 {
00220 printf("Finished distance\n");
00221 robot.unlock();
00222 break;
00223 }
00224 if (start.mSecSince() > 10000)
00225 {
00226 printf("Distance timed out\n");
00227 robot.unlock();
00228 break;
00229 }
00230 robot.unlock();
00231 ArUtil::sleep(50);
00232 }
00233 printf("Telling the robot to turn to 180, then sleeping 4 seconds\n");
00234 robot.lock();
00235 robot.setHeading(180);
00236 robot.unlock();
00237 start.setToNow();
00238 while (1)
00239 {
00240 robot.lock();
00241 if (robot.isHeadingDone(5))
00242 {
00243 printf("Finished turn\n");
00244 robot.unlock();
00245 break;
00246 }
00247 if (start.mSecSince() > 5000)
00248 {
00249 printf("Turn timed out\n");
00250 robot.unlock();
00251 break;
00252 }
00253 robot.unlock();
00254 ArUtil::sleep(100);
00255 }
00256 printf("Telling the robot to turn to 90, then sleeping 2 seconds\n");
00257 robot.lock();
00258 robot.setHeading(90);
00259 robot.unlock();
00260 start.setToNow();
00261 while (1)
00262 {
00263 robot.lock();
00264 if (robot.isHeadingDone(5))
00265 {
00266 printf("Finished turn\n");
00267 robot.unlock();
00268 break;
00269 }
00270 if (start.mSecSince() > 5000)
00271 {
00272 printf("turn timed out\n");
00273 robot.unlock();
00274 break;
00275 }
00276 robot.unlock();
00277 ArUtil::sleep(100);
00278 }
00279 printf("Setting vel2 to 200 mm/sec on both wheels, then sleeping 3 seconds\n");
00280 robot.lock();
00281 robot.setVel2(200, 200);
00282 robot.unlock();
00283 ArUtil::sleep(3000);
00284 printf("Stopping the robot, then sleeping for 2 seconds\n");
00285 robot.lock();
00286 robot.stop();
00287 robot.unlock();
00288 ArUtil::sleep(2000);
00289 printf("Setting velocity to 200 mm/sec then sleeping 3 seconds\n");
00290 robot.lock();
00291 robot.setVel(200);
00292 robot.unlock();
00293 ArUtil::sleep(3000);
00294 printf("Stopping the robot, then sleeping for 2 seconds\n");
00295 robot.lock();
00296 robot.stop();
00297 robot.unlock();
00298 ArUtil::sleep(2000);
00299 printf("Setting vel2 with 0 on left wheel, 200 mm/sec on right, then sleeping 5 seconds\n");
00300 robot.lock();
00301 robot.setVel2(0, 200);
00302 robot.unlock();
00303 ArUtil::sleep(5000);
00304 printf("Telling the robot to rotate at 50 deg/sec then sleeping 5 seconds\n");
00305 robot.lock();
00306 robot.setRotVel(50);
00307 robot.unlock();
00308 ArUtil::sleep(5000);
00309 printf("Telling the robot to rotate at -50 deg/sec then sleeping 5 seconds\n");
00310 robot.lock();
00311 robot.setRotVel(-50);
00312 robot.unlock();
00313 ArUtil::sleep(5000);
00314 printf("Setting vel2 with 0 on both wheels, then sleeping 3 seconds\n");
00315 robot.lock();
00316 robot.setVel2(0, 0);
00317 robot.unlock();
00318 ArUtil::sleep(3000);
00319 printf("Now having the robot change heading by -125 degrees, then sleeping for 6 seconds\n");
00320 robot.lock();
00321 robot.setDeltaHeading(-125);
00322 robot.unlock();
00323 ArUtil::sleep(6000);
00324 printf("Now having the robot change heading by 45 degrees, then sleeping for 6 seconds\n");
00325 robot.lock();
00326 robot.setDeltaHeading(45);
00327 robot.unlock();
00328 ArUtil::sleep(6000);
00329 printf("Setting vel2 with 200 on left wheel, 0 on right wheel, then sleeping 5 seconds\n");
00330 robot.lock();
00331 robot.setVel2(200, 0);
00332 robot.unlock();
00333 ArUtil::sleep(5000);
00334
00335 printf("Done with tests, exiting\n");
00336
00337 Aria::shutdown();
00338 return 0;
00339 }
00340