diff --git a/Apps/Inc/UpdateDisplay.h b/Apps/Inc/UpdateDisplay.h index a2204518f..3d2bf91e1 100644 --- a/Apps/Inc/UpdateDisplay.h +++ b/Apps/Inc/UpdateDisplay.h @@ -95,6 +95,28 @@ UpdateDisplayError_t UpdateDisplay_SetVelocity(uint32_t mphTenths); */ UpdateDisplayError_t UpdateDisplay_SetAccel(uint8_t percent); +/** + * @brief Sets the forward slider value on the display + * @param percent forward power in percent + * @returns UpdateDisplayError_t + */ +UpdateDisplayError_t UpdateDisplay_SetForwardPower(uint8_t percent); + +/** + * @brief Sets the regeneration power slider value on the display + * @param percent regenerative power in percent + * @returns UpdateDisplayError_t + */ +UpdateDisplayError_t UpdateDisplay_SetRegenPower(int32_t percent); + +/** + * @brief Sets both regeneration and forward power slider value. + * @param forwardPercent + * @param regenPercent + * @returns UpdateDisplayError_t +*/ +void UpdateDisplay_SetBackEMF(uint8_t accPercent, int32_t regenPercent); + /** * @brief Sets the array indicator state on the display * @param state array contactor on (true) or off (false) diff --git a/Apps/Src/ReadTritium.c b/Apps/Src/ReadTritium.c index a9799bcbc..4064369ae 100755 --- a/Apps/Src/ReadTritium.c +++ b/Apps/Src/ReadTritium.c @@ -17,8 +17,11 @@ uint16_t Motor_FaultBitmap = T_NONE; -static float Motor_RPM = CAR_STOPPED; //Car is stopped until velocity is read -static float Motor_Velocity = CAR_STOPPED; //^^^^ +static float Motor_RPM = CAR_STOPPED; //Car is stopped until velocity is read +static float Motor_Velocity = CAR_STOPPED; //^^^^ +static float Back_EMF = 0; +static float Bus_Current = 0; +static float Bus_Voltage = 0; /** * @brief Returns highest priority tritium error code @@ -101,16 +104,39 @@ void Task_ReadTritium(void *p_arg){ Car_Velocity = ((Car_Velocity / 160934) * 10); //Converting from m/h to mph, multiplying by 10 to make value "larger" for displaying UpdateDisplay_SetVelocity(Car_Velocity); + } + + case MC_BUS:{ + memcpy(&Bus_Voltage, &dataBuf.data[0], sizeof(float)); + memcpy(&Bus_Current, &dataBuf.data[4], sizeof(float)); + + //Bus voltage is in bytes 0-4 + Bus_Voltage = *((float*)(&dataBuf.data[0])); + + //Bus Current is in bytes 4-8 + Bus_Current = *((float*)(&dataBuf.data[4])); + + int32_t Forward_Power = Bus_Voltage * Bus_Current; + + // Divide by maximum power to input percentage + UpdateDisplay_SetForwardPower(Forward_Power / 5000); + } + + case BACKEMF:{ + memcpy(&Back_EMF, &dataBuf.data[0], sizeof(float)); // BEMFq (The peak of the phase to neutral motor voltage) + + //BackEMF is in bytes 0-4 + Back_EMF = *((float*)(&dataBuf.data[0])); + int32_t Regen_Power = (Back_EMF * 100) * (Bus_Current * 100); // Fixed point factor (100) + // Divide by maximum power to input percentage + UpdateDisplay_SetRegenPower(Regen_Power / 5000); } default:{ break; //for cases not handled currently } - } - - } OSTimeDlyHMSM(0, 0, 0, 10, OS_OPT_TIME_HMSM_NON_STRICT, &err); diff --git a/Apps/Src/SendTritium.c b/Apps/Src/SendTritium.c index f6610bb96..c336881fc 100644 --- a/Apps/Src/SendTritium.c +++ b/Apps/Src/SendTritium.c @@ -646,7 +646,7 @@ void Task_SendTritium(void *p_arg){ state.stateHandler(); // do what the current state does #ifndef SENDTRITIUM_EXPOSE_VARS readInputs(); // read inputs from the system - UpdateDisplay_SetAccel(accelPedalPercent); + UpdateDisplay_SetBackEMF(accelPedalPercent, brakePedalPercent); #endif state.stateDecider(); // decide what the next state is diff --git a/Apps/Src/UpdateDisplay.c b/Apps/Src/UpdateDisplay.c index 1356491e2..77cfbe913 100644 --- a/Apps/Src/UpdateDisplay.c +++ b/Apps/Src/UpdateDisplay.c @@ -38,6 +38,7 @@ typedef enum{ // Non-boolean components VELOCITY, ACCEL_METER, + REGEN_METER, SOC, SUPP_BATT, CRUISE_ST, @@ -55,6 +56,7 @@ const char* compStrings[15]= { // Non-boolean components "vel", "accel", + "regen", "soc", "supp", "cruiseSt", @@ -306,6 +308,40 @@ UpdateDisplayError_t UpdateDisplay_SetAccel(uint8_t percent){ return ret; } +UpdateDisplayError_t UpdateDisplay_SetForwardPower(uint8_t percent) { + static uint8_t lastPercentPower = 0; + if(percent == lastPercentPower){ + return UPDATEDISPLAY_ERR_NO_CHANGE; + } + + UpdateDisplayError_t ret = UpdateDisplay_SetComponent(MOTOR, percent); + assertUpdateDisplayError(ret); + + if(ret == UPDATEDISPLAY_ERR_NONE) lastPercentPower = percent; + return ret; +} + +UpdateDisplayError_t UpdateDisplay_SetRegenPower(int32_t percent){ + static uint8_t lastPercentRegen = 0; + if(percent == lastPercentRegen){ + return UPDATEDISPLAY_ERR_NO_CHANGE; + } + + UpdateDisplayError_t ret = UpdateDisplay_SetComponent(REGEN_METER, percent); + assertUpdateDisplayError(ret); + + if(ret == UPDATEDISPLAY_ERR_NONE) lastPercentRegen = percent; + + // In Nextion, we are displaying regeneration slider by + // modifying the background percentage. + return (100 - ret); +} + +void UpdateDisplay_SetBackEMF(uint8_t forwardPercent, int32_t regenPercent) { + UpdateDisplay_SetForwardPower(forwardPercent); + UpdateDisplay_SetRegenPower(regenPercent); +} + UpdateDisplayError_t UpdateDisplay_SetArray(bool state){ static bool lastState = false; if(state == lastState){ diff --git a/Tests/Test_App_DisplayRegen.c b/Tests/Test_App_DisplayRegen.c new file mode 100644 index 000000000..fa0f99b1f --- /dev/null +++ b/Tests/Test_App_DisplayRegen.c @@ -0,0 +1,94 @@ +// General Imports. +#include "common.h" +#include "config.h" +#include "os.h" +#include "Tasks.h" + +// Device specfic imports. +#include "UpdateDisplay.h" +#include "ReadTritium.h" + +#define STACK_SIZE 128 + +static OS_TCB Task1TCB; +static OS_TCB Task2TCB; +static CPU_STK Task1Stk[STACK_SIZE]; +static CPU_STK Task2Stk[STACK_SIZE]; + +int main() { + OS_ERR err; + OSInit(&err); + if (err != OS_ERR_NONE) { + printf("OS error code %d\n", err); + } + + // SendTritium + OSTaskCreate( + (OS_TCB*)&SendTritium_TCB, + (CPU_CHAR*)"SendTritium", + (OS_TASK_PTR)Task_SendTritium, + (void*) NULL, + (OS_PRIO)TASK_SEND_TRITIUM_PRIO, + (CPU_STK*)SendTritium_Stk, + (CPU_STK_SIZE)WATERMARK_STACK_LIMIT/10, + (CPU_STK_SIZE)TASK_SEND_TRITIUM_STACK_SIZE, + (OS_MSG_QTY) 0, + (OS_TICK)NULL, + (void*)NULL, + (OS_OPT)(OS_OPT_TASK_STK_CLR), + (OS_ERR*)&err + ); + + if (err != OS_ERR_NONE) { + printf("SendTritium Task error code %d\n", err); + } + + // ReadTritium + OSTaskCreate( + (OS_TCB*)&ReadTritium_TCB, + (CPU_CHAR*)"ReadTritium", + (OS_TASK_PTR)Task_ReadTritium, + (void*) NULL, + (OS_PRIO)TASK_READ_TRITIUM_PRIO, + (CPU_STK*)SendTritium_Stk, + (CPU_STK_SIZE)WATERMARK_STACK_LIMIT/10, + (CPU_STK_SIZE)TASK_READ_TRITIUM_STACK_SIZE, + (OS_MSG_QTY) 0, + (OS_TICK)NULL, + (void*)NULL, + (OS_OPT)(OS_OPT_TASK_STK_CLR), + (OS_ERR*)&err + ); + + if (err != OS_ERR_NONE) { + printf("ReadTritium Task error code %d\n", err); + } + + // UpdateDisplay + OSTaskCreate( + (OS_TCB *)&UpdateDisplay_TCB, + (CPU_CHAR *)"UpdateDisplay_TCB", + (OS_TASK_PTR)Task_UpdateDisplay, + (void *)NULL, + (OS_PRIO)TASK_UPDATE_DISPLAY_PRIO, + (CPU_STK *)UpdateDisplay_Stk, + (CPU_STK_SIZE)DEFAULT_STACK_SIZE / 10, + (CPU_STK_SIZE)DEFAULT_STACK_SIZE, + (OS_MSG_QTY)0, + (OS_TICK)NULL, + (void *)NULL, + (OS_OPT)(OS_OPT_TASK_STK_CLR), + (OS_ERR *)&err); + + if (err != OS_ERR_NONE) { + printf("UpdateDisplay Task error code %d\n", err); + } + + OSStart(&err); + + if (err != OS_ERR_NONE) { + printf("OS error code %d\n", err); + } + + return 0; +} \ No newline at end of file