Repository navigation
Expand file tree
/
Copy pathwasp.cpp
More file actions
154 lines (129 loc) · 5.15 KB
/
Copy pathwasp.cpp
File metadata and controls
154 lines (129 loc) · 5.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/////////////////////////////////////////////////////////////////////////////////
// @file wasp_m.cpp
// @brief Wasp implementation
// @author Chip Brommer
/////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////
//
// Includes:
// name reason included
// ------------------ ------------------------
#include "wasp.h" // Header
//
/////////////////////////////////////////////////////////////////////////////////
Wasp::Wasp(const std::string& settingsLocation, const std::string& buildLocation, const std::string& configLocation) :
m_settings(settingsLocation), m_build(buildLocation), m_config(configLocation),
m_name("WASP"), m_logger(), m_signalManger(m_logger), m_imuManager(m_logger),
m_gpsManager(m_logger), m_webServer(m_logger), m_initialized(false)
{
// Load the configs and catch any failures
if (!m_settings.Load())
{
std::cerr << "[WASP] - ERROR - Failed to Load/Create settings file.\n";
}
if (!m_build.Load())
{
std::cerr << "[WASP] - ERROR - Failed to Load/Create build file.\n";
}
if (!m_config.Load())
{
std::cerr << "[WASP] - ERROR - Failed to Load/Create config file.\n";
}
// Start the logger, if file logging is enabled in config, enable it.
if (m_settings.data.fileLoggingEnabled && !m_settings.data.logFilePath.empty())
{
m_logger.EnableFileLogging(m_settings.data.logFilePath);
}
m_loggingThread = std::thread([this] { m_logger.Run(); });
// Sleep a little while the logger sets up
std::this_thread::sleep_for(std::chrono::milliseconds(100));
// Start the web server
std::string dir = WEB_FILES_DIR;
m_webServer.Configure(m_settings.data.webPort, dir);
m_webThread = std::thread([this] { m_webServer.Start(); });
// Initialize the signal manager PWMs
m_logger.AddLog(m_name, LogClient::LogLevel::Info, "Starting Signal Manager.");
m_signalManger.ReadyFin(SignalManager::FIN::ONE, m_config.data.fin1Path, m_config.data.fin1Channel, m_config.data.finMinDegrees, m_config.data.finMaxDegrees);
m_signalManger.ReadyFin(SignalManager::FIN::TWO, m_config.data.fin2Path, m_config.data.fin2Channel, m_config.data.finMinDegrees, m_config.data.finMaxDegrees);
m_signalManger.ReadyFin(SignalManager::FIN::THREE, m_config.data.fin3Path, m_config.data.fin3Channel, m_config.data.finMinDegrees, m_config.data.finMaxDegrees);
m_signalManger.ReadyFin(SignalManager::FIN::FOUR, m_config.data.fin4Path, m_config.data.fin4Channel, m_config.data.finMinDegrees, m_config.data.finMaxDegrees);
m_signalThread = std::thread([this] { m_signalManger.Start(); });
// Configure the IMU
if(!m_imuManager.Configure(m_config.data.imuUnit, m_config.data.imuPort, m_config.data.imuBaudRate))
{
m_logger.AddLog(m_name, LogClient::LogLevel::Info, "IMU Manager failed to configure, exiting.");
// Close();
//return;
}
// Configure the GPS
if (!m_gpsManager.Configure(m_config.data.gpsUnit, m_config.data.gpsPort, m_config.data.gpsBaudRate))
{
m_logger.AddLog(m_name, LogClient::LogLevel::Info, "GPS Manager failed to configure, exiting.");
//Close();
//return;
}
m_initialized = true;
}
Wasp::~Wasp()
{
Close();
}
void Wasp::Execute()
{
if (!m_initialized) return;
m_run = true;
while (m_run)
{
// Check for GPS updates
int gpsRead = m_gpsManager.Read();
if (gpsRead < 0) break;
else if (gpsRead > 0)
{
m_gpsData = m_gpsManager.GetCommonData();
}
nlohmann::json json = {
{"data", {
{"hour", m_gpsData.hour},
{"min", m_gpsData.min},
{"sec", m_gpsData.sec},
{"latitude", m_gpsData.latitude},
{"longitude", m_gpsData.longitude},
{"altitude", m_gpsData.altitude}
}}
};
static int sendCount = 0;
if (sendCount == 5)
{
json = {
{"message", {
{"text", "This is a sample message."},
{"timeout", 5 }
}},
{"data", {
{"hour", m_gpsData.hour},
{"min", m_gpsData.min},
{"sec", m_gpsData.sec},
{"latitude", m_gpsData.latitude},
{"longitude", m_gpsData.longitude},
{"altitude", m_gpsData.altitude}
}}
};
}
if (m_webServer.SendJsonOverWebSocket(json))
{
sendCount++;
}
}
}
void Wasp::Close()
{
// Stop the execution loop
m_run = false;
m_signalManger.Stop();
if (m_signalThread.joinable()) m_signalThread.join();
m_webServer.Stop();
if (m_webThread.joinable()) m_webThread.join();
// Close the logger last but wait until all logs have been written
m_logger.Stop(true);
if (m_loggingThread.joinable()) m_loggingThread.join();
}