Midnight Sun Firmware
Loading...
Searching...
No Matches
can_scheduler.h
1#pragma once
2
3/************************************************************************************************
4 * @file can_scheduler.h
5 *
6 * @brief Header file defining the CanScheduler class
7 *
8 * @date 2025-01-04
9 * @author Aryan Kashem
10 ************************************************************************************************/
11
14/* Standard library Headers */
15#include <atomic>
16#include <string>
17
18/* Inter-component Headers */
19#include <arpa/inet.h>
20#include <fcntl.h>
21#include <linux/can.h>
22#include <linux/can/bcm.h>
23#include <net/if.h>
24#include <sys/ioctl.h>
25#include <sys/socket.h>
26#include <sys/types.h>
27#include <unistd.h>
28
29/* Intra-component Headers */
30
45 private:
46 const std::string CAN_INTERFACE_NAME = "vcan0";
48 static const constexpr unsigned int FAST_CYCLE_SPEED_MS = 1U;
49 static const constexpr unsigned int MEDIUM_CYCLE_SPEED_MS = 100U;
50 static const constexpr unsigned int SLOW_CYCLE_SPEED_MS = 1000U;
52 static const constexpr unsigned int SLOW_CYCLE_BCM_ID = 0U;
53 static const constexpr unsigned int MEDIUM_CYCLE_BCM_ID = 1U;
54 static const constexpr unsigned int FAST_CYCLE_BCM_ID = 2U;
56 static const constexpr unsigned int NUM_FAST_CYCLE_MESSAGES = 3U;
57 static const constexpr unsigned int NUM_MEDIUM_CYCLE_MESSAGES = 5U;
58 static const constexpr unsigned int NUM_SLOW_CYCLE_MESSAGES = 5U;
59 static const constexpr unsigned int NUM_TOTAL_MESSAGES = 13U;
60 static const constexpr unsigned int MAX_MESSAGE_LENGTH = 8U;
62 static const constexpr unsigned int FAST_BMS_CARRIER_BATTERY_VT_FRAME_INDEX = 0U;
63 static const constexpr unsigned int FAST_CAN_COMMUNICATION_FAST_ONE_SHOT_MSG_FRAME_INDEX = 1U;
64 static const constexpr unsigned int FAST_CENTRE_CONSOLE_CC_PEDAL_FRAME_INDEX = 2U;
66 static const constexpr unsigned int MEDIUM_BMS_CARRIER_BATTERY_STATUS_FRAME_INDEX = 0U;
67 static const constexpr unsigned int MEDIUM_CAN_COMMUNICATION_MEDIUM_ONE_SHOT_MSG_FRAME_INDEX = 1U;
68 static const constexpr unsigned int MEDIUM_CENTRE_CONSOLE_CC_INFO_FRAME_INDEX = 2U;
69 static const constexpr unsigned int MEDIUM_CENTRE_CONSOLE_CC_STEERING_FRAME_INDEX = 3U;
70 static const constexpr unsigned int MEDIUM_CENTRE_CONSOLE_CC_REGEN_PERCENTAGE_FRAME_INDEX = 4U;
72 static const constexpr unsigned int SLOW_BMS_CARRIER_BATTERY_INFO_FRAME_INDEX = 0U;
73 static const constexpr unsigned int SLOW_BMS_CARRIER_AFE1_STATUS_FRAME_INDEX = 1U;
74 static const constexpr unsigned int SLOW_BMS_CARRIER_AFE2_STATUS_FRAME_INDEX = 2U;
75 static const constexpr unsigned int SLOW_BMS_CARRIER_AFE3_STATUS_FRAME_INDEX = 3U;
76 static const constexpr unsigned int SLOW_CAN_COMMUNICATION_SLOW_ONE_SHOT_MSG_FRAME_INDEX = 4U;
81 struct {
82 struct bcm_msg_head msg_head;
83 struct can_frame frame[NUM_FAST_CYCLE_MESSAGES];
85
89 struct {
90 struct bcm_msg_head msg_head;
91 struct can_frame frame[NUM_MEDIUM_CYCLE_MESSAGES];
93
97 struct {
98 struct bcm_msg_head msg_head;
99 struct can_frame frame[NUM_SLOW_CYCLE_MESSAGES];
101
103 std::atomic<bool> m_isConnected;
110 void scheduleCanMessages();
111
112 public:
117 CanScheduler();
118
124 void startCanScheduler();
129 void update_battery_status_fault(uint16_t fault_value);
134 void update_battery_status_fault_val(uint16_t fault_val_value);
139 void update_battery_status_aux_batt_v(uint16_t aux_batt_v_value);
144 void update_battery_status_afe_status(uint8_t afe_status_value);
149 void update_battery_vt_voltage(uint16_t voltage_value);
154 void update_battery_vt_current(uint16_t current_value);
159 void update_battery_vt_temperature(uint16_t temperature_value);
164 void update_battery_vt_batt_perc(uint16_t batt_perc_value);
169 void update_battery_info_fan1(uint8_t fan1_value);
174 void update_battery_info_fan2(uint8_t fan2_value);
179 void update_battery_info_max_cell_v(uint16_t max_cell_v_value);
184 void update_battery_info_min_cell_v(uint16_t min_cell_v_value);
189 void update_afe1_status_id(uint8_t id_value);
194 void update_afe1_status_temp(uint8_t temp_value);
199 void update_afe1_status_v1(uint16_t v1_value);
204 void update_afe1_status_v2(uint16_t v2_value);
209 void update_afe1_status_v3(uint16_t v3_value);
214 void update_afe2_status_id(uint8_t id_value);
219 void update_afe2_status_temp(uint8_t temp_value);
224 void update_afe2_status_v1(uint16_t v1_value);
229 void update_afe2_status_v2(uint16_t v2_value);
234 void update_afe2_status_v3(uint16_t v3_value);
239 void update_afe3_status_id(uint8_t id_value);
244 void update_afe3_status_temp(uint8_t temp_value);
249 void update_afe3_status_v1(uint16_t v1_value);
254 void update_afe3_status_v2(uint16_t v2_value);
259 void update_afe3_status_v3(uint16_t v3_value);
264 void update_fast_one_shot_msg_sig1(uint16_t sig1_value);
269 void update_fast_one_shot_msg_sig2(uint16_t sig2_value);
274 void update_medium_one_shot_msg_sig1(uint16_t sig1_value);
279 void update_medium_one_shot_msg_sig2(uint16_t sig2_value);
284 void update_slow_one_shot_msg_sig1(uint16_t sig1_value);
289 void update_slow_one_shot_msg_sig2(uint16_t sig2_value);
294 void update_cc_pedal_throttle_output(uint32_t throttle_output_value);
299 void update_cc_pedal_brake_output(uint8_t brake_output_value);
304 void update_cc_info_target_velocity(uint32_t target_velocity_value);
309 void update_cc_info_drive_state(uint8_t drive_state_value);
314 void update_cc_info_cruise_control(uint8_t cruise_control_value);
319 void update_cc_info_regen_braking(uint8_t regen_braking_value);
324 void update_cc_info_hazard_enabled(uint8_t hazard_enabled_value);
329 void update_cc_steering_input_cc(uint8_t input_cc_value);
334 void update_cc_steering_input_lights(uint8_t input_lights_value);
339 void update_cc_regen_percentage_percent(uint32_t percent_value);
340};
341
Class that handles message scheduling over a SocketCAN interface.
Definition: can_scheduler.h:44
void update_afe3_status_v1(uint16_t v1_value)
Update the CAN value for afe3_status v1.
Definition: can_scheduler.cc:447
void update_battery_vt_current(uint16_t current_value)
Update the CAN value for battery_vt current.
Definition: can_scheduler.cc:208
static const constexpr unsigned int MEDIUM_CYCLE_SPEED_MS
Definition: can_scheduler.h:49
void update_medium_one_shot_msg_sig1(uint16_t sig1_value)
Update the CAN value for medium_one_shot_msg sig1.
Definition: can_scheduler.cc:512
void update_afe3_status_temp(uint8_t temp_value)
Update the CAN value for afe3_status temp.
Definition: can_scheduler.cc:435
void update_cc_steering_input_cc(uint8_t input_cc_value)
Update the CAN value for cc_steering input_cc.
Definition: can_scheduler.cc:654
CanScheduler()
Constructs a CanScheduler object.
Definition: can_scheduler.cc:22
static const constexpr unsigned int SLOW_CAN_COMMUNICATION_SLOW_ONE_SHOT_MSG_FRAME_INDEX
Definition: can_scheduler.h:76
void update_battery_status_fault(uint16_t fault_value)
Update the CAN value for battery_status fault.
Definition: can_scheduler.cc:144
void update_afe2_status_v1(uint16_t v1_value)
Update the CAN value for afe2_status v1.
Definition: can_scheduler.cc:384
static const constexpr unsigned int SLOW_BMS_CARRIER_AFE1_STATUS_FRAME_INDEX
Definition: can_scheduler.h:73
void update_battery_status_aux_batt_v(uint16_t aux_batt_v_value)
Update the CAN value for battery_status aux_batt_v.
Definition: can_scheduler.cc:170
void update_battery_vt_batt_perc(uint16_t batt_perc_value)
Update the CAN value for battery_vt batt_perc.
Definition: can_scheduler.cc:234
std::atomic< bool > m_isConnected
Definition: can_scheduler.h:103
void update_battery_info_max_cell_v(uint16_t max_cell_v_value)
Update the CAN value for battery_info max_cell_v.
Definition: can_scheduler.cc:271
void update_medium_one_shot_msg_sig2(uint16_t sig2_value)
Update the CAN value for medium_one_shot_msg sig2.
Definition: can_scheduler.cc:525
void update_afe1_status_v2(uint16_t v2_value)
Update the CAN value for afe1_status v2.
Definition: can_scheduler.cc:334
void update_afe3_status_id(uint8_t id_value)
Update the CAN value for afe3_status id.
Definition: can_scheduler.cc:423
void update_afe2_status_temp(uint8_t temp_value)
Update the CAN value for afe2_status temp.
Definition: can_scheduler.cc:372
const std::string CAN_INTERFACE_NAME
Definition: can_scheduler.h:46
void update_afe1_status_v3(uint16_t v3_value)
Update the CAN value for afe1_status v3.
Definition: can_scheduler.cc:347
static const constexpr unsigned int SLOW_CYCLE_BCM_ID
Definition: can_scheduler.h:52
static const constexpr unsigned int MEDIUM_CAN_COMMUNICATION_MEDIUM_ONE_SHOT_MSG_FRAME_INDEX
Definition: can_scheduler.h:67
void update_cc_info_hazard_enabled(uint8_t hazard_enabled_value)
Update the CAN value for cc_info hazard_enabled.
Definition: can_scheduler.cc:642
void update_battery_info_fan2(uint8_t fan2_value)
Update the CAN value for battery_info fan2.
Definition: can_scheduler.cc:259
static const constexpr unsigned int SLOW_CYCLE_SPEED_MS
Definition: can_scheduler.h:50
void update_battery_status_fault_val(uint16_t fault_val_value)
Update the CAN value for battery_status fault_val.
Definition: can_scheduler.cc:157
void update_battery_info_min_cell_v(uint16_t min_cell_v_value)
Update the CAN value for battery_info min_cell_v.
Definition: can_scheduler.cc:284
void update_cc_info_regen_braking(uint8_t regen_braking_value)
Update the CAN value for cc_info regen_braking.
Definition: can_scheduler.cc:630
static const constexpr unsigned int FAST_CYCLE_BCM_ID
Definition: can_scheduler.h:54
static const constexpr unsigned int MEDIUM_BMS_CARRIER_BATTERY_STATUS_FRAME_INDEX
Definition: can_scheduler.h:66
struct can_frame frame[NUM_FAST_CYCLE_MESSAGES]
Definition: can_scheduler.h:83
static const constexpr unsigned int NUM_TOTAL_MESSAGES
Definition: can_scheduler.h:59
static const constexpr unsigned int SLOW_BMS_CARRIER_AFE3_STATUS_FRAME_INDEX
Definition: can_scheduler.h:75
void update_cc_info_cruise_control(uint8_t cruise_control_value)
Update the CAN value for cc_info cruise_control.
Definition: can_scheduler.cc:618
void update_cc_info_drive_state(uint8_t drive_state_value)
Update the CAN value for cc_info drive_state.
Definition: can_scheduler.cc:606
void update_afe2_status_v3(uint16_t v3_value)
Update the CAN value for afe2_status v3.
Definition: can_scheduler.cc:410
static const constexpr unsigned int FAST_CAN_COMMUNICATION_FAST_ONE_SHOT_MSG_FRAME_INDEX
Definition: can_scheduler.h:63
static const constexpr unsigned int NUM_SLOW_CYCLE_MESSAGES
Definition: can_scheduler.h:58
void update_cc_pedal_throttle_output(uint32_t throttle_output_value)
Update the CAN value for cc_pedal throttle_output.
Definition: can_scheduler.cc:564
void update_cc_info_target_velocity(uint32_t target_velocity_value)
Update the CAN value for cc_info target_velocity.
Definition: can_scheduler.cc:591
static const constexpr unsigned int SLOW_BMS_CARRIER_AFE2_STATUS_FRAME_INDEX
Definition: can_scheduler.h:74
static const constexpr unsigned int NUM_FAST_CYCLE_MESSAGES
Definition: can_scheduler.h:56
void update_cc_steering_input_lights(uint8_t input_lights_value)
Update the CAN value for cc_steering input_lights.
Definition: can_scheduler.cc:666
static const constexpr unsigned int MEDIUM_CENTRE_CONSOLE_CC_REGEN_PERCENTAGE_FRAME_INDEX
Definition: can_scheduler.h:70
void update_slow_one_shot_msg_sig2(uint16_t sig2_value)
Update the CAN value for slow_one_shot_msg sig2.
Definition: can_scheduler.cc:551
static const constexpr unsigned int FAST_BMS_CARRIER_BATTERY_VT_FRAME_INDEX
Definition: can_scheduler.h:62
static const constexpr unsigned int NUM_MEDIUM_CYCLE_MESSAGES
Definition: can_scheduler.h:57
void update_afe2_status_v2(uint16_t v2_value)
Update the CAN value for afe2_status v2.
Definition: can_scheduler.cc:397
void update_battery_status_afe_status(uint8_t afe_status_value)
Update the CAN value for battery_status afe_status.
Definition: can_scheduler.cc:183
struct CanScheduler::@14 canSlowCycleBCM
Slow cycle Broadcast Manager message for the Linux Kernel.
struct bcm_msg_head msg_head
Definition: can_scheduler.h:82
void update_afe2_status_id(uint8_t id_value)
Update the CAN value for afe2_status id.
Definition: can_scheduler.cc:360
int m_bcmCanSocket
Definition: can_scheduler.h:102
void update_battery_info_fan1(uint8_t fan1_value)
Update the CAN value for battery_info fan1.
Definition: can_scheduler.cc:247
void startCanScheduler()
Starts the CAN scheduler and sets all messages to 0. Must only be called once.
Definition: can_scheduler.cc:116
static const constexpr unsigned int MEDIUM_CENTRE_CONSOLE_CC_STEERING_FRAME_INDEX
Definition: can_scheduler.h:69
void update_fast_one_shot_msg_sig1(uint16_t sig1_value)
Update the CAN value for fast_one_shot_msg sig1.
Definition: can_scheduler.cc:486
static const constexpr unsigned int MEDIUM_CENTRE_CONSOLE_CC_INFO_FRAME_INDEX
Definition: can_scheduler.h:68
static const constexpr unsigned int SLOW_BMS_CARRIER_BATTERY_INFO_FRAME_INDEX
Definition: can_scheduler.h:72
void update_afe1_status_v1(uint16_t v1_value)
Update the CAN value for afe1_status v1.
Definition: can_scheduler.cc:321
void update_slow_one_shot_msg_sig1(uint16_t sig1_value)
Update the CAN value for slow_one_shot_msg sig1.
Definition: can_scheduler.cc:538
void update_cc_regen_percentage_percent(uint32_t percent_value)
Update the CAN value for cc_regen_percentage percent.
Definition: can_scheduler.cc:678
void scheduleCanMessages()
Schedules all CAN data by updating the Broacast Manager socket.
Definition: can_scheduler.cc:27
static const constexpr unsigned int MEDIUM_CYCLE_BCM_ID
Definition: can_scheduler.h:53
void update_battery_vt_voltage(uint16_t voltage_value)
Update the CAN value for battery_vt voltage.
Definition: can_scheduler.cc:195
void update_battery_vt_temperature(uint16_t temperature_value)
Update the CAN value for battery_vt temperature.
Definition: can_scheduler.cc:221
void update_afe1_status_id(uint8_t id_value)
Update the CAN value for afe1_status id.
Definition: can_scheduler.cc:297
static const constexpr unsigned int FAST_CENTRE_CONSOLE_CC_PEDAL_FRAME_INDEX
Definition: can_scheduler.h:64
static const constexpr unsigned int MAX_MESSAGE_LENGTH
Definition: can_scheduler.h:60
void update_afe1_status_temp(uint8_t temp_value)
Update the CAN value for afe1_status temp.
Definition: can_scheduler.cc:309
void update_fast_one_shot_msg_sig2(uint16_t sig2_value)
Update the CAN value for fast_one_shot_msg sig2.
Definition: can_scheduler.cc:499
static const constexpr unsigned int FAST_CYCLE_SPEED_MS
Definition: can_scheduler.h:48
struct CanScheduler::@12 canFastCycleBCM
Fast cycle Broadcast Manager message for the Linux Kernel.
struct CanScheduler::@13 canMediumCycleBCM
Medium cycle Broadcast Manager message for the Linux Kernel.
void update_afe3_status_v2(uint16_t v2_value)
Update the CAN value for afe3_status v2.
Definition: can_scheduler.cc:460
void update_cc_pedal_brake_output(uint8_t brake_output_value)
Update the CAN value for cc_pedal brake_output.
Definition: can_scheduler.cc:579
void update_afe3_status_v3(uint16_t v3_value)
Update the CAN value for afe3_status v3.
Definition: can_scheduler.cc:473