YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
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 if (!this->parseParams(config)) {return false;}
33
35
36 //create the data vector:
37 this->m_channelsNum = 1;
38 m_orientation_sensors.resize(m_channelsNum);
39 m_position_sensors.resize(m_channelsNum);
40
41 return PeriodicThread::start();
42}
43
45{
46 yCTrace(FAKE_POSITION_SENSOR);
47 //stop the thread
48 PeriodicThread::stop();
49
50 return true;
51}
52
54{
55 yCTrace(FAKE_POSITION_SENSOR);
56 return true;
57}
58
60{
61 m_mutex.lock();
62
63 // Do fake stuff
64 double timeNow = yarp::os::Time::now();
65
66 for (size_t i = 0; i < m_position_sensors.size(); i++)
67 {
68 m_position_sensors[i].m_timestamp = timeNow;
69 m_position_sensors[i].m_status = yarp::dev::MAS_status::MAS_OK;
70 for (auto it= m_position_sensors[i].m_data.begin(); it != m_position_sensors[i].m_data.end(); it++)
71 {
72 *it = *it + 0.001;
73 }
74 }
75 for (size_t i = 0; i < m_orientation_sensors.size(); i++)
76 {
77 m_orientation_sensors[i].m_timestamp = timeNow;
78 m_orientation_sensors[i].m_status = yarp::dev::MAS_status::MAS_OK;
79 for (auto it = m_orientation_sensors[i].m_data.begin(); it != m_orientation_sensors[i].m_data.end(); it++)
80 {
81 *it = *it - 0.001;
82 }
83 }
84
85 m_mutex.unlock();
86}
87
89{
90 yCTrace(FAKE_POSITION_SENSOR);
91}
92
93
95{
96 std::lock_guard<std::mutex> myLockGuard(m_mutex);
97 return m_position_sensors.size();
98}
99
101{
102 std::lock_guard<std::mutex> myLockGuard (m_mutex);
103 if (sens_index >= m_position_sensors.size()) return yarp::dev::MAS_status::MAS_UNKNOWN;
104 return m_position_sensors[sens_index].m_status;
105}
106
107bool FakePositionSensor::getPositionSensorName(size_t sens_index, std::string& name) const
108{
109 std::lock_guard<std::mutex> myLockGuard(m_mutex);
110 if (sens_index >= m_position_sensors.size()) return false;
111 name = m_position_sensors[sens_index].m_name;
112 return true;
113}
114
115bool FakePositionSensor::getPositionSensorFrameName(size_t sens_index, std::string& frameName) const
116{
117 std::lock_guard<std::mutex> myLockGuard(m_mutex);
118 if (sens_index >= m_position_sensors.size()) return false;
119 frameName = m_position_sensors[sens_index].m_framename;
120 return true;
121}
122
123bool FakePositionSensor::getPositionSensorMeasure(size_t sens_index, yarp::sig::Vector& xyz, double& timestamp) const
124{
125 std::lock_guard<std::mutex> myLockGuard(m_mutex);
126 if (sens_index >= m_position_sensors.size()) return false;
127 timestamp = m_position_sensors[sens_index].m_timestamp;
128 xyz = m_position_sensors[sens_index].m_data;
129 return true;
130}
131
133{
134 std::lock_guard<std::mutex> myLockGuard(m_mutex);
135 return m_orientation_sensors.size();
136}
137
139{
140 std::lock_guard<std::mutex> myLockGuard(m_mutex);
141 if (sens_index >= m_orientation_sensors.size()) return yarp::dev::MAS_status::MAS_UNKNOWN;
142 return m_orientation_sensors[sens_index].m_status;
143}
144
145bool FakePositionSensor::getOrientationSensorName(size_t sens_index, std::string& name) const
146{
147 std::lock_guard<std::mutex> myLockGuard(m_mutex);
148 if (sens_index >= m_orientation_sensors.size()) return false;
149 name = m_orientation_sensors[sens_index].m_name;
150 return true;
151}
152
153bool FakePositionSensor::getOrientationSensorFrameName(size_t sens_index, std::string& frameName) const
154{
155 std::lock_guard<std::mutex> myLockGuard(m_mutex);
156 if (sens_index >= m_orientation_sensors.size()) return false;
157 frameName = m_orientation_sensors[sens_index].m_framename;
158 return true;
159}
160
161bool FakePositionSensor::getOrientationSensorMeasureAsRollPitchYaw(size_t sens_index, yarp::sig::Vector& xyz, double& timestamp) const
162{
163 std::lock_guard<std::mutex> myLockGuard(m_mutex);
164 if (sens_index >= m_orientation_sensors.size()) return false;
165 timestamp = m_orientation_sensors[sens_index].m_timestamp;
166 xyz = m_orientation_sensors[sens_index].m_data;
167 return true;
168}
bool parseParams(const yarp::os::Searchable &config) override
Parse the DeviceDriver parameters.
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:31
#define yCTrace(component,...)
#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_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