Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | Related Pages | Examples

ArACTS.cpp

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

Generated on Tue Feb 20 10:51:38 2007 for Aria by  doxygen 1.4.0