Skip to content

Commit

Permalink
Merge branch 'develop' into regen-braking-jack
Browse files Browse the repository at this point in the history
  • Loading branch information
Sabramz committed Jul 30, 2024
2 parents 3bb3a93 + e4da055 commit 32fde07
Show file tree
Hide file tree
Showing 11 changed files with 142 additions and 14 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/build-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:
timeout-minutes: 10
steps:
- name: Checkout code
uses: actions/checkout@v3
uses: actions/checkout@v4
with:
submodules: recursive

Expand All @@ -17,4 +17,4 @@ jobs:
if ! make; then
echo "The application has failed to build."
exit 1 # This will cause the workflow to fail
fi
fi
20 changes: 20 additions & 0 deletions .github/workflows/format-check.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: Code Style Check

on: [push]

jobs:
formatting-check:
name: Check format of C
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
submodules: recursive
- name: Run clang-format style check for C/C++ sources
uses: Northeastern-Electric-Racing/clang-format-action@main
with:
clang-format-version: '18'
# only check core, embedded base covered internally
check-path: "Core/"
# use the clang-format from embedded base
format-filepath: "./Drivers/Embedded-Base/clang-format"
6 changes: 6 additions & 0 deletions Core/.clang-format-ignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
./*/stm32*
./*/sys*
./*/FreeRTOS*
./*/freertos*
./*/main*
./Startup/*
13 changes: 9 additions & 4 deletions Core/Inc/cerberus_conf.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,25 @@
#define SHUTDOWN_MONITOR_DELAY 500 /* ms */
#define NERO_DELAY_TIME 100 /* ms*/
#define LV_READ_DELAY 1000
#define YELLOW_LED_BLINK_DELAY 500 /* ms */
#define TEMP_SENS_SAMPLE_DELAY 200 /* ms */
#define IMU_SAMPLE_DELAY 500 /* ms */
#define FUSES_SAMPLE_DELAY 1000 /* ms */
#define SHUTDOWN_MONITOR_DELAY 500 /* ms */
#define NERO_DELAY_TIME 100 /* ms*/
#define LV_READ_DELAY 1000
#define SERIAL_MONITOR_DELAY
#define CAN_ROUTER_DELAY
#define CAN_DISPATCH_DELAY 5
#define BMS_CAN_MONITOR_DELAY 4000
#define CAN_DISPATCH_DELAY 5
#define BMS_CAN_MONITOR_DELAY 4000
#define STATE_MACHINE_DELAY
#define TORQUE_CALC_DELAY
#define FAULT_HANDLE_DELAY

/* Pedal tuning */
#define PEDALS_SAMPLE_DELAY 20 /* ms */
#define BRAKE1_OFFSET 0
#define BRAKE1_MAX_VAL 0
#define BRAKE2_OFFSET 0
#define BRAKE2_MAX_VAL 0
#define ACCEL1_OFFSET 980
#define ACCEL1_MAX_VAL 1866
#define ACCEL2_OFFSET 1780
Expand Down
19 changes: 19 additions & 0 deletions Core/Inc/dti.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,35 @@
/* Message IDs from DTI CAN Datasheet */
#define DTI_CANID_ERPM 0x416 /* ERPM, Duty, Input Voltage */
#define DTI_CANID_CURRENTS 0x436 /* AC Current, DC Current */
#define DTI_CANID_ERPM 0x416 /* ERPM, Duty, Input Voltage */
#define DTI_CANID_CURRENTS 0x436 /* AC Current, DC Current */
#define DTI_CANID_TEMPS_FAULT 0x456 /* Controller Temp, Motor Temp, Faults */
#define DTI_CANID_ID_IQ 0x476 /* Id, Iq values */
#define DTI_CANID_SIGNALS \
0x496 /* Throttle signal, Brake signal, IO, Drive enable */
#define DTI_QUEUE_SIZE 5

#define TIRE_DIAMETER 16 /* inches */
#define GEAR_RATIO 47 / 13.0 /* unitless */
#define POLE_PAIRS 10 /* unitless */
#define DTI_CANID_ID_IQ 0x476 /* Id, Iq values */
#define DTI_CANID_SIGNALS \
0x496 /* Throttle signal, Brake signal, IO, Drive enable */
#define DTI_QUEUE_SIZE 5

#define TIRE_DIAMETER 16 /* inches */
#define GEAR_RATIO 47 / 13.0 /* unitless */
#define POLE_PAIRS 10 /* unitless */

