YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
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
27
28constexpr double EARTH_GRAVITY = -9.81;
29}
30
37 rpy({0.0, 0.0, 0.0}),
38 gravity({0.0, 0.0, EARTH_GRAVITY, 0.0}),
39 dcm(4, 4),
40 accels({0.0, 0.0, 0.0, 0.0}),
41 nchannels(DEFAULT_NCHANNELS),
42 dummy_value(DEFAULT_DUMMY_VALUE)
43{
44 dcm.zero();
45}
46
48{
49 close();
50}
51
53{
54 if (!this->parseParams(config)) {return false;}
55
56 double dperiod = m_period / 1000.0;
58
59 start();
60 return true;
61}
62
64{
66 return true;
67}
68
69bool FakeIMU::threadInit()
70{
71 lastStamp.update();
72 return true;
73}
74
75void FakeIMU::run()
76{
77 static double count=10;
78
79 rpy[0] = 0;
80 rpy[1] = count * 3.14/180;
81 rpy[2] = 0;
82
83 dcm = rpy2dcm(rpy);
84 accels = gravity * dcm;
85
86 lastStamp.update();
87
88 dummy_value = count;
89 if (!m_constantValue) {
90 count++;
91 }
92
93 if (count >= 360) {
94 count = 0;
95 }
96}
97
98yarp::dev::MAS_status FakeIMU::genericGetStatus(size_t sens_index) const
99{
100 if (sens_index!=0) {
102 }
103
105}
106
107bool FakeIMU::genericGetSensorName(size_t sens_index, std::string &name) const
108{
109 if (sens_index!=0) {
110 return false;
111 }
112
113 name = m_sensorName;
114 return true;
115}
116
117bool FakeIMU::genericGetFrameName(size_t sens_index, std::string &frameName) const
118{
119 if (sens_index!=0) {
120 return false;
121 }
122
123 frameName = m_frameName;
124 return true;
125}
126
128{
129 return 1;
130}
131
133{
134 return genericGetStatus(sens_index);
135}
136
137bool FakeIMU::getThreeAxisGyroscopeName(size_t sens_index, std::string &name) const
138{
139 return genericGetSensorName(sens_index, name);
140}
141
142bool FakeIMU::getThreeAxisGyroscopeFrameName(size_t sens_index, std::string &frameName) const
143{
144 return genericGetFrameName(sens_index, frameName);
145}
146
147bool FakeIMU::getThreeAxisGyroscopeMeasure(size_t sens_index, yarp::sig::Vector& out, double& timestamp) const
148{
149 if (sens_index!=0) {
150 return false;
151 }
152
153 out.resize(3);
154 out[0] = dummy_value;
155 out[1] = dummy_value;
156 out[2] = dummy_value;
157
158 // Workaround for https://github.com/robotology/yarp/issues/1610
159 yarp::os::Stamp copyStamp(lastStamp);
160 timestamp = copyStamp.getTime();
161
162 return true;
163}
164
166{
167 return 1;
168}
169
174
175bool FakeIMU::getThreeAxisLinearAccelerometerName(size_t sens_index, std::string &name) const
176{
177 return genericGetSensorName(sens_index, name);
178}
179
180bool FakeIMU::getThreeAxisLinearAccelerometerFrameName(size_t sens_index, std::string &frameName) const
181{
182 return genericGetFrameName(sens_index, frameName);
183}
184
186{
187 if (sens_index!=0) {
188 return false;
189 }
190
191 out.resize(3);
192 out[0] = accels[0];
193 out[1] = accels[1];
194 out[2] = accels[2];
195
196 // Workaround for https://github.com/robotology/yarp/issues/1610
197 yarp::os::Stamp copyStamp(lastStamp);
198 timestamp = copyStamp.getTime();
199
200 return true;
201}
202
204{
205 return 1;
206}
207
209{
210 return genericGetStatus(sens_index);
211}
212
213bool FakeIMU::getThreeAxisMagnetometerName(size_t sens_index, std::string &name) const
214{
215 return genericGetSensorName(sens_index, name);
216}
217
218bool FakeIMU::getThreeAxisMagnetometerFrameName(size_t sens_index, std::string &frameName) const
219{
220 return genericGetFrameName(sens_index, frameName);
221}
222
224{
225 if (sens_index!=0) {
226 return false;
227 }
228
229 out.resize(3);
230 out[0] = dummy_value;
231 out[1] = dummy_value;
232 out[2] = dummy_value;
233
234 // Workaround for https://github.com/robotology/yarp/issues/1610
235 yarp::os::Stamp copyStamp(lastStamp);
236 timestamp = copyStamp.getTime();
237
238 return true;
239}
240
242{
243 return 1;
244}
245
247{
248 return genericGetStatus(sens_index);
249}
250
251bool FakeIMU::getOrientationSensorName(size_t sens_index, std::string &name) const
252{
253 return genericGetSensorName(sens_index, name);
254}
255
256bool FakeIMU::getOrientationSensorFrameName(size_t sens_index, std::string &frameName) const
257{
258 return genericGetFrameName(sens_index, frameName);
259}
260
262{
263 if (sens_index!=0) {
264 return false;
265 }
266
267 rpy_out.resize(3);
268 rpy_out[0] = dummy_value;
269 rpy_out[1] = dummy_value;
270 rpy_out[2] = dummy_value;
271
272 // Workaround for https://github.com/robotology/yarp/issues/1610
273 yarp::os::Stamp copyStamp(lastStamp);
274 timestamp = copyStamp.getTime();
275
276 return true;
277}
const double DEFAULT_PERIOD
bool parseParams(const yarp::os::Searchable &config) override
Parse the DeviceDriver parameters.
fakeIMU : fake device implementing the device interface typically implemented by an Inertial Measurem...
Definition FakeIMU.h:29
bool getThreeAxisMagnetometerFrameName(size_t sens_index, std::string &frameName) const override
Get the name of the frame of the specified sensor.
Definition FakeIMU.cpp:218
yarp::dev::MAS_status getThreeAxisGyroscopeStatus(size_t sens_index) const override
Get the status of the specified sensor.
Definition FakeIMU.cpp:132
~FakeIMU() override
Definition FakeIMU.cpp:47
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:185
bool getOrientationSensorFrameName(size_t sens_index, std::string &frameName) const override
Get the name of the frame of the specified sensor.
Definition FakeIMU.cpp:256
size_t getNrOfOrientationSensors() const override
Get the number of orientation sensors exposed by this device.
Definition FakeIMU.cpp:241
yarp::dev::MAS_status getThreeAxisLinearAccelerometerStatus(size_t sens_index) const override
Get the status of the specified sensor.
Definition FakeIMU.cpp:170
bool getThreeAxisLinearAccelerometerName(size_t sens_index, std::string &name) const override
Get the name of the specified sensor.
Definition FakeIMU.cpp:175
bool getThreeAxisGyroscopeName(size_t sens_index, std::string &name) const override
Get the name of the specified sensor.
Definition FakeIMU.cpp:137
bool getThreeAxisGyroscopeMeasure(size_t sens_index, yarp::sig::Vector &out, double &timestamp) const override
Get the last reading of the gyroscope.
Definition FakeIMU.cpp:147
bool close() override
Close the DeviceDriver.
Definition FakeIMU.cpp:63
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:223
bool getThreeAxisMagnetometerName(size_t sens_index, std::string &name) const override
Get the name of the specified sensor.
Definition FakeIMU.cpp:213
size_t getNrOfThreeAxisLinearAccelerometers() const override
Get the number of three axis linear accelerometers exposed by this device.
Definition FakeIMU.cpp:165
bool getThreeAxisGyroscopeFrameName(size_t sens_index, std::string &frameName) const override
Get the name of the frame of the specified sensor.
Definition FakeIMU.cpp:142
bool open(yarp::os::Searchable &config) override
Open the DeviceDriver.
Definition FakeIMU.cpp:52
size_t getNrOfThreeAxisMagnetometers() const override
Get the number of magnetometers exposed by this device.
Definition FakeIMU.cpp:203
bool getOrientationSensorName(size_t sens_index, std::string &name) const override
Get the name of the specified sensor.
Definition FakeIMU.cpp:251
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:261
size_t getNrOfThreeAxisGyroscopes() const override
Get the number of three axis gyroscopes exposed by this sensor.
Definition FakeIMU.cpp:127
bool getThreeAxisLinearAccelerometerFrameName(size_t sens_index, std::string &frameName) const override
Get the name of the frame of the specified sensor.
Definition FakeIMU.cpp:180
yarp::dev::MAS_status getThreeAxisMagnetometerStatus(size_t sens_index) const override
Get the status of the specified sensor.
Definition FakeIMU.cpp:208
yarp::dev::MAS_status getOrientationSensorStatus(size_t sens_index) const override
Get the status of the specified sensor.
Definition FakeIMU.cpp:246
A mini-server for performing network communication in the background.
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:31
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
void resize(size_t size) override
Resize the vector.
Definition Vector.h:221
#define YARP_LOG_COMPONENT(name,...)
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.