SensorDevice.h
Go to the documentation of this file.
1/*
2 * This file is part of ArmarX.
3 *
4 * ArmarX is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * ArmarX is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 * @package RobotAPI
17 * @author Raphael Grimm ( raphael dot grimm at kit dot edu )
18 * @date 2017
19 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20 * GNU General Public License
21 */
22
23#pragma once
24
25#include <IceUtil/Time.h>
26
28#include "../util.h"
29#include "DeviceBase.h"
30
32{
34}
35
37{
38 using namespace DeviceTags;
39}
40
41namespace armarx
42{
44
45 /**
46 * @brief This class represents some part of the robot providing sensor values.
47 *
48 * The \ref SensorValueBase "SensorValue" is accessed via \ref getSensorValue.
49 *
50 * A \ref SensorDevice could also represent a virtual sensor reporting some status of the software
51 * (e.g. Timings):
52 * Examples for \ref SensorValueBase "Sensorvalues" are:
53 * \li Position, Velocity, Torque, Current, Temperature of an actuator
54 * \li Forces and Torques of an Force-Torque-Sensor
55 * \li Timings of the \ref ControlThread
56 * \li Level of a battery power supply
57 * \li 3d Velocity of a mobile Platform
58 */
59 class SensorDevice : public virtual DeviceBase
60 {
61 public:
62 /// @brief A static const nullptr in case a const ref to a nullptr needs to be returned
63 static const SensorDevicePtr NullPtr;
64
65 /// @brief Create a SensorDevice with the given name
66 SensorDevice(const std::string& name) : DeviceBase{name}
67 {
68 }
69
70 /// @return The SensorDevice's sensor value
71 virtual const SensorValueBase* getSensorValue() const = 0;
72
73 /// @return The SensorDevice's sensor value casted to the given type (may be nullptr)
74 template <class T>
75 const T*
77 {
78 return getSensorValue()->asA<const T*>();
79 }
80
81 /**
82 * @brief Returns the \ref SensorValueBase "SensorValue's" type as a string.
83 * @param withoutNamespaceSpecifier Whether namespace specifiers should be removed from the name.
84 * @return The \ref SensorValueBase "SensorValues" type as a string.
85 * @see getSensorValue
86 */
87 std::string getSensorValueType(bool withoutNamespaceSpecifier = false) const;
88
89 /// @return The reporting frame name of this sensor (e.g. The frame some force torque values are in).
90 /// This may be empty for virtual sensors e.g. execution time sensors.
91 virtual std::string getReportingFrame() const;
92
93 /**
94 * @brief This is a hook for implementations to read the sensor value from a bus.
95 * @param sensorValuesTimestamp The current timestamp
96 * @param timeSinceLastIteration The time delta since the last call
97 */
98 virtual void
99 rtReadSensorValues(const IceUtil::Time& sensorValuesTimestamp,
100 const IceUtil::Time& timeSinceLastIteration)
101 {
102 }
103
104 private:
106 /// @brief The owning \ref RobotUnit. (is set when this \ref SensorDevice is added to the \ref RobotUnit)
107 std::atomic<RobotUnitModule::Devices*> owner{nullptr};
108 };
109
110 template <class SensorValueType>
111 class SensorDeviceTemplate : public virtual SensorDevice
112 {
113 static_assert(std::is_base_of<SensorValueBase, SensorValueType>::value,
114 "SensorValueType has to inherit SensorValueBase");
115
116 public:
117 SensorDeviceTemplate(const std::string& name) : DeviceBase(name), SensorDevice(name)
118 {
119 }
120
121 const SensorValueType* getSensorValue() const final override;
122
123 SensorValueType sensorValue;
124 };
125} // namespace armarx
126
127//inline functions
128namespace armarx
129{
130 inline std::string
131 SensorDevice::getSensorValueType(bool withoutNamespaceSpecifier) const
132 {
133 return getSensorValue()->getSensorValueType(withoutNamespaceSpecifier);
134 }
135
136 inline std::string
138 {
139 return {};
140 }
141
142 template <class SensorValueType>
143 inline const SensorValueType*
148} // namespace armarx
#define TYPEDEF_PTRS_SHARED(T)
#define TYPEDEF_PTRS_HANDLE(T)
DeviceBase(const std::string &name)
Create a Device with the given name.
Definition DeviceBase.h:33
This Module manages sensor and control devices for a RobotUnit and only allows save and sane access.
SensorDeviceTemplate(const std::string &name)
const SensorValueType * getSensorValue() const final override
This class represents some part of the robot providing sensor values.
virtual std::string getReportingFrame() const
std::string getSensorValueType(bool withoutNamespaceSpecifier=false) const
Returns the SensorValue's type as a string.
const T * getSensorValue() const
virtual void rtReadSensorValues(const IceUtil::Time &sensorValuesTimestamp, const IceUtil::Time &timeSinceLastIteration)
This is a hook for implementations to read the sensor value from a bus.
static const SensorDevicePtr NullPtr
A static const nullptr in case a const ref to a nullptr needs to be returned.
SensorDevice(const std::string &name)
Create a SensorDevice with the given name.
virtual const SensorValueBase * getSensorValue() const =0
The SensorValueBase class.
const T * asA() const
This file offers overloads of toIce() and fromIce() functions for STL container types.