Skip to content

Commit

Permalink
Working version for turning array ON with precharge. Changed names an…
Browse files Browse the repository at this point in the history
…d started writing test cases for motor precharge
  • Loading branch information
diyarajon committed Sep 23, 2023
1 parent 9a33d88 commit b7f74c8
Show file tree
Hide file tree
Showing 9 changed files with 179 additions and 59 deletions.
7 changes: 5 additions & 2 deletions Apps/Inc/ReadCarCAN.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
#define SAT_BUF_LENGTH 5

// Precharge Delay times in seconds
#define PRECHARGE_MOTOR_DELAY 7 // 7 Seconds, need to change
#define PRECHARGE_ARRAY_DELAY 2 // 2 Seconds, need to change
#define PRECHARGE_MOTOR_DELAY 1 // 7 Seconds, need to change
#define PRECHARGE_ARRAY_DELAY 1 // 2 Seconds, need to change

/**
* Error types
Expand Down Expand Up @@ -54,4 +54,7 @@ uint8_t SOC_Get(void);
*/
uint32_t SBPV_Get(void);

// Getter function for array ignition status
bool PreChargeComplete_Get(void);

#endif
148 changes: 108 additions & 40 deletions Apps/Src/ReadCarCAN.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "Minions.h"
#include "os.h"
#include "os_cfg_app.h"
#include "config.h"

// Array saturation threshold is halfway between 0 and max saturation value (half of summation from one to the number of positions)
#define ARRAY_SATURATION_THRESHOLD (((SAT_BUF_LENGTH + 1) * SAT_BUF_LENGTH) / 4)
Expand Down Expand Up @@ -44,8 +45,8 @@ static int8_t HVPlusMinusChargeMsgSaturation = 0;
static uint8_t HVPlusMinusMotorOldestMsgIdx = 0;

// Array ignition (IGN_1) pin status
static bool arrayContactorIgnitionStatus = false;
// Array ignition (IGN_1) pin status
static bool arrayIgnitionStatus = false;
// Array ignition (IGN_2) pin status
static bool motorControllerIgnitionStatus = false;

// Boolean to indicate precharge status
Expand Down Expand Up @@ -73,7 +74,12 @@ int8_t ChargeMsgSaturation_Get(){

// Getter function for array ignition status
bool ArrayIgnitionStatus_Get(void){
return arrayContactorIgnitionStatus;
return arrayIgnitionStatus;
}

// Getter function for array ignition status
bool PreChargeComplete_Get(void){
return arrayBypassPrechargeComplete;
}

// Getter function for SOC
Expand Down Expand Up @@ -103,12 +109,6 @@ static void updateHVPlusMinusSaturation(int8_t chargeMessage){
newSaturation += HVPlusMinusChargeMsgBuffer[(HVPlusMinusMotorOldestMsgIdx + i) % SAT_BUF_LENGTH] * (i + 1);
}
HVPlusMinusChargeMsgSaturation = newSaturation;

if(chargeMessage == -1){
chargeEnable = false;
}else if(HVPlusMinusChargeMsgSaturation >= MOTOR_SATURATION_THRESHOLD){
chargeEnable = true;
}
}

