YARP
Yet Another Robot Platform
fakeIMU.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2020 Istituto Italiano di Tecnologia (IIT)
3  * All rights reserved.
4  *
5  * This software may be modified and distributed under the terms of the
6  * BSD-3-Clause license. See the accompanying LICENSE file for details.
7  */
8 
9 #include "fakeIMU.h"
10 
11 #include <yarp/os/Thread.h>
12 #include <yarp/os/Time.h>
13 #include <yarp/os/Semaphore.h>
14 #include <yarp/os/Stamp.h>
15 #include <yarp/os/LogComponent.h>
16 #include <yarp/os/LogStream.h>
17 
18 #include <string>
19 
20 using namespace yarp::os;
21 using namespace yarp::dev;
22 using namespace yarp::sig;
23 using namespace yarp::math;
24 
25 namespace {
26 YARP_LOG_COMPONENT(FAKEIMU, "yarp.device.fakeIMU")
27 constexpr double DEFAULT_PERIOD = 0.01; // seconds
28 constexpr int DEFAULT_NCHANNELS = 12;
29 constexpr double DEFAULT_DUMMY_VALUE = 0.0;
30 constexpr const char* DEFAULT_SENSOR_NAME = "sensorName";
31 constexpr const char* DEFAULT_FRAME_NAME = "frameName";
32 
33 constexpr double EARTH_GRAVITY = -9.81;
34 }
35 
42  rpy({0.0, 0.0, 0.0}),
43  gravity({0.0, 0.0, EARTH_GRAVITY, 0.0}),
44  dcm(4, 4),
45  accels({0.0, 0.0, 0.0, 0.0}),
46  nchannels(DEFAULT_NCHANNELS),
47  dummy_value(DEFAULT_DUMMY_VALUE),
48  m_sensorName(DEFAULT_SENSOR_NAME),
49  m_frameName(DEFAULT_FRAME_NAME)
50 {
51  dcm.zero();
52 }
53 
55 {
56  close();
57 }
58 
60 {
61  double period;
62  if( config.check("period")) {
63  period = config.find("period").asInt32() / 1000.0;
64  setPeriod(period);
65  } else {
66  yCInfo(FAKEIMU) << "Using default period of " << DEFAULT_PERIOD << " s";
67  }
68 
69  constantValue = config.check("constantValue");
70 
71  start();
72  return true;
73 }
74 
76 {
77  fakeIMU::stop();
78  return true;
79 }
80 
82 {
83  if(out.size() != nchannels)
84  out.resize(nchannels);
85 
86  out.zero();
87 
88  // Euler angle
89  for(unsigned int i=0; i<3; i++)
90  {
91  out[i] = dummy_value;
92  }
93 
94  // accelerations
95  for(unsigned int i=0; i<3; i++)
96  {
97  out[3+i] = accels[i];
98  }
99 
100  // gyro
101  for(unsigned int i=0; i<3; i++)
102  {
103  out[6+i] = dummy_value;
104  }
105 
106  // magnetometer
107  for(unsigned int i=0; i<3; i++)
108  {
109  out[9+i] = dummy_value;
110  }
111 
112  return true;
113 }
114 
115 bool fakeIMU::getChannels(int *nc)
116 {
117  *nc=nchannels;
118  return true;
119 }
120 
121 bool fakeIMU::calibrate(int ch, double v)
122 {
123  yCWarning(FAKEIMU, "Not implemented yet");
124  return false;
125 }
126 
127 bool fakeIMU::threadInit()
128 {
129  lastStamp.update();
130  return true;
131 }
132 
133 
134 void fakeIMU::run()
135 {
136  static double count=10;
137 
138  rpy[0] = 0;
139  rpy[1] = count * 3.14/180;
140  rpy[2] = 0;
141 
142  dcm = rpy2dcm(rpy);
143  accels = gravity * dcm;
144 
145  lastStamp.update();
146 
147  dummy_value = count;
148  if (!constantValue) {
149  count++;
150  }
151 
152  if(count >= 360)
153  count = 0;
154 }
155 
157 {
158  return lastStamp;
159 }
160 
161 yarp::dev::MAS_status fakeIMU::genericGetStatus(size_t sens_index) const
162 {
163  if (sens_index!=0) {
165  }
166 
168 }
169 
170 bool fakeIMU::genericGetSensorName(size_t sens_index, std::string &name) const
171 {
172  if (sens_index!=0) {
173  return false;
174  }
175 
176  name = m_sensorName;
177  return true;
178 }
179 
180 bool fakeIMU::genericGetFrameName(size_t sens_index, std::string &frameName) const
181 {
182  if (sens_index!=0) {
183  return false;
184  }
185 
186  frameName = m_frameName;
187  return true;
188 }
189 
191 {
192  return 1;
193 }
194 
196 {
197  return genericGetStatus(sens_index);
198 }
199 
200 bool fakeIMU::getThreeAxisGyroscopeName(size_t sens_index, std::string &name) const
201 {
202  return genericGetSensorName(sens_index, name);
203 }
204 
205 bool fakeIMU::getThreeAxisGyroscopeFrameName(size_t sens_index, std::string &frameName) const
206 {
207  return genericGetFrameName(sens_index, frameName);
208 }
209 
210 bool fakeIMU::getThreeAxisGyroscopeMeasure(size_t sens_index, yarp::sig::Vector& out, double& timestamp) const
211 {
212  if (sens_index!=0) {
213  return false;
214  }
215 
216  out.resize(3);
217  out[0] = dummy_value;
218  out[1] = dummy_value;
219  out[2] = dummy_value;
220 
221  // Workaround for https://github.com/robotology/yarp/issues/1610
222  yarp::os::Stamp copyStamp(lastStamp);
223  timestamp = copyStamp.getTime();
224 
225  return true;
226 }
227 
229 {
230  return 1;
231 }
232 
234 {
235  return genericGetStatus(sens_index);
236 }
237 
238 bool fakeIMU::getThreeAxisLinearAccelerometerName(size_t sens_index, std::string &name) const
239 {
240  return genericGetSensorName(sens_index, name);
241 }
242 
243 bool fakeIMU::getThreeAxisLinearAccelerometerFrameName(size_t sens_index, std::string &frameName) const
244 {
245  return genericGetFrameName(sens_index, frameName);
246 }
247 
248 bool fakeIMU::getThreeAxisLinearAccelerometerMeasure(size_t sens_index, yarp::sig::Vector& out, double& timestamp) const
249 {
250  if (sens_index!=0) {
251  return false;
252  }
253 
254  out.resize(3);
255  out[0] = accels[0];
256  out[1] = accels[1];
257  out[2] = accels[2];
258 
259  // Workaround for https://github.com/robotology/yarp/issues/1610
260  yarp::os::Stamp copyStamp(lastStamp);
261  timestamp = copyStamp.getTime();
262 
263  return true;
264 }
265 
267 {
268  return 1;
269 }
270 
272 {
273  return genericGetStatus(sens_index);
274 }
275 
276 bool fakeIMU::getThreeAxisMagnetometerName(size_t sens_index, std::string &name) const
277 {
278  return genericGetSensorName(sens_index, name);
279 }
280 
281 bool fakeIMU::getThreeAxisMagnetometerFrameName(size_t sens_index, std::string &frameName) const
282 {
283  return genericGetFrameName(sens_index, frameName);
284 }
285 
286 bool fakeIMU::getThreeAxisMagnetometerMeasure(size_t sens_index, yarp::sig::Vector& out, double& timestamp) const
287 {
288  if (sens_index!=0) {
289  return false;
290  }
291 
292  out.resize(3);
293  out[0] = dummy_value;
294  out[1] = dummy_value;
295  out[2] = dummy_value;
296 
297  // Workaround for https://github.com/robotology/yarp/issues/1610
298  yarp::os::Stamp copyStamp(lastStamp);
299  timestamp = copyStamp.getTime();
300 
301  return true;
302 }
303 
305 {
306  return 1;
307 }
308 
310 {
311  return genericGetStatus(sens_index);
312 }
313 
314 bool fakeIMU::getOrientationSensorName(size_t sens_index, std::string &name) const
315 {
316  return genericGetSensorName(sens_index, name);
317 }
318 
319 bool fakeIMU::getOrientationSensorFrameName(size_t sens_index, std::string &frameName) const
320 {
321  return genericGetFrameName(sens_index, frameName);
322 }
323 
324 bool fakeIMU::getOrientationSensorMeasureAsRollPitchYaw(size_t sens_index, yarp::sig::Vector& rpy_out, double& timestamp) const
325 {
326  if (sens_index!=0) {
327  return false;
328  }
329 
330  rpy_out.resize(3);
331  rpy_out[0] = dummy_value;
332  rpy_out[1] = dummy_value;
333  rpy_out[2] = dummy_value;
334 
335  // Workaround for https://github.com/robotology/yarp/issues/1610
336  yarp::os::Stamp copyStamp(lastStamp);
337  timestamp = copyStamp.getTime();
338 
339  return true;
340 }
LogStream.h
fakeIMU::getOrientationSensorFrameName
bool getOrientationSensorFrameName(size_t sens_index, std::string &frameName) const override
Get the name of the frame of the specified sensor.
Definition: fakeIMU.cpp:319
yarp::sig::VectorOf::resize
void resize(size_t size) override
Resize the vector.
Definition: Vector.h:254
yarp::os::Searchable
A base class for nested structures that can be searched.
Definition: Searchable.h:69
yarp::sig
Signal processing.
Definition: Image.h:25
yCWarning
#define yCWarning(component,...)
Definition: LogComponent.h:146
YARP_LOG_COMPONENT
#define YARP_LOG_COMPONENT(name,...)
Definition: LogComponent.h:80
fakeIMU::getNrOfThreeAxisGyroscopes
size_t getNrOfThreeAxisGyroscopes() const override
Get the number of three axis gyroscopes exposed by this sensor.
Definition: fakeIMU.cpp:190
fakeIMU::getThreeAxisMagnetometerMeasure
bool getThreeAxisMagnetometerMeasure(size_t sens_index, yarp::sig::Vector &out, double &timestamp) const override
Get the last reading of the specified sensor.
Definition: fakeIMU.cpp:286
yarp::math
Definition: FrameTransform.h:18
DEFAULT_PERIOD
#define DEFAULT_PERIOD
Definition: fakeMicrophone.h:19
fakeIMU::getLastInputStamp
yarp::os::Stamp getLastInputStamp() override
Return the time stamp relative to the last acquisition.
Definition: fakeIMU.cpp:156
yarp::dev
An interface for the device drivers.
Definition: audioBufferSizeData.cpp:17
yarp::math::rpy2dcm
yarp::sig::Matrix rpy2dcm(const yarp::sig::Vector &rpy)
Converts roll-pitch-yaw angles in the corresponding dcm (direction cosine matrix) rotation matrix (de...
Definition: math.cpp:818
fakeIMU::getThreeAxisGyroscopeFrameName
bool getThreeAxisGyroscopeFrameName(size_t sens_index, std::string &frameName) const override
Get the name of the frame of the specified sensor.
Definition: fakeIMU.cpp:205
fakeIMU::getNrOfOrientationSensors
size_t getNrOfOrientationSensors() const override
Get the number of orientation sensors exposed by this device.
Definition: fakeIMU.cpp:304
fakeIMU::close
bool close() override
Close the DeviceDriver.
Definition: fakeIMU.cpp:75
yarp::sig::VectorOf< double >
fakeIMU::calibrate
bool calibrate(int ch, double v) override
Calibrate the sensor, single channel.
Definition: fakeIMU.cpp:121
fakeIMU::getThreeAxisGyroscopeMeasure
bool getThreeAxisGyroscopeMeasure(size_t sens_index, yarp::sig::Vector &out, double &timestamp) const override
Get the last reading of the gyroscope.
Definition: fakeIMU.cpp:210
fakeIMU::getThreeAxisLinearAccelerometerName
bool getThreeAxisLinearAccelerometerName(size_t sens_index, std::string &name) const override
Get the name of the specified sensor.
Definition: fakeIMU.cpp:238
fakeIMU::getNrOfThreeAxisMagnetometers
size_t getNrOfThreeAxisMagnetometers() const override
Get the number of magnetometers exposed by this device.
Definition: fakeIMU.cpp:266
fakeIMU::read
bool read(yarp::sig::Vector &out) override
Read a vector from the sensor.
Definition: fakeIMU.cpp:81
Stamp.h
fakeIMU::~fakeIMU
~fakeIMU() override
Definition: fakeIMU.cpp:54
yarp::os::Stamp::getTime
double getTime() const
Get the time stamp.
Definition: Stamp.cpp:37
fakeIMU.h
yarp::os::Searchable::check
virtual bool check(const std::string &key) const =0
Check if there exists a property of the given name.
yarp::sig::VectorOf::zero
void zero()
Zero the elements of the vector.
Definition: Vector.h:377
fakeIMU::getOrientationSensorStatus
yarp::dev::MAS_status getOrientationSensorStatus(size_t sens_index) const override
Get the status of the specified sensor.
Definition: fakeIMU.cpp:309
Thread.h
fakeIMU::getThreeAxisGyroscopeName
bool getThreeAxisGyroscopeName(size_t sens_index, std::string &name) const override
Get the name of the specified sensor.
Definition: fakeIMU.cpp:200
yarp::os::Searchable::find
virtual Value & find(const std::string &key) const =0
Gets a value corresponding to a given keyword.
fakeIMU::getThreeAxisMagnetometerName
bool getThreeAxisMagnetometerName(size_t sens_index, std::string &name) const override
Get the name of the specified sensor.
Definition: fakeIMU.cpp:276
Semaphore.h
fakeIMU::getThreeAxisLinearAccelerometerMeasure
bool getThreeAxisLinearAccelerometerMeasure(size_t sens_index, yarp::sig::Vector &out, double &timestamp) const override
Get the last reading of the specified sensor.
Definition: fakeIMU.cpp:248
yarp::os::PeriodicThread
An abstraction for a periodic thread.
Definition: PeriodicThread.h:25
fakeIMU::getNrOfThreeAxisLinearAccelerometers
size_t getNrOfThreeAxisLinearAccelerometers() const override
Get the number of three axis linear accelerometers exposed by this device.
Definition: fakeIMU.cpp:228
LogComponent.h
yarp::os::Stamp
An abstraction for a time stamp and/or sequence number.
Definition: Stamp.h:25
yarp::os::Value::asInt32
virtual std::int32_t asInt32() const
Get 32-bit integer value.
Definition: Value.cpp:207
yCInfo
#define yCInfo(component,...)
Definition: LogComponent.h:135
yarp::os
An interface to the operating system, including Port based communication.
Definition: AbstractCarrier.h:17
fakeIMU::open
bool open(yarp::os::Searchable &config) override
Open the DeviceDriver.
Definition: fakeIMU.cpp:59
fakeIMU::getThreeAxisGyroscopeStatus
yarp::dev::MAS_status getThreeAxisGyroscopeStatus(size_t sens_index) const override
Get the status of the specified sensor.
Definition: fakeIMU.cpp:195
fakeIMU::getOrientationSensorMeasureAsRollPitchYaw
bool getOrientationSensorMeasureAsRollPitchYaw(size_t sens_index, yarp::sig::Vector &rpy, double &timestamp) const override
Get the last reading of the orientation sensor as roll pitch yaw.
Definition: fakeIMU.cpp:324
fakeIMU::getThreeAxisLinearAccelerometerFrameName
bool getThreeAxisLinearAccelerometerFrameName(size_t sens_index, std::string &frameName) const override
Get the name of the frame of the specified sensor.
Definition: fakeIMU.cpp:243
yarp::dev::MAS_status
MAS_status
Status of a given analog sensor exposed by a multiple analog sensors interface.
Definition: MultipleAnalogSensorsInterfaces.h:37
yarp::os::PeriodicThread::stop
void stop()
Call this to stop the thread, this call blocks until the thread is terminated (and releaseThread() ca...
Definition: PeriodicThread.cpp:296
yarp::dev::MAS_ERROR
@ MAS_ERROR
The sensor is in generic error state.
Definition: MultipleAnalogSensorsInterfaces.h:39
Time.h
yarp::sig::VectorOf::size
size_t size() const
Definition: Vector.h:355
fakeIMU::getThreeAxisLinearAccelerometerStatus
yarp::dev::MAS_status getThreeAxisLinearAccelerometerStatus(size_t sens_index) const override
Get the status of the specified sensor.
Definition: fakeIMU.cpp:233
fakeIMU::getThreeAxisMagnetometerFrameName
bool getThreeAxisMagnetometerFrameName(size_t sens_index, std::string &frameName) const override
Get the name of the frame of the specified sensor.
Definition: fakeIMU.cpp:281
fakeIMU
fakeIMU : fake device implementing the device interface typically implemented by an Inertial Measurem...
Definition: fakeIMU.h:43
yarp::dev::MAS_OK
@ MAS_OK
The sensor is working correctly.
Definition: MultipleAnalogSensorsInterfaces.h:38
fakeIMU::getChannels
bool getChannels(int *nc) override
Get the number of channels of the sensor.
Definition: fakeIMU.cpp:115
fakeIMU::getOrientationSensorName
bool getOrientationSensorName(size_t sens_index, std::string &name) const override
Get the name of the specified sensor.
Definition: fakeIMU.cpp:314
fakeIMU::getThreeAxisMagnetometerStatus
yarp::dev::MAS_status getThreeAxisMagnetometerStatus(size_t sens_index) const override
Get the status of the specified sensor.
Definition: fakeIMU.cpp:271