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;
49extern std::vector<std::recursive_mutex> g_soem_mailbox_mutexes;
50extern std::atomic<bool> g_pdo_mapping_in_progress;
51
52// Requests a full re-enumeration of the EtherCAT bus from within the cyclic
53// SOEM loop. The SOEM recovery path sets this when it detects that slaves
54// dropped off the bus and returned (a power brown-out): such slaves lose their
55// SyncManager/FMMU/PDO configuration and cannot be healed in place, so the
56// machine manager's run loop watches this flag and escalates to the proven
57// VirtualDeviceManager::init() re-enumeration (which also emits the
58// DEINITIALIZING/INITIALIZING lifecycle events clients depend on). It is
59// cleared once that stop()/init() sequence has been kicked off.
60extern std::atomic<bool> g_reinitialization_required;
61
63
64// Using Ethernet Master
65extern std::vector<std::string> g_ethernet_ip_addresses;
66extern std::string g_ethernet_pdo_mode;
67
68// Number of mocked devices
69extern size_t g_mock_devices;
70
71// Path to a directory holding the templates for mock devices
72extern std::string g_templates_path;
73
74// Root directory for persistent user data (OD cache, etc.)
75extern std::filesystem::path g_data_dir;
76
82 private:
83 static std::map<std::string, std::unique_ptr<InternalLogger>> self_map;
85 constexpr static std::uint32_t internal_log_size = 0x200000;
86 std::mutex internal_log_mutex;
88 uint32_t type;
89 std::string name;
90
91 public:
92 std::string g_internal_log;
95 std::vector<std::string> logPreVec;
97 std::vector<std::string> logMsgVec;
99 std::vector<std::string> logIndVec;
102 std::deque<std::string> assertEqStrQue;
103 std::deque<void*> assertEqUserDataQue;
105 bool m_pop;
107 void (*assertEqCallback)(const std::string& pre, const std::string& str1,
108 const std::string& str2, void* userData,
109 int& retval);
110
112 static InternalLogger* getInstance(const std::string& name) {
113 if (InternalLogger::self_map.find(name) == InternalLogger::self_map.end()) {
114 InternalLogger::self_map.insert(
115 {name, std::make_unique<InternalLogger>()});
116 InternalLogger::self_map.at(name).get()->name = name;
117 InternalLogger::self_map.at(name).get()->type = 0;
118 InternalLogger::self_map.at(name).get()->assertEqCallback = nullptr;
119 InternalLogger::self_map.at(name).get()->m_pop = true;
120 }
121 return InternalLogger::self_map.at(name).get();
122 }
123
124 static void setType(const std::string& name, uint32_t type) {
125 InternalLogger::self_map.at(name).get()->type = type;
126 }
127
138 void log(const loguru::Message& message) {
139 std::unique_lock<std::mutex> lk(internal_log_mutex);
140 if (type == 0) {
141 g_internal_log += message.preamble;
142 g_internal_log += message.indentation;
143 g_internal_log += message.message;
144 g_internal_log += "\n";
145 if (g_internal_log.size() > internal_log_size) {
146 // Erase the first 12.5% of the internal log
147 auto pos = g_internal_log.find_first_of('\n', internal_log_size >> 3u);
148 g_internal_log.erase(0, pos + 1);
149 }
150 } else if (type == 1) {
151 logPreVec.push_back(message.preamble);
152 logMsgVec.push_back(message.message);
153 logIndVec.push_back(message.indentation);
154 if (!assertEqStrQue.empty()) {
155 if (assertEqCallback != nullptr) {
156 size_t loopSize = assertEqStrQue.size();
157 for (size_t i = 0; i < loopSize; i++) {
158 int retval = 0;
159 (*assertEqCallback)(logPreVec.back(), logMsgVec.back(),
161 retval);
162 if (retval != 0) {
163 assertEqStrQue.clear();
164 assertEqUserDataQue.clear();
165 break;
166 }
167 if (m_pop) {
168 assertEqStrQue.pop_front();
169 assertEqUserDataQue.pop_front();
170 }
171 }
172 }
173 }
174 }
175 }
176
179 static std::string& get_log(const std::string& name) {
181 std::unique_lock<std::mutex> lk(pil->internal_log_mutex);
183 return pil->g_internal_log_copy;
184 }
185
187 static void log_to_internal_log(void* user_data,
188 const loguru::Message& message) {
189 reinterpret_cast<InternalLogger*>(user_data)->log(message);
190 }
191
193 void setAssertEqCallback(void (*func)(const std::string& pre,
194 const std::string& str1,
195 const std::string& str2, void* userData,
196 int& retval)) {
197 assertEqCallback = func;
198 }
199
201 void pushAssertEq(const std::string& str, void* userData = nullptr) {
202 assertEqStrQue.push_front(str);
203 assertEqUserDataQue.push_front(userData);
204 }
205
206 void setPop() { m_pop = true; }
207 void clearPop() { m_pop = false; }
208
209 void resetState() {
210 g_internal_log.clear();
211 g_internal_log_copy.clear();
212 logPreVec.clear();
213 logMsgVec.clear();
214 logIndVec.clear();
215 assertEqStrQue.clear();
216 assertEqUserDataQue.clear();
217 }
218};
219
220// Motion Master running in end of the line testing mode
221extern bool g_eol_mode;
222
223// Disable the in-memory cache of device files read over FoE. When true, every
224// read_file() fetches fresh content from the device instead of serving a
225// previously cached result.
226extern bool g_disable_file_cache;
227
228// Skip writing the custom PDO mapping (from 'ui.config.json') during slave
229// initialization. When true, the device keeps whatever PDO mapping it already
230// has and Motion Master simply reads it back. Useful when a device/firmware
231// rejects the mapping SDO writes (wkc=0), which would otherwise leave monitored
232// objects unmapped and force slow per-parameter SDO reads.
233extern bool g_skip_pdo_mapping;
234
235// Disable per-sample SDO polling of monitored parameters. When true, the
236// background parameter refresher is never started, so monitoring serves only
237// the values already in the PDO process image (which update every cycle for
238// free); any monitored object that is NOT PDO-mapped holds the value it had at
239// startup instead of being re-read. The refresher shares the single SOEM port
240// with the cyclic process-data exchange, and on some firmware its SDO traffic
241// starves that exchange and the process image freezes. Turning it off keeps the
242// process data flowing cleanly at the cost of static non-PDO signals.
243extern 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:36
std::string g_driver_name
Definition global.cc:18
std::vector< std::recursive_mutex > g_soem_mailbox_mutexes
Definition global.cc:27
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:53
uint8 g_pIOmap[4096]
Definition global.cc:14
bool g_disable_file_cache
Definition global.cc:37
size_t g_mock_devices
Definition global.cc:32
std::string g_templates_path
Definition global.cc:33
uint16_t g_autotuning_port
Definition global.cc:5
std::atomic< int > g_slavecount
Definition global.cc:28
std::atomic< bool > g_reinitialization_required
Definition global.cc:30
std::filesystem::path g_data_dir
Definition global.cc:34
bool g_skip_pdo_mapping
Definition global.cc:38
volatile sig_atomic_t g_sig_caught
Definition global.cc:16
std::atomic< bool > g_pdo_mapping_in_progress
Definition global.cc:29
std::string g_ethernet_pdo_mode
Definition global.cc:54
bool g_disable_sdo_polling
Definition global.cc:39
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:43
Logging class.
Definition global.h:81
void setAssertEqCallback(void(*func)(const std::string &pre, const std::string &str1, const std::string &str2, void *userData, int &retval))
Definition global.h:193
static void log_to_internal_log(void *user_data, const loguru::Message &message)
Definition global.h:187
bool m_pop
Definition global.h:105
void log(const loguru::Message &message)
Callback from loguru.
Definition global.h:138
void clearPop()
Definition global.h:207
std::deque< void * > assertEqUserDataQue
Definition global.h:103
std::vector< std::string > logPreVec
Definition global.h:95
std::deque< std::string > assertEqStrQue
Definition global.h:102
void resetState()
Definition global.h:209
std::string g_internal_log_copy
Definition global.h:93
static std::string & get_log(const std::string &name)
Definition global.h:179
static void setType(const std::string &name, uint32_t type)
Definition global.h:124
void(* assertEqCallback)(const std::string &pre, const std::string &str1, const std::string &str2, void *userData, int &retval)
Definition global.h:107
std::vector< std::string > logIndVec
Definition global.h:99
static InternalLogger * getInstance(const std::string &name)
Definition global.h:112
std::vector< std::string > logMsgVec
Definition global.h:97
std::string g_internal_log
Definition global.h:92
void setPop()
Definition global.h:206
void pushAssertEq(const std::string &str, void *userData=nullptr)
Definition global.h:201