diff --git a/.github/workflows/c-cpp.yml b/.github/workflows/c-cpp.yml index d031e92a0..2c1b365dd 100644 --- a/.github/workflows/c-cpp.yml +++ b/.github/workflows/c-cpp.yml @@ -11,13 +11,13 @@ jobs: steps: - uses: actions/checkout@v2 + - name: Initialize Submodules + run: git submodule update --init --recursive - name: Install tools run: | + ./Embedded-Sharepoint/Scripts/install.sh -i sudo apt -y update - sudo apt -y install gcc-arm-none-eabi - - name: Initialize Submodules - run: git submodule update --init --recursive - name: clean run: make clean - - name: make stm32f413 - run: make stm32f413 + - name: make leader + run: make leader diff --git a/Apps/Inc/SendTritium.h b/Apps/Inc/SendTritium.h index 65ed7be29..8791f3833 100644 --- a/Apps/Inc/SendTritium.h +++ b/Apps/Inc/SendTritium.h @@ -12,10 +12,10 @@ #include "common.h" -#define SENDTRITIUM_PRINT_MES +//#define SENDTRITIUM_PRINT_MES -#define MOTOR_MSG_PERIOD 100 -#define FSM_PERIOD 100 +#define MOTOR_MSG_PERIOD 100 // in ms +#define FSM_PERIOD 100 // in ms #define DEBOUNCE_PERIOD 2 // in units of FSM_PERIOD #define MOTOR_MSG_COUNTER_THRESHOLD (MOTOR_MSG_PERIOD)/(FSM_PERIOD) diff --git a/Apps/Inc/Tasks.h b/Apps/Inc/Tasks.h index fdd45efb2..ad052d3d3 100644 --- a/Apps/Inc/Tasks.h +++ b/Apps/Inc/Tasks.h @@ -31,10 +31,11 @@ #define TASK_READ_TRITIUM_PRIO 3 #define TASK_SEND_TRITIUM_PRIO 4 #define TASK_READ_CAR_CAN_PRIO 5 -#define TASK_UPDATE_DISPLAY_PRIO 6 -#define TASK_SEND_CAR_CAN_PRIO 7 -#define TASK_DEBUG_DUMP_PRIO 8 -#define TASK_COMMAND_LINE_PRIO 9 +#define TASK_SEND_CAR_CAN_PRIO 6 +#define TASK_PUT_IOSTATE_PRIO 7 +#define TASK_UPDATE_DISPLAY_PRIO 8 +#define TASK_DEBUG_DUMP_PRIO 9 +#define TASK_COMMAND_LINE_PRIO 10 /** * Stack Sizes diff --git a/Apps/Src/ReadCarCAN.c b/Apps/Src/ReadCarCAN.c index 07e7f39dc..64897dd4b 100644 --- a/Apps/Src/ReadCarCAN.c +++ b/Apps/Src/ReadCarCAN.c @@ -235,9 +235,9 @@ static void updateHVPlusMinusSaturation(int8_t messageState){ */ void attemptTurnMotorControllerPBCOn(void){ if(mcPBCComplete){ - Contactors_Set(MOTOR_CONTROLLER_PRECHARGE_BYPASS_CONTACTOR, ON, true); - UpdateDisplay_SetMotor(true); - } + Contactors_Set(MOTOR_CONTROLLER_PRECHARGE_BYPASS_CONTACTOR, ON, true); + UpdateDisplay_SetMotor(true); + } } /** diff --git a/Apps/Src/ReadTritium.c b/Apps/Src/ReadTritium.c index 3b08c339b..6b1055ce6 100755 --- a/Apps/Src/ReadTritium.c +++ b/Apps/Src/ReadTritium.c @@ -36,41 +36,26 @@ static void motorWatchdog(void *tmr, void *p_arg) { } -/* OBJECTIVES: -Objective 1: -- Receive motor status message from MotorController (18.4.2) -- interpret error status - - if error - - assertOSError -- determine information important to telementry - - Telemetry -- determine information important for storage - - acquire mutex on Storage Array - - Store information in Storage Array (based on index) - - release mutex on Storage Array - -Objective 2: -- create function able to read data from Storage Array - - pend on Storage Array mutex - - acquire Storage Array mutex - - read information of array index - - release Storage Array mutex -*/ - void Task_ReadTritium(void *p_arg){ OS_ERR err; CANDATA_t dataBuf = {0}; - // Timer doesn't seem to trigger without initial delay? Might be an RTOS bug - OSTmrCreate(&MotorWatchdog, "Motor watchdog", MOTOR_TIMEOUT_TICKS, MOTOR_TIMEOUT_TICKS, OS_OPT_TMR_PERIODIC, motorWatchdog, NULL, &err); - assertOSError(err); - OSTmrStart(&MotorWatchdog, &err); - assertOSError(err); + static bool watchdogCreated = false; while (1){ ErrorStatus status = CANbus_Read(&dataBuf, true, MOTORCAN); if (status == SUCCESS){ + if(!watchdogCreated){ // Timer doesn't seem to trigger without initial delay? Might be an RTOS bug + OSTmrCreate(&MotorWatchdog, "Motor watchdog", MOTOR_TIMEOUT_TICKS, MOTOR_TIMEOUT_TICKS, OS_OPT_TMR_PERIODIC, motorWatchdog, NULL, &err); + assertOSError(err); + + OSTmrStart(&MotorWatchdog, &err); + assertOSError(err); + + watchdogCreated = true; + } + switch(dataBuf.ID){ case MOTOR_STATUS:{ // motor status error flags is in bytes 4-5 @@ -107,9 +92,6 @@ void Task_ReadTritium(void *p_arg){ SendCarCAN_Put(dataBuf); // Forward message on CarCAN for telemetry } - - OSTimeDlyHMSM(0, 0, 0, 10, OS_OPT_TIME_HMSM_NON_STRICT, &err); - assertOSError(err); } } diff --git a/Apps/Src/SendCarCAN.c b/Apps/Src/SendCarCAN.c index 612d1a35d..0272b8b6d 100644 --- a/Apps/Src/SendCarCAN.c +++ b/Apps/Src/SendCarCAN.c @@ -20,13 +20,15 @@ #define IO_STATE_DLY_MS 250u +#define SENDCARCAN_MSG_SKIP_CTR 3 + // Task_PutIOState OS_TCB putIOState_TCB; CPU_STK putIOState_Stk[TASK_SEND_CAR_CAN_STACK_SIZE]; //fifo #define FIFO_TYPE CANDATA_t -#define FIFO_SIZE 16 +#define FIFO_SIZE 50 #define FIFO_NAME SendCarCAN_Q #include "fifo.h" @@ -48,22 +50,32 @@ uint8_t get_SendCarCAN_Q_Space(void) { /** * @brief Wrapper to put new message in the CAN queue - * @param message the CAN message to put in SendCarCAN Queue */ void SendCarCAN_Put(CANDATA_t message){ OS_ERR err; CPU_TS ticks; - - OSMutexPend(&CarCAN_Mtx, 0, OS_OPT_PEND_BLOCKING, &ticks, &err); - assertOSError(err); + bool success = false; - bool success = SendCarCAN_Q_put(&CANFifo, message); + static uint8_t carcan_ctr = 0; + + if(carcan_ctr > SENDCARCAN_MSG_SKIP_CTR){ + OSMutexPend(&CarCAN_Mtx, 0, OS_OPT_PEND_BLOCKING, &ticks, &err); + assertOSError(err); - OSMutexPost(&CarCAN_Mtx, OS_OPT_POST_NONE, &err); - assertOSError(err); + success = SendCarCAN_Q_put(&CANFifo, message); - if(success) OSSemPost(&CarCAN_Sem4, OS_OPT_POST_1, &err); - assertOSError(err); + OSMutexPost(&CarCAN_Mtx, OS_OPT_POST_NONE, &err); + assertOSError(err); + + carcan_ctr = 0; + } + carcan_ctr++; + + + if(success) { + OSSemPost(&CarCAN_Sem4, OS_OPT_POST_1, &err); + assertOSError(err); + } } /** @@ -97,7 +109,7 @@ void Task_SendCarCAN(void *p_arg){ (CPU_CHAR*)"PutIOState", (OS_TASK_PTR)Task_PutIOState, (void*)NULL, - (OS_PRIO)TASK_SEND_CAR_CAN_PRIO, // Round-robin with SendCarCAN task + (OS_PRIO)TASK_PUT_IOSTATE_PRIO, (CPU_STK*)putIOState_Stk, (CPU_STK_SIZE)WATERMARK_STACK_LIMIT, (CPU_STK_SIZE)TASK_SEND_CAR_CAN_STACK_SIZE, @@ -119,13 +131,11 @@ void Task_SendCarCAN(void *p_arg){ assertOSError(err); bool res = SendCarCAN_Q_get(&CANFifo, &message); - assertOSError(err); OSMutexPost(&CarCAN_Mtx, OS_OPT_POST_NONE, &err); assertOSError(err); if(res) CANbus_Send(message, true, CARCAN); - } } @@ -141,7 +151,6 @@ static void putIOState(void){ // Get minion information for(pin_t pin = 0; pin < NUM_PINS; pin++){ bool pinState = Minions_Read(pin); - if(pin == IGN_1 || pin == IGN_2) pinState = !pinState; message.data[2] |= pinState << pin; } @@ -150,6 +159,7 @@ static void putIOState(void){ bool contactorState = (Contactors_Get(contactor) == ON) ? true : false; message.data[3] |= contactorState << contactor; } + // Tell BPS if the array contactor should be on message.data[3] |= (!Minions_Read(IGN_1) || !Minions_Read(IGN_2)) << 2; @@ -164,6 +174,6 @@ static void Task_PutIOState(void *p_arg) { while (1) { putIOState(); OSTimeDlyHMSM(0, 0, 0, IO_STATE_DLY_MS, OS_OPT_TIME_HMSM_STRICT, &err); - } - + assertOSError(err); + } } diff --git a/Apps/Src/SendTritium.c b/Apps/Src/SendTritium.c index 6ae26c2d0..9f6f85307 100644 --- a/Apps/Src/SendTritium.c +++ b/Apps/Src/SendTritium.c @@ -32,7 +32,7 @@ #define MIN_CRUISE_VELOCITY mpsToRpm(20.0f) // rpm #define MAX_GEARSWITCH_VELOCITY mpsToRpm(8.0f) // rpm -#define BRAKE_PEDAL_THRESHOLD 15 // percent +#define BRAKE_PEDAL_THRESHOLD 50 // percent #define ACCEL_PEDAL_THRESHOLD 10 // percent #define ONEPEDAL_BRAKE_THRESHOLD 25 // percent @@ -62,13 +62,10 @@ float velocitySetpoint = 0; float cruiseVelSetpoint = 0; // Current observed velocity - -#ifndef SENDTRITIUM_EXPOSE_VARS static float velocityObserved = 0; // Counter for sending setpoints to motor static uint8_t motorMsgCounter = 0; -#endif // Debouncing counters static uint8_t onePedalCounter = 0; @@ -668,6 +665,10 @@ void Task_SendTritium(void *p_arg){ #endif state.stateDecider(); // decide what the next state is + // Disable velocity controlled mode by always overwriting velocity to the maximum + // in the appropriate direction. + velocitySetpoint = (velocitySetpoint>0)?MAX_VELOCITY:-MAX_VELOCITY; + // Drive #ifdef SENDTRITIUM_PRINT_MES dumpInfo(); diff --git a/Apps/Src/Tasks.c b/Apps/Src/Tasks.c index 02b71c6e2..af6382e1d 100644 --- a/Apps/Src/Tasks.c +++ b/Apps/Src/Tasks.c @@ -103,17 +103,17 @@ void throwTaskError(error_code_t errorCode, callback_t errorCallback, error_sche #if DEBUG == 1 // Print the error that caused this fault - printf("\n\rCurrent Error Code: 0x%04x\n\r", errorCode); + // printf("\n\rCurrent Error Code: 0x%04x\n\r", errorCode); - // Print the errors for each applications with error data - printf("\n\rAll application errors:\n\r"); - printf("Error_ReadCarCAN: 0x%04x\n\r", Error_ReadCarCAN); - printf("Error_ReadTritium: 0x%04x\n\r", Error_ReadTritium); - printf("Error_UpdateDisplay: 0x%04x\n\r", Error_UpdateDisplay); + // // Print the errors for each applications with error data + // printf("\n\rAll application errors:\n\r"); + // printf("Error_ReadCarCAN: 0x%04x\n\r", Error_ReadCarCAN); + // printf("Error_ReadTritium: 0x%04x\n\r", Error_ReadTritium); + // printf("Error_UpdateDisplay: 0x%04x\n\r", Error_UpdateDisplay); - // Delay so that we're not constantly printing - for (int i = 0; i < 9999999; i++) { - } + // // Delay so that we're not constantly printing + // for (int i = 0; i < 9999999; i++) { + // } #endif } diff --git a/Apps/Src/main.c b/Apps/Src/main.c index aeb19ff15..62a8f33ba 100644 --- a/Apps/Src/main.c +++ b/Apps/Src/main.c @@ -18,8 +18,6 @@ #include "UpdateDisplay.h" #include "SendCarCAN.h" -#define IGN_CONT_PERIOD 100 - int main(void) { // Disable interrupts __disable_irq(); @@ -56,8 +54,6 @@ int main(void) { assertOSError(err); while(1); - - return 0; } void Task_Init(void *p_arg){ @@ -65,17 +61,11 @@ void Task_Init(void *p_arg){ // Start systick OS_CPU_SysTickInit(SystemCoreClock / (CPU_INT32U) OSCfg_TickRate_Hz); - - OSTimeDlyHMSM(0,0,5,0,OS_OPT_TIME_HMSM_STRICT,&err); - - assertOSError(err); // Initialize drivers Pedals_Init(); - OSTimeDlyHMSM(0,0,5,0,OS_OPT_TIME_HMSM_STRICT,&err); - OSTimeDlyHMSM(0,0,10,0,OS_OPT_TIME_HMSM_STRICT,&err); BSP_UART_Init(UART_2); - CANbus_Init(CARCAN, (CANId_t*)carCANFilterList, NUM_CARCAN_FILTERS); + CANbus_Init(CARCAN, carCANFilterList, NUM_CARCAN_FILTERS); CANbus_Init(MOTORCAN, NULL, NUM_MOTORCAN_FILTERS); Contactors_Init(); Display_Init(); @@ -85,6 +75,24 @@ void Task_Init(void *p_arg){ UpdateDisplay_Init(); SendCarCAN_Init(); + // Initialize ReadTritium + OSTaskCreate( + (OS_TCB*)&ReadTritium_TCB, + (CPU_CHAR*)"ReadTritium", + (OS_TASK_PTR)Task_ReadTritium, + (void*)NULL, + (OS_PRIO)TASK_READ_TRITIUM_PRIO, + (CPU_STK*)ReadTritium_Stk, + (CPU_STK_SIZE)WATERMARK_STACK_LIMIT, + (CPU_STK_SIZE)TASK_READ_TRITIUM_STACK_SIZE, + (OS_MSG_QTY)0, + (OS_TICK)0, + (void*)NULL, + (OS_OPT)(OS_OPT_TASK_STK_CLR|OS_OPT_TASK_SAVE_FP), + (OS_ERR*)&err + ); + assertOSError(err); + // Initialize SendTritium OSTaskCreate( (OS_TCB*)&SendTritium_TCB, @@ -98,7 +106,7 @@ void Task_Init(void *p_arg){ (OS_MSG_QTY)0, (OS_TICK)0, (void*)NULL, - (OS_OPT)(OS_OPT_TASK_STK_CLR|OS_OPT_TASK_SAVE_FP|OS_OPT_TASK_SAVE_FP), + (OS_OPT)(OS_OPT_TASK_STK_CLR|OS_OPT_TASK_SAVE_FP), (OS_ERR*)&err ); assertOSError(err); @@ -116,7 +124,7 @@ void Task_Init(void *p_arg){ (OS_MSG_QTY)0, (OS_TICK)0, (void*)NULL, - (OS_OPT)(OS_OPT_TASK_STK_CLR|OS_OPT_TASK_SAVE_FP|OS_OPT_TASK_SAVE_FP), + (OS_OPT)(OS_OPT_TASK_STK_CLR|OS_OPT_TASK_SAVE_FP), (OS_ERR*)&err ); assertOSError(err); @@ -134,29 +142,11 @@ void Task_Init(void *p_arg){ (OS_MSG_QTY)0, (OS_TICK)0, (void*)NULL, - (OS_OPT)(OS_OPT_TASK_STK_CLR|OS_OPT_TASK_SAVE_FP|OS_OPT_TASK_SAVE_FP), - (OS_ERR*)&err - ); - assertOSError(err); - - // Initialize ReadTritium - OSTaskCreate( - (OS_TCB*)&ReadTritium_TCB, - (CPU_CHAR*)"ReadTritium", - (OS_TASK_PTR)Task_ReadTritium, - (void*)NULL, - (OS_PRIO)TASK_READ_TRITIUM_PRIO, - (CPU_STK*)ReadTritium_Stk, - (CPU_STK_SIZE)WATERMARK_STACK_LIMIT, - (CPU_STK_SIZE)TASK_READ_TRITIUM_STACK_SIZE, - (OS_MSG_QTY)0, - (OS_TICK)0, - (void*)NULL, - (OS_OPT)(OS_OPT_TASK_STK_CLR|OS_OPT_TASK_SAVE_FP|OS_OPT_TASK_SAVE_FP), + (OS_OPT)(OS_OPT_TASK_STK_CLR|OS_OPT_TASK_SAVE_FP), (OS_ERR*)&err ); assertOSError(err); - + // Initialize SendCarCAN OSTaskCreate( (OS_TCB*)&SendCarCAN_TCB, @@ -170,13 +160,10 @@ void Task_Init(void *p_arg){ (OS_MSG_QTY)0, (OS_TICK)0, (void*)NULL, - (OS_OPT)(OS_OPT_TASK_STK_CLR|OS_OPT_TASK_SAVE_FP|OS_OPT_TASK_SAVE_FP), + (OS_OPT)(OS_OPT_TASK_STK_CLR|OS_OPT_TASK_SAVE_FP), (OS_ERR*)&err ); assertOSError(err); - - while(1){ - CPU_WaitForInt(); - } + OSTaskDel(NULL, &err); } diff --git a/BSP/STM32F413/Makefile b/BSP/STM32F413/Makefile index 62a2a6274..bfc0a49b9 100644 --- a/BSP/STM32F413/Makefile +++ b/BSP/STM32F413/Makefile @@ -77,7 +77,7 @@ SF = st-flash CPU = -mcpu=cortex-m4 # float-abi -FLOAT-ABI = -mfloat-abi=soft +FLOAT-ABI = -mfloat-abi=hard # mcu MCU = $(CPU) -mthumb $(FPU) $(FLOAT-ABI) diff --git a/Drivers/Inc/CANConfig.h b/Drivers/Inc/CANConfig.h index 9364a4bf3..0035e2f37 100644 --- a/Drivers/Inc/CANConfig.h +++ b/Drivers/Inc/CANConfig.h @@ -14,8 +14,8 @@ /** * Filter Lists for CarCAN and MotorCAN */ -#define NUM_CARCAN_FILTERS 6 -#define NUM_MOTORCAN_FILTERS 0 +#define NUM_CARCAN_FILTERS 4 +#define NUM_MOTORCAN_FILTERS 7 extern CANId_t carCANFilterList[NUM_CARCAN_FILTERS]; extern CANId_t motorCANFilterList[NUM_MOTORCAN_FILTERS]; diff --git a/Drivers/Inc/CANbus.h b/Drivers/Inc/CANbus.h index 46e8b8447..fcc01383e 100644 --- a/Drivers/Inc/CANbus.h +++ b/Drivers/Inc/CANbus.h @@ -29,7 +29,7 @@ */ typedef enum { BPS_TRIP = 0x002, - BPS_CONTACTOR = 0x101, + BPS_CONTACTOR = 0x102, STATE_OF_CHARGE = 0x106, SUPPLEMENTAL_VOLTAGE = 0x10B, MOTOR_DRIVE = 0x221, @@ -45,6 +45,7 @@ typedef enum { TEMPERATURE = 0x24B, ODOMETER_AMPHOURS = 0x24E, ARRAY_CONTACTOR_STATE_CHANGE = 0x24F, + SLIP_SPEED = 0x257, CONTROL_MODE = 0x580, IO_STATE = 0x581, MAX_CAN_ID diff --git a/Drivers/Src/CANConfig.c b/Drivers/Src/CANConfig.c index ce8548d1f..23135d00b 100644 --- a/Drivers/Src/CANConfig.c +++ b/Drivers/Src/CANConfig.c @@ -35,6 +35,7 @@ const CANLUT_T CANLUT[MAX_CAN_ID] = { [TEMPERATURE] = {NOIDX, DOUBLE}, /** TEMPERATURE **/ [ODOMETER_AMPHOURS] = {NOIDX, DOUBLE}, /** ODOMETER_AMPHOURS **/ [ARRAY_CONTACTOR_STATE_CHANGE] = {NOIDX, BYTE }, /** ARRAY_CONTACTOR_STATE_CHANGE **/ + [SLIP_SPEED] = {NOIDX, DOUBLE}, [MOTOR_DRIVE] = {NOIDX, DOUBLE}, /** MOTOR_DRIVE **/ [MOTOR_POWER] = {NOIDX, DOUBLE}, /** MOTOR_POWER **/ [MOTOR_RESET] = {NOIDX, DOUBLE}, /** MOTOR_RESET **/ @@ -53,4 +54,13 @@ CANId_t carCANFilterList[NUM_CARCAN_FILTERS] = { STATE_OF_CHARGE, SUPPLEMENTAL_VOLTAGE }; -CANId_t motorCANFilterList[NUM_MOTORCAN_FILTERS] = {}; + +CANId_t motorCANFilterList[NUM_MOTORCAN_FILTERS] = { + MC_BUS, + VELOCITY, + BACKEMF, + TEMPERATURE, + ODOMETER_AMPHOURS, + SLIP_SPEED, + MOTOR_STATUS +}; diff --git a/Drivers/Src/CANbus.c b/Drivers/Src/CANbus.c index 68ab5ddbf..413a5c734 100755 --- a/Drivers/Src/CANbus.c +++ b/Drivers/Src/CANbus.c @@ -15,8 +15,6 @@ static OS_SEM CANBus_ReceiveSem4[NUM_CAN]; // sem4 to count how many msgs in our static OS_MUTEX CANbus_TxMutex[NUM_CAN]; // mutex to lock tx line static OS_MUTEX CANbus_RxMutex[NUM_CAN]; // mutex to lock Rx line - - /** * @brief this function will be passed down to the BSP layer to trigger on RX events. Increments the receive semaphore to signal message in hardware mailbox. Do not access directly outside this driver. * @param bus The CAN bus to operate on. Should be CARCAN or MOTORCAN. diff --git a/Makefile b/Makefile index afea3bbe3..3a5cc919e 100644 --- a/Makefile +++ b/Makefile @@ -29,10 +29,10 @@ LEADER = controls-leader all: @echo "${RED}Not enough arguments. Call: ${ORANGE}make help${NC}" -.PHONY: stm32f413 -stm32f413: leader +.PHONY: leader +leader: stm32f413 -leader: +stm32f413: @echo "${YELLOW}Compiling for leader...${NC}" $(MAKE) -C BSP -C STM32F413 -j TARGET=$(LEADER) TEST=$(TEST_LEADER) @echo "${BLUE}Compiled for leader! Jolly Good!${NC}"