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>
13#include <yarp/os/LogStream.h>
14
15#include <string>
16
17using namespace yarp::os;
18using namespace yarp::dev;
19using namespace yarp::sig;
20using namespace yarp::math;
21
22namespace {
23YARP_LOG_COMPONENT(FAKEIMU, "yarp.device.fakeIMU")
24constexpr double DEFAULT_PERIOD = 0.01; // seconds
25constexpr int DEFAULT_NCHANNELS = 12;
26constexpr double DEFAULT_DUMMY_VALUE = 0.0;
27constexpr const char* DEFAULT_SENSOR_NAME = "sensorName";
28constexpr const char* DEFAULT_FRAME_NAME = "frameName";
29
30constexpr 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{
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
114{
115 *nc=nchannels;
116 return true;
117}
118
119bool fakeIMU::calibrate(int ch, double v)
120{
121 yCWarning(FAKEIMU, "Not implemented yet");
122 return false;
123}
124
125bool fakeIMU::threadInit()
126{
127 lastStamp.update();
128 return true;
129}
130
131
132void 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
155yarp::dev::MAS_status fakeIMU::genericGetStatus(size_t sens_index) const
156{
157 if (sens_index!=0) {
159 }
160
162}
163
164bool fakeIMU::genericGetSensorName(size_t sens_index, std::string &name) const
165{
166 if (sens_index!=0) {
167 return false;
168 }
169
170 name = m_sensorName;
171 return true;
172}
173
174bool fakeIMU::genericGetFrameName(size_t sens_index, std::string &frameName) const
175{
176 if (sens_index!=0) {
177 return false;
178 }
179
180 frameName = m_frameName;
181 return true;
182}
183
185{
186 return 1;
187}
188
190{
191 return genericGetStatus(sens_index);
192}
193
194bool fakeIMU::getThreeAxisGyroscopeName(size_t sens_index, std::string &name) const
195{
196 return genericGetSensorName(sens_index, name);
197}
198
199bool fakeIMU::getThreeAxisGyroscopeFrameName(size_t sens_index, std::string &frameName) const
200{
201 return genericGetFrameName(sens_index, frameName);
202}
203
204bool fakeIMU::getThreeAxisGyroscopeMeasure(size_t sens_index, yarp::sig::Vector& out, double& timestamp) const
205{
206 if (sens_index!=0) {
207 return false;
208 }
209
210 out.resize(3);
211 out[0] = dummy_value;
212 out[1] = dummy_value;
213 out[2] = dummy_value;
214
215 // Workaround for https://github.com/robotology/yarp/issues/1610
216 yarp::os::Stamp copyStamp(lastStamp);
217 timestamp = copyStamp.getTime();
218
219 return true;
220}
221
223{
224 return 1;
225}
226
228{
229 return genericGetStatus(sens_index);
230}
231
232bool fakeIMU::getThreeAxisLinearAccelerometerName(size_t sens_index, std::string &name) const
233{
234 return genericGetSensorName(sens_index, name);
235}
236
237bool fakeIMU::getThreeAxisLinearAccelerometerFrameName(size_t sens_index, std::string &frameName) const
238{
239 return genericGetFrameName(sens_index, frameName);
240}
241
242bool fakeIMU::getThreeAxisLinearAccelerometerMeasure(size_t sens_index, yarp::sig::Vector& out, double& timestamp) const
243{
244 if (sens_index!=0) {
245 return false;
246 }
247
248 out.resize(3);
249 out[0] = accels[0];
250 out[1] = accels[1];
251 out[2] = accels[2];
252
253 // Workaround for https://github.com/robotology/yarp/issues/1610
254 yarp::os::Stamp copyStamp(lastStamp);
255 timestamp = copyStamp.getTime();
256
257 return true;
258}
259
261{
262 return 1;
263}
264
266{
267 return genericGetStatus(sens_index);
268}
269
270bool fakeIMU::getThreeAxisMagnetometerName(size_t sens_index, std::string &name) const
271{
272 return genericGetSensorName(sens_index, name);
273}
274
275bool fakeIMU::getThreeAxisMagnetometerFrameName(size_t sens_index, std::string &frameName) const
276{
277 return genericGetFrameName(sens_index, frameName);
278}
279
280bool fakeIMU::getThreeAxisMagnetometerMeasure(size_t sens_index, yarp::sig::Vector& out, double& timestamp) const
281{
282 if (sens_index!=0) {
283 return false;
284 }
285
286 out.resize(3);
287 out[0] = dummy_value;
288 out[1] = dummy_value;
289 out[2] = dummy_value;
290
291 // Workaround for https://github.com/robotology/yarp/issues/1610
292 yarp::os::Stamp copyStamp(lastStamp);
293 timestamp = copyStamp.getTime();
294
295 return true;
296}
297
299{
300 return 1;
301}
302
304{
305 return genericGetStatus(sens_index);
306}
307
308bool fakeIMU::getOrientationSensorName(size_t sens_index, std::string &name) const
309{
310 return genericGetSensorName(sens_index, name);
311}
312
313bool fakeIMU::getOrientationSensorFrameName(size_t sens_index, std::string &frameName) const
314{
315 return genericGetFrameName(sens_index, frameName);
316}
317
318bool fakeIMU::getOrientationSensorMeasureAsRollPitchYaw(size_t sens_index, yarp::sig::Vector& rpy_out, double& timestamp) const
319{
320 if (sens_index!=0) {
321 return false;
322 }
323
324 rpy_out.resize(3);
325 rpy_out[0] = dummy_value;
326 rpy_out[1] = dummy_value;
327 rpy_out[2] = dummy_value;
328
329 // Workaround for https://github.com/robotology/yarp/issues/1610
330 yarp::os::Stamp copyStamp(lastStamp);
331 timestamp = copyStamp.getTime();
332
333 return true;
334}
fakeIMU : fake device implementing the device interface typically implemented by an Inertial Measurem...
Definition: fakeIMU.h:38
size_t getNrOfOrientationSensors() const override
Get the number of orientation sensors exposed by this device.
Definition: fakeIMU.cpp:298
bool getThreeAxisGyroscopeFrameName(size_t sens_index, std::string &frameName) const override
Get the name of the frame of the specified sensor.
Definition: fakeIMU.cpp:199
bool getThreeAxisMagnetometerName(size_t sens_index, std::string &name) const override
Get the name of the specified sensor.
Definition: fakeIMU.cpp:270
bool getThreeAxisGyroscopeName(size_t sens_index, std::string &name) const override
Get the name of the specified sensor.
Definition: fakeIMU.cpp:194
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:189
size_t getNrOfThreeAxisMagnetometers() const override
Get the number of magnetometers exposed by this device.
Definition: fakeIMU.cpp:260
bool getThreeAxisLinearAccelerometerFrameName(size_t sens_index, std::string &frameName) const override
Get the name of the frame of the specified sensor.
Definition: fakeIMU.cpp:237
yarp::dev::MAS_status getThreeAxisMagnetometerStatus(size_t sens_index) const override
Get the status of the specified sensor.
Definition: fakeIMU.cpp:265
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:318
bool getOrientationSensorFrameName(size_t sens_index, std::string &frameName) const override
Get the name of the frame of the specified sensor.
Definition: fakeIMU.cpp:313
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:280
yarp::dev::MAS_status getThreeAxisLinearAccelerometerStatus(size_t sens_index) const override
Get the status of the specified sensor.
Definition: fakeIMU.cpp:227
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:242
size_t getNrOfThreeAxisLinearAccelerometers() const override
Get the number of three axis linear accelerometers exposed by this device.
Definition: fakeIMU.cpp:222
bool getThreeAxisGyroscopeMeasure(size_t sens_index, yarp::sig::Vector &out, double &timestamp) const override
Get the last reading of the gyroscope.
Definition: fakeIMU.cpp:204
bool getOrientationSensorName(size_t sens_index, std::string &name) const override
Get the name of the specified sensor.
Definition: fakeIMU.cpp:308
bool getThreeAxisLinearAccelerometerName(size_t sens_index, std::string &name) const override
Get the name of the specified sensor.
Definition: fakeIMU.cpp:232
yarp::dev::MAS_status getOrientationSensorStatus(size_t sens_index) const override
Get the status of the specified sensor.
Definition: fakeIMU.cpp:303
bool getThreeAxisMagnetometerFrameName(size_t sens_index, std::string &frameName) const override
Get the name of the frame of the specified sensor.
Definition: fakeIMU.cpp:275
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:184
An abstraction for a periodic thread.
bool setPeriod(double period)
Set the (new) period of the thread.
bool start()
Call this to start the 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:63
virtual bool check(const std::string &key) const =0
Check if there exists a property of the given name.
virtual Value & find(const std::string &key) const =0
Gets a value corresponding to a given keyword.
An abstraction for a time stamp and/or sequence number.
Definition: Stamp.h:21
void update()
Set the timestamp to the current time, and increment the sequence number (wrapping to 0 if the sequen...
Definition: Stamp.cpp:124
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:220
size_t size() const
Definition: Vector.h:321
void zero()
Zero the elements of the vector.
Definition: Vector.h:343
#define yCInfo(component,...)
Definition: LogComponent.h:171
#define yCWarning(component,...)
Definition: LogComponent.h:192
#define YARP_LOG_COMPONENT(name,...)
Definition: LogComponent.h:76
For streams capable of holding different kinds of content, check what they actually have.
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.