BeoSubIMU.C
Go to the documentation of this file.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
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037 #include "BeoSub/BeoSubIMU.H"
00038 #include <string>
00039
00040 void *IMU_run(void *c);
00041
00042
00043 void *IMU_run(void *c)
00044 {
00045 BeoSubIMU *d = (BeoSubIMU *) c;
00046 d ->run();
00047 return NULL;
00048 }
00049
00050
00051 BeoSubIMUListener::~BeoSubIMUListener()
00052 { }
00053
00054
00055 BeoSubIMU::BeoSubIMU(OptionManager& mgr, const std::string& descrName,
00056 const std::string& tagName, const char *dev) :
00057 ModelComponent(mgr, descrName, tagName),
00058 itsSerial(new Serial(mgr, descrName+" Serial Port", tagName+"SerialPort")),
00059 itsKeepgoing(true)
00060 {
00061 itsSerial->configure(dev, 115200, "8N1", false, true, 0);
00062 addSubComponent(itsSerial);
00063 pthread_mutex_init(&itsLock, NULL);
00064 }
00065
00066
00067 void BeoSubIMU::start2()
00068 { pthread_create(&itsRunner, NULL, &IMU_run, (void *) this); }
00069
00070
00071 void BeoSubIMU::stop1()
00072 {
00073 itsKeepgoing = false;
00074 usleep(300000);
00075 }
00076
00077
00078 void BeoSubIMU::setListener(rutz::shared_ptr<BeoSubIMUListener> listener)
00079 { itsListener = listener; }
00080
00081
00082 BeoSubIMU::~BeoSubIMU()
00083 {
00084 pthread_mutex_destroy(&itsLock);
00085 }
00086
00087
00088 void BeoSubIMU::run()
00089 {
00090 while(itsKeepgoing)
00091 {
00092 const int psize = 22;
00093 unsigned char packet[psize];
00094 packet[0] = 0x7F; packet[1] = 0xFF;
00095 int checksum;
00096
00097 do
00098 {
00099
00100 int count = 2; checksum = 0x7FFF;
00101 bool tagFound = false, FFFound = false;
00102
00103 while (count < psize)
00104 {
00105
00106 unsigned char c; int size = itsSerial->read(&c, 1);
00107 if (size != 1) { PLERROR("Serial port read error"); continue; }
00108
00109
00110 if (tagFound)
00111 {
00112 packet[count] = c;
00113 if (count % 2 == 0) checksum += int(c);
00114 else checksum += ((int(c)) << (sizeof(char)*8));
00115 ++ count;
00116 }
00117
00118
00119 if (c == 0xFF) FFFound = true;
00120
00121
00122 if (FFFound && c == 0x7F) tagFound = true;
00123 }
00124 } while ((checksum & 0x0000FFFF));
00125
00126
00127 pthread_mutex_lock(&itsLock);
00128 itsXaccel = accelConvert((packet[5] << 8) + packet[4]);
00129 itsYaccel = accelConvert((packet[9] << 8) + packet[8]);
00130 itsZaccel = accelConvert((packet[13] << 8) + packet[12]);
00131 itsXvel = rateConvert((packet[7] << 8) + packet[6]);
00132 itsYvel = rateConvert((packet[11] << 8) + packet[10]);
00133 itsZvel = rateConvert((packet[15] << 8) + packet[14]);
00134
00135
00136 if (itsListener.is_valid())
00137 itsListener->newData(itsXaccel, itsYaccel, itsZaccel,
00138 itsXvel, itsYvel, itsZvel);
00139 pthread_mutex_unlock(&itsLock);
00140 }
00141
00142 pthread_exit(0);
00143 }
00144
00145
00146 float BeoSubIMU::accelConvert(int data)
00147 {
00148 if (data > 0x7FFF)
00149 {
00150 int temp = (((~data)+1) & 0x0000FFFF);
00151 return -temp / 326.3F;
00152 }
00153 else return data / 326.3F;
00154 }
00155
00156
00157 float BeoSubIMU::rateConvert(int data)
00158 {
00159 if (data > 0x7FFF)
00160 {
00161 int temp = (((~data)+1) & 0x0000FFFF);
00162 return -temp / 100.0F;
00163 }
00164 else return data / 100.0F;
00165 }
00166
00167
00168 float BeoSubIMU::getXaccel()
00169 {
00170 pthread_mutex_lock(&itsLock);
00171 const float ret = itsXaccel;
00172 pthread_mutex_unlock(&itsLock);
00173 return ret;
00174 }
00175
00176
00177 float BeoSubIMU::getYaccel()
00178 {
00179 pthread_mutex_lock(&itsLock);
00180 const float ret = itsYaccel;
00181 pthread_mutex_unlock(&itsLock);
00182 return ret;
00183 }
00184
00185
00186 float BeoSubIMU::getZaccel()
00187 {
00188 pthread_mutex_lock(&itsLock);
00189 const float ret = itsZaccel;
00190 pthread_mutex_unlock(&itsLock);
00191 return ret;
00192 }
00193
00194
00195 Angle BeoSubIMU::getXvel()
00196 {
00197 pthread_mutex_lock(&itsLock);
00198 const Angle ret = itsXvel;
00199 pthread_mutex_unlock(&itsLock);
00200 return ret;
00201 }
00202
00203
00204 Angle BeoSubIMU::getYvel()
00205 {
00206 pthread_mutex_lock(&itsLock);
00207 const Angle ret = itsYvel;
00208 pthread_mutex_unlock(&itsLock);
00209 return ret;
00210 }
00211
00212
00213 Angle BeoSubIMU::getZvel()
00214 {
00215 pthread_mutex_lock(&itsLock);
00216 const Angle ret = itsZvel;
00217 pthread_mutex_unlock(&itsLock);
00218 return ret;
00219 }
00220
00221
00222
00223
00224
00225