Skip to content

Commit 6a58097

Browse files
committed
Inputs + Common: implements input handling for wheel and panel from old bottom shell
This commit implements many of the inputs that were previously handled in the old bottom shell repository. It implements the brake switch, forward/reverse, and whether the message came from the wheel or the switch panel. It also stores information about the cruise regen. This also implements a templated utility function that can parse a CAN message buffer into a compile-time specified data type at a compile-time specified offset. This removes the need for modifying the FlexCAN_T4 library while providing us similar levels of functionality.
1 parent 11b607f commit 6a58097

File tree

4 files changed

+111
-1
lines changed

4 files changed

+111
-1
lines changed

platformio.ini

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ build_flags =
2323
-Isrc/common
2424
;-Isrc/gps
2525
;-Isrc/heartbeat
26-
;-Isrc/inputs
26+
-Isrc/inputs
2727
;-Isrc/logging
2828
;-Isrc/misc
2929
;-Isrc/motor

src/common/common.h

+14
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,18 @@
2020
#define HEARTBEAT_WA 512
2121
#define HEARTBEAT_SLEEP 100
2222

23+
/**
24+
* @brief A templated function for parsing a CAN message buffer into desired data types.
25+
* @details This
26+
*/
27+
template<typename T, int offset>
28+
T parseCANBuf(const CAN_message_t &msg) {
29+
// make sure we can store read enough data from CAN buffer
30+
constexpr size_t dataBounds = sizeof(T)*(offset+1);
31+
static_assert(dataBounds <= 8, "can't read buffer! index out of bounds");
32+
T ret;
33+
memcpy(&ret, &msg.buf[0] + offset * sizeof(T), sizeof(T));
34+
return ret;
35+
}
36+
2337
#endif

src/inputs/inputs.cpp

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#include "inputs.h"
2+
3+
struct {
4+
// true if from wheel, false if from panel
5+
bool fromWheel;
6+
// true if forward, false if backward
7+
bool wheelForward;
8+
// true if brake switch is set
9+
bool brakeSwitch;
10+
// if you're in cruise control and this flag is set, regen breaking will keep you at cruise
11+
// speed if you're going down a hill
12+
// true if cruise regen is enabled
13+
bool cruiseRegen;
14+
} switchBoard;
15+
16+
bool getForwardReverse() {
17+
if (switchBoard.fromWheel) {
18+
return digitalRead(FORWARD_REVERSE_IN) == 1;
19+
} else {
20+
return switchBoard.wheelForward;
21+
}
22+
}
23+
24+
inline bool getBrakeSwitch() {
25+
switchBoard.brakeSwitch = digitalRead(BRAKE_SWITCH);
26+
return switchBoard.brakeSwitch;
27+
}
28+
29+
inline bool getCruiseRegen() {
30+
return switchBoard.cruiseRegen;
31+
}
32+
33+
inline bool isWheel() {
34+
return switchBoard.fromWheel;
35+
}
36+
37+
inline void handleWheelOrPanel(CAN_message_t msg) {
38+
switchBoard.fromWheel = (parseCANBuf<short, 0>(msg) % 2) == 0;
39+
}
40+
41+
inline void handleWheelForRev(CAN_message_t msg) {
42+
switchBoard.wheelForward = (parseCANBuf<short, 0>(msg) % 2) == 0;
43+
}
44+
45+
inline void handleCruiseRegen(CAN_message_t msg) {
46+
switchBoard.cruiseRegen = (parseCANBuf<short, 1>(msg) % 2) != 0;
47+
}

src/inputs/inputs.h

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
//! Input data that modifies internal state.
2+
3+
#ifndef INPUTS_H
4+
#define INPUTS_u
5+
6+
#include "common.h"
7+
8+
/**
9+
* @brief Returns whether the brake is currently pressed to any degree.
10+
*/
11+
bool getBrakeSwitch();
12+
/**
13+
* @brief Returns whether the car is currently moving forwards or backwards. Set from both wheel and panel.
14+
*/
15+
bool getForwardReverse();
16+
/**
17+
* @brief Returns whether the car is using cruise regen or not.
18+
* @details Cruise Regen is where a car uses magnetic regen to maintain the speed of the car's cruise
19+
* control in the event it slows down due to, for example, rolling down a hill.
20+
*/
21+
bool getCruiseRegen();
22+
/**
23+
* @brief Returns whether the previous CAN message input came from the wheel or the panel.
24+
*/
25+
bool isWheel();
26+
27+
/**
28+
* @brief Sets internal state to if an input came from the wheel or the panel.
29+
* @details The internal state is stored privately in the associated implementation file.
30+
* @param msg CAN message to parse
31+
* @see isWheel()
32+
*/
33+
void handleWheelOrPanel(const CAN_message_t &msg);
34+
/**
35+
* @brief Parses a CAN message and updates internal state to whether the car should move forwards or backwards.
36+
* @details The internal state is stored privately in the associated implementation file.
37+
* @param msg CAN message to parse
38+
*/
39+
void handleWheelForRev(const CAN_message_t &msg);
40+
/**
41+
* @brief Parses a CAN message and updates internal state to whether the car should do "cruise regen".
42+
* @details Cruise Regen is where a car uses magnetic regen to maintain the speed of the car's cruise
43+
* control in the event it slows down due to, for example, rolling down a hill. The internal state is
44+
* stored privately in the associated implementation file.
45+
* @param msg CAN message to parse
46+
*/
47+
void handleCruiseRegen(const CAN_message_t &msg);
48+
49+
#endif

0 commit comments

Comments
 (0)