Skip to content

Commit

Permalink
Deleted most putIOStateTimer thigns, added a macro and counter in the…
Browse files Browse the repository at this point in the history
… sendCarCAN loop to send the IO message at approximately the same frequency as before (250 ms), but this time using loops, delays, and counters. It won't be as accurate, but it should still be at a relatively similar frequency.
  • Loading branch information
KnockbackNemo committed Oct 20, 2023
1 parent 5185426 commit 90cb9c6
Showing 1 changed file with 15 additions and 36 deletions.
51 changes: 15 additions & 36 deletions Apps/Src/SendCarCAN.c
Original file line number Diff line number Diff line change
@@ -1,34 +1,33 @@
#include "common.h"
#include "os_cfg_app.h"

#include "Tasks.h"
#include "SendCarCAN.h"

#include "CANbus.h"
#include "Minions.h"
#include "Contactors.h"
#include "Pedals.h"
#include "Tasks.h"
#include "SendCarCAN.h"
#include "SendTritium.h"

#define IO_STATE_DLY_MS 250u
#define SENDCARCAN_LOOP_DLY_MS 50u // How often we should check the CAN queue (in ms delay). Must send faster than queue messages get put in
#define IO_STATE_DLY_COUNT (IO_STATE_DLY_MS / SENDCARCAN_LOOP_DLY_MS) // The number of periods to wait before sending IO state message
#define IO_STATE_TMR_DLY_TS ((IO_STATE_TMR_DLY_MS * OS_CFG_TMR_TASK_RATE_HZ) / (1000u))// **** delete?

// fifo
#define FIFO_TYPE CANDATA_t
#define FIFO_SIZE 256
#define FIFO_NAME SendCarCAN_Q
#include "fifo.h"

#define SENDCARCAN_QUEUE_CHECK_MS 50 // How often we should check the CAN queue (in ms delay). Must send faster than queue messages get put in
static SendCarCAN_Q_t CANFifo;

static OS_SEM CarCAN_Sem4;
static OS_MUTEX CarCAN_Mtx;
static OS_SEM PutIOTimerAlert_Sem4;

#define IO_STATE_TMR_DLY_MS 250u
#define IO_STATE_TMR_DLY_TS ((IO_STATE_TMR_DLY_MS * OS_CFG_TMR_TASK_RATE_HZ) / (1000u))
static OS_TMR IOStateTmr;
static uint8_t IOStateDlyCounter = 0;



static void putIOStateTimerAlert(void *p_tmr, void *p_arg);
static void putIOState(void);

/**
Expand Down Expand Up @@ -63,36 +62,23 @@ void Task_SendCarCAN(void *p_arg){
OSSemCreate(&CarCAN_Sem4, "CarCAN_Sem4", 0, &err);
assertOSError(OS_SEND_CAN_LOC, err);

OSSemCreate(&PutIOTimerAlert_Sem4, "PutIOTimerAlert_Sem4", 0, &err);
assertOSError(OS_SEND_CAN_LOC, err);

SendCarCAN_Q_renew(&CANFifo);

// Set up timer to put car data message every IO_STATE_TMR_DLY_MS ms
OSTmrCreate(&IOStateTmr,
"IO State CAN Message Timer",
0,
IO_STATE_TMR_DLY_TS,
OS_OPT_TMR_PERIODIC,
putIOStateTimerAlert,
NULL,
&err
);
assertOSError(OS_SEND_CAN_LOC, err);
// Set up timer to put car data message every IO_STATE_TMR_DLY_MS ms //***************

OSTmrStart(&IOStateTmr, &err);
assertOSError(OS_SEND_CAN_LOC, err);

CANDATA_t message;
memset(&message, 0, sizeof(CANDATA_t));

while (1) {

OSSemPend(&PutIOTimerAlert_Sem4, 0, OS_OPT_PEND_NON_BLOCKING, &ticks, &err);
if (err != OS_ERR_PEND_WOULD_BLOCK) { // The timer fired; it's time to send an IO state message

if (++IOStateDlyCounter >= IO_STATE_DLY_COUNT) { // Send IO State message approximately every IO_STATE_DLY_MS
IOStateDlyCounter = 0;
putIOState();
}


// Check if there's something to send in the queue (either IOState or Car state from sendTritium)
OSSemPend(&CarCAN_Sem4, 0, OS_OPT_PEND_NON_BLOCKING, &ticks, &err);
if (err != OS_ERR_PEND_WOULD_BLOCK) { // We have a message in the queue to send
Expand All @@ -109,7 +95,7 @@ void Task_SendCarCAN(void *p_arg){
if(res) CANbus_Send(message, true, CARCAN);
}

OSTimeDlyHMSM(0, 0, 0, SENDCARCAN_QUEUE_CHECK_MS, OS_OPT_TIME_HMSM_STRICT, &err);
OSTimeDlyHMSM(0, 0, 0, SENDCARCAN_LOOP_DLY_MS, OS_OPT_TIME_HMSM_STRICT, &err);
assertOSError(OS_SEND_CAN_LOC, err);

}
Expand Down Expand Up @@ -140,10 +126,3 @@ static void putIOState(void){

SendCarCAN_Put(message);
}

// Callback function for the putIOState timer
static void putIOStateTimerAlert(void *p_tmr, void *p_arg){
OS_ERR err;
OSSemPost(&PutIOTimerAlert_Sem4, OS_OPT_POST_1, &err);
assertOSError(OS_SEND_CAN_LOC, err);
}

0 comments on commit 90cb9c6

Please sign in to comment.