Motion Master
Loading...
Searching...
No Matches
global.h
Go to the documentation of this file.
1#pragma once
2
3#ifdef WINDOWS_BUILD
4#include <windows.h>
5#endif
6#include <atomic>
7#include <csignal>
8#include <filesystem>
9#include <iostream>
10#include <map>
11#include <mutex>
12#include <queue>
13#include <string>
14#include <thread>
15#include <unordered_map>
16#include <vector>
17
18#include "ethercat.h"
19#include "loguru.h"
21
22#ifndef M_PI
23#define M_PI 3.14159265358979323846
24#endif
25
27
28// Port the standalone-autotuning HTTP server is started on and reached at.
29extern uint16_t g_autotuning_port;
30
31extern uint8 g_pIOmap[4096];
32
33// SIGINT or SIGTERM caught
34extern volatile sig_atomic_t g_sig_caught;
35
36extern std::string g_driver_name;
37
40
41extern uint16_t g_req_res_port;
42extern uint16_t g_pub_sub_port;
43
44extern std::atomic<int> g_slavecount;
45
46// Using SOEM
47extern std::string g_soem_mac_address;
48extern std::string g_soem_ifname;
49
50// Requests a full re-enumeration of the EtherCAT bus from within the cyclic
51// SOEM loop. The SOEM recovery path sets this when it detects that slaves
52// dropped off the bus and returned (a power brown-out): such slaves lose their
53// SyncManager/FMMU/PDO configuration and cannot be healed in place, so the
54// machine manager's run loop watches this flag and escalates to the proven
55// VirtualDeviceManager::init() re-enumeration (which also emits the
56// DEINITIALIZING/INITIALIZING lifecycle events clients depend on). It is
57// cleared once that stop()/init() sequence has been kicked off.
58extern std::atomic<bool> g_reinitialization_required;
59
60// Requests that the EtherCAT port be torn down and reopened (ec_close +
61// ec_init) at the start of the next re-enumeration, instead of only re-running
62// ec_config_init on the still-open port. Set by the SOEM init path when a
63// reconnected slave reaches PRE-OP with an unresponsive mailbox: that wedged
64// state lives in the open port and is cleared neither by ec_config_init nor by
65// waiting (both were observed to loop indefinitely) - only by reopening the
66// port, which is what a Motion Master process restart does. wait_for_slaves()
67// consumes it, keeping it set until ec_init succeeds so a failed reopen
68// retries.
69extern std::atomic<bool> g_ethercat_port_reopen_required;
70
72
73// Using Ethernet Master
74extern std::vector<std::string> g_ethernet_ip_addresses;
75extern std::string g_ethernet_pdo_mode;
76
77// Number of mocked devices
78extern size_t g_mock_devices;
79
80// Path to a directory holding the templates for mock devices
81extern std::string g_templates_path;
82
83// Root directory for persistent user data (OD cache, etc.)
84extern std::filesystem::path g_data_dir;
85
91 private:
92 static std::map<std::string, std::unique_ptr<InternalLogger>> self_map;
94 constexpr static std::uint32_t internal_log_size = 0x200000;
95 std::mutex internal_log_mutex;
97 uint32_t type;
98 std::string name;
99
100 public:
101 std::string g_internal_log;
104 std::vector<std::string> logPreVec;
106 std::vector<std::string> logMsgVec;
108 std::vector<std::string> logIndVec;
111 std::deque<std::string> assertEqStrQue;
112 std::deque<void*> assertEqUserDataQue;
114 bool m_pop;
116 void (*assertEqCallback)(const std::string& pre, const std::string& str1,
117 const std::string& str2, void* userData,
118 int& retval);
119
121 static InternalLogger* getInstance(const std::string& name) {
122 if (InternalLogger::self_map.find(name) == InternalLogger::self_map.end()) {
123 InternalLogger::self_map.insert(
124 {name, std::make_unique<InternalLogger>()});
125 InternalLogger::self_map.at(name).get()->name = name;
126 InternalLogger::self_map.at(name).get()->type = 0;
127 InternalLogger::self_map.at(name).get()->assertEqCallback = nullptr;
128 InternalLogger::self_map.at(name).get()->m_pop = true;
129 }
130 return InternalLogger::self_map.at(name).get();
131 }
132
133 static void setType(const std::string& name, uint32_t type) {
134 InternalLogger::self_map.at(name).get()->type = type;
135 }
136
147 void log(const loguru::Message& message) {
148 std::unique_lock<std::mutex> lk(internal_log_mutex);
149 if (type == 0) {
150 g_internal_log += message.preamble;
151 g_internal_log += message.indentation;
152 g_internal_log += message.message;
153 g_internal_log += "\n";
154 if (g_internal_log.size() > internal_log_size) {
155 // Erase the first 12.5% of the internal log
156 auto pos = g_internal_log.find_first_of('\n', internal_log_size >> 3u);
157 g_internal_log.erase(0, pos + 1);
158 }
159 } else if (type == 1) {
160 logPreVec.push_back(message.preamble);
161 logMsgVec.push_back(message.message);
162 logIndVec.push_back(message.indentation);
163 if (!assertEqStrQue.empty()) {
164 if (assertEqCallback != nullptr) {
165 size_t loopSize = assertEqStrQue.size();
166 for (size_t i = 0; i < loopSize; i++) {
167 int retval = 0;
168 (*assertEqCallback)(logPreVec.back(), logMsgVec.back(),
170 retval);
171 if (retval != 0) {
172 assertEqStrQue.clear();
173 assertEqUserDataQue.clear();
174 break;
175 }
176 if (m_pop) {
177 assertEqStrQue.pop_front();
178 assertEqUserDataQue.pop_front();
179 }
180 }
181 }
182 }
183 }
184 }
185
188 static std::string& get_log(const std::string& name) {
190 std::unique_lock<std::mutex> lk(pil->internal_log_mutex);
192 return pil->g_internal_log_copy;
193 }
194
196 static void log_to_internal_log(void* user_data,
197 const loguru::Message& message) {
198 reinterpret_cast<InternalLogger*>(user_data)->log(message);
199 }
200
202 void setAssertEqCallback(void (*func)(const std::string& pre,
203 const std::string& str1,
204 const std::string& str2, void* userData,
205 int& retval)) {
206 assertEqCallback = func;
207 }
208
210 void pushAssertEq(const std::string& str, void* userData = nullptr) {
211 assertEqStrQue.push_front(str);
212 assertEqUserDataQue.push_front(userData);
213 }
214
215 void setPop() { m_pop = true; }
216 void clearPop() { m_pop = false; }
217
218 void resetState() {
219 g_internal_log.clear();
220 g_internal_log_copy.clear();
221 logPreVec.clear();
222 logMsgVec.clear();
223 logIndVec.clear();
224 assertEqStrQue.clear();
225 assertEqUserDataQue.clear();
226 }
227};
228
229// Motion Master running in end of the line testing mode
230extern bool g_eol_mode;
231
232// Disable the in-memory cache of device files read over FoE. When true, every
233// read_file() fetches fresh content from the device instead of serving a
234// previously cached result.
235extern bool g_disable_file_cache;
236
237// Skip writing the custom PDO mapping (from 'ui.config.json') during slave
238// initialization. When true, the device keeps whatever PDO mapping it already
239// has and Motion Master simply reads it back. Useful when a device/firmware
240// rejects the mapping SDO writes (wkc=0), which would otherwise leave monitored
241// objects unmapped and force slow per-parameter SDO reads.
242extern bool g_skip_pdo_mapping;
243
244// Disable per-sample SDO polling of monitored parameters. When true, the
245// background parameter refresher is never started, so monitoring serves only
246// the values already in the PDO process image (which update every cycle for
247// free); any monitored object that is NOT PDO-mapped holds the value it had at
248// startup instead of being re-read. The refresher shares the single SOEM port
249// with the cyclic process-data exchange, and on some firmware its SDO traffic
250// starves that exchange and the process image freezes. Turning it off keeps the
251// process data flowing cleanly at the cost of static non-PDO signals.
252extern bool g_disable_sdo_polling;
Manages communication and control for an external autotuning process.
Definition standalone_autotuning.h:289
bool g_eol_mode
Definition global.cc:35
std::string g_driver_name
Definition global.cc:18
std::string g_soem_mac_address
Definition global.cc:25
uint16_t g_pub_sub_port
Definition global.cc:23
std::vector< std::string > g_ethernet_ip_addresses
Definition global.cc:54
uint8 g_pIOmap[4096]
Definition global.cc:14
bool g_disable_file_cache
Definition global.cc:36
size_t g_mock_devices
Definition global.cc:31
std::string g_templates_path
Definition global.cc:32
uint16_t g_autotuning_port
Definition global.cc:5
std::atomic< int > g_slavecount
Definition global.cc:27
std::atomic< bool > g_reinitialization_required
Definition global.cc:28
std::filesystem::path g_data_dir
Definition global.cc:33
bool g_skip_pdo_mapping
Definition global.cc:37
volatile sig_atomic_t g_sig_caught
Definition global.cc:16
std::string g_ethernet_pdo_mode
Definition global.cc:55
bool g_disable_sdo_polling
Definition global.cc:38
std::atomic< bool > g_ethercat_port_reopen_required
Definition global.cc:29
MasterType g_master_type
Definition global.cc:20
uint16_t g_req_res_port
Definition global.cc:22
std::string g_soem_ifname
Definition global.cc:26
StandaloneAutotuning g_standalone_autotuning
Definition global.cc:11
MasterType
Definition global.h:38
@ kUnspecified
Definition global.h:38
@ kSoem
Definition global.h:38
@ kMock
Definition global.h:38
@ kSpoe
Definition global.h:38
void g_soem_update_slavecount()
Definition global.cc:42
Logging class.
Definition global.h:90
void setAssertEqCallback(void(*func)(const std::string &pre, const std::string &str1, const std::string &str2, void *userData, int &retval))
Definition global.h:202
static void log_to_internal_log(void *user_data, const loguru::Message &message)
Definition global.h:196
bool m_pop
Definition global.h:114
void log(const loguru::Message &message)
Callback from loguru.
Definition global.h:147
void clearPop()
Definition global.h:216
std::deque< void * > assertEqUserDataQue
Definition global.h:112
std::vector< std::string > logPreVec
Definition global.h:104
std::deque< std::string > assertEqStrQue
Definition global.h:111
void resetState()
Definition global.h:218
std::string g_internal_log_copy
Definition global.h:102
static std::string & get_log(const std::string &name)
Definition global.h:188
static void setType(const std::string &name, uint32_t type)
Definition global.h:133
void(* assertEqCallback)(const std::string &pre, const std::string &str1, const std::string &str2, void *userData, int &retval)
Definition global.h:116
std::vector< std::string > logIndVec
Definition global.h:108
static InternalLogger * getInstance(const std::string &name)
Definition global.h:121
std::vector< std::string > logMsgVec
Definition global.h:106
std::string g_internal_log
Definition global.h:101
void setPop()
Definition global.h:215
void pushAssertEq(const std::string &str, void *userData=nullptr)
Definition global.h:210