Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
.vscode/c_cpp_properties.json
.vscode/launch.json
.vscode/ipch
.cache/*
410 changes: 410 additions & 0 deletions compile_commands.json

Large diffs are not rendered by default.

42 changes: 41 additions & 1 deletion include/apps.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,44 @@
#pragma once

/* Accelerator Pedal Position Sensor
See T.11.8 and T.11.9 for more info
Requirments:
- Detect implausibility where there is a 10% difference or more between the two
sensors
- Shut down main motors when implausibility exists for 100ms or more
- Fail safe (shut down main motors) when short to ground or supply, open,
mechanicaly impossible, or otherwise broken
- Fail safe within 500ms of detecting a failure

*/

#include <Arduino.h>
#include <SPI.h>
#include <Wire.h>

// ------------ EXTERNAL LIBRARIES ------------
#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
#include <Nextion.h>
#include <due_can.h>

namespace APPS {

double
get_apps_reading(); // Returns pedal position (%) or -1.0 on implausibility

extern const double PEDAL_MIN;
extern const double PEDAL_MAX;
extern const double PEDAL_MAX;
constexpr int APPS_1_PIN = A6;
constexpr int APPS_2_PIN = A7;

// Define pedal calibration constants - UPDATED based on actual measurements
// These are RAW ADC values (not voltages), since values DECREASE when pressed
constexpr int APPS1_RAW_MIN = 477; // Value when pedal is fully pressed (100%)
constexpr int APPS1_RAW_MAX = 702; // Value when pedal is released (0%)
constexpr int APPS2_RAW_MIN = 478; // Value when pedal is fully pressed (100%)
constexpr int APPS2_RAW_MAX = 701; // Value when pedal is released (0%)

// APPS
constexpr float APPS_PLAUSIBILITY_THRESHOLD = 10.0f; // T11.8.9
} // namespace APPS
37 changes: 18 additions & 19 deletions include/header.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
/**
* @file header.h
* @brief Central header file including libraries, constants, pin definitions, and function prototypes
* @brief Central header file including libraries, constants, pin definitions,
* and function prototypes
* @author Shane Whelan (UCD Formula Student)
* @date 2025-04-27
*/
*/

// TODO: calibration below
// consider using deceleration directly from MPU if required by rules
Expand All @@ -23,7 +24,8 @@
#include <SPI.h>
#include <Wire.h>

// Undefine Arduino's min/max macros to avoid conflicts with C++ standard library
// Undefine Arduino's min/max macros to avoid conflicts with C++ standard
// library
#undef max
#undef min

Expand All @@ -42,7 +44,8 @@

// ------------ CONSTANTS ------------
// --- General ---
const int DEBUG_MODE = 4; // 0=Off, 1=Essential, 2=Verbose, 3=Very Verbose, 4=Max Debug
const int DEBUG_MODE =
4; // 0=Off, 1=Essential, 2=Verbose, 3=Very Verbose, 4=Max Debug

// VCU-HACK: Set to true to bypass BMS checks for bench testing without a BMS.
// TODO: MUST BE FALSE FOR VEHICLE OPERATION.
Expand All @@ -52,8 +55,7 @@ const bool BENCH_TESTING_MODE = true;
// Analog Pins
const int BRAKE_PRESSURE_SENSOR_PIN_FRONT = A0;
const int BRAKE_PRESSURE_SENSOR_PIN_REAR = A1;
const int APPS_1_PIN = A6;
const int APPS_2_PIN = A7;

// Digital Pins
const int BRAKE_LIGHT_PIN = 7;
const int ERROR_PIN_START = 22; // Start pin for error monitoring
Expand All @@ -62,16 +64,14 @@ const int ERROR_PIN_END = 37; // End pin for error monitoring
// --- Thresholds & Parameters ---
// Brake System
const int BRAKE_LIGHT_THRESHOLD = 109; // Calibrated
const int BRAKE_LIGHT_HYSTERESIS = 2; // Calibrated
const float TILT_THRESHOLD_DEG = 5.8; // For MPU6050 brake light activation
const int APPS_BRAKE_PLAUSIBILITY_THRESHOLD = 25; // % APPS request threshold for brake plausibility check (Rule EV.5.7)

// APPS
const float APPS_PLAUSIBILITY_THRESHOLD = 10.0f; // % difference threshold (Rule EV.5.6)
const int BRAKE_LIGHT_HYSTERESIS = 2; // Calibrated
const float TILT_THRESHOLD_DEG = 5.8; // For MPU6050 brake light activation

// Timing
const unsigned long APPS_PLAUSIBILITY_TIMEOUT_MS = 100; // Max time for APPS implausibility (Rule EV.5.6.3)
const unsigned long APPS_BRAKE_PLAUSIBILITY_TIMEOUT_MS = 500; // Max time for APPS/Brake implausibility (Rule EV.2.3.1)
const unsigned long APPS_PLAUSIBILITY_TIMEOUT_MS =
100; // Max time for APPS implausibility (Rule EV.5.6.3)
const unsigned long APPS_BRAKE_PLAUSIBILITY_TIMEOUT_MS =
500; // Max time for APPS/Brake implausibility (Rule EV.2.3.1)

// ------------ GLOBAL OBJECT INSTANCES (declared extern here) ------------
extern Adafruit_MPU6050 mpu;
Expand All @@ -91,25 +91,24 @@ extern float batteryVoltage;
extern int motorTemperature;
extern bool mpuInitialized;


// ------------ FUNCTION PROTOTYPES ------------

