generated from RIT-EVT/project-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHIB.cpp
More file actions
98 lines (77 loc) · 2.99 KB
/
HIB.cpp
File metadata and controls
98 lines (77 loc) · 2.99 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
#include <HIB.hpp>
#include <dev/RedundantADC.hpp>
namespace HIB {
HIB::HIB(DEV::RedundantADC& throttle, DEV::RedundantADC& brake)
: throttle(throttle), brake(brake) {
// Initialize payload to 0's
for (uint8_t i = 0; i < payloadLength; i++) {
payload[i] = 0;
}
}
// Reads the 2 sets of 3 ADCs and processes their errors while packaging them for CAN
void HIB::process() {
// Read in the voltages from the throttle
readThrottleVoltage();
readBrakeVoltage();
if (throttleVoltage < 300) {
throttleVoltage = 0;
}
if (brakeVoltage < 300) {
brakeVoltage = 0;
}
payload[0] = static_cast<uint8_t>(throttleVoltage >> 8 & 0xFF);
payload[1] = static_cast<uint8_t>(throttleVoltage & 0xFF);
payload[2] = static_cast<uint8_t>(brakeVoltage >> 8 & 0xFF);
payload[3] = static_cast<uint8_t>(brakeVoltage & 0xFF);
if (acceptableThrottleMarginErrors > ACCEPTABLE_MARGIN_ERROR_COUNT) {
acceptableThrottleMarginErrors = ACCEPTABLE_MARGIN_ERROR_COUNT + 1;
payload[4] |= 0b01;
}
if (acceptableBrakeMarginErrors > ACCEPTABLE_MARGIN_ERROR_COUNT) {
acceptableBrakeMarginErrors = ACCEPTABLE_MARGIN_ERROR_COUNT + 1;
payload[4] |= 0b10;
}
if (precisionThrottleMarginErrors > PRECISION_MARGIN_ERROR_COUNT) {
precisionThrottleMarginErrors = PRECISION_MARGIN_ERROR_COUNT + 1;
payload[4] |= 0b01;
}
if (precisionBrakeMarginErrors > PRECISION_MARGIN_ERROR_COUNT) {
precisionBrakeMarginErrors = PRECISION_MARGIN_ERROR_COUNT + 1;
payload[4] |= 0b10;
}
if (comparisonThrottleErrors > COMPARISON_ERROR_COUNT) {
comparisonThrottleErrors = COMPARISON_ERROR_COUNT + 1;
payload[4] |= 0b01;
}
if (comparisonBrakeErrors > COMPARISON_ERROR_COUNT) {
comparisonBrakeErrors = COMPARISON_ERROR_COUNT + 1;
payload[4] |= 0b10;
}
}
void HIB::readThrottleVoltage() {
const DEV::RedundantADC::Status status = throttle.read(throttleVoltage); // gets the errors and voltage from the ADC cluster
// Increment the status of each error if it is received
if (status == DEV::RedundantADC::Status::ACCEPTABLE_MARGIN_EXCEEDED) {
acceptableThrottleMarginErrors++;
}
if (status == DEV::RedundantADC::Status::PRECISION_MARGIN_EXCEEDED) {
precisionThrottleMarginErrors++;
}
if (status == DEV::RedundantADC::Status::COMPARISON_ERROR) {
comparisonThrottleErrors++;
}
}
void HIB::readBrakeVoltage() {
const DEV::RedundantADC::Status status = brake.read(brakeVoltage); // gets the errors and voltage from the ADC cluster
// Increment the status of each error if it is received
if (status == DEV::RedundantADC::Status::ACCEPTABLE_MARGIN_EXCEEDED) {
acceptableBrakeMarginErrors++;
}
if (status == DEV::RedundantADC::Status::PRECISION_MARGIN_EXCEEDED) {
precisionBrakeMarginErrors++;
}
if (status == DEV::RedundantADC::Status::COMPARISON_ERROR) {
comparisonBrakeErrors++;
}
}
}