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
23 changes: 21 additions & 2 deletions include/apps.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,23 @@
#pragma once

//APPS = Accelerator Pedal Position Sensor
//-----------------------CONSTANTS------------------------
extern const double PEDAL_MIN;
extern const double PEDAL_MAX;
extern const double PEDAL_MAX;

// APPS Pins
const int APPS_1_PIN = A6;
const int APPS_2_PIN = A7;

// APPS_PLAUSIBILITY_THRESHOLDS
// Brake Sensor
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)

// TIMEOUTS
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)

//-----------------------FUNCTION PROTOTYPES------------------------
double get_apps_reading(); // Returns pedal position (%) or -1.0 on implausibility
49 changes: 49 additions & 0 deletions include/bpps.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#ifndef BPPS_H
#define BPPS_H
/**
* @file bpps.h
* @brief Handles reading brake pressure sensors, MPU6050 tilt sensor, and controls brake light
* @author Muhammad Ibrahim Mansoor (UCD Formula Student)
* @date 2025-10-9
*/

//BPPS = Brake Pedal Position Sensor
//----------------- CONSTANTS ------------------
//PINS
// Analog Pins (INPUT)
const int BRAKE_PRESSURE_SENSOR_PIN_FRONT = A0;
const int BRAKE_PRESSURE_SENSOR_PIN_REAR = A1;

// Digital Pins (OUTPUT)
const int BRAKE_LIGHT_PIN = 7;

//GYRO
extern bool mpuInitialized;
const float TILT_THRESHOLD_DEG = 5.8; // For MPU6050 brake light activation

// Brake Light calibration constants
const int BRAKE_LIGHT_THRESHOLD = 109; // Calibrated
const int BRAKE_LIGHT_HYSTERESIS = 2; // Calibrated

//--------------- GLOBAL VARIABLES --------------------

//BRAKE PRESSURE VARIABLES
extern int brakePressureFront;
extern int brakePressureRear;
extern int brakePressureCombined;
//BRAKE IDLE VALUES
extern int brakeIdleValueFront;
extern int brakeIdleValueRear;
extern int brakeIdleValueCombined;

extern int dynamicBrakeThreshold;
extern bool brakeInitialized;

//------------ GLOBAL OBJECTS ---------------
extern Adafruit_MPU6050 mpu;
#endif // BPPS_H

//------------ FUNCTION PROTOTYPES -----------
bool initializeMPU(); // Initialize MPU6050 sensor
void brake_light(); // Reads brake pressure, MPU, controls brake light
void recalibrate_brake_idle(); // Recalibrate brake idle values
9 changes: 9 additions & 0 deletions include/dashboard.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#ifndef DASHBOARD_H
#define DASHBOARD_H

// ------------ FUNCTION PROTOTYPES ------------
void dash_setup(); // Setup for Nextion display
void dash_loop(); // Update loop for Nextion display
void sendPlottableData(); // Send data for plotting

#endif // DASHBOARD_H
23 changes: 23 additions & 0 deletions include/error_monitor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#ifndef ERROR_MONITOR_H
#define ERROR_MONITOR_H
// ------------ CONSTANTS ------------
// --- 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.
const bool BENCH_TESTING_MODE = true;

// --- Pins ---
const int ERROR_PIN_START = 22; // Start pin for error monitoring
const int ERROR_PIN_END = 37; // End pin for error monitoring

// ------------ FUNCTION PROTOTYPES ------------
void monitor_errors_setup(); // Setup error monitoring
void monitor_errors_loop(); // Error monitoring loop
void checkCriticalSystems(); // Check critical systems and update warnings
const char* getErrorString(int errorCode); // Get error string from error code



#endif // ERROR_MONITOR_H
72 changes: 7 additions & 65 deletions include/header.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* @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(2025 update: central header file that also contains error checking and logging stuff)
* @author Shane Whelan (UCD Formula Student)
* @date 2025-04-27
*/
Expand Down Expand Up @@ -39,79 +39,21 @@
#include "apps.h" // APPS reading constants/functions
#include "bamocar-due.h" // Bamocar motor controller library
#include "simple_can.h" // For standardising a central module for CAN comms
#include "bpps.h" // Brake pedal position sensor
#include "dashboard.h" // Dashboard display functions
#include "error_monitor.h" // Error monitoring functions

// ------------ CONSTANTS ------------
// --- General ---
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.
const bool BENCH_TESTING_MODE = true;

// --- Pins ---
// 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
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)

// 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)

// ------------ GLOBAL OBJECT INSTANCES (declared extern here) ------------
extern Adafruit_MPU6050 mpu;

// ------------ GLOBAL VARIABLES ------------
extern int brakePressureFront;
extern int brakePressureRear;
extern int brakePressureCombined;
extern int brakeIdleValueFront;
extern int brakeIdleValueRear;
extern int brakeIdleValueCombined;
extern int dynamicBrakeThreshold;
extern bool brakeInitialized;
// ------------ GLOBAL VARIABLES ------------ Add to bamocar-due.h?
extern int vehicleSpeed;
extern int motorRPM;
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

// --- 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

// --- Utility Functions ---
bool initializeMPU(); // Initialize MPU6050 sensor
void motor_control_update(); // Handle motor control logic including safety checks // add to bamocar-due.h?

#endif // HEADER_H

1 change: 1 addition & 0 deletions src/apps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

// Define pedal calibration constants - UPDATED based on actual measurements
// These are RAW ADC values (not voltages), since values DECREASE when pressed
//add to apps.h?
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%)
Expand Down
4 changes: 2 additions & 2 deletions src/brake_light.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ int brakeIdleValueRear = 0;
int brakeIdleValueCombined = 0;
int dynamicBrakeThreshold = 0;
bool brakeInitialized = false;
const int BRAKE_THRESHOLD_DELTA = 10; // Threshold above idle value
const int BRAKE_THRESHOLD_DELTA = 10; // Threshold above idle value //add to bpps.h?

extern Adafruit_MPU6050 mpu;
extern Adafruit_MPU6050 mpu;
extern bool mpuInitialized;

bool initializeMPU() {
Expand Down
4 changes: 2 additions & 2 deletions src/dashboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ bool systemAlert = false;
bool systemWarning = false;
char alertMessageStr[50] = "";
char warningMessageStr[50] = "";
int motorTempWarningThreshold = 80;
float batteryVoltageMinThreshold = 20.0;
int motorTempWarningThreshold = 80; //add to header?
float batteryVoltageMinThreshold = 20.0; //add to header?
int systemErrorCode = 0;

// Define Nextion objects for page 2
Expand Down