-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathArduinoProxy.cpp
More file actions
173 lines (144 loc) · 4.55 KB
/
ArduinoProxy.cpp
File metadata and controls
173 lines (144 loc) · 4.55 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include "ArduinoProxy.h"
#include "IO/ArduinoEncoder.h"
#include <spdlog/spdlog.h>
#include <sys/poll.h>
// If we haven't received a pin update in that duration, remove it from the map
const auto pinStateTimeout = std::chrono::seconds(5);
ArduinoProxy::ArduinoProxy() = default;
ArduinoProxy::~ArduinoProxy() = default;
ArduinoProxy *ArduinoProxy::getInstance()
{
static ArduinoProxy instance;
return &instance;
}
void ArduinoProxy::initialize()
{
std::lock_guard<std::mutex> lockGuard(serialMutex);
if ((fd = serialOpen("/dev/ttyAMA0", 57600)) < 0)
{
SPDLOG_LOGGER_ERROR(logger, "Error while opening serial communication!");
return;
}
inititialized = true;
IO::initialize();
}
bool ArduinoProxy::isInitialized()
{
return inititialized;
}
void ArduinoProxy::run()
{
std::vector<char> buffer;
while (true)
{
struct pollfd pfds[1] = {fd, POLLIN};
poll(pfds, 1, -1);
while (serialDataAvail(fd) > 0)
{
char value = serialGetchar(fd);
buffer.push_back(value);
if (value == 0x0)
{
auto msg = ArduinoEncoder::decode<RocketryProto::ArduinoOut>(buffer.data(), buffer.size());
handleArduinoMessage(msg);
buffer.clear();
}
}
}
}
void ArduinoProxy::handleArduinoMessage(const RocketryProto::ArduinoOut &arduinoOut)
{
auto event = arduinoOut.eventmessage().type();
auto eventData = arduinoOut.eventmessage().data();
auto error = arduinoOut.errormessage().type();
auto errorData = arduinoOut.errormessage().data();
switch (arduinoOut.data_case())
{
case RocketryProto::ArduinoOut::kEventMessage:
if (eventData != 0)
{
SPDLOG_LOGGER_INFO(logger, "Arduino Event: {} {}", RocketryProto::EventTypes_Name(event), eventData);
}
else
{
SPDLOG_LOGGER_INFO(logger, "Arduino Event: {}", RocketryProto::EventTypes_Name(event));
}
break;
case RocketryProto::ArduinoOut::kErrorMessage:
if (eventData != 0)
{
SPDLOG_LOGGER_WARN(logger, "Arduino Error: {} {}", RocketryProto::ErrorTypes_Name(error), errorData);
}
else
{
SPDLOG_LOGGER_WARN(logger, "Arduino Error: {}", RocketryProto::ErrorTypes_Name(error));
}
break;
case RocketryProto::ArduinoOut::kServoState: {
std::lock_guard<std::mutex> lockGuard(stateMutex);
const auto &servoState = arduinoOut.servostate();
servoStates[servoState.pin()] = {servoState.position(), std::chrono::steady_clock::now()};
}
break;
case RocketryProto::ArduinoOut::kDigitalState: {
std::lock_guard<std::mutex> lockGuard(stateMutex);
const auto &digitalState = arduinoOut.digitalstate();
digitalStates[digitalState.pin()] = {digitalState.activated(), std::chrono::steady_clock::now()};
}
break;
case RocketryProto::ArduinoOut::DATA_NOT_SET:
SPDLOG_LOGGER_WARN(logger, "Data field not set in Arduino message. ");
break;
}
}
void ArduinoProxy::send(const RocketryProto::ArduinoIn &data)
{
if (inititialized)
{
std::lock_guard<std::mutex> lockGuard(serialMutex);
helper::SharedArray<char> encodedData = ArduinoEncoder::encode(data);
serialPutchar(fd, 0);
for (int i = 0; i < encodedData.length; i++)
{
serialPutchar(fd, encodedData.data[i]);
}
}
}
/**
* Returns the state of the pin. If we don't know the state, throws a std::out_of_range.
*/
bool ArduinoProxy::getDigitalState(int pin)
{
std::lock_guard<std::mutex> lockGuard(stateMutex);
auto state = digitalStates.at(pin);
auto now = std::chrono::steady_clock::now();
if (now - state.second >= pinStateTimeout)
{
digitalStates.erase(pin);
SPDLOG_ERROR("Arduino stopped reporting digital pin {}", pin);
throw std::out_of_range("Pin has been removed");
}
else
{
return state.first;
}
}
/**
* Returns the state of the pin. If we don't know the state, throws a std::out_of_range.
*/
int ArduinoProxy::getServoState(int pin)
{
std::lock_guard<std::mutex> lockGuard(stateMutex);
auto state = servoStates.at(pin);
auto now = std::chrono::steady_clock::now();
if (now - state.second >= pinStateTimeout)
{
servoStates.erase(pin);
SPDLOG_ERROR("Arduino stopped reporting servo pin {}", pin);
throw std::out_of_range("Pin has been removed");
}
else
{
return state.first;
}
}