-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.cpp
More file actions
437 lines (338 loc) · 12.5 KB
/
main.cpp
File metadata and controls
437 lines (338 loc) · 12.5 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
#if defined(__linux__) && defined(MRC_DAEMON_BUILD)
#include <signal.h>
#endif
#include <stdio.h>
#include "version.h"
#include <linux/can.h>
#include <linux/can/raw.h>
#include <net/if.h>
#include <sys/ioctl.h>
#include <filesystem>
#include <wpi/net/EventLoopRunner.hpp>
#include <wpi/net/uv/Poll.hpp>
#include <wpi/net/uv/FsEvent.hpp>
#include <wpi/net/uv/Timer.hpp>
#include "wpi/nt/NetworkTableInstance.hpp"
#include "wpi/nt/RawTopic.hpp"
#include "wpi/nt/IntegerTopic.hpp"
#include "wpi/nt/DoubleTopic.hpp"
#include "systemd/sd-device.h"
#include "SerialPort.h"
#include <deque>
#include "wpi/nt/BooleanTopic.hpp"
#include <wpi/util/timestamp.h>
#include "ReceiveStateMachine.h"
#include "MessageNumbers.h"
#include "wpi/math/controller/PIDController.hpp"
#include "wpi/math/controller/SimpleMotorFeedforward.hpp"
#include <wpi/units/length.hpp>
#include <wpi/units/velocity.hpp>
#include <wpi/units/voltage.hpp>
#include <wpi/units/acceleration.hpp>
#include "ExpansionHubNtState.h"
#include "ExpansionHubSerial.h"
#include "EnabledState.h"
#include "SystemDUsbMonitor.h"
#include "systemd-utils.h"
struct ExpansionHubState {
uint64_t lastLoop = wpi::util::Now();
int socketHandle{-1};
eh::ExpansionHubNtState ntStore;
const wpi::nt::NetworkTableInstance* ntInstance;
unsigned busId{0};
std::unique_ptr<eh::ExpansionHubSerial> currentHub;
~ExpansionHubState() {
if (socketHandle != -1) {
close(socketHandle);
}
}
void OnUpdate(bool canEnable);
void SendInitial();
void SendCommands(bool canEnable, bool deviceReset);
bool StartUvLoop(unsigned bus, const wpi::nt::NetworkTableInstance& ntInst,
wpi::net::uv::Loop& loop);
void OnDeviceAdded(std::unique_ptr<eh::ExpansionHubSerial> hub);
void OnDeviceRemoved(std::string_view port);
};
void ExpansionHubState::SendInitial() {
// Make sure keep alive is the first thing sent, its needed for recovery
currentHub->SendKeepAlive();
currentHub->GetModuleStatus();
// Do encoder resets next
for (int i = 0; i < NUM_MOTORS_PER_HUB; i++) {
auto reset = ntStore.motors[i].resetEncoderSubscriber.GetAtomic(false);
if (reset.time != ntStore.motors[i].lastResetTime) {
ntStore.motors[i].lastResetTime = reset.time;
ntStore.motors[i].doReset = true;
}
if (ntStore.motors[i].doReset) {
currentHub->SendEncoderResetRequest(i);
}
}
// Do requests next
currentHub->SendBatteryRequest();
currentHub->SendBulkInput();
// Then the servos
for (int i = 0; i < NUM_SERVOS_PER_HUB; i++) {
currentHub->SendServoPulseWidth(
i, ntStore.servos[i].pulseWidthSubscriber.Get(1500));
}
currentHub->Flush();
}
void ExpansionHubState::SendCommands(bool canEnable, bool deviceReset) {
// We've unrolled these so we can control updates together to make
// sure they happen as close as possible.
if (deviceReset) {
for (int i = 0; i < NUM_MOTORS_PER_HUB; i++) {
ntStore.motors[i].enabledSubscriber.ForceReset();
ntStore.motors[i].floatOn0Subscriber.ForceReset();
}
for (int i = 0; i < NUM_SERVOS_PER_HUB; i++) {
ntStore.servos[i].enabledSubscriber.ForceReset();
ntStore.servos[i].framePeriodSubscriber.ForceReset();
}
}
// Compute motor powers
std::pair<double, int> motorPowers[NUM_MOTORS_PER_HUB];
for (int i = 0; i < NUM_MOTORS_PER_HUB; i++) {
motorPowers[i] =
ntStore.motors[i].ComputeMotorPower(ntStore.lastBattery);
}
// Then send the 4 motors.
for (int i = 0; i < NUM_MOTORS_PER_HUB; i++) {
if (motorPowers[i].second < 0) {
currentHub->SendMotorConstantPower(i, motorPowers[i].first);
} else if (motorPowers[i].second < 4) {
// TODO, determine if a follower is following a follower, and what
// to do
currentHub->SendMotorConstantPower(
i, motorPowers[motorPowers[i].second].first);
} else {
currentHub->SendMotorConstantPower(i, 0.0);
}
}
// Then get the motor currents
for (int i = 0; i < NUM_MOTORS_PER_HUB; i++) {
currentHub->SendMotorCurrentRequest(i);
}
// Then all the things that are not expect to change, but we want to keep
// sending in case of reset.
for (int i = 0; i < NUM_MOTORS_PER_HUB; i++) {
auto sendFloatOn0 = ntStore.motors[i].floatOn0Subscriber.Get();
if (sendFloatOn0.has_value()) {
currentHub->SendMotorMode(i, *sendFloatOn0);
}
}
for (int i = 0; i < NUM_MOTORS_PER_HUB; i++) {
auto sendEnable =
ntStore.motors[i].enabledSubscriber.GetWithCanEnable(canEnable);
if (sendEnable.has_value()) {
currentHub->SendMotorEnable(i, *sendEnable);
}
}
for (int i = 0; i < NUM_SERVOS_PER_HUB; i++) {
auto servoConfig = ntStore.servos[i].framePeriodSubscriber.Get();
if (servoConfig.has_value()) {
currentHub->SendServoConfiguration(i, *servoConfig);
}
}
for (int i = 0; i < NUM_SERVOS_PER_HUB; i++) {
auto sendEnable =
ntStore.servos[i].enabledSubscriber.GetWithCanEnable(canEnable);
if (sendEnable.has_value()) {
currentHub->SendServoEnable(i, *sendEnable);
}
}
currentHub->Flush();
}
void ExpansionHubState::OnDeviceAdded(
std::unique_ptr<eh::ExpansionHubSerial> hub) {
currentHub = std::move(hub);
ntStore.Initialize(*ntInstance, busId);
ntStore.isConnectedPublisher.Set(true);
printf("Device added\n");
ntStore.numNacks = 0;
ntStore.numNacksPublisher.Set(ntStore.numNacks);
ntStore.numCrcFailures = 0;
ntStore.numCrcFailuresPublisher.Set(ntStore.numCrcFailures);
ntStore.numMissedSendLoops = 0;
ntStore.numMissedSendLoopsPublisher.Set(ntStore.numMissedSendLoops);
for (int i = 0; i < NUM_MOTORS_PER_HUB; i++) {
ntStore.motors[i].enabledSubscriber.ForceReset();
ntStore.motors[i].floatOn0Subscriber.ForceReset();
}
for (int i = 0; i < NUM_SERVOS_PER_HUB; i++) {
ntStore.servos[i].enabledSubscriber.ForceReset();
ntStore.servos[i].framePeriodSubscriber.ForceReset();
}
currentHub->SetCallbacks(
[this](bool canEnable, bool deviceReset) {
SendCommands(canEnable, deviceReset);
},
&ntStore);
// TODO we only want the timer running if we have a device.
}
void ExpansionHubState::OnDeviceRemoved(std::string_view path) {
if (currentHub && path == currentHub->SerialPath()) {
ntStore.isConnectedPublisher.Set(false);
currentHub.reset();
}
}
void ExpansionHubState::OnUpdate(bool canEnable) {
if (!currentHub) {
return;
}
if (!currentHub->HasFinishedSynchronous()) {
currentHub->RunSynchronousSteps();
return;
}
auto now = wpi::util::Now();
auto delta = now - lastLoop;
bool allowSend = currentHub->AllowSend();
if (!allowSend && delta < 1000000) {
printf("Skipping due to outstanding\n");
ntStore.numMissedSendLoops++;
ntStore.numMissedSendLoopsPublisher.Set(ntStore.numMissedSendLoops);
return;
} else if (!allowSend && delta >= 1000000) {
printf("1 second timeout. Attempting to recover\n");
currentHub->Recover();
}
lastLoop = now;
if (delta > 23000) {
printf("Delta time %lu\n", delta);
}
currentHub->StartTransaction(canEnable);
SendInitial();
}
static void OnDeviceRemoved(
std::array<ExpansionHubState, NUM_USB_BUSES>& states,
const std::string& devPath) {
std::string_view path = devPath;
for (auto&& i : states) {
i.OnDeviceRemoved(path);
}
}
static void OnDeviceAdded(wpi::net::uv::Loop& loop,
std::array<ExpansionHubState, NUM_USB_BUSES>& states,
int busNum, const std::string& devPath) {
if (states[busNum].currentHub != nullptr) {
printf("Received duplicate bus, likely race condition\n");
return;
}
int ret = eh::OpenRhspSerialPort(devPath.c_str());
if (ret < 0) {
printf("OpenRhspSerialPort failed %d\n", ret);
return;
}
std::unique_ptr<eh::ExpansionHubSerial> sl =
std::make_unique<eh::ExpansionHubSerial>();
bool isInit = sl->Initialize(loop, ret, devPath);
printf("initialized %d\n", isInit ? 1 : 0);
sl->RunSynchronousSteps();
states[busNum].OnDeviceAdded(std::move(sl));
}
bool ExpansionHubState::StartUvLoop(unsigned bus,
const wpi::nt::NetworkTableInstance& ntInst,
wpi::net::uv::Loop& loop) {
if (bus >= NUM_USB_BUSES) {
return false;
}
busId = bus;
ntInstance = &ntInst;
return true;
}
int main() {
printf("Starting ExpansionHubDaemon\n");
printf("\tBuild Hash: %s\n", MRC_GetGitHash());
printf("\tBuild Timestamp: %s\n", MRC_GetBuildTimestamp());
#if defined(__linux__) && defined(MRC_DAEMON_BUILD)
sigset_t signal_set;
sigemptyset(&signal_set);
sigaddset(&signal_set, SIGTERM);
sigaddset(&signal_set, SIGINT);
sigprocmask(SIG_BLOCK, &signal_set, nullptr);
#endif
eh::EnabledState enabledState;
if (!enabledState.Initialize()) {
printf("Failed to open control data.\n");
return -1;
}
std::array<ExpansionHubState, NUM_USB_BUSES> states;
eh::SystemDUsbMonitor usbMonitor{
[&states](wpi::net::uv::Loop& loop, int busNum,
const std::string& devPath) {
OnDeviceAdded(loop, states, busNum, devPath);
},
[&states](const std::string& devPath) {
OnDeviceRemoved(states, devPath);
}};
auto ntInst = wpi::nt::NetworkTableInstance::Create();
ntInst.SetServer({"localhost"}, 6810);
ntInst.StartClient("ExpansionHubDaemon");
wpi::net::EventLoopRunner loopRunner;
struct LoopStorage {
std::array<ExpansionHubState, NUM_USB_BUSES>* hubStates;
wpi::net::uv::Loop* loop;
} loopStorage{
.hubStates = &states,
.loop = nullptr,
};
bool success = false;
loopRunner.ExecSync([&success, &states, &loopStorage, &ntInst, &usbMonitor,
&enabledState](wpi::net::uv::Loop& loop) {
loopStorage.loop = &loop;
for (size_t i = 0; i < states.size(); i++) {
success = states[i].StartUvLoop(i, ntInst, loop);
if (!success) {
return;
}
}
auto usbMonResult = usbMonitor.Initialize(&loop);
if (!usbMonResult) {
printf("SystemDUsbMonitor::Initialize failed\n");
success = false;
return;
}
auto poll = wpi::net::uv::Poll::Create(loop, usbMonitor.GetFd());
if (!poll) {
printf("Poll create failed\n");
success = false;
return;
}
poll->pollEvent.connect(
[&usbMonitor](int flags) { usbMonitor.HandleEvent(); });
poll->Start(UV_READABLE);
auto sendTimer = wpi::net::uv::Timer::Create(loop);
sendTimer->timeout.connect([&states, &enabledState]() {
bool system_watchdog = enabledState.IsEnabled();
for (auto&& dev : states) {
dev.OnUpdate(system_watchdog);
}
});
wpi::units::millisecond_t millis = eh::PidConstants::Period;
uint64_t rawMillis = static_cast<uint64_t>(millis.value());
sendTimer->Start(wpi::net::uv::Timer::Time{rawMillis},
wpi::net::uv::Timer::Time{rawMillis});
// Enumerate everything for initial checking
usbMonitor.DoInitialCheck();
});
if (!success) {
loopRunner.Stop();
return -1;
}
systemd_utils::notify_ready();
{
#if defined(__linux__) && defined(MRC_DAEMON_BUILD)
int sig = 0;
sigwait(&signal_set, &sig);
#else
(void)getchar();
#endif
}
systemd_utils::notify_stopping();
ntInst.StopClient();
wpi::nt::NetworkTableInstance::Destroy(ntInst);
loopRunner.Stop();
return 0;
}