/**
Expand All @@ -128,6 +128,12 @@ static void updateHVArraySaturation(int8_t chargeMessage){
newSaturation += HVArrayChargeMsgBuffer[(HVArrayOldestMsgIdx + i) % SAT_BUF_LENGTH] * (i + 1);
}
HVArrayChargeMsgSaturation = newSaturation;

if(chargeMessage == -1){
chargeEnable = false;
}else if(HVArrayChargeMsgSaturation >= ARRAY_SATURATION_THRESHOLD){
chargeEnable = true;
}
}

/**
Expand Down Expand Up @@ -164,27 +170,81 @@ static void setMotorControllerBypassPrechargeComplete(void *p_tmr, void *p_arg){
static void updatePrechargeContactors(void){
Minion_Error_t Merr;

arrayContactorIgnitionStatus = Minion_Read_Pin(IGN_1, &Merr);
arrayIgnitionStatus = Minion_Read_Pin(IGN_1, &Merr);
motorControllerIgnitionStatus = Minion_Read_Pin(IGN_2, &Merr);

// Array Contactor is turned off if Ignition 1 is off else
if(motorControllerIgnitionStatus == false){
Contactors_Set(MOTOR_BYPASS_PRECHARGE_CONTACTOR, OFF, true); // need to confirm the GPIO pins.
if(arrayIgnitionStatus == true && motorControllerIgnitionStatus == false){
if(arrayBypassPrechargeComplete == true && chargeEnable == true){
Contactors_Set(ARRAY_BYPASS_PRECHARGE_CONTACTOR, ON, true); // Turn on
UpdateDisplay_SetArray(true);
arrayBypassPrechargeComplete = false;
printf("\r\nreached ARR END\r\n");
}
Contactors_Set(MOTOR_CONTROLLER_BYPASS_PRECHARGE_CONTACTOR, OFF, true);
UpdateDisplay_SetMotor(false);
}else if(arrayContactorIgnitionStatus == false){
Contactors_Set(ARRAY_BYPASS_PRECHARGE_CONTACTOR, OFF, true);

}else if(arrayIgnitionStatus == false && motorControllerIgnitionStatus == true){
if(arrayBypassPrechargeComplete == true && chargeEnable == true){
Contactors_Set(ARRAY_BYPASS_PRECHARGE_CONTACTOR, ON, true); // Turn off
UpdateDisplay_SetArray(true);
arrayBypassPrechargeComplete = false;
}

if(motorControllerBypassPrechargeComplete == true && HVPlusMinusChargeMsgSaturation >= MOTOR_SATURATION_THRESHOLD){
Contactors_Set(MOTOR_CONTROLLER_BYPASS_PRECHARGE_CONTACTOR, ON, true);
UpdateDisplay_SetMotor(true);
}
}else if(arrayIgnitionStatus == false && motorControllerIgnitionStatus == false){
Contactors_Set(ARRAY_BYPASS_PRECHARGE_CONTACTOR, OFF, true); // Turn off
UpdateDisplay_SetArray(false);

Contactors_Set(MOTOR_CONTROLLER_BYPASS_PRECHARGE_CONTACTOR, OFF, true); // Turn off
UpdateDisplay_SetMotor(false);
}else{
assertReadCarCANError(READCARCAN_ERR_MISSED_MSG);
}
else if(arrayBypassPrechargeComplete == true && chargeEnable == true){
Contactors_Set(ARRAY_BYPASS_PRECHARGE_CONTACTOR, ON, false); // Turn on array contactor

// Set precharge complete variable to false if precharge happens again

motorControllerBypassPrechargeComplete = false;

}

/*
// IGNITION is set to Array AND Motor Controller -- not possible, so assert error
if(arrayIgnitionStatus == true && motorControllerIgnitionStatus == true){
assertReadCarCANError(READCARCAN_ERR_MISSED_MSG);
return; // Return because both PBCs have been addressed
// IGNITION SET is not set to MC, so turn off MC BPC and update display.
}else if(motorControllerIgnitionStatus == false){
Contactors_Set(MOTOR_CONTROLLER_BYPASS_PRECHARGE_CONTACTOR, OFF, true);
UpdateDisplay_SetMotor(false);
// If Ignition is not set to MC and is not set to array, then turn off array BPC
if(arrayIgnitionStatus == false){
Contactors_Set(ARRAY_BYPASS_PRECHARGE_CONTACTOR, OFF, true);
UpdateDisplay_SetMotor(false);
return; // Return because both PBCs have been addressed
}
// IGNITION SET is set to Motor Controller
}else{
Contactors_Set(MOTOR_CONTROLLER_BYPASS_PRECHARGE_CONTACTOR, OFF, true);
UpdateDisplay_SetMotor(false);
}
// If array BPC has completed precharge and charging is enabled, then turn on contactor.
if(arrayBypassPrechargeComplete == true && chargeEnable == true){
Contactors_Set(ARRAY_BYPASS_PRECHARGE_CONTACTOR, ON, true); // Turn off
UpdateDisplay_SetArray(true);
}else if(motorControllerBypassPrechargeComplete == true && HVPlusMinusChargeMsgSaturation > MOTOR_SATURATION_THRESHOLD){
Contactors_Set(MOTOR_BYPASS_PRECHARGE_CONTACTOR, ON, false); // Turn on array contactor
UpdateDisplay_SetMotor(true);
}

arrayBypassPrechargeComplete = false; // Set precharge complete variable to false if precharge happens again
};
// Set precharge complete variable to false if precharge happens again
arrayBypassPrechargeComplete = false;
motorControllerBypassPrechargeComplete = false;
}
*/

