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
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043 class Joydrive : public ArASyncTask
00044 {
00045 public:
00046
00047 Joydrive(ArRobot *robot);
00048
00049 ~Joydrive(void) {}
00050
00051
00052
00053 virtual void * runThread(void *arg);
00054
00055 protected:
00056
00057 ArJoyHandler myJoyHandler;
00058
00059 ArRobot *myRobot;
00060 };
00061
00062
00063 Joydrive::Joydrive(ArRobot *robot)
00064 {
00065 setThreadName("Joydrive");
00066
00067 myRobot = robot;
00068
00069 myJoyHandler.init();
00070
00071 myJoyHandler.setSpeeds(40, 700);
00072
00073
00074 if (myJoyHandler.haveJoystick())
00075 {
00076 printf("Have a joystick\n\n");
00077 }
00078
00079 else
00080 {
00081 printf("Do not have a joystick, set up the joystick then rerun the program\n\n");
00082 Aria::shutdown();
00083 exit(0);
00084 }
00085
00086
00087 create();
00088 }
00089
00090
00091 void *Joydrive::runThread(void *arg)
00092 {
00093 threadStarted();
00094
00095 int trans, rot;
00096
00097
00098
00099 while (myRunning)
00100 {
00101
00102 myRobot->lock();
00103 if (!myRobot->isConnected())
00104 {
00105 myRobot->unlock();
00106 break;
00107 }
00108
00109 printf("\rx %6.1f y %6.1f tth %6.1f vel %7.1f mpacs %3d ",
00110 myRobot->getX(), myRobot->getY(), myRobot->getTh(),
00111 myRobot->getVel(), myRobot->getMotorPacCount());
00112 fflush(stdout);
00113
00114 if (myJoyHandler.haveJoystick() && (myJoyHandler.getButton(1) ||
00115 myJoyHandler.getButton(2)))
00116 {
00117
00118 myJoyHandler.getAdjusted(&rot, &trans);
00119
00120 myRobot->setVel(trans);
00121 myRobot->setRotVel(-rot);
00122 }
00123
00124 else
00125 {
00126 myRobot->setVel(0);
00127 myRobot->setRotVel(0);
00128 }
00129
00130 myRobot->unlock();
00131
00132 ArUtil::sleep(50);
00133 }
00134
00135 return NULL;
00136 }
00137
00138
00139
00140
00141
00142 class ConnHandler
00143 {
00144 public:
00145
00146 ConnHandler(ArRobot *robot, Joydrive *jd);
00147
00148 ~ConnHandler(void) {}
00149
00150 void connected(void);
00151
00152 void connFail(void);
00153
00154 void disconnected(void);
00155 protected:
00156
00157 ArRobot *myRobot;
00158
00159 Joydrive *myJoydrive;
00160
00161 ArFunctorC<ConnHandler> *myConnectedCB;
00162 ArFunctorC<ConnHandler> *myConnFailCB;
00163 ArFunctorC<ConnHandler> *myDisconnectedCB;
00164
00165 };
00166
00167
00168 ConnHandler::ConnHandler(ArRobot *robot, Joydrive *jd)
00169 {
00170
00171 myRobot = robot;
00172 myJoydrive = jd;
00173
00174
00175 myConnectedCB = new ArFunctorC<ConnHandler>(this, &ConnHandler::connected);
00176 myRobot->addConnectCB(myConnectedCB, ArListPos::FIRST);
00177 myConnFailCB = new ArFunctorC<ConnHandler>(this, &ConnHandler::connFail);
00178 myRobot->addFailedConnectCB(myConnFailCB, ArListPos::FIRST);
00179 myDisconnectedCB = new ArFunctorC<ConnHandler>(this,
00180 &ConnHandler::disconnected);
00181 myRobot->addDisconnectNormallyCB(myDisconnectedCB, ArListPos::FIRST);
00182 myRobot->addDisconnectOnErrorCB(myDisconnectedCB, ArListPos::FIRST);
00183 }
00184
00185
00186
00187 void ConnHandler::connected(void)
00188 {
00189 myRobot->comInt(ArCommands::SONAR, 0);
00190 myRobot->comInt(ArCommands::ENABLE, 1);
00191 myRobot->comInt(ArCommands::SOUNDTOG, 0);
00192 }
00193
00194
00195 void ConnHandler::connFail(void)
00196 {
00197 printf("Failed to connect.\n");
00198 myRobot->stopRunning();
00199 myJoydrive->stopRunning();
00200 Aria::shutdown();
00201 return;
00202 }
00203
00204
00205 void ConnHandler::disconnected(void)
00206 {
00207 printf("Lost connection\n");
00208 myRobot->stopRunning();
00209 myJoydrive->stopRunning();
00210 Aria::shutdown();
00211 return;
00212 }
00213
00214 int main(int argc, char **argv)
00215 {
00216 std::string str;
00217 int ret;
00218
00219
00220 ArTcpConnection con;
00221
00222 ArRobot robot;
00223
00224
00225 Joydrive joyd(&robot);
00226
00227
00228 ConnHandler ch(&robot, &joyd);
00229
00230
00231 Aria::init(Aria::SIGHANDLE_THREAD);
00232
00233
00234 if ((ret = con.open()) != 0)
00235 {
00236 str = con.getOpenMessage(ret);
00237 printf("Open failed: %s\n", str.c_str());
00238 Aria::shutdown();
00239 return 1;
00240 }
00241
00242
00243
00244 robot.setDeviceConnection(&con);
00245
00246
00247 robot.runAsync(false);
00248
00249
00250
00251 if (!robot.asyncConnect())
00252 {
00253 printf(
00254 "asyncConnect failed because robot is not running in its own thread.\n");
00255 Aria::shutdown();
00256 return 1;
00257 }
00258
00259
00260 printf("Waiting for the robot's run to exit.\n");
00261 robot.waitForRunExit();
00262
00263 printf("exiting main\n");
00264 Aria::shutdown();
00265 return 0;
00266 }
00267
00268