typedef struct {
int32_t rpm; /* SCALE: 1 UNITS: Rotations per Minute */
int16_t duty_cycle; /* SCALE: 10 UNITS: Percentage */
int16_t input_voltage; /* SCALE: 1 UNITS: Volts */
int16_t ac_current; /* SCALE: 10 UNITS: Amps */
int16_t dc_current; /* SCALE: 10 UNITS: Amps */
int16_t contr_temp; /* SCALE: 10 UNITS: Degrees Celsius */
int16_t motor_temp; /* SCALE: 10 UNITS: Degrees Celsius */
uint8_t fault_code; /* SCALE: 1 UNITS: No units just a number */
typedef struct {
int32_t rpm; /* SCALE: 1 UNITS: Rotations per Minute */
int16_t duty_cycle; /* SCALE: 10 UNITS: Percentage */
Expand Down
5 changes: 5 additions & 0 deletions Core/Inc/monitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ void vIMUMonitor(void *pv_params);
extern osThreadId_t imu_monitor_handle;
extern const osThreadAttr_t imu_monitor_attributes;

/* Task for Monitoring the TSMS sense on PDU CTRL expander*/
void vTsmsMonitor(void *pv_params);
extern osThreadId_t tsms_monitor_handle;
extern const osThreadAttr_t tsms_monitor_attributes;

/* Task for Monitoring the Fuses on PDU */
void vFusingMonitor(void *pv_params);
extern osThreadId_t fusing_monitor_handle;
Expand Down
1 change: 1 addition & 0 deletions Core/Src/dti.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

#define CAN_QUEUE_SIZE 5 /* messages */
#define SAMPLES 20
#define SAMPLES 20
static osMutexAttr_t dti_mutex_attributes;

dti_t *dti_init()
Expand Down
2 changes: 2 additions & 0 deletions Core/Src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,8 @@ int main(void)
steeringio_buttons_monitor_handle = osThreadNew(vSteeringIOButtonsMonitor, wheel, &steeringio_buttons_monitor_attributes);
pedals_monitor_handle = osThreadNew(vPedalsMonitor, mpu, &pedals_monitor_attributes);
assert(pedals_monitor_handle);
tsms_monitor_handle = osThreadNew(vTsmsMonitor, pdu, &tsms_monitor_attributes);
assert(tsms_monitor_handle);
fusing_monitor_handle = osThreadNew(vFusingMonitor, pdu, &fusing_monitor_attributes);
assert(fusing_monitor_handle);
// shutdown_monitor_handle = osThreadNew(vShutdownMonitor, pdu, &shutdown_monitor_attributes);
Expand Down
69 changes: 65 additions & 4 deletions Core/Src/monitor.c
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ void vPedalsMonitor(void *pv_params)
mpu_t *mpu = (mpu_t *)pv_params;

uint8_t counter = 0;
static int index = 0;

for (;;) {
read_pedals(mpu, adc_data);
Expand Down Expand Up @@ -268,8 +269,24 @@ void vPedalsMonitor(void *pv_params)
// printf("Brake 1: %ld\r\n", adc_data[BRAKEPIN_1]);
// printf("Brake 2: %ld\r\n", adc_data[BRAKEPIN_2]);

is_braking = (adc_data[BRAKEPIN_1] + adc_data[BRAKEPIN_2]) / 2 >
PEDAL_BRAKE_THRESH;
static float buffer[10] = { 0 };

uint16_t brake_avg =
(adc_data[BRAKEPIN_1] + adc_data[BRAKEPIN_2]) / 2;
// Add the new value to the buffer
buffer[index] = brake_avg;

// Increment the index, wrapping around if necessary
index = (index + 1) % buffer_size;

// Calculate the average of the buffer
float sum = 0.0;
for (int i = 0; i < buffer_size; ++i) {
sum += buffer[i];
}
float average_brake = sum / buffer_size;

is_braking = average_brake > PEDAL_BRAKE_THRESH;
brake_state = is_braking;

osMessageQueuePut(brakelight_signal, &is_braking, 0U, 0U);
Expand Down Expand Up @@ -402,6 +419,52 @@ void vIMUMonitor(void *pv_params)
}
}

osThreadId_t tsms_monitor_handle;
const osThreadAttr_t tsms_monitor_attributes = {
.name = "TsmsMonitor",
.stack_size = 32 * 8,
.priority = (osPriority_t)osPriorityHigh,
};

void vTsmsMonitor(void *pv_params)
{
fault_data_t fault_data = { .id = FUSE_MONITOR_FAULT,
.severity = DEFCON5 };
pdu_t *pdu = (pdu_t *)pv_params;

bool tsms_status = false;
nertimer_t tsms_debounce_timer = { .active = false };

for (;;) {
/* If we got a reliable TSMS reading, handle transition to and out of ACTIVE*/
if (!read_tsms_sense(pdu, &tsms_status)) {
printf("Checking pdu");

// Timer has not been started, and there is a change in TSMS, so start the timer
if (tsms != tsms_status &&
!is_timer_active(&tsms_debounce_timer)) {
start_timer(&tsms_debounce_timer, 500);
}
// During debouncing, the tsms reading changes, so end the debounce period
else if (tsms == tsms_status &&
!is_timer_expired(&tsms_debounce_timer)) {
cancel_timer(&tsms_debounce_timer);
}
// The TSMS reading has been consistent thorughout the debounce period
else if (is_timer_expired(&tsms_debounce_timer)) {
tsms = tsms_status;
}
if (get_func_state() == ACTIVE && tsms == false) {
set_home_mode();
}
} else {
queue_fault(&fault_data);
}

osDelay(100);
}
}

osThreadId_t fusing_monitor_handle;
const osThreadAttr_t fusing_monitor_attributes = {
.name = "FusingMonitor",
Expand Down Expand Up @@ -520,8 +583,6 @@ void vShutdownMonitor(void *pv_params)
queue_fault(&fault_data);
}

// serial_print("TSMS: %d\r\n", tsms_status);

osDelay(SHUTDOWN_MONITOR_DELAY);
}
}
Expand Down
16 changes: 12 additions & 4 deletions Core/Src/state_machine.c
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
#include "state_machine.h"
#include "fault.h"
#include "can_handler.h"
#include "fault.h"
#include "nero.h"
#include "pdu.h"
#include "queues.h"
#include "serial_monitor.h"
#include "nero.h"
#include "pdu.h"
#include "queues.h"
#include <stdbool.h>
#include <stdio.h>
#include <assert.h>

