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 <iostream>
9#include <map>
10#include <mutex>
11#include <queue>
12#include <string>
13#include <thread>
14#include <unordered_map>
15#include <vector>
16
17#include "ethercat.h"
18#include "loguru.h"
20
21#ifndef M_PI
22#define M_PI 3.14159265358979323846
23#endif
24
26
27extern uint8 g_pIOmap[4096];
28
29// SIGINT or SIGTERM caught
30extern volatile sig_atomic_t g_sig_caught;
31
34
35extern uint16_t g_req_res_port;
36extern uint16_t g_pub_sub_port;
37
38extern std::atomic<int> g_slavecount;
39
40// Using SOEM
41extern std::string g_soem_mac_address;
42extern std::string g_soem_ifname;
43extern std::vector<std::recursive_mutex> g_soem_mailbox_mutexes;
44extern std::atomic<bool> g_pdo_mapping_in_progress;
45
47
48// Using Ethernet Master
49extern std::vector<std::string> g_ethernet_ip_addresses;
50
51// Number of mocked devices
52extern size_t g_mock_devices;
53
54// Path to a directory holding the templates for mock devices
55extern std::string g_templates_path;
56
62 public:
63 private:
64 static std::map<std::string, std::unique_ptr<InternalLogger>> self_map;
66 constexpr static std::uint32_t internal_log_size = 0x200000;
67 std::mutex internal_log_mutex;
69 uint32_t type;
70 std::string name;
71
72 public:
73 std::string g_internal_log;
76 std::vector<std::string> logPreVec;
78 std::vector<std::string> logMsgVec;
80 std::vector<std::string> logIndVec;
83 std::deque<std::string> assertEqStrQue;
84 std::deque<void*> assertEqUserDataQue;
86 bool m_pop;
88 void (*assertEqCallback)(const std::string& pre, const std::string& str1,
89 const std::string& str2, void* userData,
90 int& retval);
91
93 static InternalLogger* getInstance(const std::string& name) {
94 if (InternalLogger::self_map.find(name) == InternalLogger::self_map.end()) {
95 InternalLogger::self_map.insert(
96 {name, std::make_unique<InternalLogger>()});
97 InternalLogger::self_map.at(name).get()->name = name;
98 InternalLogger::self_map.at(name).get()->type = 0;
99 InternalLogger::self_map.at(name).get()->assertEqCallback = nullptr;
100 InternalLogger::self_map.at(name).get()->m_pop = true;
101 }
102 return InternalLogger::self_map.at(name).get();
103 }
104
105 static void setType(const std::string& name, uint32_t type) {
106 InternalLogger::self_map.at(name).get()->type = type;
107 }
108
119 void log(const loguru::Message& message) {
120 std::unique_lock<std::mutex> lk(internal_log_mutex);
121 if (type == 0) {
122 g_internal_log += message.preamble;
123 g_internal_log += message.indentation;
124 g_internal_log += message.message;
125 g_internal_log += "\n";
126 if (g_internal_log.size() > internal_log_size) {
127 // Erase the first 12.5% of the internal log
128 auto pos = g_internal_log.find_first_of('\n', internal_log_size >> 3u);
129 g_internal_log.erase(0, pos + 1);
130 }
131 } else if (type == 1) {
132 logPreVec.push_back(message.preamble);
133 logMsgVec.push_back(message.message);
134 logIndVec.push_back(message.indentation);
135 if (!assertEqStrQue.empty()) {
136 if (assertEqCallback != nullptr) {
137 size_t loopSize = assertEqStrQue.size();
138 for (size_t i = 0; i < loopSize; i++) {
139 int retval = 0;
140 (*assertEqCallback)(logPreVec.back(), logMsgVec.back(),
142 retval);
143 if (retval != 0) {
144 assertEqStrQue.clear();
145 assertEqUserDataQue.clear();
146 break;
147 }
148 if (m_pop) {
149 assertEqStrQue.pop_front();
150 assertEqUserDataQue.pop_front();
151 }
152 }
153 }
154 }
155 }
156 }
157
160 static std::string& get_log(const std::string& name) {
162 std::unique_lock<std::mutex> lk(pil->internal_log_mutex);
164 return pil->g_internal_log_copy;
165 }
166
168 static void log_to_internal_log(void* user_data,
169 const loguru::Message& message) {
170 reinterpret_cast<InternalLogger*>(user_data)->log(message);
171 }
172
174 void setAssertEqCallback(void (*func)(const std::string& pre,
175 const std::string& str1,
176 const std::string& str2, void* userData,
177 int& retval)) {
178 assertEqCallback = func;
179 }
180
182 void pushAssertEq(const std::string& str, void* userData = nullptr) {
183 assertEqStrQue.push_front(str);
184 assertEqUserDataQue.push_front(userData);
185 }
186
187 void setPop() { m_pop = true; }
188 void clearPop() { m_pop = false; }
189
190 void resetState() {
191 g_internal_log.clear();
192 g_internal_log_copy.clear();
193 logPreVec.clear();
194 logMsgVec.clear();
195 logIndVec.clear();
196 assertEqStrQue.clear();
197 assertEqUserDataQue.clear();
198 }
199};
200
201// Motion Master running in end of the line testing mode
202extern bool g_eol_mode;
Manages communication and control for an external autotuning process.
Definition: standalone_autotuning.h:269
uint8_t type
Definition: co_dictionary.h:1
const char ** name
Definition: co_dictionary.h:7
bool g_eol_mode
Definition: global.cc:32
MasterType
Definition: global.h:32
std::vector< std::recursive_mutex > g_soem_mailbox_mutexes
Definition: global.cc:25
std::string g_soem_mac_address
Definition: global.cc:23
uint16_t g_pub_sub_port
Definition: global.cc:21
std::vector< std::string > g_ethernet_ip_addresses
Definition: global.cc:46
uint8 g_pIOmap[4096]
Definition: global.cc:14
size_t g_mock_devices
Definition: global.cc:29
void g_soem_update_slavecount()
Definition: global.cc:36
std::string g_templates_path
Definition: global.cc:30
std::atomic< int > g_slavecount
Definition: global.cc:26
volatile sig_atomic_t g_sig_caught
Definition: global.cc:16
std::atomic< bool > g_pdo_mapping_in_progress
Definition: global.cc:27
MasterType g_master_type
Definition: global.cc:18
uint16_t g_req_res_port
Definition: global.cc:20
std::string g_soem_ifname
Definition: global.cc:24
StandaloneAutotuning g_standalone_autotuning
Logging class.
Definition: global.h:61
void setAssertEqCallback(void(*func)(const std::string &pre, const std::string &str1, const std::string &str2, void *userData, int &retval))
Definition: global.h:174
static void log_to_internal_log(void *user_data, const loguru::Message &message)
Definition: global.h:168
bool m_pop
Definition: global.h:86
void log(const loguru::Message &message)
Callback from loguru.
Definition: global.h:119
void clearPop()
Definition: global.h:188
std::deque< void * > assertEqUserDataQue
Definition: global.h:84
std::vector< std::string > logPreVec
Definition: global.h:76
std::deque< std::string > assertEqStrQue
Definition: global.h:83
void resetState()
Definition: global.h:190
std::string g_internal_log_copy
Definition: global.h:74
static std::string & get_log(const std::string &name)
Definition: global.h:160
static void setType(const std::string &name, uint32_t type)
Definition: global.h:105
void(* assertEqCallback)(const std::string &pre, const std::string &str1, const std::string &str2, void *userData, int &retval)
Definition: global.h:88
std::vector< std::string > logIndVec
Definition: global.h:80
static InternalLogger * getInstance(const std::string &name)
Definition: global.h:93
std::vector< std::string > logMsgVec
Definition: global.h:78
std::string g_internal_log
Definition: global.h:73
void setPop()
Definition: global.h:187
void pushAssertEq(const std::string &str, void *userData=nullptr)
Definition: global.h:182