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

ArServerInfoSensor.cpp

Go to the documentation of this file.
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 "Aria.h"
00027 #include "ArExport.h"
00028 #include "ArServerInfoSensor.h"
00029 
00030 AREXPORT ArServerInfoSensor::ArServerInfoSensor(ArServerBase *server, ArRobot *robot) :
00031   myGetSensorListCB(this, &ArServerInfoSensor::getSensorList),
00032   myGetSensorCurrentCB(this, &ArServerInfoSensor::getSensorCurrent),
00033   myGetSensorCumulativeCB(this, &ArServerInfoSensor::getSensorCumulative)
00034   
00035 {
00036   myRobot = robot;
00037   myServer = server;
00038   
00039   if (myServer != NULL)
00040   {
00041     /*
00042       These are set as RETURN_COMPLEX since they won't work with the
00043       connection forwarding stuff (since that can't deal with requests
00044       set up like this)
00045     */
00046     myServer->addData("getSensorList", 
00047                       "gets a list of sensors attached to the robot",
00048                       &myGetSensorListCB, 
00049                       "none", 
00050                       "byte2: numSensors,  repeating for numSensors: string: sensorName", 
00051                       "SensorInfo", "RETURN_COMPLEX");
00052 
00053     myServer->addData("getSensorCurrent", 
00054                       "gets the current sensor readings for requested sensors",
00055                       &myGetSensorCurrentCB,
00056                       "string: sensorName",
00057                       "byte2: numReadings string: sensorName, repeating for numReadings times: byte4: x byte4: y.... if numReadings is -1 it means no sensor by that name",
00058                       "SensorInfo", "RETURN_COMPLEX");
00059 
00060     myServer->addData("getSensorCumulative", 
00061                       "gets the cumulative sensor readings for requested sensors",
00062                       &myGetSensorCumulativeCB,
00063                       "string: sensorName",
00064                       "byte2: numReadings string: sensorName, repeating for numReadings times: byte4: x byte4: y.... if numReadings is -1 it means no sensor by that name",
00065                       "SensorInfo", "RETURN_COMPLEX");
00066   }
00067 }
00068 
00069 AREXPORT ArServerInfoSensor::~ArServerInfoSensor() 
00070 {
00071 
00072 }
00073 
00074 AREXPORT void ArServerInfoSensor::getSensorList(ArServerClient *client, 
00075                                           ArNetPacket *packet)
00076 {
00077   ArNetPacket sendPacket;
00078   std::list<ArRangeDevice *> *devList;
00079   std::list<ArRangeDevice *>::iterator it;
00080 
00081   myRobot->lock();
00082   devList = myRobot->getRangeDeviceList();
00083   
00084   if (devList == NULL)
00085   {
00086     myRobot->unlock();
00087     client->sendPacketUdp(&sendPacket);
00088     return;
00089   }
00090   
00091   sendPacket.byte2ToBuf(devList->size());
00092 
00093   for (it = devList->begin(); it != devList->end(); it++)
00094   {
00095     sendPacket.strToBuf((*it)->getName());
00096   }
00097   myRobot->unlock();
00098   client->sendPacketUdp(&sendPacket);
00099 }
00100 
00101 
00102 AREXPORT void ArServerInfoSensor::getSensorCurrent(ArServerClient *client, 
00103                                         ArNetPacket *packet)
00104 {
00105   ArRangeDevice *dev;
00106   char sensor[512];
00107   std::list<ArPoseWithTime *> *readings;
00108   std::list<ArPoseWithTime *>::iterator it;
00109 
00110   while (packet->getDataLength() > packet->getDataReadLength())
00111   {
00112     ArNetPacket sendPacket;
00113 
00114     // find out the sensor they want
00115     packet->bufToStr(sensor, sizeof(sensor));
00116     myRobot->lock();
00117     if ((dev = myRobot->findRangeDevice(sensor)) == NULL)
00118     {
00119       myRobot->unlock();
00120       ArLog::log(ArLog::Verbose, "ArServerInfoSensor::getSensorCurrent: No sensor %s", sensor);
00121       sendPacket.byte2ToBuf(-1);
00122       sendPacket.strToBuf(sensor);
00123       client->sendPacketUdp(&sendPacket);
00124       continue;
00125     }
00126     
00127     myRobot->unlock();
00128     dev->lockDevice();
00129     readings = dev->getCurrentBuffer();
00130     if (readings == NULL)
00131     {
00132       dev->unlockDevice();
00133       ArLog::log(ArLog::Verbose, "ArServerInfoSensor::getSensorCurrent: No current buffer for %s", sensor);
00134       sendPacket.byte2ToBuf(0);
00135       sendPacket.strToBuf(sensor);
00136       client->sendPacketUdp(&sendPacket);
00137       continue;
00138     } 
00139     
00140     sendPacket.byte2ToBuf(readings->size());
00141     sendPacket.strToBuf(sensor);
00142     for (it = readings->begin(); it != readings->end(); it++)
00143     {
00144       sendPacket.byte4ToBuf(ArMath::roundInt((*it)->getX()));
00145       sendPacket.byte4ToBuf(ArMath::roundInt((*it)->getY()));
00146     }
00147     dev->unlockDevice();
00148     client->sendPacketUdp(&sendPacket);
00149   }
00150   
00151 
00152 }
00153 
00154 AREXPORT void ArServerInfoSensor::getSensorCumulative(ArServerClient *client, 
00155                                         ArNetPacket *packet)
00156 {
00157   ArRangeDevice *dev;
00158   char sensor[512];
00159   std::list<ArPoseWithTime *> *readings;
00160   std::list<ArPoseWithTime *>::iterator it;
00161 
00162   while (packet->getDataLength() > packet->getDataReadLength())
00163   {
00164     ArNetPacket sendPacket;  
00165     // find out the sensor they want
00166     packet->bufToStr(sensor, sizeof(sensor));
00167     myRobot->lock();
00168     if ((dev = myRobot->findRangeDevice(sensor)) == NULL)
00169     {
00170       myRobot->unlock();
00171       ArLog::log(ArLog::Verbose, "ArServerInfoSensor::getSensorCumulative: No sensor %s", sensor);
00172       sendPacket.byte2ToBuf(-1);
00173       sendPacket.strToBuf(sensor);
00174       client->sendPacketUdp(&sendPacket);
00175       continue;
00176     }
00177     
00178     myRobot->unlock();
00179     dev->lockDevice();
00180     readings = dev->getCumulativeBuffer();
00181     if (readings == NULL)
00182     {
00183       dev->unlockDevice();
00184       ArLog::log(ArLog::Verbose, "ArServerInfoSensor::getSensorCumulative: No current buffer for %s", sensor);
00185       sendPacket.byte2ToBuf(0);
00186       sendPacket.strToBuf(sensor);
00187       client->sendPacketUdp(&sendPacket);
00188       continue;
00189     } 
00190     
00191     sendPacket.byte2ToBuf(readings->size());
00192     sendPacket.strToBuf(sensor);
00193     for (it = readings->begin(); it != readings->end(); it++)
00194     {
00195       sendPacket.byte4ToBuf(ArMath::roundInt((*it)->getX()));
00196       sendPacket.byte4ToBuf(ArMath::roundInt((*it)->getY()));
00197     }
00198     dev->unlockDevice();
00199     client->sendPacketUdp(&sendPacket);
00200   }
00201 
00202 }

Generated on Tue Feb 20 10:51:50 2007 for ArNetworking by  doxygen 1.4.0