#define STATE_TRANS_QUEUE_SIZE 4

Expand All @@ -31,14 +33,20 @@ static const bool valid_trans_to_from[MAX_FUNC_STATES][MAX_FUNC_STATES] = {
{ true, true, false, true }, /* BOOT */
{ false, true, true, true }, /* READY */
{ false, true, true, true }, /* DRIVING */
{ true, true, false, true }, /* BOOT */
{ false, true, true, true }, /* READY */
{ false, true, true, true }, /* DRIVING */
{ false, true, false, true } /* FAULTED */
};

osThreadId_t sm_director_handle;
const osThreadAttr_t sm_director_attributes = {
.name = "State Machine Director",
const osThreadAttr_t sm_director_attributes = {
.name = "State Machine Director",
.stack_size = 128 * 8,
.priority = (osPriority_t)osPriorityRealtime2,
.priority = (osPriority_t)osPriorityRealtime2,
};

static osMessageQueueId_t state_trans_queue;
Expand All @@ -63,7 +71,7 @@ int queue_state_transition(state_req_t new_state)
return 0;

/* Transitioning between drive states is always allowed */
//TODO: Make sure motor is not spinning before switching
// TODO: Make sure motor is not spinning before switching

/* If we are turning ON the motor, blare RTDS */
if (cerberus_state.drive == NOT_DRIVING) {
Expand Down Expand Up @@ -178,7 +186,7 @@ void vStateMachineDirector(void *pv_params)
continue;

/* Transitioning between drive states is always allowed */
//TODO: Make sure motor is not spinning before switching
// TODO: Make sure motor is not spinning before switching

/* If we are turning ON the motor, blare RTDS */
if (cerberus_state.drive == NOT_DRIVING) {
Expand Down
1 change: 1 addition & 0 deletions Core/Src/torque.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

/* DO NOT ATTEMPT TO SEND TORQUE COMMANDS LOWER THAN THIS VALUE */
#define MIN_COMMAND_FREQ 60 /* Hz */
#define MIN_COMMAND_FREQ 60 /* Hz */
#define MAX_COMMAND_DELAY 1000 / MIN_COMMAND_FREQ /* ms */

static float torque_limit_percentage = 1.0;
Expand Down

0 comments on commit 32fde07

Please sign in to comment.