BusErrorHandler.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 Stefan Reither( stefan dot reither at kit dot edu)
20  * @date 2021
21  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22  * GNU General Public License
23  */
24 
25 #pragma once
26 
27 #include <atomic>
28 #include <thread>
29 
31 
32 
34 {
35  class SlaveInterface;
36  using SlaveList = std::vector<SlaveInterface*>;
37 
38  class Bus;
39 
40  /**
41  * @class BusErrorHandler
42  * @ingroup Library-ethercat
43  * @brief Brief description of class BusErrorHandler.
44  *
45  * Detailed description of class BusErrorHandler.
46  */
48  {
49  public:
50  BusErrorHandler(Bus* bus);
51  ~BusErrorHandler() override;
52 
53  void rtHandleBusErrors();
54  void rtHandleSlaveErrors();
55 
56  bool hasError() const;
57  bool isReinitializationActive() const;
58 
60 
61  void init(int wkc);
62 
63 
64  private:
65  Bus* bus;
66 
67  enum class SlaveState
68  {
69  Operational,
70  Lost,
71  Reinitializating
72  };
73 
74  // This member must be updated whenever ...
75  // - the workCounter changes
76  // - foundAgainSlaves are fully recovered
77  // - ?
78  std::map<SlaveInterface*, SlaveState> slaveStates;
79  std::atomic_bool allSlavesReinitialized{true};
80 
81  int secondLastUnexpectedWorkCounter = 0;
82  int expectedWorkCounter = -1;
83 
84  bool busErrorFound = false;
85  bool slaveErrorFound = false;
86 
87  void rtUpdateSlaveStates();
88 
89  void rtMarkLostSlavesInSOEMStruct() const;
90 
91  void reinitializeSlaves();
92 
93  std::thread slaveReinitializingThread;
94  std::atomic_bool slaveReinitializingThreadRunning{true};
95  void slaveReinitializingLoop();
96 
97  bool areAllSlavesReinitialized() const;
98 
99  /**
100  * @brief
101  */
102  class Watchdog
103  {
104  public:
105  Watchdog(std::uint32_t minDurationUS);
106  bool expired() const;
107  void reset();
108 
109  private:
110  std::uint32_t minDuration;
111  IceUtil::Time lastTime;
112  };
113 
114  Watchdog workerCounterWatchdog;
115  };
116 } // namespace armarx::control::ethercat
armarx::control::ethercat::BusErrorHandler::rtHandleSlaveErrors
void rtHandleSlaveErrors()
Definition: BusErrorHandler.cpp:44
armarx::control::ethercat::BusErrorHandler
Brief description of class BusErrorHandler.
Definition: BusErrorHandler.h:47
armarx::control::ethercat::SlaveInterface
Brief description of class SlaveInterface.
Definition: SlaveInterface.h:29
armarx::control::ethercat::BusErrorHandler::rtHandleBusErrors
void rtHandleBusErrors()
Definition: BusErrorHandler.cpp:37
armarx::control::ethercat::BusErrorHandler::init
void init(int wkc)
Definition: BusErrorHandler.cpp:94
armarx::control::ethercat::BusErrorHandler::~BusErrorHandler
~BusErrorHandler() override
Definition: BusErrorHandler.cpp:30
armarx::control::ethercat::BusErrorHandler::hasError
bool hasError() const
Definition: BusErrorHandler.cpp:76
armarx::control::ethercat::Bus
Brief description of class Bus.
Definition: Bus.h:55
armarx::control::ethercat
Definition: Bus.cpp:24
armarx::armem::Time
armarx::core::time::DateTime Time
Definition: forward_declarations.h:13
armarx::control::ethercat::SlaveList
std::vector< SlaveInterface * > SlaveList
Definition: BusErrorHandler.h:36
armarx::control::ethercat::BusErrorHandler::isReinitializationActive
bool isReinitializationActive() const
Definition: BusErrorHandler.cpp:82
armarx::Logging
Base Class for all Logging classes.
Definition: Logging.h:232
armarx::control::ethercat::BusErrorHandler::isSlaveLostOrDuringReinitialization
bool isSlaveLostOrDuringReinitialization(SlaveInterface *slave) const
Definition: BusErrorHandler.cpp:88
armarx::control::ethercat::BusErrorHandler::BusErrorHandler
BusErrorHandler(Bus *bus)
Definition: BusErrorHandler.cpp:23
Logging.h