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 "ArExport.h"
00027 #include "ariaOSDef.h"
00028 #include "ArAMPTU.h"
00029 #include "ArCommands.h"
00030 #include "ArLog.h"
00031 #include "ArRobot.h"
00032
00033 AREXPORT ArAMPTUPacket::ArAMPTUPacket(ArTypes::UByte2 bufferSize) :
00034 ArBasePacket(bufferSize, 3)
00035 {
00036 myUnitNumber = 0;
00037 }
00038
00039 AREXPORT ArAMPTUPacket::~ArAMPTUPacket()
00040 {
00041
00042 }
00043
00044 AREXPORT void ArAMPTUPacket::byteToBuf(ArTypes::Byte val)
00045 {
00046 if (myLength + 1 > myMaxLength)
00047 {
00048 ArLog::log(ArLog::Terse, "ArAMPTUPacket::uByteToBuf: Trying to add beyond length of buffer.");
00049 return;
00050 }
00051 myBuf[myLength] = val;
00052 ++myLength;
00053 }
00054
00055 AREXPORT void ArAMPTUPacket::byte2ToBuf(ArTypes::Byte2 val)
00056 {
00057 if ((myLength + 2) > myMaxLength)
00058 {
00059 ArLog::log(ArLog::Terse, "ArAMPTUPacket::Byte2ToBuf: Trying to add beyond length of buffer.");
00060 return;
00061 }
00062 myBuf[myLength] = val/255;
00063 ++myLength;
00064 myBuf[myLength] = val%255;
00065 ++myLength;
00066 }
00067
00068 AREXPORT void ArAMPTUPacket::finalizePacket(void)
00069 {
00070 int length = myLength;
00071 myLength = 0;
00072 byteToBuf('P');
00073 byteToBuf('T');
00074 byteToBuf('0' + myUnitNumber);
00075 myLength = length;
00076 }
00077
00084 AREXPORT unsigned char ArAMPTUPacket::getUnitNumber(void)
00085 {
00086 return myUnitNumber;
00087 }
00088
00096 AREXPORT bool ArAMPTUPacket::setUnitNumber(unsigned char unitNumber)
00097 {
00098 if (unitNumber > 7)
00099 return false;
00100
00101 myUnitNumber = unitNumber;
00102 return true;
00103 }
00104
00109 AREXPORT ArAMPTU::ArAMPTU(ArRobot *robot, int unitNumber) :
00110 ArPTZ(robot)
00111 {
00112 myRobot = robot;
00113 myPanSlew = 0;
00114 myTiltSlew = 0;
00115 myPan = 0;
00116 myTilt = 0;
00117 myUnitNumber = unitNumber;
00118 }
00119
00120 AREXPORT ArAMPTU::~ArAMPTU()
00121 {
00122 }
00123
00124 AREXPORT bool ArAMPTU::init(void)
00125 {
00126 if (!myPacket.setUnitNumber(myUnitNumber))
00127 {
00128 ArLog::log(ArLog::Terse, "ArAMPTU::init: the unit number is invalid.");
00129 return false;
00130 }
00131 myPacket.empty();
00132 myPacket.byteToBuf(ArAMPTUCommands::INIT);
00133 if (!sendPacket(&myPacket))
00134 return false;
00135
00136 myPacket.empty();
00137 myPacket.byteToBuf(ArAMPTUCommands::RESP);
00138 myPacket.byteToBuf(0);
00139 if (!sendPacket(&myPacket))
00140 return false;
00141
00142 if (!panTilt(0, 0))
00143 return false;
00144
00145 return true;
00146 }
00147
00148 AREXPORT bool ArAMPTU::pan(double deg)
00149 {
00150 if (deg > getMaxPosPan())
00151 deg = getMaxPosPan();
00152 if (deg < getMaxNegPan())
00153 deg = getMaxNegPan();
00154
00155 myPacket.empty();
00156 myPacket.byteToBuf(ArAMPTUCommands::ABSPAN);
00157 myPacket.byte2ToBuf(ArMath::roundInt(deg +
00158 (getMaxPosPan() - getMaxNegPan())/2));
00159
00160 myPan = deg;
00161 return sendPacket(&myPacket);
00162 }
00163
00164 AREXPORT bool ArAMPTU::panRel(double 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(ArMath::roundInt(fabs(deg)));
00180
00181 return sendPacket(&myPacket);
00182 }
00183
00184 AREXPORT bool ArAMPTU::tilt(double 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(ArMath::roundInt(deg +
00194 (getMaxPosTilt() - getMaxNegTilt())/2));
00195
00196 myTilt = deg;
00197 return sendPacket(&myPacket);
00198 }
00199
00200 AREXPORT bool ArAMPTU::tiltRel(double deg)
00201 {
00202 if (deg + myTilt > getMaxPosTilt())
00203 deg = getMaxPosTilt() - myTilt;
00204 if (deg + myTilt < getMaxNegTilt())
00205 deg = getMaxNegTilt() - myTilt;
00206
00207 myTilt += deg;
00208 myPacket.empty();
00209
00210 if (deg >= 0)
00211 myPacket.byteToBuf(ArAMPTUCommands::RELTILTU);
00212 else
00213 myPacket.byteToBuf(ArAMPTUCommands::RELTILTD);
00214
00215 myPacket.byteToBuf(ArMath::roundInt(fabs(deg)));
00216
00217 return sendPacket(&myPacket);
00218 }
00219
00220 AREXPORT bool ArAMPTU::panTilt(double panDeg, double tiltDeg)
00221 {
00222 if (panDeg > getMaxPosPan())
00223 panDeg = getMaxPosPan();
00224 if (panDeg < getMaxNegPan())
00225 panDeg = getMaxNegPan();
00226
00227 if (tiltDeg > getMaxPosTilt())
00228 tiltDeg = getMaxPosTilt();
00229 if (tiltDeg < getMaxNegTilt())
00230 tiltDeg = getMaxNegTilt();
00231
00232 if (myPan - panDeg == 0 && myTilt - tiltDeg == 0)
00233 return true;
00234 if (myPan - panDeg == 0)
00235 return tilt(tiltDeg);
00236 if (myTilt - tiltDeg == 0)
00237 return pan(panDeg);
00238 myPan = panDeg;
00239 myTilt = tiltDeg;
00240
00241
00242
00243 myPacket.empty();
00244 myPacket.byteToBuf(ArAMPTUCommands::PANTILT);
00245 myPacket.byte2ToBuf(ArMath::roundInt(myPan +
00246 (getMaxPosPan() - getMaxNegPan())/2));
00247 myPacket.byteToBuf(ArMath::roundInt(myTilt + (getMaxPosTilt() -
00248 getMaxNegTilt())/2));
00249 return sendPacket(&myPacket);
00250 }
00251
00252 AREXPORT bool ArAMPTU::panTiltRel(double panDeg, double tiltDeg)
00253 {
00254 if (panDeg + myPan > getMaxPosPan())
00255 panDeg = getMaxPosPan() - myPan;
00256 if (panDeg + myPan < getMaxNegPan())
00257 panDeg = getMaxNegPan() - myPan;
00258
00259 if (tiltDeg + myTilt > getMaxPosTilt())
00260 tiltDeg = getMaxPosTilt() - myTilt;
00261 if (tiltDeg + myTilt < getMaxNegTilt())
00262 tiltDeg = getMaxNegTilt() - myTilt;
00263
00264 myPan += panDeg;
00265 myTilt += tiltDeg;
00266
00267 if (panDeg == 0 && tiltDeg == 0)
00268 return true;
00269 if (panDeg == 0)
00270 return tiltRel(tiltDeg);
00271 if (tiltDeg == 0)
00272 return panRel(panDeg);
00273
00274 myPacket.empty();
00275 if (panDeg >= 0 && tiltDeg >= 0)
00276 myPacket.byteToBuf(ArAMPTUCommands::PANTILTUCW);
00277 else if (panDeg >= 0 && tiltDeg < 0)
00278 myPacket.byteToBuf(ArAMPTUCommands::PANTILTDCW);
00279 else if (panDeg < 0 && tiltDeg >= 0)
00280 myPacket.byteToBuf(ArAMPTUCommands::PANTILTUCCW);
00281 else
00282 myPacket.byteToBuf(ArAMPTUCommands::PANTILTDCCW);
00283
00284 myPacket.byte2ToBuf(ArMath::roundInt(fabs(panDeg)));
00285 myPacket.byte2ToBuf(ArMath::roundInt(fabs(tiltDeg)));
00286
00287 return sendPacket(&myPacket);
00288 }
00289
00290 AREXPORT bool ArAMPTU::panSlew(double deg)
00291 {
00292 if (deg > MAX_PAN_SLEW)
00293 deg = MAX_PAN_SLEW;
00294 if (deg < MIN_SLEW)
00295 deg = MIN_SLEW;
00296
00297 myPanSlew = deg;
00298 myPacket.empty();
00299 myPacket.byteToBuf(ArAMPTUCommands::PANSLEW);
00300 myPacket.byteToBuf((int)(256 - (3840 / (float)deg)));
00301 return sendPacket(&myPacket);
00302 }
00303
00304 AREXPORT bool ArAMPTU::tiltSlew(double deg)
00305 {
00306 if (deg > MAX_TILT_SLEW)
00307 deg = MAX_TILT_SLEW;
00308 if (deg < MIN_SLEW)
00309 deg = MIN_SLEW;
00310
00311 myTiltSlew = deg;
00312 myPacket.empty();
00313 myPacket.byteToBuf(ArAMPTUCommands::TILTSLEW);
00314 myPacket.byteToBuf((int)(256 - (3840 / (float)deg)));
00315 return sendPacket(&myPacket);
00316 }
00317
00318 AREXPORT bool ArAMPTU::pause(void)
00319 {
00320 myPacket.empty();
00321 myPacket.byteToBuf(ArAMPTUCommands::PAUSE);
00322 return sendPacket(&myPacket);
00323 }
00324
00325 AREXPORT bool ArAMPTU::resume(void)
00326 {
00327 myPacket.empty();
00328 myPacket.byteToBuf(ArAMPTUCommands::CONT);
00329 return sendPacket(&myPacket);
00330 }
00331
00332 AREXPORT bool ArAMPTU::purge(void)
00333 {
00334 myPacket.empty();
00335 myPacket.byteToBuf(ArAMPTUCommands::PURGE);
00336 return sendPacket(&myPacket);
00337 }
00338
00339 AREXPORT bool ArAMPTU::requestStatus(void)
00340 {
00341 myPacket.empty();
00342 myPacket.byteToBuf(ArAMPTUCommands::STATUS);
00343 return sendPacket(&myPacket);
00344 }
00345