00001 /* 00002 ActivMedia Robotics Interface for Applications (ARIA) 00003 Copyright (C) 2004,2005 ActivMedia Robotics, LLC 00004 00005 00006 This program is free software; you can redistribute it and/or modify 00007 it under the terms of the GNU General Public License as published by 00008 the Free Software Foundation; either version 2 of the License, or 00009 (at your option) any later version. 00010 00011 This program is distributed in the hope that it will be useful, 00012 but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 GNU General Public License for more details. 00015 00016 You should have received a copy of the GNU General Public License 00017 along with this program; if not, write to the Free Software 00018 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00019 00020 If you wish to redistribute ARIA under different terms, contact 00021 ActivMedia Robotics for information about a commercial version of ARIA at 00022 robots@activmedia.com or 00023 ActivMedia Robotics, 19 Columbia Drive, Amherst, NH 03031; 800-639-9481 00024 00025 */ 00026 00027 #include "ArExport.h" 00028 #include "ariaOSDef.h" 00029 #include "ArAMPTU.h" 00030 #include "ArCommands.h" 00031 #include "ArLog.h" 00032 #include "ArRobot.h" 00033 00034 ArAMPTUPacket::ArAMPTUPacket(ArTypes::UByte2 bufferSize) : 00035 ArBasePacket(bufferSize, 3) 00036 { 00037 myUnitNumber = 0; 00038 } 00039 00040 ArAMPTUPacket::~ArAMPTUPacket() 00041 { 00042 00043 } 00044 00045 void ArAMPTUPacket::byteToBuf(ArTypes::Byte val) 00046 { 00047 if (myLength + 1 > myMaxLength) 00048 { 00049 ArLog::log(ArLog::Terse, "ArAMPTUPacket::uByteToBuf: Trying to add beyond length of buffer."); 00050 return; 00051 } 00052 myBuf[myLength] = val; 00053 ++myLength; 00054 } 00055 00056 void ArAMPTUPacket::byte2ToBuf(ArTypes::Byte2 val) 00057 { 00058 if ((myLength + 2) > myMaxLength) 00059 { 00060 ArLog::log(ArLog::Terse, "ArAMPTUPacket::Byte2ToBuf: Trying to add beyond length of buffer."); 00061 return; 00062 } 00063 myBuf[myLength] = val/255;//(val & 0xff00) >> 8; 00064 ++myLength; 00065 myBuf[myLength] = val%255;//(val & 0x00ff); 00066 ++myLength; 00067 } 00068 00069 void ArAMPTUPacket::finalizePacket(void) 00070 { 00071 int length = myLength; 00072 myLength = 0; 00073 byteToBuf('P'); 00074 byteToBuf('T'); 00075 byteToBuf('0' + myUnitNumber); 00076 myLength = length; 00077 } 00078 00085 unsigned char ArAMPTUPacket::getUnitNumber(void) 00086 { 00087 return myUnitNumber; 00088 } 00089 00097 bool ArAMPTUPacket::setUnitNumber(unsigned char unitNumber) 00098 { 00099 if (unitNumber > 7) 00100 return false; 00101 00102 myUnitNumber = unitNumber; 00103 return true; 00104 } 00105 00110 ArAMPTU::ArAMPTU(ArRobot *robot, int unitNumber) : 00111 ArPTZ(robot) 00112 { 00113 myRobot = robot; 00114 myPanSlew = 0; 00115 myTiltSlew = 0; 00116 myPan = 0; 00117 myTilt = 0; 00118 myUnitNumber = unitNumber; 00119 } 00120 00121 ArAMPTU::~ArAMPTU() 00122 { 00123 } 00124 00125 bool ArAMPTU::init(void) 00126 { 00127 if (!myPacket.setUnitNumber(myUnitNumber)) 00128 { 00129 ArLog::log(ArLog::Terse, "ArAMPTU::init: the unit number is invalid."); 00130 return false; 00131 } 00132 myPacket.empty(); 00133 myPacket.byteToBuf(ArAMPTUCommands::INIT); 00134 if (!sendPacket(&myPacket)) 00135 return false; 00136 00137 myPacket.empty(); 00138 myPacket.byteToBuf(ArAMPTUCommands::RESP); 00139 myPacket.byteToBuf(0); 00140 if (!sendPacket(&myPacket)) 00141 return false; 00142 00143 if (!panTilt(0, 0)) 00144 return false; 00145 00146 return true; 00147 } 00148 00149 bool ArAMPTU::pan(int deg) 00150 { 00151 if (deg > getMaxPosPan()) 00152 deg = getMaxPosPan(); 00153 if (deg < getMaxNegPan()) 00154 deg = getMaxNegPan(); 00155 00156 myPacket.empty(); 00157 myPacket.byteToBuf(ArAMPTUCommands::ABSPAN); 00158 myPacket.byte2ToBuf(deg + (getMaxPosPan() - getMaxNegPan())/2); 00159 00160 myPan = deg; 00161 return sendPacket(&myPacket); 00162 } 00163 00164 bool ArAMPTU::panRel(int deg) 00165 { 00166 if (deg + myPan > getMaxPosPan()) 00167 deg = getMaxPosPan() - myPan; 00168 if (deg + myPan < getMaxNegPan()) 00169 deg = getMaxNegPan() - myPan; 00170 00171 myPan += deg; 00172 myPacket.empty(); 00173 00174 if (deg >= 0) 00175 myPacket.byteToBuf(ArAMPTUCommands::RELPANCW); 00176 else 00177 myPacket.byteToBuf(ArAMPTUCommands::RELPANCCW); 00178 00179 myPacket.byte2ToBuf(abs(deg)); 00180 00181 return sendPacket(&myPacket); 00182 } 00183 00184 bool ArAMPTU::tilt(int deg) 00185 { 00186 if (deg > getMaxPosTilt()) 00187 deg = getMaxPosTilt(); 00188 if (deg < getMaxNegTilt()) 00189 deg = getMaxNegTilt(); 00190 00191 myPacket.empty(); 00192 myPacket.byteToBuf(ArAMPTUCommands::ABSTILT); 00193 myPacket.byteToBuf(deg + (getMaxPosTilt() - getMaxNegTilt())/2); 00194 00195 myTilt = deg; 00196 return sendPacket(&myPacket); 00197 } 00198 00199 bool ArAMPTU::tiltRel(int deg) 00200 { 00201 if (deg + myTilt > getMaxPosTilt()) 00202 deg = getMaxPosTilt() - myTilt; 00203 if (deg + myTilt < getMaxNegTilt()) 00204 deg = getMaxNegTilt() - myTilt; 00205 00206 myTilt += deg; 00207 myPacket.empty(); 00208 00209 if (deg >= 0) 00210 myPacket.byteToBuf(ArAMPTUCommands::RELTILTU); 00211 else 00212 myPacket.byteToBuf(ArAMPTUCommands::RELTILTD); 00213 00214 myPacket.byteToBuf(abs(deg)); 00215 00216 return sendPacket(&myPacket); 00217 } 00218 00219 bool ArAMPTU::panTilt(int panDeg, int tiltDeg) 00220 { 00221 if (panDeg > getMaxPosPan()) 00222 panDeg = getMaxPosPan(); 00223 if (panDeg < getMaxNegPan()) 00224 panDeg = getMaxNegPan(); 00225 00226 if (tiltDeg > getMaxPosTilt()) 00227 tiltDeg = getMaxPosTilt(); 00228 if (tiltDeg < getMaxNegTilt()) 00229 tiltDeg = getMaxNegTilt(); 00230 00231 if (myPan - panDeg == 0 && myTilt - tiltDeg == 0) 00232 return true; 00233 if (myPan - panDeg == 0) 00234 return tilt(tiltDeg); 00235 if (myTilt - tiltDeg == 0) 00236 return pan(panDeg); 00237 myPan = panDeg; 00238 myTilt = tiltDeg; 00239 00240 00241 00242 myPacket.empty(); 00243 myPacket.byteToBuf(ArAMPTUCommands::PANTILT); 00244 myPacket.byte2ToBuf(myPan + (getMaxPosPan() - getMaxNegPan())/2); 00245 myPacket.byteToBuf(myTilt + (getMaxPosTilt() - getMaxNegTilt())/2); 00246 return sendPacket(&myPacket); 00247 } 00248 00249 bool ArAMPTU::panTiltRel(int panDeg, int tiltDeg) 00250 { 00251 if (panDeg + myPan > getMaxPosPan()) 00252 panDeg = getMaxPosPan() - myPan; 00253 if (panDeg + myPan < getMaxNegPan()) 00254 panDeg = getMaxNegPan() - myPan; 00255 00256 if (tiltDeg + myTilt > getMaxPosTilt()) 00257 tiltDeg = getMaxPosTilt() - myTilt; 00258 if (tiltDeg + myTilt < getMaxNegTilt()) 00259 tiltDeg = getMaxNegTilt() - myTilt; 00260 00261 myPan += panDeg; 00262 myTilt += tiltDeg; 00263 00264 if (panDeg == 0 && tiltDeg == 0) 00265 return true; 00266 if (panDeg == 0) 00267 return tiltRel(tiltDeg); 00268 if (tiltDeg == 0) 00269 return panRel(panDeg); 00270 00271 myPacket.empty(); 00272 if (panDeg >= 0 && tiltDeg >= 0) 00273 myPacket.byteToBuf(ArAMPTUCommands::PANTILTUCW); 00274 else if (panDeg >= 0 && tiltDeg < 0) 00275 myPacket.byteToBuf(ArAMPTUCommands::PANTILTDCW); 00276 else if (panDeg < 0 && tiltDeg >= 0) 00277 myPacket.byteToBuf(ArAMPTUCommands::PANTILTUCCW); 00278 else 00279 myPacket.byteToBuf(ArAMPTUCommands::PANTILTDCCW); 00280 00281 myPacket.byte2ToBuf(abs(panDeg)); 00282 myPacket.byte2ToBuf(abs(tiltDeg)); 00283 00284 return sendPacket(&myPacket); 00285 } 00286 00287 bool ArAMPTU::panSlew(int deg) 00288 { 00289 if (deg > MAX_PAN_SLEW) 00290 deg = MAX_PAN_SLEW; 00291 if (deg < MIN_SLEW) 00292 deg = MIN_SLEW; 00293 00294 myPanSlew = deg; 00295 myPacket.empty(); 00296 myPacket.byteToBuf(ArAMPTUCommands::PANSLEW); 00297 myPacket.byteToBuf((int)(256 - (3840 / (float)deg))); 00298 return sendPacket(&myPacket); 00299 } 00300 00301 bool ArAMPTU::tiltSlew(int deg) 00302 { 00303 if (deg > MAX_TILT_SLEW) 00304 deg = MAX_TILT_SLEW; 00305 if (deg < MIN_SLEW) 00306 deg = MIN_SLEW; 00307 00308 myTiltSlew = deg; 00309 myPacket.empty(); 00310 myPacket.byteToBuf(ArAMPTUCommands::TILTSLEW); 00311 myPacket.byteToBuf((int)(256 - (3840 / (float)deg))); 00312 return sendPacket(&myPacket); 00313 } 00314 00315 bool ArAMPTU::pause(void) 00316 { 00317 myPacket.empty(); 00318 myPacket.byteToBuf(ArAMPTUCommands::PAUSE); 00319 return sendPacket(&myPacket); 00320 } 00321 00322 bool ArAMPTU::resume(void) 00323 { 00324 myPacket.empty(); 00325 myPacket.byteToBuf(ArAMPTUCommands::CONT); 00326 return sendPacket(&myPacket); 00327 } 00328 00329 bool ArAMPTU::purge(void) 00330 { 00331 myPacket.empty(); 00332 myPacket.byteToBuf(ArAMPTUCommands::PURGE); 00333 return sendPacket(&myPacket); 00334 } 00335 00336 bool ArAMPTU::requestStatus(void) 00337 { 00338 myPacket.empty(); 00339 myPacket.byteToBuf(ArAMPTUCommands::STATUS); 00340 return sendPacket(&myPacket); 00341 } 00342