00001 /*!@file GumbotI.cpp Gumbot service implimantation */ 00002 00003 //////////////////////////////////////////////////////////////////// // 00004 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2001 by the // 00005 // University of Southern California (USC) and the iLab at USC. // 00006 // See http://iLab.usc.edu for information about this project. // 00007 // //////////////////////////////////////////////////////////////////// // 00008 // Major portions of the iLab Neuromorphic Vision Toolkit are protected // 00009 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency // 00010 // in Visual Environments, and Applications'' by Christof Koch and // 00011 // Laurent Itti, California Institute of Technology, 2001 (patent // 00012 // pending; application number 09/912,225 filed July 23, 2001; see // 00013 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status). // 00014 // //////////////////////////////////////////////////////////////////// // 00015 // This file is part of the iLab Neuromorphic Vision C++ Toolkit. // 00016 // // 00017 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can // 00018 // redistribute it and/or modify it under the terms of the GNU General // 00019 // Public License as published by the Free Software Foundation; either // 00020 // version 2 of the License, or (at your option) any later version. // 00021 // // 00022 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope // 00023 // that it will be useful, but WITHOUT ANY WARRANTY; without even the // 00024 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR // 00025 // PURPOSE. See the GNU General Public License for more details. // 00026 // // 00027 // You should have received a copy of the GNU General Public License // 00028 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write // 00029 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, // 00030 // Boston, MA 02111-1307 USA. // 00031 // //////////////////////////////////////////////////////////////////// // 00032 // 00033 // Primary maintainer for this file: Lior Elazary <lelazary@yahoo.com> 00034 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/Robots/Gumbot/GumbotService/GumbotI.cpp $ 00035 // $Id: GumbotI.cpp 10794 2009-02-08 06:21:09Z itti $ 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 //start the serial device 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; //drive command 00127 cmd[1] = ((short int)itsCurrentSpeed&0xFFFF)>>8; //velocity high byte 00128 cmd[2] = ((short int)itsCurrentSpeed&0xFF); //velocity low byte 00129 00130 if (itsCurrentSteering == 0) //drive striaght 00131 { 00132 cmd[3] = 0x7F; //Radius high byte 00133 cmd[4] = 0xFF; //Radius low byte 00134 } else { 00135 cmd[3] = ((short int)itsCurrentSteering&0xFFFF)>>8; //Radius high byte 00136 cmd[4] = ((short int)itsCurrentSteering&0xFF); //Radius low byte 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; //Advance LED 00205 case 2: cmd[1] = 2; break; //Play Led 00206 case 3: cmd[1] = 10; break; //Both Led 00207 default: cmd[1] = 0; //default to turn off 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 // The imperial March Song 00231 // G G G E Bb G E Bb G 2D 2D 2D 2Eb Bb F# E Bb G 00232 // 55 55 55 52 58 55 52 58 55 62 62 62 63 58 54 51 58 55 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 //unsigned char s2[21] = {140, 0, 9, 55, 30, 55, 30, 55, 30, 51, 30, 58, 12, 55, 30, 51, 30, 58, 12, 55, 30}; 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 //sleep(4); 00245 //cmd[0] = 141; cmd[1] = 1; 00246 //sendData(itsSerialFd, cmd, 2); 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