YARP
Yet Another Robot Platform
fakePositionSensor.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
7
8#include <yarp/os/Time.h>
10#include <yarp/os/LogStream.h>
11
12using namespace yarp::dev;
13
14namespace {
15YARP_LOG_COMPONENT(FAKE_POSITION_SENSOR, "yarp.device.fakePositionSensor")
16}
17
18FakePositionSensor::FakePositionSensor(double period) : PeriodicThread(period),
19 m_mutex(),
20 m_channelsNum(1)
21{
22 yCTrace(FAKE_POSITION_SENSOR);
23}
24
26{
27 yCTrace(FAKE_POSITION_SENSOR);
28}
29
31{
32 yCTrace(FAKE_POSITION_SENSOR);
33 bool correct=true;
34
35 //debug
36 fprintf(stderr, "%s\n", config.toString().c_str());
37
38 if (!correct)
39 {
40 yCError(FAKE_POSITION_SENSOR) << "Insufficient parameters to FakePositionSensor\n";
41 return false;
42 }
43
44 if (config.check("sensor_period"))
45 {
46 double period=config.find("sensor_period").asFloat32();
47 setPeriod(period);
48 }
49
50 //create the data vector:
51 this->m_channelsNum = 1;
52 m_orientation_sensors.resize(m_channelsNum);
53 m_position_sensors.resize(m_channelsNum);
54
55 return PeriodicThread::start();
56}
57
59{
60 yCTrace(FAKE_POSITION_SENSOR);
61 //stop the thread
62 PeriodicThread::stop();
63
64 return true;
65}
66
68{
69 yCTrace(FAKE_POSITION_SENSOR);
70 return true;
71}
72
74{
75 m_mutex.lock();
76
77 // Do fake stuff
78 double timeNow = yarp::os::Time::now();
79
80 for (size_t i = 0; i < m_position_sensors.size(); i++)
81 {
82 m_position_sensors[i].m_timestamp = timeNow;
83 m_position_sensors[i].m_status = yarp::dev::MAS_status::MAS_OK;
84 for (auto it= m_position_sensors[i].m_data.begin(); it != m_position_sensors[i].m_data.end(); it++)
85 {
86 *it = *it + 0.001;
87 }
88 }
89 for (size_t i = 0; i < m_orientation_sensors.size(); i++)
90 {
91 m_orientation_sensors[i].m_timestamp = timeNow;
92 m_orientation_sensors[i].m_status = yarp::dev::MAS_status::MAS_OK;
93 for (auto it = m_orientation_sensors[i].m_data.begin(); it != m_orientation_sensors[i].m_data.end(); it++)
94 {
95 *it = *it - 0.001;
96 }
97 }
98
99 m_mutex.unlock();
100}
101
103{
104 yCTrace(FAKE_POSITION_SENSOR);
105}
106
107
109{
110 std::lock_guard<std::mutex> myLockGuard(m_mutex);
111 return m_position_sensors.size();
112}
113
115{
116 std::lock_guard<std::mutex> myLockGuard (m_mutex);
117 if (sens_index >= m_position_sensors.size()) return yarp::dev::MAS_status::MAS_UNKNOWN;
118 return m_position_sensors[sens_index].m_status;
119}
120
121bool FakePositionSensor::getPositionSensorName(size_t sens_index, std::string& name) const
122{
123 std::lock_guard<std::mutex> myLockGuard(m_mutex);
124 if (sens_index >= m_position_sensors.size()) return false;
125 name = m_position_sensors[sens_index].m_name;
126 return true;
127}
128
129bool FakePositionSensor::getPositionSensorFrameName(size_t sens_index, std::string& frameName) const
130{
131 std::lock_guard<std::mutex> myLockGuard(m_mutex);
132 if (sens_index >= m_position_sensors.size()) return false;
133 frameName = m_position_sensors[sens_index].m_framename;
134 return true;
135}
136
137bool FakePositionSensor::getPositionSensorMeasure(size_t sens_index, yarp::sig::Vector& xyz, double& timestamp) const
138{
139 std::lock_guard<std::mutex> myLockGuard(m_mutex);
140 if (sens_index >= m_position_sensors.size()) return false;
141 timestamp = m_position_sensors[sens_index].m_timestamp;
142 xyz = m_position_sensors[sens_index].m_data;
143 return true;
144}
145
147{
148 std::lock_guard<std::mutex> myLockGuard(m_mutex);
149 return m_orientation_sensors.size();
150}
151
153{
154 std::lock_guard<std::mutex> myLockGuard(m_mutex);
155 if (sens_index >= m_orientation_sensors.size()) return yarp::dev::MAS_status::MAS_UNKNOWN;
156 return m_orientation_sensors[sens_index].m_status;
157}
158
159bool FakePositionSensor::getOrientationSensorName(size_t sens_index, std::string& name) const
160{
161 std::lock_guard<std::mutex> myLockGuard(m_mutex);
162 if (sens_index >= m_orientation_sensors.size()) return false;
163 name = m_orientation_sensors[sens_index].m_name;
164 return true;
165}
166
167bool FakePositionSensor::getOrientationSensorFrameName(size_t sens_index, std::string& frameName) const
168{
169 std::lock_guard<std::mutex> myLockGuard(m_mutex);
170 if (sens_index >= m_orientation_sensors.size()) return false;
171 frameName = m_orientation_sensors[sens_index].m_framename;
172 return true;
173}
174
175bool FakePositionSensor::getOrientationSensorMeasureAsRollPitchYaw(size_t sens_index, yarp::sig::Vector& xyz, double& timestamp) const
176{
177 std::lock_guard<std::mutex> myLockGuard(m_mutex);
178 if (sens_index >= m_orientation_sensors.size()) return false;
179 timestamp = m_orientation_sensors[sens_index].m_timestamp;
180 xyz = m_orientation_sensors[sens_index].m_data;
181 return true;
182}
FakePositionSensor(double period=0.05)
bool getOrientationSensorFrameName(size_t sens_index, std::string &frameName) const override
Get the name of the frame of the specified sensor.
bool close() override
Close the DeviceDriver.
bool open(yarp::os::Searchable &config) override
Open the DeviceDriver.
yarp::dev::MAS_status getPositionSensorStatus(size_t sens_index) const override
Get the status of the specified sensor.
bool getOrientationSensorMeasureAsRollPitchYaw(size_t sens_index, yarp::sig::Vector &xyz, double &timestamp) const override
Get the last reading of the orientation sensor as roll pitch yaw.
bool getPositionSensorMeasure(size_t sens_index, yarp::sig::Vector &xyz, double &timestamp) const override
Get the last reading of the position sensor as x y z.
yarp::dev::MAS_status getOrientationSensorStatus(size_t sens_index) const override
Get the status of the specified sensor.
void run() override
Loop function.
bool getPositionSensorFrameName(size_t sens_index, std::string &frameName) const override
Get the name of the frame of the specified sensor.
bool threadInit() override
Initialization method.
size_t getNrOfOrientationSensors() const override
Get the number of orientation sensors exposed by this device.
void threadRelease() override
Release method.
bool getPositionSensorName(size_t sens_index, std::string &name) const override
Get the name of the specified sensor.
size_t getNrOfPositionSensors() const override
Get the number of position sensors exposed by this device.
bool getOrientationSensorName(size_t sens_index, std::string &name) const override
Get the name of the specified sensor.
bool setPeriod(double period)
Set the (new) period of the thread.
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 std::string toString() const =0
Return a standard text representation of the content of the object.
virtual Value & find(const std::string &key) const =0
Gets a value corresponding to a given keyword.
virtual yarp::conf::float32_t asFloat32() const
Get 32-bit floating point value.
Definition: Value.cpp:216
#define yCError(component,...)
Definition: LogComponent.h:213
#define yCTrace(component,...)
Definition: LogComponent.h:84
#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_UNKNOWN
The sensor is in an unknown state.
@ MAS_OK
The sensor is working correctly.
double now()
Return the current time in seconds, relative to an arbitrary starting point.
Definition: Time.cpp:121