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 #include "ArExport.h"
00028 #include "ariaOSDef.h"
00029 #include "ArACTS.h"
00030
00031
00032 ArACTS_1_2::ArACTS_1_2() :
00033 mySensorTaskCB(this, &ArACTS_1_2::actsHandler)
00034 {
00035 myRobot = NULL;
00036 myBlobsBad = true;
00037 myInverted = false;
00038 }
00039
00040 ArACTS_1_2::~ArACTS_1_2()
00041 {
00042
00043 }
00044
00057 bool ArACTS_1_2::openPort(ArRobot *robot, const char *host, int port)
00058 {
00059 int ret;
00060 std::string str;
00061 if ((ret = myConn.open(host, port)) != 0)
00062 {
00063 str = myConn.getOpenMessage(ret);
00064 ArLog::log(ArLog::Terse, "ArACTS_1_2::openPort: Open failed: %s",
00065 str.c_str());
00066 return false;
00067
00068 }
00069 myRequested = false;
00070 setRobot(robot);
00071 return true;
00072 }
00073
00078 bool ArACTS_1_2::closePort(void)
00079 {
00080 return myConn.close();
00081 }
00082
00088 bool ArACTS_1_2::requestPacket(void)
00089 {
00090 const char c = '0';
00091 if (myConn.getStatus() != ArDeviceConnection::STATUS_OPEN)
00092 {
00093 ArLog::log(ArLog::Verbose,
00094 "ArACTS_1_2::requestPacket: No connection to ACTS.\n");
00095 return false;
00096 }
00097 return myConn.write(&c, 1);
00098 }
00099
00104 bool ArACTS_1_2::requestQuit(void)
00105 {
00106 const char c = '1';
00107 if (myConn.getStatus() != ArDeviceConnection::STATUS_OPEN)
00108 {
00109 ArLog::log(ArLog::Verbose,
00110 "ArACTS_1_2::requestQuit: No connection to ACTS.\n");
00111 return false;
00112 }
00113 return myConn.write(&c, 1);
00114 }
00115
00121 bool ArACTS_1_2::receiveBlobInfo(void)
00122 {
00123 int i;
00124 char *data;
00125 int numBlobs = 0;
00126
00127 myBlobsBad = true;
00128 if (myConn.getStatus() != ArDeviceConnection::STATUS_OPEN)
00129 {
00130 ArLog::log(ArLog::Verbose,
00131 "ArACTS_1_2::receiveBlobInfo: No connection to ACTS.\n");
00132 return false;
00133 }
00134
00135 if (!myConn.read(myData, NUM_CHANNELS*4, 20))
00136 {
00137 ArLog::log(ArLog::Verbose,
00138 "ArACTS_1_2::receiveBlobInfo: Couldn't get the blob stats.\n");
00139 return false;
00140 }
00141 data = myData;
00142 for (i = 0; i < NUM_CHANNELS; i++)
00143 {
00144 myBlobIndex[i] = (*(data++))-1;
00145 myBlobIndex[i] = myBlobIndex[i] << 6;
00146 myBlobIndex[i] |= (*(data++))-1;
00147
00148 myBlobNum[i] = (*(data++))-1;
00149 myBlobNum[i] = myBlobNum[i] << 6;
00150 myBlobNum[i] |= (*(data++))-1;
00151 numBlobs += myBlobNum[i];
00152 }
00153 if (numBlobs == 0)
00154 return true;
00155 if (!myConn.read(myData, numBlobs * ACTS_BLOB_DATA_SIZE, 10))
00156 {
00157 ArLog::log(ArLog::Normal,
00158 "ArACTS_1_2::receiveBlobInfo: Couldn't read blob data.\n");
00159 return false;
00160 }
00161 myBlobsBad = false;
00162 return true;
00163 }
00164
00168 int ArACTS_1_2::getNumBlobs(int channel)
00169 {
00170
00171 if (channel >= 1 && channel <= NUM_CHANNELS)
00172 {
00173 --channel;
00174 return myBlobNum[channel];
00175 }
00176 else
00177 return -1;
00178 }
00179
00180 int ArACTS_1_2::getData(char *rawData)
00181 {
00182 int temp;
00183 temp = (*(rawData++)) - 1;
00184 temp = temp << 6;
00185 temp |= (*(rawData++)) - 1;
00186
00187 return temp;
00188 }
00189
00199 bool ArACTS_1_2::getBlob(int channel, int blobNumber, ArACTSBlob *blob)
00200 {
00201 char * blobInfo;
00202 int i;
00203 int temp;
00204
00205 if (myBlobsBad)
00206 {
00207 ArLog::log(ArLog::Verbose,
00208 "ArACTS_1_2::getBlob: There is no valid blob data.\n");
00209 return false;
00210 }
00211
00212 if (channel <= 0 || channel > NUM_CHANNELS)
00213 {
00214 ArLog::log(ArLog::Normal,
00215 "ArACTS_1_2::getBlob: Channel %d out of range 1 to %d\n",
00216 channel, NUM_CHANNELS);
00217 return false;
00218 }
00219 --channel;
00220
00221 if (blobNumber <= 0 || blobNumber > myBlobNum[channel])
00222 {
00223 ArLog::log(ArLog::Normal,
00224 "ArACTS_1_2::getBlob: Blob number %d out of range 1 to %d",
00225 blobNumber, myBlobNum[channel]);
00226 return false;
00227 }
00228 --blobNumber;
00229
00230 blobInfo = myData + (myBlobIndex[channel]+blobNumber) * ACTS_BLOB_DATA_SIZE;
00231
00232 temp = 0;
00233 for (i = 0; i < 4; i++)
00234 {
00235 temp = temp << 6;
00236 temp |= (*(blobInfo++)) - 1;
00237 }
00238 blob->setArea(temp);
00239
00240 blob->setXCG(invertX(getData(blobInfo)));
00241 blobInfo += 2;
00242
00243 blob->setYCG(invertY(getData(blobInfo)));
00244 blobInfo += 2;
00245
00246 blob->setLeft(invertX(getData(blobInfo)));
00247 blobInfo += 2;
00248
00249 blob->setRight(invertX(getData(blobInfo)));
00250 blobInfo += 2;
00251
00252 blob->setTop(invertY(getData(blobInfo)));
00253 blobInfo += 2;
00254
00255 blob->setBottom(invertY(getData(blobInfo)));
00256 blobInfo += 2;
00257
00258 return true;
00259 }
00260
00261 int ArACTS_1_2::invertX(int before)
00262 {
00263 if (myInverted)
00264 return myWidth - before;
00265 else
00266 return before;
00267 }
00268
00269 int ArACTS_1_2::invertY(int before)
00270 {
00271 if (myInverted)
00272 return myHeight - before;
00273 else
00274 return before;
00275 }
00276
00284 void ArACTS_1_2::invert(int width, int height)
00285 {
00286 myInverted = true;
00287 myWidth = true;
00288 myHeight = true;
00289 }
00290
00291 bool ArACTS_1_2::isConnected(void)
00292 {
00293 if (myConn.getStatus() != ArDeviceConnection::STATUS_OPEN)
00294 return false;
00295 else
00296 return true;
00297 }
00298
00299 ArRobot *ArACTS_1_2::getRobot(void)
00300 {
00301 return myRobot;
00302 }
00303 void ArACTS_1_2::setRobot(ArRobot *robot)
00304 {
00305 myRobot = robot;
00306 if (myRobot != NULL)
00307 {
00308 myRobot->remSensorInterpTask(&mySensorTaskCB);
00309 myRobot->addSensorInterpTask("acts Handler", 50, &mySensorTaskCB);
00310 }
00311 }
00312
00313 void ArACTS_1_2::actsHandler(void)
00314 {
00315 if (!myRequested || receiveBlobInfo())
00316 {
00317 myRequested = true;
00318 requestPacket();
00319 }
00320
00321 }