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
32namespace armarx
33{
34 void
40
41 void
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
176} // namespace armarx
#define NANOSECS_PER_SEC
Definition TimeUtil.cpp:35
std::string getConfigIdentifier()
Retrieve config identifier for this component as set in constructor.
Definition Component.cpp:90
Property< PropertyType > getProperty(const std::string &name)
TopicProxyType getTopic(const std::string &name)
Returns a proxy of the specified topic.
std::string getName() const
Retrieve name of object.
void yieldwait(long microseconds)
void onDisconnectComponent() override
armarx::PropertyDefinitionsPtr createPropertyDefinitions() override
RunningTask< RobotHealthDummy >::pointer_type dummyTask
void onConnectComponent() override
RobotHealthInterfacePrx robotHealthTopicPrx
void sleepwait(long microseconds)
void busywait(long microseconds)
static IceUtil::Time GetTime(TimeMode timeMode=TimeMode::VirtualTime)
Get the current time.
Definition TimeUtil.cpp:42
static DateTime Now()
Definition DateTime.cpp:51
static Duration MilliSeconds(std::int64_t milliSeconds)
Constructs a duration in milliseconds.
Definition Duration.cpp:48
#define ARMARX_INFO
The normal logging level.
Definition Logging.h:181
#define ARMARX_IMPORTANT
The logging level for always important information, but expected behaviour (in contrast to ARMARX_WAR...
Definition Logging.h:190
void toIce(dto::ClockType::ClockTypeEnum &dto, const ClockType &bo)
This file offers overloads of toIce() and fromIce() functions for STL container types.
IceUtil::Handle< class PropertyDefinitionContainer > PropertyDefinitionsPtr
PropertyDefinitions smart pointer type.