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