Skip to content

Commit

Permalink
Ishan's lastest changes
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanielDelgado committed Jul 17, 2024
1 parent 5bbf391 commit 9031660
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 36 deletions.
2 changes: 1 addition & 1 deletion Apps/Inc/SendTritium.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
//#define SENDTRITIUM_PRINT_MES

#define MOTOR_MSG_PERIOD 100 // in ms
#define FSM_PERIOD 100 // in ms
#define FSM_PERIOD 50 // in ms
#define DEBOUNCE_PERIOD 2 // in units of FSM_PERIOD
#define MOTOR_MSG_COUNTER_THRESHOLD (MOTOR_MSG_PERIOD)/(FSM_PERIOD)

Expand Down
6 changes: 6 additions & 0 deletions Apps/Inc/UpdateDisplay.h
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,12 @@ UpdateDisplayError_t UpdateDisplay_SetHeartbeat(uint32_t val);

UpdateDisplayError_t UpdateDisplay_SetHeatSinkTemp(uint32_t val);

UpdateDisplayError_t UpdateDisplay_SetMCVoltage(uint32_t volts);

UpdateDisplayError_t UpdateDisplay_SetMCCurrent(uint32_t val);

UpdateDisplayError_t UpdateDisplay_SetBrake(bool state);

/**
* @brief Clears the display message queue and sets the message counter semaphore value to 0
* @param none
Expand Down
11 changes: 10 additions & 1 deletion Apps/Src/ReadTritium.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
tritium_error_code_t Motor_FaultBitmap = T_NONE; //initialized to no error, changed when the motor asserts an error
static float Motor_RPM = 0;
static float Motor_Velocity = 0;
static float Motor_BusVoltage = 0;
static float Motor_BusCurrent = 0;

static OS_TMR MotorWatchdog;

Expand Down Expand Up @@ -57,9 +59,16 @@ void Task_ReadTritium(void *p_arg){
}

switch(dataBuf.ID){
case MC_BUS:{
Motor_BusVoltage = *((float*)&dataBuf.data[0]);
Motor_BusCurrent = *((float*)&dataBuf.data[4]);

UpdateDisplay_SetMCVoltage(Motor_BusVoltage*10);
UpdateDisplay_SetMCCurrent(Motor_BusCurrent*10);
}
case MOTOR_STATUS:{
// motor status error flags is in bytes 4-5
Motor_FaultBitmap = *((uint16_t*)(&dataBuf.data[4])); //Storing error flags into Motor_FaultBitmap
Motor_FaultBitmap = (*((uint16_t*)(&dataBuf.data[4])) & 0x1FE); //Storing error flags into Motor_FaultBitmap
assertTritiumError(Motor_FaultBitmap);
break;
}
Expand Down
99 changes: 66 additions & 33 deletions Apps/Src/SendTritium.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
#define MAX_GEARSWITCH_VELOCITY mpsToRpm(8.0f) // rpm

#define BRAKE_PEDAL_THRESHOLD 50 // percent
#define ACCEL_PEDAL_THRESHOLD 10 // percent
#define ACCEL_PEDAL_THRESHOLD 8 // percent

#define ONEPEDAL_BRAKE_THRESHOLD 25 // percent
#define ONEPEDAL_NEUTRAL_THRESHOLD 35 // percent
Expand Down Expand Up @@ -68,9 +68,9 @@ static float velocityObserved = 0;
static uint8_t motorMsgCounter = 0;

// Debouncing counters
static uint8_t onePedalCounter = 0;
static uint8_t cruiseEnableCounter = 0;
static uint8_t cruiseSetCounter = 0;
// static uint8_t onePedalCounter = 0;
// static uint8_t cruiseEnableCounter = 0;
// static uint8_t cruiseSetCounter = 0;

// Button states
static bool onePedalButton = false;
Expand Down Expand Up @@ -226,21 +226,37 @@ static void dumpInfo(){
static void readInputs(){

// Update pedals
brakePedalPercent = Pedals_Read(BRAKE);
static uint32_t brakeSaturationCt = 0;

uint32_t latest_pedal = Pedals_Read(BRAKE);

if(brakeSaturationCt < 3){
if(latest_pedal == 100) brakeSaturationCt++;
else if(latest_pedal == 0) brakeSaturationCt = 0;

brakePedalPercent = 0;
}
else if(brakeSaturationCt >= 3){
brakePedalPercent = 100;

if(latest_pedal == 0) brakeSaturationCt = 0;
}

accelPedalPercent = Pedals_Read(ACCELERATOR);

// Update regen enable
regenEnable = ChargeEnable_Get();
//regenEnable = ChargeEnable_Get();
regenEnable = false;

// Update buttons
if(Minions_Read(REGEN_SW) && onePedalCounter < DEBOUNCE_PERIOD){onePedalCounter++;}
else if(onePedalCounter > 0){onePedalCounter--;}
// if(Minions_Read(REGEN_SW) && onePedalCounter < DEBOUNCE_PERIOD){onePedalCounter++;}
// else if(onePedalCounter > 0){onePedalCounter--;}

if(Minions_Read(CRUZ_EN) && cruiseEnableCounter < DEBOUNCE_PERIOD){cruiseEnableCounter++;}
else if(cruiseEnableCounter > 0){cruiseEnableCounter--;}
// if(Minions_Read(CRUZ_EN) && cruiseEnableCounter < DEBOUNCE_PERIOD){cruiseEnableCounter++;}
// else if(cruiseEnableCounter > 0){cruiseEnableCounter--;}

if(Minions_Read(CRUZ_ST) && cruiseSetCounter < DEBOUNCE_PERIOD){cruiseSetCounter++;}
else if(cruiseSetCounter > 0){cruiseSetCounter--;}
// if(Minions_Read(CRUZ_ST) && cruiseSetCounter < DEBOUNCE_PERIOD){cruiseSetCounter++;}
// else if(cruiseSetCounter > 0){cruiseSetCounter--;}

// Update gears
bool forwardSwitch = Minions_Read(FOR_SW);
Expand All @@ -265,24 +281,34 @@ static void readInputs(){
else if(forwardGear) gear = FORWARD_GEAR;
else if(reverseGear) gear = REVERSE_GEAR;
else gear = NEUTRAL_GEAR;

if(gear == NEUTRAL_GEAR) UpdateDisplay_SetGear(DISP_NEUTRAL);
else if(gear == FORWARD_GEAR) UpdateDisplay_SetGear(DISP_FORWARD);
else if(gear == REVERSE_GEAR) UpdateDisplay_SetGear(DISP_REVERSE);

// Debouncing
if(onePedalCounter == DEBOUNCE_PERIOD){onePedalButton = true;}
else if(onePedalCounter == 0){onePedalButton = false;}
onePedalButton = false;
cruiseEnableButton = false;
cruiseSet = false;
// if(onePedalCounter == DEBOUNCE_PERIOD){onePedalButton = true;}
// else if(onePedalCounter == 0){onePedalButton = false;}

if(cruiseEnableCounter == DEBOUNCE_PERIOD){cruiseEnableButton = true;}
else if(cruiseEnableCounter == 0){cruiseEnableButton = false;}
// if(cruiseEnableCounter == DEBOUNCE_PERIOD){cruiseEnableButton = true;}
// else if(cruiseEnableCounter == 0){cruiseEnableButton = false;}

if(cruiseSetCounter == DEBOUNCE_PERIOD){cruiseSet = true;}
else if(cruiseSetCounter == 0){cruiseSet = false;}
// if(cruiseSetCounter == DEBOUNCE_PERIOD){cruiseSet = true;}
// else if(cruiseSetCounter == 0){cruiseSet = false;}

// Toggle
if(onePedalButton != onePedalPrevious && onePedalPrevious){onePedalEnable = !onePedalEnable;}
if(!regenEnable) onePedalEnable = false;
onePedalPrevious = onePedalButton;
// if(onePedalButton != onePedalPrevious && onePedalPrevious){onePedalEnable = !onePedalEnable;}
// if(!regenEnable) onePedalEnable = false;
// onePedalPrevious = onePedalButton;
onePedalEnable = false;
onePedalPrevious = false;

if(cruiseEnableButton != cruiseEnablePrevious && cruiseEnablePrevious){cruiseEnable = !cruiseEnable;}
cruiseEnablePrevious = cruiseEnableButton;
// if(cruiseEnableButton != cruiseEnablePrevious && cruiseEnablePrevious){cruiseEnable = !cruiseEnable;}
// cruiseEnablePrevious = cruiseEnableButton;
cruiseEnablePrevious = false;

// Get observed velocity
velocityObserved = Motor_RPM_Get();
Expand Down Expand Up @@ -342,7 +368,6 @@ static void ForwardDriveHandler(){
if(prevState.name != state.name){
UpdateDisplay_SetCruiseState(DISP_DISABLED);
UpdateDisplay_SetRegenState(DISP_DISABLED);
UpdateDisplay_SetGear(DISP_FORWARD);
}
velocitySetpoint = MAX_VELOCITY;
// printf("accelPedalPercent: %d\n\r", accelPedalPercent);
Expand Down Expand Up @@ -375,7 +400,6 @@ static void NeutralDriveHandler(){
if(prevState.name != state.name){
UpdateDisplay_SetCruiseState(DISP_DISABLED);
UpdateDisplay_SetRegenState(DISP_DISABLED);
UpdateDisplay_SetGear(DISP_NEUTRAL);
}
velocitySetpoint = MAX_VELOCITY;
currentSetpoint = 0.0f;
Expand Down Expand Up @@ -406,7 +430,6 @@ static void ReverseDriveHandler(){
if(prevState.name != state.name){
UpdateDisplay_SetCruiseState(DISP_DISABLED);
UpdateDisplay_SetRegenState(DISP_DISABLED);
UpdateDisplay_SetGear(DISP_REVERSE);
}
velocitySetpoint = -MAX_VELOCITY;
currentSetpoint = percentToFloat(map(accelPedalPercent, ACCEL_PEDAL_THRESHOLD, PEDAL_MAX, CURRENT_SP_MIN, CURRENT_SP_MAX));
Expand Down Expand Up @@ -435,7 +458,6 @@ static void RecordVelocityHandler(){
if(prevState.name != state.name){
UpdateDisplay_SetCruiseState(DISP_ACTIVE);
UpdateDisplay_SetRegenState(DISP_DISABLED);
UpdateDisplay_SetGear(DISP_FORWARD);
}
// put car in neutral while recording velocity (while button is held)
velocitySetpoint = MAX_VELOCITY;
Expand Down Expand Up @@ -570,7 +592,6 @@ static void AccelerateCruiseDecider(){
static void OnePedalDriveHandler(){
if(prevState.name != state.name){
UpdateDisplay_SetCruiseState(DISP_DISABLED);
UpdateDisplay_SetGear(DISP_FORWARD);
}
if(accelPedalPercent <= ONEPEDAL_BRAKE_THRESHOLD){
// Regen brake: Map 0 -> brake to 100 -> 0
Expand Down Expand Up @@ -618,13 +639,15 @@ static void BrakeHandler(){
if(prevState.name != state.name){
UpdateDisplay_SetCruiseState(DISP_DISABLED);
UpdateDisplay_SetRegenState(DISP_DISABLED);
UpdateDisplay_SetGear(DISP_FORWARD);
}

velocitySetpoint = MAX_VELOCITY;
currentSetpoint = 0;
cruiseEnable = false;
onePedalEnable = false;

Minions_Write(BRAKELIGHT, true);
UpdateDisplay_SetBrake(true);
}

/**
Expand All @@ -633,9 +656,19 @@ static void BrakeHandler(){
*/
static void BrakeDecider(){
if(brakePedalPercent < BRAKE_PEDAL_THRESHOLD){
if(gear == FORWARD_GEAR) state = FSM[FORWARD_DRIVE];
else if(gear == NEUTRAL_GEAR || gear == REVERSE_GEAR) state = FSM[NEUTRAL_DRIVE];
Minions_Write(BRAKELIGHT, false);
if(gear == FORWARD_GEAR){
state = FSM[FORWARD_DRIVE];
Minions_Write(BRAKELIGHT, false);
UpdateDisplay_SetBrake(false);
} else if(gear == NEUTRAL_GEAR){
state = FSM[NEUTRAL_DRIVE];
Minions_Write(BRAKELIGHT, false);
UpdateDisplay_SetBrake(false);
} else if(gear == REVERSE_GEAR){
state = FSM[REVERSE_DRIVE];
Minions_Write(BRAKELIGHT, false);
UpdateDisplay_SetBrake(false);
}
}
}

