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
71// Whether a working counter from the broadcast read in
72// g_soem_update_slavecount() can be stored as the slave count. ec_slave[] has
73// maxslave entries and entry 0 is the master, so a count of maxslave or more
74// would index past its end. SOEM's own scan applies the same limit. A negative
75// value is accepted: it means no frame came back, and callers treat a count of
76// zero or less as all slaves lost.
77bool g_soem_is_valid_slavecount(int wkc, int maxslave);
78
79// Counts the slaves with one broadcast read and stores the count. timeout_us
80// is how long the read waits for its frame. The cyclic loop passes a short one,
81// because it exchanges no process data while it waits.
82void g_soem_update_slavecount(int timeout_us = 100000);
83
84// Using Ethernet Master
85extern std::vector<std::string> g_ethernet_ip_addresses;
86extern std::string g_ethernet_pdo_mode;
87
88// Number of mocked devices
89extern size_t g_mock_devices;
90
91// Path to a directory holding the templates for mock devices
92extern std::string g_templates_path;
93
94// Root directory for persistent user data (OD cache, etc.)
95extern std::filesystem::path g_data_dir;
96
102 private:
103 static std::map<std::string, std::unique_ptr<InternalLogger>> self_map;
105 constexpr static std::uint32_t internal_log_size = 0x200000;
106 std::mutex internal_log_mutex;
108 uint32_t type;
109 std::string name;
110
111 public:
112 std::string g_internal_log;
115 std::vector<std::string> logPreVec;
117 std::vector<std::string> logMsgVec;
119 std::vector<std::string> logIndVec;
122 std::deque<std::string> assertEqStrQue;
123 std::deque<void*> assertEqUserDataQue;
125 bool m_pop;
127 void (*assertEqCallback)(const std::string& pre, const std::string& str1,
128 const std::string& str2, void* userData,
129 int& retval);
130
132 static InternalLogger* getInstance(const std::string& name) {
133 if (InternalLogger::self_map.find(name) == InternalLogger::self_map.end()) {
134 InternalLogger::self_map.insert(
135 {name, std::make_unique<InternalLogger>()});
136 InternalLogger::self_map.at(name).get()->name = name;
137 InternalLogger::self_map.at(name).get()->type = 0;
138 InternalLogger::self_map.at(name).get()->assertEqCallback = nullptr;
139 InternalLogger::self_map.at(name).get()->m_pop = true;
140 }
141 return InternalLogger::self_map.at(name).get();
142 }
143
144 static void setType(const std::string& name, uint32_t type) {
145 InternalLogger::self_map.at(name).get()->type = type;
146 }
147
158 void log(const loguru::Message& message) {
159 std::unique_lock<std::mutex> lk(internal_log_mutex);
160 if (type == 0) {
161 g_internal_log += message.preamble;
162 g_internal_log += message.indentation;
163 g_internal_log += message.message;
164 g_internal_log += "\n";
165 if (g_internal_log.size() > internal_log_size) {
166 // Erase the first 12.5% of the internal log
167 auto pos = g_internal_log.find_first_of('\n', internal_log_size >> 3u);
168 g_internal_log.erase(0, pos + 1);
169 }
170 } else if (type == 1) {
171 logPreVec.push_back(message.preamble);
172 logMsgVec.push_back(message.message);
173 logIndVec.push_back(message.indentation);
174 if (!assertEqStrQue.empty()) {
175 if (assertEqCallback != nullptr) {
176 size_t loopSize = assertEqStrQue.size();
177 for (size_t i = 0; i < loopSize; i++) {
178 int retval = 0;
179 (*assertEqCallback)(logPreVec.back(), logMsgVec.back(),
181 retval);
182 if (retval != 0) {
183 assertEqStrQue.clear();
184 assertEqUserDataQue.clear();
185 break;
186 }
187 if (m_pop) {
188 assertEqStrQue.pop_front();
189 assertEqUserDataQue.pop_front();
190 }
191 }
192 }
193 }
194 }
195 }
196
199 static std::string& get_log(const std::string& name) {
201 std::unique_lock<std::mutex> lk(pil->internal_log_mutex);
203 return pil->g_internal_log_copy;
204 }
205
207 static void log_to_internal_log(void* user_data,
208 const loguru::Message& message) {
209 reinterpret_cast<InternalLogger*>(user_data)->log(message);
210 }
211
213 void setAssertEqCallback(void (*func)(const std::string& pre,
214 const std::string& str1,
215 const std::string& str2, void* userData,
216 int& retval)) {
217 assertEqCallback = func;
218 }
219
221 void pushAssertEq(const std::string& str, void* userData = nullptr) {
222 assertEqStrQue.push_front(str);
223 assertEqUserDataQue.push_front(userData);
224 }
225
226 void setPop() { m_pop = true; }
227 void clearPop() { m_pop = false; }
228
229 void resetState() {
230 g_internal_log.clear();
231 g_internal_log_copy.clear();
232 logPreVec.clear();
233 logMsgVec.clear();
234 logIndVec.clear();
235 assertEqStrQue.clear();
236 assertEqUserDataQue.clear();
237 }
238};
239
240// Motion Master running in end of the line testing mode
241extern bool g_eol_mode;
242
243// Disable the in-memory cache of device files read over FoE. When true, every
244// read_file() fetches fresh content from the device instead of serving a
245// previously cached result.
246extern bool g_disable_file_cache;
247
248// Disable the object dictionary cache in the data directory. When true, the
249// object dictionary is read from every device on every scan, and no cache file
250// is read or written.
251extern bool g_disable_od_cache;
252
253// Skip writing the custom PDO mapping (from 'ui.config.json') during slave
254// initialization. When true, the device keeps whatever PDO mapping it already
255// has and Motion Master simply reads it back. Useful when a device/firmware
256// rejects the mapping SDO writes (wkc=0), which would otherwise leave monitored
257// objects unmapped and force slow per-parameter SDO reads.
258extern bool g_skip_pdo_mapping;
259
260// Disable per-sample SDO polling of monitored parameters. When true, the
261// background parameter refresher is never started, so monitoring serves only
262// the values already in the PDO process image (which update every cycle for
263// free); any monitored object that is NOT PDO-mapped holds the value it had at
264// startup instead of being re-read. The refresher shares the single SOEM port
265// with the cyclic process-data exchange, and on some firmware its SDO traffic
266// starves that exchange and the process image freezes. Turning it off keeps the
267// process data flowing cleanly at the cost of static non-PDO signals.
268extern 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:72
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
bool g_disable_od_cache
Definition global.cc:37
std::filesystem::path g_data_dir
Definition global.cc:33
bool g_skip_pdo_mapping
Definition global.cc:38
volatile sig_atomic_t g_sig_caught
Definition global.cc:16
std::string g_ethernet_pdo_mode
Definition global.cc:73
bool g_disable_sdo_polling
Definition global.cc:39
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(int timeout_us=100000)
Definition global.cc:47
bool g_soem_is_valid_slavecount(int wkc, int maxslave)
Definition global.cc:43
Logging class.
Definition global.h:101
void setAssertEqCallback(void(*func)(const std::string &pre, const std::string &str1, const std::string &str2, void *userData, int &retval))
Definition global.h:213
static void log_to_internal_log(void *user_data, const loguru::Message &message)
Definition global.h:207
bool m_pop
Definition global.h:125
void log(const loguru::Message &message)
Callback from loguru.
Definition global.h:158
void clearPop()
Definition global.h:227
std::deque< void * > assertEqUserDataQue
Definition global.h:123
std::vector< std::string > logPreVec
Definition global.h:115
std::deque< std::string > assertEqStrQue
Definition global.h:122
void resetState()
Definition global.h:229
std::string g_internal_log_copy
Definition global.h:113
static std::string & get_log(const std::string &name)
Definition global.h:199
static void setType(const std::string &name, uint32_t type)
Definition global.h:144
void(* assertEqCallback)(const std::string &pre, const std::string &str1, const std::string &str2, void *userData, int &retval)
Definition global.h:127
std::vector< std::string > logIndVec
Definition global.h:119
static InternalLogger * getInstance(const std::string &name)
Definition global.h:132
std::vector< std::string > logMsgVec
Definition global.h:117
std::string g_internal_log
Definition global.h:112
void setPop()
Definition global.h:226
void pushAssertEq(const std::string &str, void *userData=nullptr)
Definition global.h:221