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