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
32extern std::string g_driver_name;
33
36
37extern uint16_t g_req_res_port;
38extern uint16_t g_pub_sub_port;
39
40extern std::atomic<int> g_slavecount;
41
42// Using SOEM
43extern std::string g_soem_mac_address;
44extern std::string g_soem_ifname;
45extern std::vector<std::recursive_mutex> g_soem_mailbox_mutexes;
46extern std::atomic<bool> g_pdo_mapping_in_progress;
47
49
50// Using Ethernet Master
51extern std::vector<std::string> g_ethernet_ip_addresses;
52extern std::string g_ethernet_pdo_mode;
53
54// Number of mocked devices
55extern size_t g_mock_devices;
56
57// Path to a directory holding the templates for mock devices
58extern std::string g_templates_path;
59
65 private:
66 static std::map<std::string, std::unique_ptr<InternalLogger>> self_map;
68 constexpr static std::uint32_t internal_log_size = 0x200000;
69 std::mutex internal_log_mutex;
71 uint32_t type;
72 std::string name;
73
74 public:
75 std::string g_internal_log;
78 std::vector<std::string> logPreVec;
80 std::vector<std::string> logMsgVec;
82 std::vector<std::string> logIndVec;
85 std::deque<std::string> assertEqStrQue;
86 std::deque<void*> assertEqUserDataQue;
88 bool m_pop;
90 void (*assertEqCallback)(const std::string& pre, const std::string& str1,
91 const std::string& str2, void* userData,
92 int& retval);
93
95 static InternalLogger* getInstance(const std::string& name) {
96 if (InternalLogger::self_map.find(name) == InternalLogger::self_map.end()) {
97 InternalLogger::self_map.insert(
98 {name, std::make_unique<InternalLogger>()});
99 InternalLogger::self_map.at(name).get()->name = name;
100 InternalLogger::self_map.at(name).get()->type = 0;
101 InternalLogger::self_map.at(name).get()->assertEqCallback = nullptr;
102 InternalLogger::self_map.at(name).get()->m_pop = true;
103 }
104 return InternalLogger::self_map.at(name).get();
105 }
106
107 static void setType(const std::string& name, uint32_t type) {
108 InternalLogger::self_map.at(name).get()->type = type;
109 }
110
121 void log(const loguru::Message& message) {
122 std::unique_lock<std::mutex> lk(internal_log_mutex);
123 if (type == 0) {
124 g_internal_log += message.preamble;
125 g_internal_log += message.indentation;
126 g_internal_log += message.message;
127 g_internal_log += "\n";
128 if (g_internal_log.size() > internal_log_size) {
129 // Erase the first 12.5% of the internal log
130 auto pos = g_internal_log.find_first_of('\n', internal_log_size >> 3u);
131 g_internal_log.erase(0, pos + 1);
132 }
133 } else if (type == 1) {
134 logPreVec.push_back(message.preamble);
135 logMsgVec.push_back(message.message);
136 logIndVec.push_back(message.indentation);
137 if (!assertEqStrQue.empty()) {
138 if (assertEqCallback != nullptr) {
139 size_t loopSize = assertEqStrQue.size();
140 for (size_t i = 0; i < loopSize; i++) {
141 int retval = 0;
142 (*assertEqCallback)(logPreVec.back(), logMsgVec.back(),
144 retval);
145 if (retval != 0) {
146 assertEqStrQue.clear();
147 assertEqUserDataQue.clear();
148 break;
149 }
150 if (m_pop) {
151 assertEqStrQue.pop_front();
152 assertEqUserDataQue.pop_front();
153 }
154 }
155 }
156 }
157 }
158 }
159
162 static std::string& get_log(const std::string& name) {
164 std::unique_lock<std::mutex> lk(pil->internal_log_mutex);
166 return pil->g_internal_log_copy;
167 }
168
170 static void log_to_internal_log(void* user_data,
171 const loguru::Message& message) {
172 reinterpret_cast<InternalLogger*>(user_data)->log(message);
173 }
174
176 void setAssertEqCallback(void (*func)(const std::string& pre,
177 const std::string& str1,
178 const std::string& str2, void* userData,
179 int& retval)) {
180 assertEqCallback = func;
181 }
182
184 void pushAssertEq(const std::string& str, void* userData = nullptr) {
185 assertEqStrQue.push_front(str);
186 assertEqUserDataQue.push_front(userData);
187 }
188
189 void setPop() { m_pop = true; }
190 void clearPop() { m_pop = false; }
191
192 void resetState() {
193 g_internal_log.clear();
194 g_internal_log_copy.clear();
195 logPreVec.clear();
196 logMsgVec.clear();
197 logIndVec.clear();
198 assertEqStrQue.clear();
199 assertEqUserDataQue.clear();
200 }
201};
202
203// Motion Master running in end of the line testing mode
204extern bool g_eol_mode;
Manages communication and control for an external autotuning process.
Definition standalone_autotuning.h:271
bool g_eol_mode
Definition global.cc:34
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:48
uint8 g_pIOmap[4096]
Definition global.cc:14
size_t g_mock_devices
Definition global.cc:31
std::string g_templates_path
Definition global.cc:32
std::atomic< int > g_slavecount
Definition global.cc:28
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:49
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
MasterType
Definition global.h:34
@ kUnspecified
Definition global.h:34
@ kSoem
Definition global.h:34
@ kMock
Definition global.h:34
@ kSpoe
Definition global.h:34
void g_soem_update_slavecount()
Definition global.cc:38
StandaloneAutotuning g_standalone_autotuning
Logging class.
Definition global.h:64
void setAssertEqCallback(void(*func)(const std::string &pre, const std::string &str1, const std::string &str2, void *userData, int &retval))
Definition global.h:176
static void log_to_internal_log(void *user_data, const loguru::Message &message)
Definition global.h:170
bool m_pop
Definition global.h:88
void log(const loguru::Message &message)
Callback from loguru.
Definition global.h:121
void clearPop()
Definition global.h:190
std::deque< void * > assertEqUserDataQue
Definition global.h:86
std::vector< std::string > logPreVec
Definition global.h:78
std::deque< std::string > assertEqStrQue
Definition global.h:85
void resetState()
Definition global.h:192
std::string g_internal_log_copy
Definition global.h:76
static std::string & get_log(const std::string &name)
Definition global.h:162
static void setType(const std::string &name, uint32_t type)
Definition global.h:107
void(* assertEqCallback)(const std::string &pre, const std::string &str1, const std::string &str2, void *userData, int &retval)
Definition global.h:90
std::vector< std::string > logIndVec
Definition global.h:82
static InternalLogger * getInstance(const std::string &name)
Definition global.h:95
std::vector< std::string > logMsgVec
Definition global.h:80
std::string g_internal_log
Definition global.h:75
void setPop()
Definition global.h:189
void pushAssertEq(const std::string &str, void *userData=nullptr)
Definition global.h:184