Repository navigation
Expand file tree
/
Copy pathCar.cpp
More file actions
85 lines (75 loc) · 2 KB
/
Copy pathCar.cpp
File metadata and controls
85 lines (75 loc) · 2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/*
* Car.cpp
*
* Reads the status message and engine speed from a specific message id and stores it
* for later use by the engine filter.
*
* Created on: 20 Sep 2018
* Author: michaeln
*/
#include "Car.h"
/**
* Constructor
*/
Car::Car() :
FilterListener()
{
rpm = 0;
powerOn = false;
lastMessage = 0;
}
/**
* Sets the incoming and outgoing can bus and initializes the incoming bus
* so that enough TX mailboxes are available and the callback for incoming messages
* is attached to the due_can interrupt.
*/
void Car::initialize(CANRaw *in, CANRaw *out)
{
FilterListener::initialize(in, out);
in->begin(CAN_CAR_SPEED, 255);
in->setNumTXBoxes(CAN_CAR_NUM_TX_BOXES);
in->setRXFilter(CAN_CAR_MBOX_STATUS, 0x129, 0x7FF, false); // status and rpm from GEVCU
in->setRXFilter(1, 0, 0, false); // catch all standard messages
in->setRXFilter(2, 0, 0, true); // catch all extended messages
in->attachObj(this);
attachMBHandler(CAN_CAR_MBOX_STATUS);
attachGeneralHandler();
}
/**
* Callback function called from due_can when a message has arrived and the interrupt was triggered
*/
void Car::gotFrame(CAN_FRAME* frame, int mailbox)
{
switch (mailbox) {
case CAN_CAR_MBOX_STATUS: // read status information coming from the car
rpm = frame->data.s0;
powerOn = frame->data.s1 & 0x01;
lastMessage = millis();
break;
}
out->sendFrame(*frame);
#ifdef LOG_FRAMES
this->printFrame(frame, mailbox, "car");
#endif
}
/**
* Indicates if the car reports a running DCDC converter.
* If no message is received within a specified timeout, it is assumed the car was
* turned off.
*
* \return true if the car / DCDC converter is running and no timeout occured
*/
bool Car::isRunning()
{
return (powerOn && ((millis() - lastMessage) < CAN_CAR_TIMEOUT));
}
/**
* Get the engine rpm as reported by the electric motor controller
*
* \return the speed of the motor in rpm's
*/
uint16_t Car::getRpm()
{
return rpm;
}
Car car;