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();
100  robotHealthTopicPrx->signUp(args);
101 
102  ARMARX_INFO << "starting rinning task";
103  while (!dummyTask->isStopped())
104  {
105  auto beforeTopicCall = armarx::core::time::DateTime::Now();
106  //ARMARX_INFO << "send heartbeat";
107 
108  armarx::core::time::dto::DateTime now;
110  robotHealthTopicPrx->heartbeat(getName(), now);
111  auto afterTopicCall = armarx::core::time::DateTime::Now();
112 
113  if (sleepmode == "nanosleep")
114  {
115  NanoSleep(10 * 1000 * 1000); // wait 10 milliseconds
116  }
117  else if (sleepmode == "sleepwait")
118  {
119  sleepwait(10 * 1000); // wait 10 milliseconds
120  }
121  else if (sleepmode == "yieldwait")
122  {
123  yieldwait(10 * 1000); // wait 10 milliseconds
124  }
125  else if (sleepmode == "busywait")
126  {
127  busywait(10 * 1000); // wait 10 milliseconds
128  }
129  else if (sleepmode == "std::this_thread::sleep_for")
130  {
131  std::this_thread::sleep_for(std::chrono::microseconds(10 * 1000));
132  }
133  else
134  {
135  throw LocalException("Unknown sleepmode.");
136  }
137 
138  auto afterSleep = armarx::core::time::DateTime::Now();
139  auto topicCallDelay = afterTopicCall - beforeTopicCall;
140  auto sleepDelay = afterSleep - afterTopicCall;
141  if (sleepDelay.toMicroSeconds() > 20000)
142  {
143  ARMARX_IMPORTANT << sleepmode << " took long: " << sleepDelay << "us";
144  }
145  if (topicCallDelay.toMicroSeconds() > 1000)
146  {
147  ARMARX_IMPORTANT << "topic took long: " << topicCallDelay << "us";
148  }
149  }
150 
151  robotHealthTopicPrx->unregister(args.identifier);
152  }
153 
154  void
156  {
157  //ARMARX_IMPORTANT << "onDisconnectComponent";
158  dummyTask->stop();
159  }
160 
161  void
163  {
164  //ARMARX_IMPORTANT << "onExitComponent";
165  dummyTask->stop();
166  }
167 
170  {
173  }
174 } // 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:183
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:55
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:33
RobotHealthDummy.h
ice_conversions.h
armarx::RunningTask
Definition: ArmarXMultipleObjectsScheduler.h:35
armarx::RobotHealthDummy::onDisconnectComponent
void onDisconnectComponent() override
Definition: RobotHealthDummy.cpp:155
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:74
armarx::RobotHealthDummy::createPropertyDefinitions
armarx::PropertyDefinitionsPtr createPropertyDefinitions() override
Definition: RobotHealthDummy.cpp:169
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:174
armarx::PropertyUser::getProperty
Property< PropertyType > getProperty(const std::string &name)
Property creation and retrieval.
Definition: PropertyUser.h:179
armarx::RobotHealthDummyPropertyDefinitions
Definition: RobotHealthDummy.h:38
IceUtil::Handle< class PropertyDefinitionContainer >
armarx::RobotHealthDummy::onExitComponent
void onExitComponent() override
Definition: RobotHealthDummy.cpp:162
armarx::ManagedIceObject::getName
std::string getName() const
Retrieve name of object.
Definition: ManagedIceObject.cpp:107
armarx::PropertyDefinitionsPtr
IceUtil::Handle< class PropertyDefinitionContainer > PropertyDefinitionsPtr
PropertyDefinitions smart pointer type.
Definition: forward_declarations.h:34
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:28
armarx::core::time::Duration::MilliSeconds
static Duration MilliSeconds(std::int64_t milliSeconds)
Constructs a duration in milliseconds.
Definition: Duration.cpp:55
armarx::RobotHealthDummy::yieldwait
void yieldwait(long microseconds)
Definition: RobotHealthDummy.cpp:72