// --- Sensor/Input Modules ---
double get_apps_reading(); // Returns pedal position (%) or -1.0 on implausibility
void brake_light(); // Reads brake pressure, MPU, controls brake light
void recalibrate_brake_idle(); // Recalibrate brake idle values

// --- Actuator/Control Modules ---
void motor_control_update(); // Handle motor control logic including safety checks
void motor_control_update(); // Handle motor control logic including safety
// checks

// --- Monitoring/Dashboard Modules ---
void monitor_errors_setup(); // Setup error monitoring
void monitor_errors_loop(); // Error monitoring loop
void dash_setup(); // Setup for Nextion display
void dash_loop(); // Update loop for Nextion display
void checkCriticalSystems(); // Check critical systems and update warnings
const char* getErrorString(int errorCode); // Get error string from error code
void sendPlottableData(); // Send data for plotting
const char *getErrorString(int errorCode); // Get error string from error code
void sendPlottableData(); // Send data for plotting

// --- Utility Functions ---
bool initializeMPU(); // Initialize MPU6050 sensor
Expand Down
60 changes: 42 additions & 18 deletions src/apps.cpp
Original file line number Diff line number Diff line change
@@ -1,42 +1,41 @@
/**
* @file apps_sensor_reader.cpp
* @brief Handles reading and validating Accelerator Pedal Position Sensors (APPS)
* @brief Handles reading and validating Accelerator Pedal Position Sensors
* (APPS)
* @author Shane Whelan (UCD Formula Student)
* @date 2025-07-17
*/

#include "apps.h"
#include "header.h"
#include "variant.h"
#include <cmath> // For std::fabs
#include <limits> // For std::numeric_limits

// Define pedal calibration constants - UPDATED based on actual measurements
// These are RAW ADC values (not voltages), since values DECREASE when pressed
const int APPS1_RAW_MIN = 477; // Value when pedal is fully pressed (100%)
const int APPS1_RAW_MAX = 702; // Value when pedal is released (0%)
const int APPS2_RAW_MIN = 478; // Value when pedal is fully pressed (100%)
const int APPS2_RAW_MAX = 701; // Value when pedal is released (0%)
using namespace APPS;

/**
* @brief Reads APPS sensors, checks for plausibility, and returns average pedal position
* @return Pedal position (0.0 to 100.0) if sensors are plausible, -1.0 if implausible
* @brief Reads APPS sensors, checks for plausibility, and returns average pedal
* position
* @return Pedal position (0.0 to 100.0) if sensors are plausible, -1.0 if
* implausible
*/
double get_apps_reading() {
double APPS::get_apps_reading() {
int apps_1_raw = analogRead(APPS_1_PIN);
int apps_2_raw = analogRead(APPS_2_PIN);

// Convert raw ADC values directly to percentage (0-100) - INVERTED values
// Note: values decrease as pedal is pressed, so we invert the calculation
double apps_1_percent = 100.0 * (APPS1_RAW_MAX - apps_1_raw) /
(APPS1_RAW_MAX - APPS1_RAW_MIN);
double apps_2_percent = 100.0 * (APPS2_RAW_MAX - apps_2_raw) /
(APPS2_RAW_MAX - APPS2_RAW_MIN);
double apps_1_percent =
100.0 * (APPS1_RAW_MAX - apps_1_raw) / (APPS1_RAW_MAX - APPS1_RAW_MIN);
double apps_2_percent =
100.0 * (APPS2_RAW_MAX - apps_2_raw) / (APPS2_RAW_MAX - APPS2_RAW_MIN);

// Clamp values to 0-100 range
apps_1_percent = constrain(apps_1_percent, 0.0, 100.0);
apps_2_percent = constrain(apps_2_percent, 0.0, 100.0);

// Check for implausibility (FSUK EV.5.6: deviation > 10%)
if (std::fabs(apps_1_percent - apps_2_percent) >
// Check for implausibility (FSUK T11.8.9: deviation > 10%)
if (std::fabs(apps_1_percent - apps_2_percent) >
APPS_PLAUSIBILITY_THRESHOLD) {
if (DEBUG_MODE) {
Serial.print("APPS Implausibility Detected! APPS1: ");
Expand All @@ -48,6 +47,31 @@ double get_apps_reading() {
return -1.0; // Indicate implausibility
}

// Verify electrical plausiblity as per T11.9.2
if (apps_1_raw > APPS1_RAW_MAX || apps_1_raw < APPS1_RAW_MIN ||
apps_2_raw > APPS2_RAW_MAX || apps_2_raw < APPS2_RAW_MIN) {
if (DEBUG_MODE) {
Serial.print("APP Output Exceeds Limits!");
Serial.print("APPS1 Raw: ");
Serial.print(apps_1_raw);
Serial.print(", APPS1 Max: ");
Serial.print(APPS1_RAW_MAX);
Serial.print(", APPS1 Min: ");
Serial.print(APPS1_RAW_MIN);

Serial.print(", APPS2 Raw: ");
Serial.print(apps_2_raw);
Serial.print(", APPS2 Max: ");
Serial.print(APPS2_RAW_MAX);
Serial.print(", APPS2 Min: ");
Serial.println(APPS2_RAW_MIN);
}
return -1.0;
// It should be acceptable to treat this the same as an implausibility as
// the requirements for implausibilites are more strict than those for SCS
// faults
}

// Return the average percentage if plausible
double average_percent = (apps_1_percent + apps_2_percent) / 2.0;

Expand All @@ -69,4 +93,4 @@ double get_apps_reading() {
}

return average_percent;
}
}
Loading