SAE Teensy ECU
IIT SAE Microcontroller programming
Loading...
Searching...
No Matches
Heartbeat.cpp
Go to the documentation of this file.
1
11//@cond
12
13#include "Heartbeat.h"
14#include "Canbus.h"
15#include "ECUGlobalConfig.h"
16#include "Heartbeat.def"
17#include "Log.h"
18#include "Pins.h"
19#include "stdint.h"
20#include "stdlib.h"
21#include <set>
22
23namespace Heartbeat {
24static IntervalTimer canbusPinUpdate;
25static elapsedMillis lastBeat;
26static volatile int lastTime = 0;
27
28static LOG_TAG ID = "HeartBeat";
29
30static std::set<beatFunc> funcs;
31
32static void toggleLED() {
33 static bool on = false;
34 on = !on;
35 Pins::setPinValue(PINS_BOTH_LED, on);
36}
37
38static void beat() {
39 Canbus::sendData(ADD_HEART);
40 toggleLED();
41
42 for (auto f : funcs) {
43 f();
44 }
45}
46
47void beginBeating() {
48 canbusPinUpdate.priority(10);
49 canbusPinUpdate.begin(beat, CONF_HEARTBEAT_INTERVAL_MILLIS * 1000);
50}
51
52static void receiveBeat(uint32_t, volatile uint8_t *) {
53 lastTime = lastBeat;
54 lastBeat = 0;
55 toggleLED();
56}
57
58void beginReceiving() {
59 Canbus::addCallback(ADD_HEART, receiveBeat);
60}
61
62int checkBeat() {
64 Log.w(ID, "Heartbeat is taking too long", lastBeat);
65 Log.p("lag", "Heartbeat is taking too long", lastBeat);
66 return 0;
67 }
68 return 1;
69}
70
71void addCallback(beatFunc func) {
72 funcs.insert(func);
73}
74
75} // namespace Heartbeat
76 //@endcond
FlexCAN_T4 wrapper.
Configure global build properties.
#define CONF_HEARTBEAT_INTERVAL_MILLIS
The interval the beat should target.
Definition Heartbeat.def:22
#define CONF_HEARTBEAT_TIMEOUT_MILLI
The allowed delay between beats.
Definition Heartbeat.def:28
Make one ECU tell the other it is alive.
Special logging functionality.
const char * LOG_TAG
Type definition of logging tags This typedef is necessary to allow for easier manipulation of code by...
Definition Log.h:48
Logging::Log_t Log
The global logging object.
Update, set, and get predefined pin values.
A module used to both ensure a connection to both ECUs using CAN and to periodically run callbacks.
Definition Heartbeat.h:30
void addCallback(beatFunc func)
Add a callback to be run at each haertbeat.
void beginReceiving()
Set callback to receive beats over CAN.
int checkBeat()
Poll if a beat has been received.
void(* beatFunc)(void)
Function called each time the heart beats.
Definition Heartbeat.h:35
void beginBeating()
begin sending a beat signal at a set interval
void setPinValue(uint8_t GPIO_Pin, int value)
Set the pin value of a predefined pin.
void w(LOG_TAG TAG, LOG_MSG message)
Log a string using a warning tag.
void p(LOG_TAG name, LOG_MSG prettyName, const uint32_t number, int mediate=false)
Post a monitored value.