Formula Student Electronics & Software
The code for the embedded software
Loading...
Searching...
No Matches
apps.cpp
Go to the documentation of this file.
1#include "apps.h"
2#include "debug.h"
3#include "can.h"
4
5#include <Arduino.h>
6#include <elapsedMillis.h>
7
10
11volatile uint16_t brakeValue;
12bool APPsTimeout = false;
13
16
17int average(int* buffer, int n) {
18 int sum = 0;
19 for (int i = 0; i < n; i++) {
20 sum += buffer[i];
21 }
22 return sum / n;
23}
24
25void bufferInsert(int* buffer, int n, int value) {
26 for (int i = 0; i < n - 1; i++) {
27 buffer[i] = buffer[i + 1];
28 }
29 buffer[n - 1] = value;
30}
31
32int scaleApps2(int apps2) {
34}
35
36bool plausibility(int v_apps1, int v_apps2) {
37 if (v_apps1 < v_apps2)
38 return false;
39
40 if (v_apps1 > APPS_1_UPPER_BOUND || v_apps1 < APPS_1_LOWER_BOUND)
41 return false;
42
43 if (v_apps2 > APPS_2_UPPER_BOUND || v_apps2 < APPS_2_LOWER_BOUND)
44 return false;
45
46 if (v_apps1 >= APPS1_DEAD_THRESHOLD)
47 return v_apps2 >= APPS1_DEADZONE_EQUIVALENT - APPS_MAX_ERROR_ABS && v_apps2 <= APPS_2_UPPER_BOUND;
48
49 if (v_apps2 <= APPS2_DEAD_THRESHOLD)
50 return v_apps1 >= APPS_1_LOWER_BOUND && v_apps1 <= APPS2_DEADZONE_EQUIVALENT + APPS_MAX_ERROR_ABS;
51
52 int v2_expected = scaleApps2(v_apps2);
53 int plausibility_value = abs(v2_expected - v_apps1) * 100 / v_apps1;
54
55 return (plausibility_value < APPS_MAX_ERROR_PERCENT);
56}
57
59 int torqueVal = 0;
60
62 torqueVal = apps1;
63 else
64 torqueVal = scaleApps2(apps2);
65
66 int bamoMax = BAMOCAR_MAX;
67 int bamoMin = BAMOCAR_MIN;
68 int appsMax = APPS_MAX;
69 int appsMin = APPS_MIN;
70
71 if (torqueVal > appsMax)
72 torqueVal = appsMax;
73 if (torqueVal < appsMin)
74 torqueVal = appsMin;
75
76 // maps sensor value to bamocar range
77 torqueVal = map(torqueVal, appsMin, appsMax, bamoMin, bamoMax);
78
79 return torqueVal >= BAMOCAR_MAX ? BAMOCAR_MAX : torqueVal;
80}
81
82int readApps() {
83 int v_apps1 = analogRead(APPS_1_PIN);
84 int v_apps2 = analogRead(APPS_2_PIN);
85
88
89 v_apps1 = average(avgBuffer1, AVG_SAMPLES);
90 v_apps2 = average(avgBuffer2, AVG_SAMPLES);
91 sendAPPS(v_apps1,v_apps2);
92
93#ifdef APPS_DEBUG
94 INFO("APPS1: %d\tAPPS2: %d\t", v_apps1, v_apps2);
95#endif // APPS_DEBUG
96
97 bool plausible = plausibility(v_apps1, v_apps2);
98
100#ifdef APPS_DEBUG
101 ERROR("APPS Implausible\n");
102#endif // APPS_DEBUG
103 return -1;
104 }
105
106 if (plausible)
108
109 int bamocarValue = convertToBamocarScale(v_apps1, v_apps2);
110
111#ifdef APPS_DEBUG
112 INFO("Plausible\t Torque Request:%d\t", bamocarValue);
113#endif // APPS_DEBUG
114
115 if (APPsTimeout) {
116 LOG("APPS Blocked\n");
117 if (bamocarValue == 0) {
118 APPsTimeout = false;
119 LOG("APPS Available\n");
120 } else
121 return 0;
122 }
123
124 float pedalTravelPercent = ((float)bamocarValue / BAMOCAR_MAX) * 100.0;
125
126#ifdef APPS_DEBUG
127 INFO("Travel \%: %.2f\tBrake Val: %d\tBrake Timer: ", pedalTravelPercent, brakeValue);
128 Serial.println(appsBrakePlausibilityTimer);
129#endif // APPS_DEBUG
130
131 if (brakeValue >= BRAKE_BLOCK_THRESHOLD && pedalTravelPercent >= 25.0) {
133 ERROR("APPS and Brake Implausible\n");
134 APPsTimeout = true;
135 return 0;
136 }
137 } else
139
140 return bamocarValue;
141}
bool plausibility(int v_apps1, int v_apps2)
Definition apps.cpp:36
elapsedMillis appsBrakePlausibilityTimer
Definition apps.cpp:9
void bufferInsert(int *buffer, int n, int value)
Definition apps.cpp:25
elapsedMillis appsImplausibilityTimer
Definition apps.cpp:8
volatile uint16_t brakeValue
Definition apps.cpp:11
int readApps()
Definition apps.cpp:82
int scaleApps2(int apps2)
Definition apps.cpp:32
int avgBuffer2[AVG_SAMPLES]
Definition apps.cpp:15
int convertToBamocarScale(int apps1, int apps2)
Definition apps.cpp:58
bool APPsTimeout
Definition apps.cpp:12
int avgBuffer1[AVG_SAMPLES]
Definition apps.cpp:14
#define APPS_1_LOWER_BOUND
Definition apps.h:8
#define BAMOCAR_MAX
Definition apps.h:23
#define APPS_1_PIN
Definition apps.h:4
#define APPS_MAX_ERROR_ABS
Definition apps.h:27
#define APPS_MAX
Definition apps.h:21
#define APPS_LINEAR_OFFSET
Definition apps.h:19
#define APPS_MIN
Definition apps.h:22
#define APPS_1_UPPER_BOUND
Definition apps.h:7
#define APPS_MAX_ERROR_PERCENT
Definition apps.h:26
#define BRAKE_BLOCK_THRESHOLD
Definition apps.h:31
#define APPS_2_LOWER_BOUND
Definition apps.h:11
#define APPS1_DEADZONE_EQUIVALENT
Definition apps.h:16
#define APPS_2_UPPER_BOUND
Definition apps.h:10
#define APPS_BRAKE_PLAUSIBILITY_TIMEOUT_MS
Definition apps.h:34
#define BAMOCAR_MIN
Definition apps.h:24
#define APPS2_DEADZONE_EQUIVALENT
Definition apps.h:17
#define APPS2_DEAD_THRESHOLD
Definition apps.h:14
#define APPS_IMPLAUSIBLE_TIMEOUT_MS
Definition apps.h:33
#define APPS1_DEAD_THRESHOLD
Definition apps.h:13
#define APPS_2_PIN
Definition apps.h:5
int apps2
Definition main.cpp:65
int apps1
Definition main.cpp:64
#define AVG_SAMPLES
Definition main.cpp:9
void sendAPPS(int val1, int val2)
Definition can.cpp:227
#define ERROR(...)
Definition debug.h:35
#define LOG(...)
Definition debug.h:25
#define INFO(...)
Definition debug.h:20
long average
Definition message.cpp:14