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

ArACTS.cpp

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 "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 }

Generated on Wed Oct 19 12:56:34 2005 for Aria by  doxygen 1.4.0