Skip to content

Commit 49f90e2

Browse files
authored
CAN Rearchitecture (#337)
* CAN: got this tx signal mapping almost compiling * CAN: More signal map work * CAN: Got msg map working * CAN: add mbed error warnings * CAN: add dual CAN bus support, add simple test app * CAN: use std::unordered_map::contains() * CAN: add can filtering function * CAN: start updating arm app * CAN: migrate apps to new CAN library * CAN: add switch CAN bus to apps, add PID tuning handler to science and gimbal * CAN: fix multiple filters, revert to try_get(), add can_load_test script * CAN: add diagnostic messages * CAN: add test-stress-can app * CAN: edit CAN diagnostics messages * CAN: fix clang format * CAN: make can load test script log streamed vs one-shots * CAN: swap CAN 1 CAN 2 pins * CAN: Add RX one-shot messages to msg map * CAN: fix test-lookup-table * CAN: kinda fix hw filter issue (TODO: fix mbed bug) * CAN: fix bus switching * CAN: minor fixes * CAN: add safety check, fix signal value enum naming * Add num CAN rx/tx faults signals to msg maps * CAN: rebase master * remove leftover lib folder * CAN: fix test-can build errors * Fix test-stress-can buld errors * Update submodule commit
1 parent 5c90e4e commit 49f90e2

File tree

32 files changed

+2281
-563
lines changed

32 files changed

+2281
-563
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ add_app_subdirectory(${TEST_APPS_DIR}/test-pid)
111111
add_app_subdirectory(${TEST_APPS_DIR}/test-pwm)
112112
add_app_subdirectory(${TEST_APPS_DIR}/test-pwmin)
113113
add_app_subdirectory(${TEST_APPS_DIR}/test-quadrature64cpr)
114+
add_app_subdirectory(${TEST_APPS_DIR}/test-stress-can)
114115
add_app_subdirectory(${TEST_APPS_DIR}/test-units)
115116
add_app_subdirectory(${TEST_APPS_DIR}/test-watchdog)
116117

libs/can/CMakeLists.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,20 @@ add_library(CANBus STATIC)
2121
target_sources(CANBus PRIVATE src/CANBus.cpp)
2222
target_include_directories(CANBus PUBLIC include)
2323
target_link_libraries(CANBus
24+
PRIVATE
25+
uwrt-mars-rover-hw-bridge
26+
mbed-os
27+
)
28+
29+
add_library(CANInterface STATIC)
30+
target_sources(CANInterface PRIVATE src/CANInterface.cpp)
31+
target_include_directories(CANInterface PUBLIC include)
32+
target_link_libraries(CANInterface
2433
PUBLIC
2534
mbed-os
35+
mbed-events
2636
PRIVATE
2737
uwrt-mars-rover-hw-bridge
38+
CANBus
39+
CANMsg
2840
)

libs/can/include/CANBus.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class CANBus : public CAN {
1414
/* Filter out incoming CAN msgs
1515
*/
1616
int setFilter(HWBRIDGE::CANFILTER filter, CANFormat format = CANAny,
17-
uint16_t mask = HWBRIDGE::ROVERCONFIG::ROVER_CANID_FILTER_MASK, int handle = 0);
17+
uint16_t mask = HWBRIDGE::ROVER_CANID_FILTER_MASK, int handle = 0);
1818

1919
can_t *getHandle();
2020

libs/can/include/CANInterface.h

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
#pragma once
2+
3+
#include "CANBus.h"
4+
#include "CANMsg.h"
5+
#include "hw_bridge.h"
6+
#include "mbed.h"
7+
8+
class CANInterface {
9+
public:
10+
typedef struct {
11+
// CAN bus pins
12+
PinName can1_RX;
13+
PinName can1_TX;
14+
PinName can2_RX;
15+
PinName can2_TX;
16+
17+
// Message maps and handlers
18+
HWBRIDGE::CANMsgMap *rxMsgMap;
19+
HWBRIDGE::CANMsgMap *txMsgMap;
20+
const CANMsg::CANMsgHandlerMap *rxOneShotMsgHandler;
21+
22+
// Bus frequency
23+
uint32_t frequency_hz = HWBRIDGE::ROVER_CANBUS_FREQUENCY_HZ;
24+
} Config;
25+
26+
// Initialize CAN interface
27+
CANInterface(const Config &config);
28+
29+
// Queue up a one shot message to be sent
30+
bool sendOneShotMessage(CANMsg &msg, Kernel::Clock::duration_u32 timeout);
31+
32+
// Update a TX CAN signal
33+
bool setTXSignalValue(HWBRIDGE::CANID msgID, HWBRIDGE::CANSIGNAL signalName, HWBRIDGE::CANSignalValue_t signalValue);
34+
35+
// Read a RX CAN signal
36+
bool getRXSignalValue(HWBRIDGE::CANID msgID, HWBRIDGE::CANSIGNAL signalName, HWBRIDGE::CANSignalValue_t &signalValue);
37+
38+
// Switch CAN bus
39+
bool switchCANBus(HWBRIDGE::CANBUSID canBusID);
40+
41+
// Set CAN bus hw filter
42+
bool setFilter(HWBRIDGE::CANFILTER filter, CANFormat format = CANStandard,
43+
uint16_t mask = HWBRIDGE::ROVER_CANID_FILTER_MASK, int handle = 0);
44+
45+
// For diagnostic purposes
46+
uint32_t getNumStreamedMsgsReceived(void);
47+
uint32_t getNumOneShotMsgsReceived(void);
48+
uint32_t getNumStreamedMsgsSent(void);
49+
uint32_t getNumOneShotMsgsSent(void);
50+
51+
uint16_t getNumCANRXFaults(void);
52+
uint16_t getNumCANTXFaults(void);
53+
54+
private:
55+
static constexpr osPriority RX_POSTMAN_THREAD_PRIORITY = osPriorityRealtime;
56+
static constexpr osPriority RX_CLIENT_THREAD_PRIORITY = osPriorityAboveNormal;
57+
static constexpr osPriority TX_PROCESSOR_THREAD_PRIORITY = osPriorityBelowNormal;
58+
static constexpr std::chrono::milliseconds TX_INTERDELAY = 1ms;
59+
static constexpr std::chrono::milliseconds TX_PERIOD = 10ms;
60+
61+
void rxISR(void);
62+
void rxPostman(void);
63+
void rxClient(void);
64+
void txProcessor(void);
65+
66+
CANBus m_CANBus1;
67+
CANBus m_CANBus2;
68+
CANBus *m_activeCANBus;
69+
70+
Thread m_rxPostmanThread;
71+
Thread m_rxClientThread;
72+
Thread m_txProcessorThread;
73+
74+
Mutex m_rxMutex;
75+
Mutex m_txMutex;
76+
77+
Mail<CANMsg, 100> m_rxMailbox;
78+
Mail<CANMsg, 32> m_txMailboxOneShot;
79+
EventQueue m_rxEventQueue;
80+
81+
HWBRIDGE::CANMsgMap *m_rxMsgMap;
82+
HWBRIDGE::CANMsgMap *m_txMsgMap;
83+
84+
const CANMsg::CANMsgHandlerMap *m_rxOneShotMsgHandler;
85+
86+
// For diagnostic purposes
87+
uint32_t m_numStreamedMsgsReceived;
88+
uint32_t m_numOneShotMsgsReceived;
89+
uint32_t m_numStreamedMsgsSent;
90+
uint32_t m_numOneShotMsgsSent;
91+
92+
uint16_t m_numCANRXFaults;
93+
uint16_t m_numCANTXFaults;
94+
};

libs/can/include/CANMsg.h

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,15 @@ class CANMsg : public CANMessage {
88
private:
99
using CAN_Message::id;
1010

11+
static mbed_error_status_t defaultCANMsgHandler(void) {
12+
MBED_WARNING(MBED_MAKE_ERROR(MBED_MODULE_APPLICATION, MBED_ERROR_CODE_INVALID_ARGUMENT),
13+
"Invalid key to CANMsgHandlerMap");
14+
return MBED_ERROR_CODE_INVALID_ARGUMENT;
15+
}
16+
1117
public:
12-
using CANMsgHandler = mbed_error_status_t (*)(CANMsg &);
13-
using CANMsgHandlerMap =
14-
Utility::LookupTable<HWBRIDGE::CANID, CANMsg::CANMsgHandler,
15-
+[](CANMsg &) -> mbed_error_status_t { return MBED_ERROR_CODE_INVALID_ARGUMENT; }>;
18+
using CANMsgHandler = mbed_error_status_t (*)(void);
19+
using CANMsgHandlerMap = Utility::LookupTable<HWBRIDGE::CANID, CANMsg::CANMsgHandler, &defaultCANMsgHandler>;
1620

1721
template <class T>
1822
union CANPayload {
@@ -66,6 +70,14 @@ class CANMsg : public CANMessage {
6670
this->len = sizeof(T);
6771
}
6872

73+
/** Set the payload data with custom length
74+
*/
75+
template <class T>
76+
void setPayload(const T value, size_t length) {
77+
setPayload(value);
78+
len = length;
79+
}
80+
6981
/** Get the payload data
7082
*/
7183
template <class T>

libs/can/src/CANBus.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@ int CANBus::setFilter(HWBRIDGE::CANFILTER canFilter, CANFormat format, uint16_t
1010

1111
can_t *CANBus::getHandle() {
1212
return &_can;
13-
}
13+
}

0 commit comments

Comments
 (0)