diff --git a/306-controller_interface.X/app/can_message.c b/306-controller_interface.X/app/can_message.c new file mode 100644 index 0000000..45645c7 --- /dev/null +++ b/306-controller_interface.X/app/can_message.c @@ -0,0 +1,12 @@ +/** + * @author Rémi Heredero + * @version 1.0.0 + * @date August 2023 + * @file can_message.c + */ + +#include "../middleware/can_interface.h" + +void CM_controller_alive(void* p) { + CAN_Send(0x0, 0xF, 0); +} \ No newline at end of file diff --git a/306-controller_interface.X/app/can_message.h b/306-controller_interface.X/app/can_message.h new file mode 100644 index 0000000..0973672 --- /dev/null +++ b/306-controller_interface.X/app/can_message.h @@ -0,0 +1,29 @@ +/** + * @author Rémi Heredero + * @version 1.0.0 + * @date August 2023 + * @file can_message.h + */ + +#ifndef CAN_MESSAGE_H +#define CAN_MESSAGE_H + + +/* +S R M +1 0 F CONTROL_ALIVE - - - - +1 2 0 JOY_SETUP Mode Param1 Param2 aliveTime +1 3 0 DISPLAY_SETUP reset - - aliveTime +1 3 2 DISPLAY_SPEED valH valL - - +1 3 3 DISPLAY_DIRECTION direction - - - +1 4 0 DRIVE_SETUP Reset/init speedTime stopTime aliveTime +1 4 1 DRIVE_POWER valH valL - - +1 5 0 STEERING_SETUP Reset/init homing setCenter aliveTime +1 5 1 STEERING_SET valHH valH valL valLL +1 6 0 SETUP_CONTROL batteryVoltTime batteryCurrentTime batteryEnergyTime aliveTime +*/ + +void CM_controller_alive(void* p); + +#endif /* CAN_MESSAGE_H */ + diff --git a/306-controller_interface.X/app/factory/factory.c b/306-controller_interface.X/app/factory/factory.c index 2cce0d7..68712a4 100644 --- a/306-controller_interface.X/app/factory/factory.c +++ b/306-controller_interface.X/app/factory/factory.c @@ -30,6 +30,10 @@ LED* l8() { return &theFactory.l8_; } +WATCHDOG* WDcontroller(){ + return &theFactory.WDcontroller_; +} + //initialize all objects void Factory_init() { @@ -54,6 +58,13 @@ void Factory_init() { CAN_init(); CAN_setSender(1); LED_off(l1()); + + // TODO init EPROM interface + + // TODO init watchdog with EPROM CST + WATCHDOG_init(WDcontroller()); + CAR_CST.CONTROL_ALIVE_TIME = 10; + WATCHDOG_setTime(WDcontroller(), CAR_CST.CONTROL_ALIVE_TIME); } void foo(uint8_t a, uint8_t b, uint32_t c){ @@ -69,9 +80,12 @@ void foo(uint8_t a, uint8_t b, uint32_t c){ void Factory_build() { ECAN_SetRXBnInterruptHandler(CAN_newMsg); CAN_onReceiveCan(foo); + + WATCHDOG_onAlive(WDcontroller(), CM_controller_alive, NULL); } //start all state machines void Factory_start() { CAN_startBehaviour(); + WATCHDOG_startBehaviour(WDcontroller()); } diff --git a/306-controller_interface.X/app/factory/factory.h b/306-controller_interface.X/app/factory/factory.h index 737a8e3..df8b9ad 100644 --- a/306-controller_interface.X/app/factory/factory.h +++ b/306-controller_interface.X/app/factory/factory.h @@ -12,9 +12,13 @@ #include #include +#include "../car.h" +#include "../can_message.h" #include "../../board/led/led.h" #include "../../board/button/button.h" +#include "../../middleware/alive_checker.h" #include "../../middleware/can_interface.h" +#include "../../middleware/watchdog.h" typedef struct { @@ -26,6 +30,8 @@ typedef struct { LED l6_; LED l7_; LED l8_; + + WATCHDOG WDcontroller_; } Factory; @@ -44,5 +50,7 @@ LED* l6(); LED* l7(); LED* l8(); +WATCHDOG* WDcontroller(); + #endif \ No newline at end of file diff --git a/306-controller_interface.X/middleware/watchdog.c b/306-controller_interface.X/middleware/watchdog.c new file mode 100644 index 0000000..5840bf5 --- /dev/null +++ b/306-controller_interface.X/middleware/watchdog.c @@ -0,0 +1,90 @@ +/** + * @author Rémi Heredero + * @version 1.0.0 + * @date August 2023 + * @file watchdog.c + */ + +#include "watchdog.h" + +void WATCHDOG_init(WATCHDOG* me){ + me->state = STWD_INIT; + me->time = 10; + me->alive.f = NULL; +} + +void WATCHDOG_startBehaviour(WATCHDOG* me){ + POST(me, &WATCHDOG_processEvent, evWDinit, 0, 0); +} + +bool WATCHDOG_processEvent(Event* ev) { + bool processed = false; + WATCHDOG* me = (WATCHDOG*)Event_getTarget(ev); + WATCHDOG_STATES oldState = me->state; + evIDT evid = Event_getId(ev); + uint64_t data = Event_getData(ev); + + switch (me->state) { // onState + case STWD_INIT: + if (ev->id == evWDinit) { + me->state = STWD_ALIVE; + WATCHDOG_emitPoll(me, 10*me->time, 0); + } + break; + + case STWD_ALIVE: + if (ev->id == evWDpoll) { + WATCHDOG_emitPoll(me, 10*me->time, 0); + if (me->alive.f != NULL) { + me->alive.f(me->alive.p); + } + } + break; + } + + if(oldState != me->state){ + switch (oldState) { // onExit + case STWD_INIT: + break; + + case STWD_ALIVE: + break; + } + + switch (me->state) { // onEntry + case STWD_INIT: + break; + + case STWD_ALIVE: + break; + } + + processed = true; + } + return processed; +} + +/************* + * Callbacks * + *************/ + +void WATCHDOG_onAlive(WATCHDOG* me, WATCHDOG_CALLBACK_FUNCTION f, void* p) { + me->alive.f = f; + me->alive.p = p; +} + +/************ + * EMITTERS * + ************/ + +void WATCHDOG_emitPoll(WATCHDOG* me, uint16_t t, int64_t data) { + POST(me, &WATCHDOG_processEvent, evWDpoll, t, data); +} + +/*********** + * SETTERS * + ***********/ + +void WATCHDOG_setTime(WATCHDOG* me, uint8_t v) { + me->time = v; +} diff --git a/306-controller_interface.X/middleware/watchdog.h b/306-controller_interface.X/middleware/watchdog.h new file mode 100644 index 0000000..ed2d184 --- /dev/null +++ b/306-controller_interface.X/middleware/watchdog.h @@ -0,0 +1,83 @@ +/** + * @author Rémi Heredero + * @version 1.0.0 + * @date August 2023 + * @file watchdog.h + */ +#ifndef WATCHDOG_H +#define WATCHDOG_H + +#include "../xf/xf.h" + +typedef enum { + STWD_INIT, + STWD_ALIVE +} WATCHDOG_STATES; + +typedef enum { + evWDinit = 20, + evWDpoll +} WATCHDOG_EVENTS; + +typedef void (*WATCHDOG_CALLBACK_FUNCTION)(void*); +typedef struct { + WATCHDOG_CALLBACK_FUNCTION f; // function + void* p; // param(s) +} WATCHDOG_CALLBACK; + +typedef struct { + WATCHDOG_STATES state; + uint8_t time; + WATCHDOG_CALLBACK alive; +} WATCHDOG; + +/** + * Initialize the WATCHDOG + * @param me the WATCHDOG itself + */ +void WATCHDOG_init(WATCHDOG* me); + +/** + * Start the WATCHDOG state machine + * @param me the WATCHDOG itself + */ +void WATCHDOG_startBehaviour(WATCHDOG* me); + +/** + * Process the event + * @param ev the event to process + * @return true if the event is processed + */ +bool WATCHDOG_processEvent(Event* ev); + +/************* + * Callbacks * + *************/ + +/** + * Set the callback function to call when the WATCHDOG is entering state alive + * @param me the WATCHDOG itself + * @param f the function to call + * @param p the param(s) to pass to the function + */ +void WATCHDOG_onAlive(WATCHDOG* me, WATCHDOG_CALLBACK_FUNCTION f, void* p); + +/************ + * EMITTERS * + ************/ + +/** + * Emit the poll event + * @param me the WATCHDOG itself + * @param t time to wait in ms before triggering event + * @param data data to put on the event for XF + */ +void WATCHDOG_emitPoll(WATCHDOG* me, uint16_t t, int64_t data); + +/*********** + * SETTERS * + ***********/ + +void WATCHDOG_setTime(WATCHDOG* me, uint8_t v); + +#endif diff --git a/306-controller_interface.X/nbproject/configurations.xml b/306-controller_interface.X/nbproject/configurations.xml index 11b024b..0fc2e44 100644 --- a/306-controller_interface.X/nbproject/configurations.xml +++ b/306-controller_interface.X/nbproject/configurations.xml @@ -7,6 +7,7 @@ app/factory/factory.h app/car.h + app/can_message.h board/led/led.h @@ -25,6 +26,7 @@ middleware/can_interface.h middleware/alive_checker.h + middleware/watchdog.h xf/event.h @@ -41,6 +43,7 @@ projectFiles="true"> app/factory/factory.c + app/can_message.c board/led/led.c @@ -59,6 +62,7 @@ middleware/can_interface.c middleware/alive_checker.c + middleware/watchdog.c xf/event.c @@ -210,6 +214,7 @@ + @@ -240,7 +245,7 @@ - + @@ -285,7 +290,7 @@ - +