RTUnit.h
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * Copyright (C) 2011-2017, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5  *
6  * ArmarX is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * ArmarX is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * @package ArmarX
19  * @author Mirko Waechter( mirko.waechter at kit dot edu)
20  * @date 2018
21  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22  * GNU General Public License
23  */
24 
25 #pragma once
26 
27 
28 #include <array>
29 #include <fstream>
30 #include <memory>
31 #include <sstream>
32 #include <thread>
33 
37 
39 
41 
42 /**
43  * @defgroup Library-ethercat ethercat
44  * @ingroup armarx_control
45  * A description of the library ethercat.
46  */
48 {
49 
50  /**
51  * @class RTUnit
52  * @ingroup Library-ethercat
53  * @brief Brief description of class RTUnit.
54  *
55  * Detailed description of class RTUnit.
56  */
57  class RTUnit : virtual public Logging, virtual public RobotUnit
58  {
59 
60  public:
61  RTUnit();
62  ~RTUnit() override;
63 
64  /**
65  * @see armarx::ManagedIceObject::getDefaultName()
66  */
67  std::string
68  getDefaultName() const override
69  {
70  return "EtherCATRTUnit";
71  }
72 
73  protected:
74  void onInitRobotUnit() override;
75  void onConnectRobotUnit() override;
76  void onDisconnectRobotUnit() override;
77  void onExitRobotUnit() override;
78 
79  void initializeKinematicUnit() override;
80 
81  void joinControlThread() override;
82 
83  void publish(armarx::StringVariantBaseMap debugObserverMap = {},
84  armarx::StringVariantBaseMap timingMap = {}) override;
85 
86  /**
87  * @see PropertyUser::createPropertyDefinitions()
88  */
90 
91 
92  protected:
93  static MultiNodeRapidXMLReader ReadHardwareConfigFile(std::string hardwareConfigFilePath,
94  std::string rootNodeName);
95 
96  //all the stuff to run the rt Thread
97  void startRTThread();
98 
99  // void stopRTThread();
100 
101  /** the run method of the rt thread */
102  virtual void rtRun();
103 
104  bool initBusRTThread();
105 
106  void controlLoopRTThread();
107 
108  enum class CalibrationStatus
109  {
110  Calibrating,
111  Done
112  };
113 
114  /**
115  * @brief Allows to switch controllers while calibrating
116  *
117  * use
118  * rtSetJointController(JointController* c, std::size_t index)
119  * to switch controllers
120  */
121  virtual void
123  {
124  }
125 
126  /**
127  * @brief Hook to add calibration code
128  *
129  * This function is called in the rt loop! So you should not take too long!
130  *
131  * read sensors and write targets
132  * while calibrating return CalibrationStatus::Calibrating
133  * if done return CalibrationStatus::Done
134  *
135  * @return Whether done or still calibrating
136  */
137  virtual CalibrationStatus
139  {
141  }
142 
143  bool
145  {
146  return _calibrationStatus == CalibrationStatus::Calibrating;
147  }
148 
149  std::uintmax_t
151  {
152  return _iterationCount;
153  }
154 
155  private:
157  std::atomic_uintmax_t _iterationCount = 0;
158 
159  protected:
160  void computeInertiaTorque();
161 
162  std::thread rtTask;
163  std::atomic_bool taskRunning{false};
164 
165  //timestamps for the pdo updates
167 
169 
170  VirtualRobot::RobotNodeSetPtr rtRobotJointSet, rtRobotBodySet;
171  std::vector<std::pair<VirtualRobot::RobotNodePtr, SensorValue1DoFGravityTorque*>>
173 
176  {
177  return IceUtil::Time::microSeconds(properties.rtLoopTimeInUS);
178  }
179 
180  void startBackupLogging();
182  std::thread backupLoggingTask;
183  std::atomic_bool backupLoggingRunning{false};
184 
186  {
187  None,
190  Both
191  };
192 
193  struct
194  {
195  std::string busConfigFilePath;
199  int rtLoopTimeInUS = 1000;
201 
204  unsigned int periodicLoggingHistorySize = 2;
205  std::string periodicLoggingDirectory = "/tmp/periodic_rt_logging/";
206 
208  unsigned int errorCountersReadingPeriodInMS = 100;
210 
211  std::string robotJointsNodeSet = "RobotJoints";
212  std::string robotColNodeSet = "RobotCol";
213  } properties;
214 
216 
217  };
218 
219 } // namespace armarx::control::ethercat
armarx::control::ethercat::RTUnit::joinControlThread
void joinControlThread() override
Implementations have to join their ControlThread in this hook. (used by RobotUnit::finishRunning())
Definition: RTUnit.cpp:193
armarx::control::ethercat::RTUnit::rtRobotBodySet
VirtualRobot::RobotNodeSetPtr rtRobotBodySet
Definition: RTUnit.h:170
armarx::control::ethercat::RTUnit::PeriodicLoggingMode
PeriodicLoggingMode
Definition: RTUnit.h:185
armarx::control::ethercat::RTUnit::createPropertyDefinitions
armarx::PropertyDefinitionsPtr createPropertyDefinitions() override
Definition: RTUnit.cpp:208
armarx::StringVariantBaseMap
std::map< std::string, VariantBasePtr > StringVariantBaseMap
Definition: ManagedIceObject.h:111
armarx::control::ethercat::RTUnit::initBusRTThread
bool initBusRTThread()
Definition: RTUnit.cpp:485
armarx::control::ethercat::RTUnit::resetErrorRegistersAtStartup
bool resetErrorRegistersAtStartup
Definition: RTUnit.h:209
armarx::control::ethercat::RTUnit::socketFileDescriptor
int socketFileDescriptor
Definition: RTUnit.h:196
RobotUnit.h
armarx::control::ethercat::RTUnit::startRTThread
void startRTThread()
Definition: RTUnit.cpp:168
armarx::control::ethercat::RTUnit::getIterationCount
std::uintmax_t getIterationCount()
Definition: RTUnit.h:150
armarx::control::ethercat::RTUnit::ethercatTimeouts
armarx::control::ethercat::Timeouts ethercatTimeouts
Definition: RTUnit.h:215
armarx::control::ethercat::RTUnit::startBackupLogging
void startBackupLogging()
Definition: RTUnit.cpp:828
armarx::control::ethercat::RTUnit::controlLoopRTThread
void controlLoopRTThread()
Definition: RTUnit.cpp:549
armarx::control::ethercat::RTUnit::properties
struct armarx::control::ethercat::RTUnit::@55 properties
PeriodicTask.h
armarx::control::ethercat::RTUnit::currentPDOUpdateTimestamp
IceUtil::Time currentPDOUpdateTimestamp
Definition: RTUnit.h:166
armarx::control::ethercat::RTUnit::periodicLoggingHistorySize
unsigned int periodicLoggingHistorySize
Definition: RTUnit.h:204
armarx::control::ethercat::RTUnit::rTLoopTimingCheckToleranceFactor
double rTLoopTimingCheckToleranceFactor
Definition: RTUnit.h:200
armarx::MultiNodeRapidXMLReader
Definition: MultiNodeRapidXMLReader.h:34
armarx::control::ethercat::RTUnit::errorCountersReadingPeriodInMS
unsigned int errorCountersReadingPeriodInMS
Definition: RTUnit.h:208
armarx::control::ethercat::RTUnit::RTUnit
RTUnit()
Definition: RTUnit.cpp:63
armarx::control::ethercat::RTUnit::getDefaultName
std::string getDefaultName() const override
Definition: RTUnit.h:68
armarx::control::ethercat::RTUnit::periodicLoggingIntervalInSeconds
unsigned int periodicLoggingIntervalInSeconds
Definition: RTUnit.h:203
armarx::control::ethercat::RTUnit::enableErrorCountersReading
bool enableErrorCountersReading
Definition: RTUnit.h:207
armarx::control::ethercat::RTUnit::PeriodicLoggingMode::Both
@ Both
armarx::control::ethercat::RTUnit::onExitRobotUnit
void onExitRobotUnit() override
called in onExitComponent before calling finishRunning
Definition: RTUnit.cpp:133
armarx::control::ethercat::RTUnit::CalibrationStatus::Calibrating
@ Calibrating
armarx::control::ethercat::RTUnit::periodicBackupLoggingRun
void periodicBackupLoggingRun()
Definition: RTUnit.cpp:853
armarx::control::ethercat::RTUnit::initializeKinematicUnit
void initializeKinematicUnit() override
Initializes the KinematicUnit.
Definition: RTUnit.cpp:143
armarx::control::ethercat::RTUnit::publish
void publish(armarx::StringVariantBaseMap debugObserverMap={}, armarx::StringVariantBaseMap timingMap={}) override
Publishes data.
Definition: RTUnit.cpp:202
armarx::control::ethercat::RTUnit::busConfigFilePath
std::string busConfigFilePath
Definition: RTUnit.h:195
armarx::control::ethercat::RTUnit::backupLoggingTask
std::thread backupLoggingTask
Definition: RTUnit.h:182
armarx::control::ethercat::RTUnit::rtLoopTimeInUS
int rtLoopTimeInUS
Definition: RTUnit.h:199
armarx::control::ethercat::RTUnit::CalibrationStatus::Done
@ Done
armarx::control::ethercat
Definition: Bus.cpp:24
armarx::control::ethercat::RTUnit::rtCalibrateActivateControlers
virtual void rtCalibrateActivateControlers()
Allows to switch controllers while calibrating.
Definition: RTUnit.h:122
Timeouts.h
armarx::control::ethercat::RTUnit::~RTUnit
~RTUnit() override
Definition: RTUnit.cpp:288
armarx::control::ethercat::RTUnit::taskRunning
std::atomic_bool taskRunning
Definition: RTUnit.h:163
armarx::control::ethercat::RTUnit::useTorqueVelocityModeAsDefault
bool useTorqueVelocityModeAsDefault
Definition: RTUnit.h:198
armarx::control::ethercat::RTUnit::ReadHardwareConfigFile
static MultiNodeRapidXMLReader ReadHardwareConfigFile(std::string hardwareConfigFilePath, std::string rootNodeName)
Definition: RTUnit.cpp:430
armarx::control::ethercat::RTUnit::rtTask
std::thread rtTask
Definition: RTUnit.h:162
armarx::control::ethercat::RTUnit::nodeJointDataVec
std::vector< std::pair< VirtualRobot::RobotNodePtr, SensorValue1DoFGravityTorque * > > nodeJointDataVec
Definition: RTUnit.h:172
armarx::armem::Time
armarx::core::time::DateTime Time
Definition: forward_declarations.h:13
armarx::control::ethercat::RTUnit::rtCalibrate
virtual CalibrationStatus rtCalibrate()
Hook to add calibration code.
Definition: RTUnit.h:138
armarx::control::ethercat::RTUnit::getControlThreadTargetPeriod
IceUtil::Time getControlThreadTargetPeriod() const override
The ControlThread's period.
Definition: RTUnit.h:175
armarx::control::ethercat::RTUnit::robotJointsNodeSet
std::string robotJointsNodeSet
Definition: RTUnit.h:211
armarx::control::ethercat::RTUnit::ethercatInterfaceName
std::string ethercatInterfaceName
Definition: RTUnit.h:197
armarx::control::ethercat::RTUnit::PeriodicLoggingMode::OnlyBusErrors
@ OnlyBusErrors
armarx::control::ethercat::RTUnit::periodicLoggingDirectory
std::string periodicLoggingDirectory
Definition: RTUnit.h:205
armarx::control::ethercat::RTUnit::PeriodicLoggingMode::OnlyRtLogging
@ OnlyRtLogging
armarx::Logging
Base Class for all Logging classes.
Definition: Logging.h:232
armarx::control::ethercat::RTUnit::rtIsCalibrating
bool rtIsCalibrating() const
Definition: RTUnit.h:144
armarx::control::ethercat::RTUnit::robotColNodeSet
std::string robotColNodeSet
Definition: RTUnit.h:212
armarx::control::ethercat::RTUnit::onInitRobotUnit
void onInitRobotUnit() override
called in onInitComponent
Definition: RTUnit.cpp:69
IceUtil::Handle< class PropertyDefinitionContainer >
armarx::RobotUnit
The RobotUnit class manages a robot and its controllers.
Definition: RobotUnit.h:180
armarx::control::ethercat::RTUnit::CalibrationStatus
CalibrationStatus
Definition: RTUnit.h:108
RapidXmlReader.h
armarx::control::ethercat::Timeouts
Definition: Timeouts.h:8
armarx::control::ethercat::RTUnit::computeInertiaTorque
void computeInertiaTorque()
armarx::control::ethercat::RTUnit::periodicLoggingMode
PeriodicLoggingMode periodicLoggingMode
Definition: RTUnit.h:202
MultiNodeRapidXMLReader.h
armarx::control::ethercat::RTUnit::rtRun
virtual void rtRun()
the run method of the rt thread
Definition: RTUnit.cpp:300
armarx::control::ethercat::RTUnit::robot
VirtualRobot::RobotPtr robot
Definition: RTUnit.h:168
armarx::control::ethercat::RTUnit::onConnectRobotUnit
void onConnectRobotUnit() override
called in onConnectComponent
Definition: RTUnit.cpp:115
armarx::control::ethercat::RTUnit
Brief description of class RTUnit.
Definition: RTUnit.h:57
armarx::control::ethercat::RTUnit::PeriodicLoggingMode::None
@ None
armarx::control::ethercat::RTUnit::rtRobotJointSet
VirtualRobot::RobotNodeSetPtr rtRobotJointSet
Definition: RTUnit.h:170
armarx::control::ethercat::RTUnit::onDisconnectRobotUnit
void onDisconnectRobotUnit() override
called in onDisconnecComponent
Definition: RTUnit.cpp:127
VirtualRobot::RobotPtr
std::shared_ptr< class Robot > RobotPtr
Definition: Bus.h:18
armarx::control::ethercat::RTUnit::backupLoggingRunning
std::atomic_bool backupLoggingRunning
Definition: RTUnit.h:183