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
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 #include <stdlib.h>
00039 #include <time.h>
00040 #include <iostream>
00041 #include <unistd.h>
00042 #include <fcntl.h>
00043 #include <sys/types.h>
00044 #include <sys/stat.h>
00045 #include <signal.h>
00046 #include <GumbotI.h>
00047 #include "capture.h"
00048 #include "serial.h"
00049
00050 #include <IceE/IceE.h>
00051
00052 GumbotI::GumbotI(int debug) :
00053 itsCurrentSpeed(0),
00054 itsCurrentSteering(0),
00055 itsDebug(debug)
00056 {
00057
00058 itsSerialFd = openPort("/dev/ttyS2");
00059
00060 colorspace_init();
00061 open_device ();
00062 init_device (0);
00063 start_capturing();
00064
00065 if (itsDebug)
00066 printf("Gumbot initalized\n");
00067 }
00068
00069 GumbotI::~GumbotI() {
00070 closePort(itsSerialFd);
00071 }
00072
00073
00074 float GumbotI::getSpeed(const Ice::Current&){
00075 return itsCurrentSpeed;
00076 }
00077
00078
00079 short GumbotI::setSpeed(const float speed, const Ice::Current&){
00080 if (itsDebug)
00081 printf("Setting speed to %f\n", speed);
00082 itsCurrentSpeed = speed;
00083 sendDriveCommand();
00084
00085 return 0;
00086 }
00087
00088
00089
00090 float GumbotI::getSteering(const Ice::Current&){
00091 return itsCurrentSteering;
00092 }
00093
00094
00095
00096 short GumbotI::setSteering(const float steeringPos, const Ice::Current&){
00097 if (itsDebug)
00098 printf("Setting steering to %f\n", steeringPos);
00099 itsCurrentSteering = steeringPos;
00100 sendDriveCommand();
00101 return 0;
00102 }
00103
00104
00105
00106 ImageIceMod::ImageIce GumbotI::getImageSensor(const short i, const Ice::Current&){
00107 frame* f = get_frame();
00108 int size = f->width*f->height*3;
00109
00110 ImageIceMod::ImageIce imgRet;
00111 imgRet.width = f->width;
00112 imgRet.height = f->height;
00113 imgRet.pixSize = 3;
00114
00115 imgRet.data.resize(size);
00116 std::copy(f->data, f->data + size, imgRet.data.begin());
00117
00118 return imgRet;
00119 }
00120
00121
00122
00123 void GumbotI::sendDriveCommand()
00124 {
00125 unsigned char cmd[5];
00126 cmd[0] = 137;
00127 cmd[1] = ((short int)itsCurrentSpeed&0xFFFF)>>8;
00128 cmd[2] = ((short int)itsCurrentSpeed&0xFF);
00129
00130 if (itsCurrentSteering == 0)
00131 {
00132 cmd[3] = 0x7F;
00133 cmd[4] = 0xFF;
00134 } else {
00135 cmd[3] = ((short int)itsCurrentSteering&0xFFFF)>>8;
00136 cmd[4] = ((short int)itsCurrentSteering&0xFF);
00137 }
00138
00139
00140 if (itsDebug)
00141 {
00142 printf("Sending: ");
00143 for(int i=0; i<5; i++)
00144 printf("%i ", cmd[i]);
00145 printf("\n");
00146 }
00147 sendData(itsSerialFd, cmd, 5);
00148
00149 }
00150
00151
00152 void GumbotI::sendStart(const Ice::Current&)
00153 {
00154 unsigned char cmd[1];
00155 cmd[0] = 128;
00156 sendData(itsSerialFd, cmd, 1);
00157 }
00158
00159
00160 void GumbotI::setMode(const Robots::GumbotModes mode, const Ice::Current&)
00161 {
00162 unsigned char cmd[1];
00163 cmd[0] = 0;
00164
00165 switch(mode)
00166 {
00167 case Robots::SafeMode: cmd[0] = 131; break;
00168 case Robots::FullMode: cmd[0] = 132; break;
00169 case Robots::SpotMode: cmd[0] = 134; break;
00170 case Robots::CoverMode: cmd[0] = 135; break;
00171 case Robots::CoverAndDockMode: cmd[0] = 143; break;
00172 };
00173
00174 sendData(itsSerialFd, cmd, 1);
00175
00176 }
00177
00178
00179 void GumbotI::setDemo(const short demo, const Ice::Current&)
00180 {
00181 unsigned char cmd[2];
00182 cmd[0] = 136;
00183 cmd[1] = demo;
00184
00185 if (itsDebug)
00186 {
00187 printf("Sending: ");
00188 for(int i=0; i<2; i++)
00189 printf("%i ", cmd[i]);
00190 printf("\n");
00191 }
00192 sendData(itsSerialFd, cmd, 2);
00193
00194 }
00195
00196
00197 void GumbotI::setLED(const short led, const short color, const short intensity, const Ice::Current&)
00198 {
00199 unsigned char cmd[4];
00200 cmd[0] = 139;
00201
00202 switch(led)
00203 {
00204 case 1: cmd[1] = 8; break;
00205 case 2: cmd[1] = 2; break;
00206 case 3: cmd[1] = 10; break;
00207 default: cmd[1] = 0;
00208 };
00209
00210 cmd[2] = color;
00211 cmd[3] = intensity;
00212
00213 if (itsDebug)
00214 {
00215 printf("Sending: ");
00216 for(int i=0; i<2; i++)
00217 printf("%i ", cmd[i]);
00218 printf("\n");
00219 }
00220 sendData(itsSerialFd, cmd, 4);
00221
00222 }
00223
00224
00225 void GumbotI::playSong(const short song, const Ice::Current&)
00226 {
00227
00228 if (itsDebug)
00229 printf("Play song %i\n", song);
00230
00231
00232
00233 unsigned char s1[21] = {140, 0, 9, 55, 30, 55, 30, 55, 30, 51, 30, 58, 12, 55, 30, 51, 30, 58, 12, 55, 30};
00234
00235 unsigned char s2[21] = {140, 1, 9, 62, 30, 62, 30, 62, 30, 63, 30, 58, 12, 54, 30, 51, 30, 58, 12, 55, 30};
00236
00237 unsigned char cmd[2] ;
00238
00239 sendData(itsSerialFd, s1, 21);
00240 sendData(itsSerialFd, s2, 21);
00241
00242 cmd[0] = 141; cmd[1] = song;
00243 sendData(itsSerialFd, cmd, 2);
00244
00245
00246
00247 }
00248
00249
00250 ImageIceMod::DimsIce GumbotI::getImageSensorDims(const short i, const Ice::Current&) {
00251 ImageIceMod::DimsIce dims;
00252 dims.w = -1;
00253 dims.h = -1;
00254
00255 return dims;
00256 }
00257
00258
00259 short GumbotI::getSensorValue(const short i, const Ice::Current&) {
00260 return -1;
00261 }
00262
00263
00264 void GumbotI::motorsOff(const short i, const Ice::Current&)
00265 {
00266
00267 }
00268
00269
00270 void GumbotI::setMotor(const short i, const float val, const Ice::Current&)
00271 {
00272
00273 }
00274
00275
00276 short GumbotI::sendRawCmd(const std::string& s, const Ice::Current&)
00277 {
00278 return -1;
00279 }
00280
00281
00282 void GumbotI::shutdown(const Ice::Current& c)
00283 {
00284 if (itsDebug)
00285 printf("Shutting down...\n");
00286 c.adapter->getCommunicator()->shutdown();
00287 }
00288