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 
27 #include "../SensorValues/SensorValueBase.h"
28 #include "../util.h"
29 #include "DeviceBase.h"
30 
32 {
33  TYPEDEF_PTRS_HANDLE(Devices);
34 }
35 
37 {
38  using namespace DeviceTags;
39 }
40 
41 namespace 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  {
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
128 namespace 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*
145  {
146  return &sensorValue;
147  }
148 } // namespace armarx
armarx::SensorDevice::NullPtr
static const SensorDevicePtr NullPtr
A static const nullptr in case a const ref to a nullptr needs to be returned.
Definition: SensorDevice.h:63
armarx::SensorDevice::getReportingFrame
virtual std::string getReportingFrame() const
Definition: SensorDevice.h:137
armarx::SensorDeviceTemplate
Definition: SensorDevice.h:111
armarx::SensorDevice::rtReadSensorValues
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.
Definition: SensorDevice.h:99
armarx::SensorDeviceTags
Definition: SensorDevice.h:36
armarx::SensorDevice::getSensorValue
const T * getSensorValue() const
Definition: SensorDevice.h:76
armarx::DeviceBase
Definition: DeviceBase.h:29
armarx::RobotUnitModule::TYPEDEF_PTRS_HANDLE
TYPEDEF_PTRS_HANDLE(Devices)
armarx::TYPEDEF_PTRS_SHARED
TYPEDEF_PTRS_SHARED(ControlDevice)
armarx::SensorDeviceTemplate::SensorDeviceTemplate
SensorDeviceTemplate(const std::string &name)
Definition: SensorDevice.h:117
cxxopts::value
std::shared_ptr< Value > value()
Definition: cxxopts.hpp:926
armarx::SensorDevice
This class represents some part of the robot providing sensor values.
Definition: SensorDevice.h:59
armarx::SensorDeviceTemplate::getSensorValue
const SensorValueType * getSensorValue() const final override
Definition: SensorDevice.h:144
armarx::armem::Time
armarx::core::time::DateTime Time
Definition: forward_declarations.h:13
DeviceBase.h
armarx::RobotUnitModule::Devices
This Module manages sensor and control devices for a RobotUnit and only allows save and sane access.
Definition: RobotUnitModuleDevices.h:67
armarx::SensorDevice::getSensorValueType
std::string getSensorValueType(bool withoutNamespaceSpecifier=false) const
Returns the SensorValue's type as a string.
Definition: SensorDevice.h:131
armarx::SensorDevice::SensorDevice
SensorDevice(const std::string &name)
Create a SensorDevice with the given name.
Definition: SensorDevice.h:66
armarx::RobotUnitModule
Definition: ControlDevice.h:34
T
float T
Definition: UnscentedKalmanFilterTest.cpp:35
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28