/**
* @brief Disables Array Precharge Bypass Contactor (APBC) by asserting an error. Also updates display for APBC to be open.
Expand All @@ -204,18 +264,19 @@ static void disableArrayPrechargeBypassContactor(void){
static void updateArrayPrechargeBypassContactor(void){

OS_ERR err;
if(arrayContactorIgnitionStatus == true // Ignition is ON
if(arrayIgnitionStatus == true // Ignition is ON
&& HVArrayChargeMsgSaturation >= ARRAY_SATURATION_THRESHOLD // Saturation Threshold has be met
&& (Contactors_Get(ARRAY_CONTACTOR)== OFF) // Array Contactor is OFF
&& (Contactors_Get(ARRAY_BYPASS_PRECHARGE_CONTACTOR)== OFF) // Array Contactor is OFF
&& (OSTmrStateGet(&arrayBypassPrechargeDlyTimer, &err) != OS_TMR_STATE_RUNNING)){ // and precharge is currenetly not happening
// Asserts error for OS timer start above if conditional was met
assertOSError(OS_READ_CAN_LOC, err);
// Wait to make sure precharge is finished and then restart array
printf("\r\nreached 273\r\n");
OSTmrStart(&arrayBypassPrechargeDlyTimer, &err);
}
// Asserts error for OS timer state if conditional wasn't met
assertOSError(OS_READ_CAN_LOC, err);
}
}

void Task_ReadCarCAN(void *p_arg){
OS_ERR err;
Expand All @@ -240,7 +301,7 @@ void Task_ReadCarCAN(void *p_arg){
OSTmrCreate(
&arrayBypassPrechargeDlyTimer,
"Array Bypass Precharge Delay Timer",
0,
0,
ARRAY_PRECHARGE_BYPASS_DLY_TMR_TS,
OS_OPT_TMR_ONE_SHOT,
setArrayBypassPrechargeComplete,
Expand Down Expand Up @@ -277,43 +338,49 @@ void Task_ReadCarCAN(void *p_arg){

switch(dataBuf.ID){
case BPS_TRIP: { // BPS has a fault and we need to enter fault state

// Assert the error to kill contactors, display evacuation message, and enter a nonrecoverable fault
assertReadCarCANError(READCARCAN_ERR_BPS_TRIP);

}
case BPS_CONTACTOR:{
}

case BPS_CONTACTOR:{
// Acknowledge CAN Watchdog timer
OSTmrStart(&canWatchTimer, &err);
assertOSError(OS_READ_CAN_LOC, err);

switch (dataBuf.data[0] & 0b11){ // Masking to get last two bits
case 0b00:{

case 0b00:{ // HV+/- and HV Arr did not recieve enable message
disableArrayPrechargeBypassContactor();
updateHVPlusMinusSaturation(-1);
break;
}
case 0b01:{
case 0b01:{ // Only HV Arr recieved enable message
updateHVArraySaturation(1);
updateArrayPrechargeBypassContactor();
updateHVPlusMinusSaturation(-1);
printf("\r\nreached ARR\r\n");
break;
}
case 0b10:{
case 0b10:{ // Only HV +/1 recieved enable message
disableArrayPrechargeBypassContactor();
updateHVPlusMinusSaturation(1);
break;
}
case 0b11: {
case 0b11: { // Both recieved enable message
updateHVArraySaturation(1);
updateArrayPrechargeBypassContactor();
updateHVPlusMinusSaturation(1);
break;
}
default: { // does it break for good?
default: {
// Should not be able to reach here.
break;
}
}

break;
// End of BPS Contactor Status Updates
}

case SUPPLEMENTAL_VOLTAGE: {
SBPV = (*(uint16_t *) &dataBuf.data);
UpdateDisplay_SetSBPV(SBPV); // Receive value in mV
Expand All @@ -324,9 +391,10 @@ void Task_ReadCarCAN(void *p_arg){
UpdateDisplay_SetSOC(SOC);
break;
}
default:
break;
}
default: { // Unhandled CAN message IDs
break;
}
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion Drivers/Inc/Contactors.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
typedef enum {
ARRAY_CONTACTOR = 0, // BPS
ARRAY_BYPASS_PRECHARGE_CONTACTOR, // Controls
MOTOR_BYPASS_PRECHARGE_CONTACTOR, // Controls
MOTOR_CONTROLLER_BYPASS_PRECHARGE_CONTACTOR, // Controls
NUM_CONTACTORS
} contactor_t;

Expand Down
4 changes: 2 additions & 2 deletions Drivers/Src/Contactors.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ static void setContactor(contactor_t contactor, bool state) {
case ARRAY_BYPASS_PRECHARGE_CONTACTOR :
BSP_GPIO_Write_Pin(CONTACTORS_PORT, ARRAY_PRECHARGE_BYPASS_PIN, state);
break;
case MOTOR_BYPASS_PRECHARGE_CONTACTOR :
case MOTOR_CONTROLLER_BYPASS_PRECHARGE_CONTACTOR :
BSP_GPIO_Write_Pin(CONTACTORS_PORT, MOTOR_CONTROLLER_PRECHARGE_BYPASS_PIN, state);
break;
default:
Expand Down Expand Up @@ -70,7 +70,7 @@ bool Contactors_Get(contactor_t contactor) {
case ARRAY_BYPASS_PRECHARGE_CONTACTOR :
state = BSP_GPIO_Get_State(CONTACTORS_PORT, ARRAY_PRECHARGE_BYPASS_PIN);
break;
case MOTOR_BYPASS_PRECHARGE_CONTACTOR :
case MOTOR_CONTROLLER_BYPASS_PRECHARGE_CONTACTOR :
state = BSP_GPIO_Get_State(CONTACTORS_PORT, MOTOR_CONTROLLER_PRECHARGE_BYPASS_PIN);
break;
default:
Expand Down
8 changes: 4 additions & 4 deletions Drivers/Src/Minions.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ bool Minion_Write_Output(MinionPin_t pin, bool status, Minion_Error_t* mErr){

//if(PINS_LOOKARR[pin].direction == OUTPUT){
OSMutexPend(&OutputMutex, 0, OS_OPT_PEND_BLOCKING, &timestamp, &err);
//assertOSError(OS_MINIONS_LOC, err);
assertOSError(OS_MINIONS_LOC, err);
BSP_GPIO_Write_Pin(PINS_LOOKARR[pin].port, PINS_LOOKARR[pin].pinMask, status);
OSMutexPost(&OutputMutex, OS_OPT_POST_NONE, &err);
//assertOSError(OS_MINIONS_LOC, err);
assertOSError(OS_MINIONS_LOC, err);
return true;
// }
// *mErr = MINION_ERR_WROTE_INPUT;
// return false;
*mErr = MINION_ERR_WROTE_INPUT;
return false;
}
4 changes: 2 additions & 2 deletions Tests/Test_FaultThread_Exceptions.c
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ void OSErrorTask(void* arg) {

// Helper function to see the state of the contactors
static void print_Contactors() {
printf("\n\rMotor contactor: %d", Contactors_Get(MOTOR_BYPASS_PRECHARGE_CONTACTOR));
printf("\n\rMotor contactor: %d", Contactors_Get(MOTOR_CONTROLLER_BYPASS_PRECHARGE_CONTACTOR));
printf("\n\rArray_precharge contactor: %d", Contactors_Get(ARRAY_BYPASS_PRECHARGE_CONTACTOR));
printf("\n\rArray contactor %d", Contactors_Get(ARRAY_CONTACTOR));
}
Expand Down Expand Up @@ -281,7 +281,7 @@ void Task_ManagerTask(void* arg) {

// Turn on contactors so we can see if they get turned off by the error assertion
Contactors_Set(ARRAY_CONTACTOR, ON, true);
Contactors_Set(MOTOR_BYPASS_PRECHARGE_CONTACTOR, ON, true);
Contactors_Set(MOTOR_CONTROLLER_BYPASS_PRECHARGE_CONTACTOR, ON, true);
Contactors_Set(ARRAY_BYPASS_PRECHARGE_CONTACTOR, ON, true); // Although BPS has control of the precharge contactor

print_Contactors(); // See the state of the contactors before we send the trip message
Expand Down
2 changes: 1 addition & 1 deletion Tests/Test_Ign_Cont.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ void Task1(void *arg)
assertOSError(OS_MAIN_LOC, err);

while (1){
printf("Array Contactor: %d, Motor Contactor: %d\n\r", Contactors_Get(ARRAY_CONTACTOR), Contactors_Get(MOTOR_BYPASS_PRECHARGE_CONTACTOR));
printf("Array Contactor: %d, Motor Contactor: %d\n\r", Contactors_Get(ARRAY_CONTACTOR), Contactors_Get(MOTOR_CONTROLLER_BYPASS_PRECHARGE_CONTACTOR));

OSTimeDlyHMSM(0, 0, 0, 500, OS_OPT_TIME_HMSM_STRICT, &err);
}
Expand Down
Loading

0 comments on commit b7f74c8

Please sign in to comment.