RobotHealthDummy.cpp
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::ArmarXObjects::RobotHealthDummy
17  * @author Simon Ottenhaus ( simon dot ottenhaus at kit dot edu )
18  * @date 2018
19  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20  * GNU General Public License
21  */
22 
23 #include "RobotHealthDummy.h"
24 
25 #include <time.h>
26 
27 #include <thread>
28 
31 
32 namespace armarx
33 {
34  void
36  {
38  getProperty(sleepmode, "SleepMode");
39  }
40 
41  void
43  {
44  robotHealthTopicPrx = getTopic<RobotHealthInterfacePrx>(
45  getProperty<std::string>("RobotHealthTopicName").getValue());
46  dummyTask->start();
47  }
48 
49 #define NANOSECS_PER_SEC 1000000000
50 
51  int
53  {
54  struct timespec ts;
55  ts.tv_sec = nanosec / NANOSECS_PER_SEC;
56  ts.tv_nsec = (nanosec % NANOSECS_PER_SEC);
57  return nanosleep(&ts, nullptr); // jitter up to +100ms!
58  }
59 
60  void
61  RobotHealthDummy::sleepwait(long microseconds)
62  {
63  long start = TimeUtil::GetTime().toMicroSeconds();
64  auto end = start + microseconds;
65  do
66  {
67  NanoSleep(1000);
68  } while (TimeUtil::GetTime().toMicroSeconds() < end);
69  }
70 
71  void
72  RobotHealthDummy::yieldwait(long microseconds)
73  {
74  long start = TimeUtil::GetTime().toMicroSeconds();
75  auto end = start + microseconds;
76  do
77  {
78  std::this_thread::yield();
79  } while (TimeUtil::GetTime().toMicroSeconds() < end); // jitter up to +25ms...
80  }
81 
82  void
83  RobotHealthDummy::busywait(long microseconds)
84  {
85  long start = TimeUtil::GetTime().toMicroSeconds();
86  auto end = start + microseconds;
87  do
88  {
89  ;
90  } while (TimeUtil::GetTime().toMicroSeconds() < end);
91  }
92 
93  void
95  {
96  auto args = RobotHealthHeartbeatArgs();
97  args.identifier = getName();
98  armarx::core::time::toIce(args.maximumCycleTimeError,
100  armarx::core::time::toIce(args.maximumCycleTimeWarning,
102  robotHealthTopicPrx->signUp(args);
103 
104  ARMARX_INFO << "starting rinning task";
105  while (!dummyTask->isStopped())
106  {
107  auto beforeTopicCall = armarx::core::time::DateTime::Now();
108  //ARMARX_INFO << "send heartbeat";
109 
110  armarx::core::time::dto::DateTime now;
112  robotHealthTopicPrx->heartbeat(getName(), now);
113  auto afterTopicCall = armarx::core::time::DateTime::Now();
114 
115  if (sleepmode == "nanosleep")
116  {
117  NanoSleep(10 * 1000 * 1000); // wait 10 milliseconds
118  }
119  else if (sleepmode == "sleepwait")
120  {
121  sleepwait(10 * 1000); // wait 10 milliseconds
122  }
123  else if (sleepmode == "yieldwait")
124  {
125  yieldwait(10 * 1000); // wait 10 milliseconds
126  }
127  else if (sleepmode == "busywait")
128  {
129  busywait(10 * 1000); // wait 10 milliseconds
130  }
131  else if (sleepmode == "std::this_thread::sleep_for")
132  {
133  std::this_thread::sleep_for(std::chrono::microseconds(10 * 1000));
134  }
135  else
136  {
137  throw LocalException("Unknown sleepmode.");
138  }
139 
140  auto afterSleep = armarx::core::time::DateTime::Now();
141  auto topicCallDelay = afterTopicCall - beforeTopicCall;
142  auto sleepDelay = afterSleep - afterTopicCall;
143  if (sleepDelay.toMicroSeconds() > 20000)
144  {
145  ARMARX_IMPORTANT << sleepmode << " took long: " << sleepDelay << "us";
146  }
147  if (topicCallDelay.toMicroSeconds() > 1000)
148  {
149  ARMARX_IMPORTANT << "topic took long: " << topicCallDelay << "us";
150  }
151  }
152 
153  robotHealthTopicPrx->unregister(args.identifier);
154  }
155 
156  void
158  {
159  //ARMARX_IMPORTANT << "onDisconnectComponent";
160  dummyTask->stop();
161  }
162 
163  void
165  {
166  //ARMARX_IMPORTANT << "onExitComponent";
167  dummyTask->stop();
168  }
169 
172  {
175  }
176 } // namespace armarx
armarx::RobotHealthDummy::busywait
void busywait(long microseconds)
Definition: RobotHealthDummy.cpp:83
armarx::RobotHealthDummy::dummyTask
RunningTask< RobotHealthDummy >::pointer_type dummyTask
Definition: RobotHealthDummy.h:113
armarx::RobotHealthDummy::runTask
void runTask()
Definition: RobotHealthDummy.cpp:94
armarx::RobotHealthDummy::onConnectComponent
void onConnectComponent() override
Definition: RobotHealthDummy.cpp:42
ARMARX_IMPORTANT
#define ARMARX_IMPORTANT
Definition: Logging.h:190
armarx::RobotHealthDummy::sleepmode
std::string sleepmode
Definition: RobotHealthDummy.h:115
armarx::RobotHealthDummy::sleepwait
void sleepwait(long microseconds)
Definition: RobotHealthDummy.cpp:61
armarx::core::time::DateTime::Now
static DateTime Now()
Definition: DateTime.cpp:51
armarx::RobotHealthDummy::onInitComponent
void onInitComponent() override
Definition: RobotHealthDummy.cpp:35
DateTime.h
armarx::core::time::toIce
void toIce(dto::ClockType::ClockTypeEnum &dto, const ClockType &bo)
Definition: ice_conversions.cpp:31
RobotHealthDummy.h
ice_conversions.h
armarx::RunningTask
Definition: ArmarXMultipleObjectsScheduler.h:36
armarx::RobotHealthDummy::onDisconnectComponent
void onDisconnectComponent() override
Definition: RobotHealthDummy.cpp:157
NANOSECS_PER_SEC
#define NANOSECS_PER_SEC
Definition: RobotHealthDummy.cpp:49
armarx::TimeUtil::GetTime
static IceUtil::Time GetTime(TimeMode timeMode=TimeMode::VirtualTime)
Get the current time.
Definition: TimeUtil.cpp:42
armarx::Component::getConfigIdentifier
std::string getConfigIdentifier()
Retrieve config identifier for this component as set in constructor.
Definition: Component.cpp:79
armarx::RobotHealthDummy::createPropertyDefinitions
armarx::PropertyDefinitionsPtr createPropertyDefinitions() override
Definition: RobotHealthDummy.cpp:171
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:181
armarx::PropertyUser::getProperty
Property< PropertyType > getProperty(const std::string &name)
Property creation and retrieval.
Definition: PropertyUser.h:180
armarx::RobotHealthDummyPropertyDefinitions
Definition: RobotHealthDummy.h:38
IceUtil::Handle< class PropertyDefinitionContainer >
armarx::RobotHealthDummy::onExitComponent
void onExitComponent() override
Definition: RobotHealthDummy.cpp:164
armarx::ManagedIceObject::getName
std::string getName() const
Retrieve name of object.
Definition: ManagedIceObject.cpp:108
armarx::PropertyDefinitionsPtr
IceUtil::Handle< class PropertyDefinitionContainer > PropertyDefinitionsPtr
PropertyDefinitions smart pointer type.
Definition: forward_declarations.h:35
armarx::RobotHealthDummy::NanoSleep
int NanoSleep(long nanosec)
Definition: RobotHealthDummy.cpp:52
armarx::RobotHealthDummy::robotHealthTopicPrx
RobotHealthInterfacePrx robotHealthTopicPrx
Definition: RobotHealthDummy.h:112
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27
armarx::core::time::Duration::MilliSeconds
static Duration MilliSeconds(std::int64_t milliSeconds)
Constructs a duration in milliseconds.
Definition: Duration.cpp:48
armarx::RobotHealthDummy::yieldwait
void yieldwait(long microseconds)
Definition: RobotHealthDummy.cpp:72