MCU
Loading...
Searching...
No Matches
HytechCANInterface.h
Go to the documentation of this file.
1#ifndef HYTECHCANINTERFACE
2#define HYTECHCANINTERFACE
3
4#include <tuple>
5#include "FlexCAN_T4.h"
6
7#include "hytech.h"
8#include "HyTech_CAN.h"
9
10#include "InverterInterface.h"
11#include "DashboardInterface.h"
12#include "AMSInterface.h"
13#include "SABInterface.h"
14#include "VectornavInterface.h"
15#include "HT08_CASE_types.h"
16#include "MessageQueueDefine.h"
17/*
18 struct holding interfaces processed by process_ring_buffer()
19 FL = MC1
20 FR = MC2
21 RL = MC3
22 RR = MC4
23*/
24template <typename circular_buffer>
26{
35};
36
37// the goal with the can interface is that there exists a receive call that appends to a circular buffer
38// the processing of the receive queue happens on every iteration of the loop
39// in the processing of the receive call, all of the messages received get passed to their interfaces to be unpacked
40
41/* RX buffer for CAN1 */
42extern Circular_Buffer<uint8_t, (uint32_t)16, sizeof(CAN_message_t)>
44/* RX buffer for CAN2 */
45extern Circular_Buffer<uint8_t, (uint32_t)16, sizeof(CAN_message_t)>
47/* RX buffer for CAN3 */
48extern Circular_Buffer<uint8_t, (uint32_t)16, sizeof(CAN_message_t)>
50
51/* TX buffer for CAN1 */
53/* TX buffer for CAN2 */
55/* TX buffer for CAN3 */
57
58/* Receive callback function for CAN1 that pushes to circ. buffer */
59void on_can1_receive(const CAN_message_t &msg);
60/* Receive callback function for CAN2 that pushes to circ. buffer */
61void on_can2_receive(const CAN_message_t &msg);
62/* Receive callback function for CAN3 that pushes to circ. buffer */
63void on_can3_receive(const CAN_message_t &msg);
64
65// reads from receive buffer updating the current message frame from a specific receive buffer
66// TODO ensure that all of the repeated interfaces are at the correct IDs
67/*
68 Reads from the specified receive buffer and passes through messages to
69 the callback associated with the CAN message ID.
70*/
71template <typename BufferType, typename InterfaceType>
72void process_ring_buffer(BufferType &rx_buffer, const InterfaceType &interfaces, unsigned long curr_millis)
73{
74 // TODO switch to using the global CAN receive function from the generated CAN library
75 // if(rx_buffer.size() > 0){
76
77 // Serial.println(rx_buffer.size());
78 // }
79 while (rx_buffer.available())
80 {
81 CAN_message_t recvd_msg;
82 uint8_t buf[sizeof(CAN_message_t)];
83 rx_buffer.pop_front(buf, sizeof(CAN_message_t));
84 memmove(&recvd_msg, buf, sizeof(recvd_msg));
85 switch (recvd_msg.id)
86 {
87
88 case DASHBOARD_STATE_CANID:
89 interfaces.dash_interface->read(recvd_msg);
90 break;
91
92 // AMS msg receives
93 case ID_BMS_STATUS:
94 interfaces.ams_interface->retrieve_status_CAN(curr_millis, recvd_msg);
95 break;
96 case ID_BMS_TEMPERATURES:
97 interfaces.ams_interface->retrieve_temp_CAN(recvd_msg);
98 break;
99 case ID_BMS_VOLTAGES:
100 interfaces.ams_interface->retrieve_voltage_CAN(recvd_msg);
101 break;
102 case ID_EM_MEASUREMENT:
103 interfaces.ams_interface->retrieve_em_measurement_CAN(recvd_msg);
104 break;
105 case ACU_SHUNT_MEASUREMENTS_CANID:
106 interfaces.ams_interface->retrieve_current_shunt_CAN(recvd_msg);
107 break;
108
109 // MC status msgs
110 case ID_MC1_STATUS:
111 interfaces.front_left_inv->receive_status_msg(recvd_msg);
112 break;
113 case ID_MC2_STATUS:
114 interfaces.front_right_inv->receive_status_msg(recvd_msg);
115 break;
116 case ID_MC3_STATUS:
117 interfaces.rear_left_inv->receive_status_msg(recvd_msg);
118 break;
119 case ID_MC4_STATUS:
120 interfaces.rear_right_inv->receive_status_msg(recvd_msg);
121 break;
122
123 // MC temp msgs
124 case ID_MC1_TEMPS:
125 interfaces.front_left_inv->receive_temp_msg(recvd_msg);
126 break;
127 case ID_MC2_TEMPS:
128 interfaces.front_right_inv->receive_temp_msg(recvd_msg);
129 break;
130 case ID_MC3_TEMPS:
131 interfaces.rear_left_inv->receive_temp_msg(recvd_msg);
132 break;
133 case ID_MC4_TEMPS:
134 interfaces.rear_right_inv->receive_temp_msg(recvd_msg);
135 break;
136
137 // MC energy msgs
138 case ID_MC1_ENERGY:
139 interfaces.front_left_inv->receive_energy_msg(recvd_msg);
140 break;
141 case ID_MC2_ENERGY:
142 interfaces.front_right_inv->receive_energy_msg(recvd_msg);
143 break;
144 case ID_MC3_ENERGY:
145 interfaces.rear_left_inv->receive_energy_msg(recvd_msg);
146 break;
147 case ID_MC4_ENERGY:
148 interfaces.rear_right_inv->receive_energy_msg(recvd_msg);
149 break;
150
151 // SAB msgs
152 case SAB_SUSPENSION_CANID:
153 interfaces.sab_interface->retrieve_pots_and_load_cells_CAN(recvd_msg);
154 break;
155
156 // vector nav msgs
157 case VN_VEL_CANID:
158 interfaces.vn_interface->retrieve_velocity_CAN(recvd_msg);
159 break;
160 case VN_LINEAR_ACCEL_CANID:
161 interfaces.vn_interface->retrieve_linear_accel_CAN(recvd_msg);
162 break;
163 case VN_LINEAR_ACCEL_UNCOMP_CANID:
164 interfaces.vn_interface->retrieve_uncompLinear_accel_CAN(recvd_msg);
165 break;
166 case VN_YPR_CANID:
167 interfaces.vn_interface->retrieve_ypr_CAN(recvd_msg);
168 break;
169 case VN_LAT_LON_CANID:
170 interfaces.vn_interface->retrieve_lat_lon_CAN(recvd_msg);
171 break;
172 case VN_GPS_TIME_MSG_CANID:
173 interfaces.vn_interface->retrieve_gps_time_CAN(recvd_msg);
174 break;
175 case VN_STATUS_CANID:
176 interfaces.vn_interface->retrieve_vn_status_CAN(recvd_msg); // double check this
177 break;
178 case VN_ANGULAR_RATE_CANID:
179 interfaces.vn_interface->receive_ang_rates_CAN(recvd_msg);
180 break;
181 default:
182 break;
183 }
184 }
185}
186
187/*
188 Sends out all CAN messages on the specified buffer
189*/
190template <typename bufferType>
191void send_all_CAN_msgs(bufferType &buffer, FlexCAN_T4_Base *can_interface)
192{
193 CAN_message_t msg;
194 while (buffer.available())
195 {
196 CAN_message_t msg;
197 uint8_t buf[sizeof(CAN_message_t)];
198 buffer.pop_front(buf, sizeof(CAN_message_t));
199 memmove(&msg, buf, sizeof(msg));
200 can_interface->write(msg);
201 // delayMicroseconds(2500);
202 }
203}
204
205// TODO should this be here? my reasoning right now for this being here is that right now this interface is
206// only being used on MCU and is also tied integrally into other interfaces however the existence of this
207// function necessitates the CAN_MESSAGE_BUS type from generated simulink code which in theory we want to keep
208// segregated to the CASESystem. what we probably want our own CAN message type that we can use in the shared
209// data lib that is seperate from both. idk, not worth it for now just leaving this brain dump here.
210
215template <typename bufferType>
216void enqueue_matlab_msg(bufferType *msg_queue, const CAN_MESSAGE_BUS & structure)
217{
218 CAN_message_t can_msg = {};
219 can_msg.id = structure.ID;
220 can_msg.len = structure.Length;
221 // TODO ensure memory safety of this, but this should be fine
222 memmove( can_msg.buf, structure.Data, structure.Length );
223 uint8_t buf[sizeof(CAN_message_t)] = {};
224 memmove(buf, &can_msg, sizeof(CAN_message_t));
225 msg_queue->push_back(buf, sizeof(CAN_message_t));
226}
227
228
229#endif /* HYTECHCANINTERFACE */
Circular_Buffer< uint8_t,(uint32_t) 128, sizeof(CAN_message_t)> CANBufferType
this class is for interfacing with the AMS (accumulator management system)
Definition: AMSInterface.h:27
void on_can2_receive(const CAN_message_t &msg)
CANBufferType CAN1_txBuffer
Circular_Buffer< uint8_t,(uint32_t) 16, sizeof(CAN_message_t)> CAN3_rxBuffer
void process_ring_buffer(BufferType &rx_buffer, const InterfaceType &interfaces, unsigned long curr_millis)
void send_all_CAN_msgs(bufferType &buffer, FlexCAN_T4_Base *can_interface)
CANBufferType CAN3_txBuffer
Definition: main.cpp:187
void on_can3_receive(const CAN_message_t &msg)
void enqueue_matlab_msg(bufferType *msg_queue, const CAN_MESSAGE_BUS &structure)
message enque function for matlab generated CAN_MESSAGE_BUS type
Circular_Buffer< uint8_t,(uint32_t) 16, sizeof(CAN_message_t)> CAN1_rxBuffer
void on_can1_receive(const CAN_message_t &msg)
Circular_Buffer< uint8_t,(uint32_t) 16, sizeof(CAN_message_t)> CAN2_rxBuffer
CANBufferType CAN2_txBuffer
AMSInterface * ams_interface
InverterInterface< circular_buffer > * front_left_inv
DashboardInterface * dash_interface
VNInterface< circular_buffer > * vn_interface
InverterInterface< circular_buffer > * front_right_inv
InverterInterface< circular_buffer > * rear_left_inv
InverterInterface< circular_buffer > * rear_right_inv
SABInterface * sab_interface
CAN_message_t msg