Expand Down Expand Up @@ -690,7 +723,7 @@ void Task_SendTritium(void *p_arg){

putControlModeCAN();

// Delay of MOTOR_MSG_PERIOD ms
// Delay of FSM_PERIOD ms
OSTimeDlyHMSM(0, 0, 0, FSM_PERIOD, OS_OPT_TIME_HMSM_STRICT, &err);
if (err != OS_ERR_NONE){
assertOSError(err);
Expand Down
28 changes: 27 additions & 1 deletion Apps/Src/UpdateDisplay.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ typedef enum{
ARRAY=0,
HEARTBEAT,
PACK_CURR_SIGN,
MC_CURR_SIGN,
BRAKE,
MOTOR,
// Non-boolean components
VELOCITY,
Expand All @@ -41,6 +43,8 @@ typedef enum{
PACK_VOLTAGE,
PACK_CURRENT,
PACK_TEMP,
MC_BUS_VOLTAGE,
MC_BUS_CURRENT,
HEAT_SINK_TEMP,
GEAR,
// Fault code components
Expand All @@ -56,6 +60,8 @@ const char* compStrings[NUM_COMPONENTS]= {
"arr",
"hb",
"cs",
"mcs",
"brake",
"mot",
// Non-boolean components
"vel",
Expand All @@ -67,6 +73,8 @@ const char* compStrings[NUM_COMPONENTS]= {
"pv",
"pc",
"pt",
"mcv",
"mcc",
"heatsink",
"gear",
// Fault code components
Expand Down Expand Up @@ -194,7 +202,7 @@ UpdateDisplayError_t UpdateDisplay_SetVelocity(uint32_t mphTenths){
}

UpdateDisplayError_t UpdateDisplay_SetAccel(uint8_t percent){
componentVals[ACCEL_METER] = percent;
componentVals[ACCEL_METER] = (percent > 100)?100:percent;

return UPDATEDISPLAY_ERR_NONE;
}
Expand Down Expand Up @@ -247,6 +255,24 @@ UpdateDisplayError_t UpdateDisplay_SetBattCurrent(int32_t val){
return UPDATEDISPLAY_ERR_NONE;
}

UpdateDisplayError_t UpdateDisplay_SetMCVoltage(uint32_t volts){
componentVals[MC_BUS_VOLTAGE] = (volts);

return UPDATEDISPLAY_ERR_NONE;
}

UpdateDisplayError_t UpdateDisplay_SetMCCurrent(uint32_t val){
componentVals[MC_BUS_CURRENT] = (val<0)?-val:val;
componentVals[MC_CURR_SIGN] = (val < 0)?1:0;
return UPDATEDISPLAY_ERR_NONE;
}

UpdateDisplayError_t UpdateDisplay_SetBrake(bool state){
componentVals[BRAKE] = (state)?1:0;

return UPDATEDISPLAY_ERR_NONE;
}

UpdateDisplayError_t UpdateDisplay_SetHeartbeat(uint32_t val){
componentVals[HEARTBEAT] = val;

Expand Down
Binary file modified Config/Display/display.HMI
Binary file not shown.

0 comments on commit 9031660

Please sign in to comment.