IOTShield.cpp
Go to the documentation of this file.
1 #include "IOTShield.h"
2 #include <iostream>
3 #include <sys/time.h>
4 #include <unistd.h>
5 
6 namespace iotbot
7 {
8 
9 void byteArrayToFloat(int8_t* byteArray, float* floatVar)
10 {
11  uint32_t* var = (uint32_t*)floatVar;
12  *var = byteArray[0] << 24;
13  *var |= byteArray[1] << 16;
14  *var |= byteArray[2] << 8;
15  *var |= byteArray[3];
16 }
17 
18 void floatToByteArray(float* floatVar, int8_t* byteArray)
19 {
20  uint8_t* var = (uint8_t*)(floatVar);
21  memcpy(byteArray, var, sizeof(floatVar));
22 }
23 
24 void intToByteArray(uint32_t* iVar, int8_t* byteArray)
25 {
26  uint8_t* var = (uint8_t*)(iVar);
27  memcpy(byteArray, var, sizeof(iVar));
28 }
29 
30 const char* devPath = "/dev/ttyS1";
31 
33 {
34  _timeCom = 0.0;
35  _rawdata = true;
36 
37  _rpm.resize(4);
38  _ranges.resize(4);
39  _acceleration.resize(3);
40  _angularRate.resize(3);
41  _q.resize(4);
42  _q[0] = 1.0;
43  _q[1] = 0.0;
44  _q[2] = 0.0;
45  _q[3] = 0.0;
46 #if _WITH_MRAA
47  _uart = new mraa::Uart(devPath);
48 
49  if (_uart->setBaudRate(115200) != mraa::SUCCESS) {
50  std::cerr << "Error setting parity on UART" << std::endl;
51  }
52 
53  if (_uart->setMode(8, mraa::UART_PARITY_NONE, 1) != mraa::SUCCESS) {
54  std::cerr << "Error setting parity on UART" << std::endl;
55  }
56 
57  if (_uart->setFlowcontrol(false, false) != mraa::SUCCESS) {
58  std::cerr << "Error setting flow control UART" << std::endl;
59  }
60 
61  _uart->flush();
62 #else
63  std::cerr << "UART interface not available. MRAA is missing!" << std::endl;
64 #endif
65 }
66 
68 {
69 #if _WITH_MRAA
70  delete _uart;
71 #endif
72 }
73 
75 {
76  _txBuf[0] = 0xFF;
77  _txBuf[1] = CMD_ENABLE;
78  _txBuf[10] = 0xEE;
79  sendReceive();
80  return 1;
81 }
82 
84 {
85  _txBuf[0] = 0xFF;
86  _txBuf[1] = CMD_DISABLE;
87  _txBuf[10] = 0xEE;
88  sendReceive();
89  return 1;
90 }
91 
92 bool IOTShield::setIMURawFormat(bool rawdata)
93 {
94  _txBuf[0] = 0xFF;
96  if(rawdata)
97  {
98  _txBuf[2] = 0xFF;
99  _acceleration[0] = 0;
100  _acceleration[1] = 0;
101  _acceleration[2] = 0;
102  _acceleration[3] = 0;
103  _angularRate[0] = 0;
104  _angularRate[1] = 0;
105  _angularRate[2] = 0;
106  _angularRate[3] = 0;
107  }
108  else
109  {
110  _txBuf[2] = 0x00;
111  _q[0] = 1;
112  _q[1] = 0;
113  _q[2] = 0;
114  _q[3] = 0;
115  }
116  _rawdata = rawdata;
117  sendReceive();
118  return 1;
119 }
120 
122 {
123  _txBuf[0] = 0xFF;
124  _txBuf[1] = CMD_IMUFIXZAXIS;
125  _txBuf[2] = fix;
126  sendReceive();
127  return 1;
128 }
129 
130 bool IOTShield::setTimeout(float timeout)
131 {
132  _txBuf[0] = 0xFF;
133  _txBuf[1] = CMD_UARTTIMEOUT;
134  floatToByteArray(&timeout, (int8_t*)&(_txBuf[2]));
135  sendReceive();
136  return 1;
137 }
138 
139 bool IOTShield::setDriftWeight(float weight)
140 {
141  _txBuf[0] = 0xFF;
143  floatToByteArray(&weight, (int8_t*)&(_txBuf[2]));
144  sendReceive();
145  return 1;
146 }
147 
149 {
150  _txBuf[0] = 0xFF;
152  sendReceive();
153  return 1;
154 }
155 
156 bool IOTShield::setGearRatio(float gearRatio)
157 {
158  _txBuf[0] = 0xFF;
159  _txBuf[1] = CMD_GEARRATIO;
160  floatToByteArray(&gearRatio, (int8_t*)&(_txBuf[2]));
161  sendReceive();
162  return 1;
163 }
164 
165 bool IOTShield::setTicksPerRev(float ticksPerRev)
166 {
167  _txBuf[0] = 0xFF;
168  _txBuf[1] = CMD_TICKSPERREV;
169  floatToByteArray(&ticksPerRev, (int8_t*)&(_txBuf[2]));
170  sendReceive();
171  return 1;
172 }
173 
174 bool IOTShield::setKp(float kp)
175 {
176  _txBuf[0] = 0xFF;
177  _txBuf[1] = CMD_CTL_KP;
178  floatToByteArray(&kp, (int8_t*)&(_txBuf[2]));
179  sendReceive();
180  return 1;
181 }
182 
183 bool IOTShield::setKi(float ki)
184 {
185  _txBuf[0] = 0xFF;
186  _txBuf[1] = CMD_CTL_KI;
187  floatToByteArray(&ki, (int8_t*)&(_txBuf[2]));
188  sendReceive();
189  return 1;
190 }
191 
192 bool IOTShield::setKd(float kd)
193 {
194  _txBuf[0] = 0xFF;
195  _txBuf[1] = CMD_CTL_KD;
196  floatToByteArray(&kd, (int8_t*)&(_txBuf[2]));
197  sendReceive();
198  return 1;
199 }
200 
202 {
203  _txBuf[0] = 0xFF;
204  _txBuf[1] = CMD_FREQ;
205  intToByteArray(&freq, (int8_t*)&(_txBuf[2]));
206  sendReceive();
207  return 1;
208 }
209 
211 {
212  _txBuf[0] = 0xFF;
214  floatToByteArray(&weight, (int8_t*)&(_txBuf[2]));
215  sendReceive();
216  return 1;
217 }
218 
220 {
221  _txBuf[0] = 0xFF;
223  floatToByteArray(&weight, (int8_t*)&(_txBuf[2]));
224  sendReceive();
225  return 1;
226 }
227 
228 bool IOTShield::setPWM(int8_t pwm[4])
229 {
230  _txBuf[0] = 0xFF;
231  _txBuf[1] = CMD_SETPWM;
232  memcpy(&(_txBuf[2]), pwm, 4);
233  sendReceive();
234  return 1;
235 }
236 
237 bool IOTShield::setRPM(float rpm[4])
238 {
239  int16_t irpm[4];
240  for(int i=0; i<4; i++)
241  irpm[i] = ((int16_t)(rpm[i]*100.f+0.5f));
242  _txBuf[0] = 0xFF;
243  _txBuf[1] = CMD_SETRPM;
244  memcpy(&(_txBuf[2]), irpm, 4*sizeof(int16_t));
245  sendReceive();
246  return 1;
247 }
248 
249 const std::vector<float> IOTShield::getRPM()
250 {
251  return _rpm;
252 }
253 
254 bool IOTShield::setLighting(eLighting light, unsigned char rgb[3])
255 {
256  _txBuf[0] = 0xFF;
257  _txBuf[1] = light;
258  _txBuf[2] = rgb[0];
259  _txBuf[3] = rgb[1];
260  _txBuf[4] = rgb[2];
261  _txBuf[10] = 0xEE;
262  sendReceive();
263  return 0;
264 }
265 
266 bool IOTShield::setAUX1(bool on)
267 {
268  _txBuf[0] = 0xFF;
269  _txBuf[1] = CMD_AUX1;
270  _txBuf[2] = (on ? 0x01 : 0x00);
271  for(int i=3; i<10; i++)
272  _txBuf[i] = 0;
273  _txBuf[10] = 0xEE;
274  sendReceive();
275  return 0;
276 }
277 
278 bool IOTShield::setAUX2(bool on)
279 {
280  _txBuf[0] = 0xFF;
281  _txBuf[1] = CMD_AUX2;
282  _txBuf[2] = on;
283  for(int i=3; i<10; i++)
284  _txBuf[i] = 0;
285  _txBuf[10] = 0xEE;
286  sendReceive();
287  return 0;
288 }
289 
291 {
292  return _systemVoltage;
293 }
294 
296 {
297  return _loadCurrent;
298 }
299 
300 const std::vector<float> IOTShield::getRangeMeasurements()
301 {
302  return _ranges;
303 }
304 
305 const std::vector<float> IOTShield::getAcceleration()
306 {
307  return _acceleration;
308 }
309 
310 const std::vector<float> IOTShield::getAngularRate()
311 {
312  return _angularRate;
313 }
314 
315 const std::vector<float> IOTShield::getOrientation()
316 {
317  return _q;
318 }
319 
321 {
322  return _temperature;
323 }
324 
326 {
327 #if _WITH_MRAA
328  timeval clock;
329  double now = 0.0;
330  do
331  {
332  ::gettimeofday(&clock, 0);
333  now = static_cast<double>(clock.tv_sec) + static_cast<double>(clock.tv_usec) * 1.0e-6;
334  }while((now - _timeCom) < 0.008);
335  _timeCom = now;
336  _uart->write((char*)_txBuf, 11);
337  usleep(1000);
338  _uart->read(_rxBuf, 32);
339  usleep(1000);
340  if(!_rawdata)
341  {
342  int16_t* ibuf = (int16_t*)(&_rxBuf[9]);
343  _q[0] = ((float)ibuf[0])/10000.f;
344  _q[1] = ((float)ibuf[1])/10000.f;
345  _q[2] = ((float)ibuf[2])/10000.f;
346  _q[3] = ((float)ibuf[3])/10000.f;
347 
348  int16_t temp = _rxBuf[18];
349  temp = temp << 8;
350  temp |= _rxBuf[17];
351  _temperature = ((float)temp) / 100.f;
352  }
353  else
354  {
355  for(int i=0; i<3; i++)
356  {
357  int16_t val = _rxBuf[10+2*i];
358  val = val << 8;
359  val |= _rxBuf[9+2*i];
360  // convert from mg*10 to g
361  _acceleration[i] = ((float)val)/10000.f;
362 
363  val = _rxBuf[16+2*i];
364  val = val << 8;
365  val |= _rxBuf[15+2*i];
366  // convert from mdps/10 to dps
367  _angularRate[i] = ((float)val)/100.f;
368 
369  _temperature = -273.f;
370  }
371  }
372  for(int i=0; i<4; i++)
373  {
374  int16_t val = _rxBuf[2*i+2] << 8;
375  val |= _rxBuf[2*i+1];
376  _rpm[i] = ((float)val)/100.f;
377 
378  unsigned short distanceInMM = _rxBuf[22+2*i];
379  distanceInMM = distanceInMM << 8;
380  distanceInMM |= _rxBuf[21+2*i];
381  _ranges[i] = (float)distanceInMM;
382  _ranges[i] /= 1000.f;
383  }
384  unsigned int voltage = _rxBuf[30];
385  voltage = voltage << 8;
386  voltage |= _rxBuf[29];
387  _systemVoltage = (float) voltage / 100.f;
388  uint8_t current = _rxBuf[31];
389  _loadCurrent = ((float) current)/20.f;
390 #else
391  std::cerr << "Ignoring UART communication demand." << std::endl;
392 #endif
393 }
394 
395 } // namespace
iotbot::IOTShield::setControlFrequency
bool setControlFrequency(uint32_t freq)
Definition: IOTShield.cpp:201
CMD_SETRPM
#define CMD_SETRPM
Definition: IOTShield.h:34
iotbot::IOTShield::_temperature
float _temperature
Definition: IOTShield.h:307
CMD_AUX2
#define CMD_AUX2
Definition: IOTShield.h:51
iotbot::IOTShield::setLowPassSetPoint
bool setLowPassSetPoint(float weight)
Definition: IOTShield.cpp:210
iotbot::devPath
const char * devPath
Definition: IOTShield.cpp:30
CMD_CTL_INPUTFILTER
#define CMD_CTL_INPUTFILTER
Definition: IOTShield.h:43
iotbot::IOTShield::setKd
bool setKd(float kd)
Definition: IOTShield.cpp:192
iotbot::IOTShield::_rxBuf
char _rxBuf[32]
Definition: IOTShield.h:287
CMD_FUSEIMUWEIGHT
#define CMD_FUSEIMUWEIGHT
Definition: IOTShield.h:27
iotbot::IOTShield::setIMURawFormat
bool setIMURawFormat(bool rawdata)
Definition: IOTShield.cpp:92
iotbot::IOTShield::_q
std::vector< float > _q
Definition: IOTShield.h:301
iotbot::IOTShield::IOTShield
IOTShield()
Definition: IOTShield.cpp:32
IOTShield.h
iotbot::IOTShield::setKp
bool setKp(float kp)
Definition: IOTShield.cpp:174
iotbot::IOTShield::getTemperature
const float getTemperature()
Definition: IOTShield.cpp:320
CMD_ENABLE
#define CMD_ENABLE
Definition: IOTShield.h:17
iotbot::IOTShield::setAUX1
bool setAUX1(bool on)
Definition: IOTShield.cpp:266
iotbot::IOTShield::setTicksPerRev
bool setTicksPerRev(float ticksPerRev)
Definition: IOTShield.cpp:165
CMD_UARTTIMEOUT
#define CMD_UARTTIMEOUT
Definition: IOTShield.h:28
CMD_CTL_KP
#define CMD_CTL_KP
Definition: IOTShield.h:39
iotbot::IOTShield::disable
bool disable()
Definition: IOTShield.cpp:83
iotbot::IOTShield::getAngularRate
const std::vector< float > getAngularRate()
Definition: IOTShield.cpp:310
iotbot::IOTShield::_angularRate
std::vector< float > _angularRate
Definition: IOTShield.h:299
iotbot::IOTShield::getLoadCurrent
float getLoadCurrent()
Definition: IOTShield.cpp:295
iotbot::IOTShield::_rpm
std::vector< float > _rpm
Definition: IOTShield.h:293
CMD_AUX1
#define CMD_AUX1
Definition: IOTShield.h:50
iotbot::IOTShield::getRPM
const std::vector< float > getRPM()
Definition: IOTShield.cpp:249
f
f
iotbot::IOTShield::_txBuf
char _txBuf[11]
Definition: IOTShield.h:285
iotbot::eLighting
eLighting
Definition: IOTShield.h:62
iotbot::IOTShield::getRangeMeasurements
const std::vector< float > getRangeMeasurements()
Definition: IOTShield.cpp:300
iotbot
Definition: IOTBot.cpp:6
iotbot::IOTShield::setKi
bool setKi(float ki)
Definition: IOTShield.cpp:183
iotbot::IOTShield::enable
bool enable()
Definition: IOTShield.cpp:74
iotbot::IOTShield::fixIMUZAxis
bool fixIMUZAxis(bool fix)
Definition: IOTShield.cpp:121
CMD_GEARRATIO
#define CMD_GEARRATIO
Definition: IOTShield.h:47
iotbot::IOTShield::_ranges
std::vector< float > _ranges
Definition: IOTShield.h:295
iotbot::IOTShield::calibrateIMU
bool calibrateIMU()
Definition: IOTShield.cpp:148
iotbot::IOTShield::setAUX2
bool setAUX2(bool on)
Definition: IOTShield.cpp:278
CMD_CTL_KI
#define CMD_CTL_KI
Definition: IOTShield.h:40
iotbot::IOTShield::setTimeout
bool setTimeout(float timeout)
Definition: IOTShield.cpp:130
CMD_FREQ
#define CMD_FREQ
Definition: IOTShield.h:35
iotbot::IOTShield::setGearRatio
bool setGearRatio(float gearRatio)
Definition: IOTShield.cpp:156
iotbot::IOTShield::sendReceive
void sendReceive()
Definition: IOTShield.cpp:325
iotbot::IOTShield::_acceleration
std::vector< float > _acceleration
Definition: IOTShield.h:297
iotbot::IOTShield::setRPM
bool setRPM(float rpm[4])
Definition: IOTShield.cpp:237
iotbot::IOTShield::setDriftWeight
bool setDriftWeight(float weight)
Definition: IOTShield.cpp:139
iotbot::IOTShield::getAcceleration
const std::vector< float > getAcceleration()
Definition: IOTShield.cpp:305
iotbot::IOTShield::_rawdata
bool _rawdata
Definition: IOTShield.h:305
iotbot::IOTShield::getSystemVoltage
float getSystemVoltage()
Definition: IOTShield.cpp:290
iotbot::IOTShield::setPWM
bool setPWM(int8_t pwm[4])
Definition: IOTShield.cpp:228
iotbot::byteArrayToFloat
void byteArrayToFloat(int8_t *byteArray, float *floatVar)
Definition: IOTShield.cpp:9
iotbot::floatToByteArray
void floatToByteArray(float *floatVar, int8_t *byteArray)
Definition: IOTShield.cpp:18
iotbot::IOTShield::~IOTShield
~IOTShield()
Definition: IOTShield.cpp:67
iotbot::IOTShield::setLowPassEncoder
bool setLowPassEncoder(float weight)
Definition: IOTShield.cpp:219
iotbot::IOTShield::getOrientation
const std::vector< float > getOrientation()
Definition: IOTShield.cpp:315
CMD_IMURAWDATA
#define CMD_IMURAWDATA
Definition: IOTShield.h:26
CMD_CTL_KD
#define CMD_CTL_KD
Definition: IOTShield.h:41
iotbot::IOTShield::_timeCom
double _timeCom
Definition: IOTShield.h:303
iotbot::IOTShield::setLighting
bool setLighting(eLighting light, unsigned char rgb[3])
Definition: IOTShield.cpp:254
iotbot::IOTShield::_loadCurrent
float _loadCurrent
Definition: IOTShield.h:291
CMD_IMUFIXZAXIS
#define CMD_IMUFIXZAXIS
Definition: IOTShield.h:30
iotbot::IOTShield::_systemVoltage
float _systemVoltage
Definition: IOTShield.h:289
CMD_CTL_ENCLOWPASS
#define CMD_CTL_ENCLOWPASS
Definition: IOTShield.h:44
CMD_DISABLE
#define CMD_DISABLE
Definition: IOTShield.h:18
CMD_IMUCALIBRATE
#define CMD_IMUCALIBRATE
Definition: IOTShield.h:29
CMD_TICKSPERREV
#define CMD_TICKSPERREV
Definition: IOTShield.h:48
CMD_SETPWM
#define CMD_SETPWM
Definition: IOTShield.h:33
iotbot::intToByteArray
void intToByteArray(uint32_t *iVar, int8_t *byteArray)
Definition: IOTShield.cpp:24


iotbot
Author(s): Stefan May (EduArt Robotik)
autogenerated on Wed May 24 2023 02:13:39