HeartBeatMonitor.cpp
Go to the documentation of this file.
1 // this is for emacs file handling -*- mode: c++; indent-tabs-mode: nil -*-
2 // -- BEGIN LICENSE BLOCK ----------------------------------------------
3 // This file is part of the SCHUNK Canopen Driver suite.
4 //
5 // This program is free software licensed under the LGPL
6 // (GNU LESSER GENERAL PUBLIC LICENSE Version 3).
7 // You can find a copy of this license in LICENSE folder in the top
8 // directory of the source code.
9 //
10 // © Copyright 2016 SCHUNK GmbH, Lauffen/Neckar Germany
11 // © Copyright 2016 FZI Forschungszentrum Informatik, Karlsruhe, Germany
12 // -- END LICENSE BLOCK ------------------------------------------------
13 //----------------------------------------------------------------------
20 //----------------------------------------------------------------------
21 
22 #include "HeartBeatMonitor.h"
23 #include "Logging.h"
24 
25 namespace icl_hardware {
26 namespace canopen_schunk {
27 
29  : m_period_time_ms(100),
30  m_running(false)
31 {
32  start();
33 }
34 
36 {
37  stop();
38  m_thread.join();
39 }
40 
41 void HeartBeatMonitor::registerErrorCallback (const boost::function< void() >& f)
42 {
44 }
45 
46 
48 {
49  while (true)
50  {
52  for (std::map<uint8_t, icl_core::TimeStamp>::iterator it = m_timestamp_record.begin();
53  it != m_timestamp_record.end();
54  ++it)
55  {
56  icl_core::TimeSpan time_passed = now_time - it->second;
57  LOGGING_TRACE (CanOpen, "Time passed since last heartbeat from node " <<
58  it->first << ": " << time_passed.toMSec() << "ms" << endl);
59  if (time_passed.toMSec() > m_period_time_ms)
60  {
61  LOGGING_ERROR_C (CanOpen, HeartBeatMonitor, "Missing heartbeat from node " <<
62  it->first << endl);
64  stop();
65  }
66  }
67 
68  // Wait for the thread period so that the timing is in sync.
69  try
70  {
71  boost::this_thread::sleep(boost::posix_time::milliseconds(m_period_time_ms));
72  }
73  catch(boost::thread_interrupted&)
74  {
75  return;
76  }
77  }
78 }
79 
80 
82 {
83  if (m_running)
84  {
85  LOGGING_TRACE_C(CanOpen, HeartBeatMonitor, "New heartbeat received from node " << node_id << endl);
86 
88  }
89 }
90 
92 {
93  m_running = false;
94  m_thread.interrupt();
95 }
96 
98 {
99  if (!m_running)
100  {
101  m_thread = boost::thread(&HeartBeatMonitor::workerFunction, this);
102  m_running = true;
103  }
104  else
105  {
106  LOGGING_WARNING(CanOpen, "start() called although HeartbeatMonitor thread is already running. Start request will be ignored." << endl);
107  }
108 }
109 
111 {
112  // stop monitoring to prevent thread issues
113  if (m_running)
114  {
115  stop();
116  }
117 
118  // set all heartbeat times to zero
119  for (std::map<uint8_t, icl_core::TimeStamp>::iterator it = m_timestamp_record.begin();
120  it != m_timestamp_record.end();
121  ++it)
122  {
123  it->second = icl_core::TimeStamp::now();
124  }
125 
126  // restart monitoring
127  start();
128 }
129 
130 
131 
132 
133 
134 }} // End of NS
void workerFunction()
Thread worker function for the Heartbeat monitor. This function will regularly check for missing hear...
static TimeStamp now()
f
std::map< uint8_t, icl_core::TimeStamp > m_timestamp_record
int64_t toMSec() const
unsigned char uint8_t
ThreadStream & endl(ThreadStream &stream)
#define LOGGING_WARNING(streamname, arg)
void addHeartbeat(const uint8_t node_id)
Incoming heartbeats will trigger this function to update the most recent heartbeat time...
uint16_t m_period_time_ms
Cycle period of the monitor thread.
boost::thread m_thread
monitor thread handle
void registerErrorCallback(const boost::function< void()> &f)
This function will be called as soon as one node is missing a heartbeat message. Probably this functi...
void reset()
Resets the heartbeat monitor. This will make the monitor forget about all past heartbeats and start f...
#define LOGGING_TRACE_C(streamname, classname, arg)
#define LOGGING_TRACE(streamname, arg)
#define LOGGING_ERROR_C(streamname, classname, arg)


schunk_canopen_driver
Author(s): Felix Mauch , Georg Heppner
autogenerated on Mon Jun 10 2019 15:07:49