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 
33 {
34  class SlaveInterface;
35  using SlaveList = std::vector<SlaveInterface*>;
36 
37  class Bus;
38 
39  /**
40  * @class BusErrorHandler
41  * @ingroup Library-ethercat
42  * @brief Brief description of class BusErrorHandler.
43  *
44  * Detailed description of class BusErrorHandler.
45  */
47  {
48  public:
49  BusErrorHandler(Bus* bus);
50  ~BusErrorHandler() override;
51 
52  void rtHandleBusErrors();
53  void rtHandleSlaveErrors();
54 
55  bool hasError() const;
56  bool isReinitializationActive() const;
57 
59 
60  void init(int wkc);
61 
62 
63  private:
64  Bus* bus;
65 
66  enum class SlaveState
67  {
68  Operational,
69  Lost,
70  Reinitializating
71  };
72 
73  // This member must be updated whenever ...
74  // - the workCounter changes
75  // - foundAgainSlaves are fully recovered
76  // - ?
77  std::map<SlaveInterface*, SlaveState> slaveStates;
78  std::atomic_bool allSlavesReinitialized{true};
79 
80  int secondLastUnexpectedWorkCounter = 0;
81  int expectedWorkCounter = -1;
82 
83  bool busErrorFound = false;
84  bool slaveErrorFound = false;
85 
86  void rtUpdateSlaveStates();
87 
88  void rtMarkLostSlavesInSOEMStruct() const;
89 
90  void reinitializeSlaves();
91 
92  std::thread slaveReinitializingThread;
93  std::atomic_bool slaveReinitializingThreadRunning{true};
94  void slaveReinitializingLoop();
95 
96  bool areAllSlavesReinitialized() const;
97 
98  /**
99  * @brief
100  */
101  class Watchdog
102  {
103  public:
104  Watchdog(std::uint32_t minDurationUS);
105  bool expired() const;
106  void reset();
107 
108  private:
109  std::uint32_t minDuration;
110  IceUtil::Time lastTime;
111  };
112 
113  Watchdog workerCounterWatchdog;
114  };
115 } // 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:46
armarx::control::ethercat::SlaveInterface
Brief description of class SlaveInterface.
Definition: SlaveInterface.h:27
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:56
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:35
armarx::control::ethercat::BusErrorHandler::isReinitializationActive
bool isReinitializationActive() const
Definition: BusErrorHandler.cpp:82
armarx::Logging
Base Class for all Logging classes.
Definition: Logging.h:239
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