From 33c0b3ecf99cd34b12454e52a787bc522303cf5d Mon Sep 17 00:00:00 2001 From: CL16gtgh Date: Tue, 6 Feb 2024 00:24:40 -0500 Subject: [PATCH] Fixed MCU and Telemetry interface compile issue --- include/MCU_rev15_defs.h | 4 ++ .../include/AnalogSensorsInterface.h | 5 +- lib/interfaces/include/MCUInterface.h | 68 +++++++++---------- lib/interfaces/include/ORBIS_BR10.h | 2 +- .../include/SteeringEncoderInterface.h | 6 +- lib/interfaces/include/TelemetryInterface.h | 15 ++-- lib/interfaces/src/MCUInterface.cpp | 31 +++++---- lib/interfaces/src/TelemetryInterface.cpp | 24 +++++-- lib/systems/include/SteeringSystem.h | 4 +- lib/systems/src/SteeringSystem.cpp | 4 +- src/main.cpp | 45 ++++++------ 11 files changed, 117 insertions(+), 91 deletions(-) diff --git a/include/MCU_rev15_defs.h b/include/MCU_rev15_defs.h index 9a7bc44fc..eb6308766 100644 --- a/include/MCU_rev15_defs.h +++ b/include/MCU_rev15_defs.h @@ -24,4 +24,8 @@ const unsigned long SETUP_PRESENT_ACTION_INTERVAL = 5000; const unsigned long BUZZER_ON_INTERVAL = 2000; const unsigned long INVERTER_ENABLING_TIMEOUT_INTERVAL = 5000; +// Communication speeds +const unsigned long INV_CAN_BAUDRATE = 500000; +const unsigned long TELEM_CAN_BAUDRATE = 500000; + #endif /* __MCU15_H__ */ \ No newline at end of file diff --git a/lib/interfaces/include/AnalogSensorsInterface.h b/lib/interfaces/include/AnalogSensorsInterface.h index d973244dc..7ad700a46 100644 --- a/lib/interfaces/include/AnalogSensorsInterface.h +++ b/lib/interfaces/include/AnalogSensorsInterface.h @@ -80,7 +80,10 @@ class AnalogMultiSensor /// @brief Used by systems to get data out of this device when it's self-actualizing sampling & conversion. /// @return Const ref to last data conversion. - const AnalogConversionPacket_s& get(); + const AnalogConversionPacket_s& get() + { + return data; + } /// @brief Performs unit conversions on all channels void convert() diff --git a/lib/interfaces/include/MCUInterface.h b/lib/interfaces/include/MCUInterface.h index 9a8637356..5b976f578 100644 --- a/lib/interfaces/include/MCUInterface.h +++ b/lib/interfaces/include/MCUInterface.h @@ -4,9 +4,10 @@ #include #include "FlexCAN_T4.h" #include "HyTech_CAN.h" -#include "hytech.h" +// #include "hytech.h" +#include "MessageQueueDefine.h" #include "SysClock.h" -#include "MCUStateMachine.h" +// #include "MCUStateMachine.h" const int DEFAULT_BMS_OK_READ = 5; // SHDN_D_READ const int DEFAULT_IMD_OK_READ = 4; // SHDN_C_READ @@ -52,25 +53,24 @@ class MCUInterface int pin_inv_24V_en_; public: - MCUInterface(){}; - // MCUInterface(message_queue *msg_output_queue, int bms_pin, int imd_pin, int bspd_pin, int sw_ok_pin, int bots_ok_pin, int brake_light_pin, int inv_pin, int inv_24V_pin): - // msg_queue_(msg_output_queue), - // pin_bms_ok_read_(bms_pin), - // pin_imd_ok_read_(imd_pin), - // pin_bspd_ok_read_(bspd_pin), - // pin_software_ok_read_(sw_ok_pin), - // pin_bots_ok_read_(bots_ok_pin), - // pin_brake_light_ctrl_(brake_light_pin), - // pin_inv_en_(inv_pin), - // pin_inv_24V_en_(inv_24V_pin) - // { - // // Set pin mode - // pinMode(pin_brake_light_ctrl_, OUTPUT); - // pinMode(pin_inv_en_, OUTPUT); - // pinMode(pin_inv_24V_en_, OUTPUT); - // }; - // MCUInterface(message_queue *msg_output_queue): - // MCUInterface(msg_output_queue, DEFAULT_BMS_OK_READ, DEFAULT_IMD_OK_READ, DEFAULT_BSPD_OK_READ, DEFAULT_SOFTWARE_OK_READ, DEFAULT_BOTS_OK_READ, DEFAULT_BRAKE_LIGHT_CTRL, DEFAULT_INVERTER_EN, DEFAULT_INVERTER_24V_EN) {}; + MCUInterface(CANBufferType *msg_output_queue, int bms_pin, int imd_pin, int bspd_pin, int sw_ok_pin, int bots_ok_pin, int brake_light_pin, int inv_pin, int inv_24V_pin): + msg_queue_(msg_output_queue), + pin_bms_ok_read_(bms_pin), + pin_imd_ok_read_(imd_pin), + pin_bspd_ok_read_(bspd_pin), + pin_software_ok_read_(sw_ok_pin), + pin_bots_ok_read_(bots_ok_pin), + pin_brake_light_ctrl_(brake_light_pin), + pin_inv_en_(inv_pin), + pin_inv_24V_en_(inv_24V_pin) + { + // Set pin mode + pinMode(pin_brake_light_ctrl_, OUTPUT); + pinMode(pin_inv_en_, OUTPUT); + pinMode(pin_inv_24V_en_, OUTPUT); + }; + MCUInterface(CANBufferType *msg_output_queue): + MCUInterface(msg_output_queue, DEFAULT_BMS_OK_READ, DEFAULT_IMD_OK_READ, DEFAULT_BSPD_OK_READ, DEFAULT_SOFTWARE_OK_READ, DEFAULT_BOTS_OK_READ, DEFAULT_BRAKE_LIGHT_CTRL, DEFAULT_INVERTER_EN, DEFAULT_INVERTER_24V_EN) {}; /* Initialize shutdown circuit input readings */ void init(); @@ -88,7 +88,7 @@ class MCUInterface /* Update MCU_status CAN (main loop) */ // State machine - void update_mcu_status_CAN_fsm(CAR_STATE fsm_state); + // void update_mcu_status_CAN_fsm(CAR_STATE fsm_state); // Systems void update_mcu_status_CAN_drivetrain(bool has_error); void update_mcu_status_CAN_safety(bool is_ok); @@ -100,20 +100,20 @@ class MCUInterface void update_mcu_status_CAN_dashboard(bool is_pressed); /* Enqueue MCU_status CAN */ - void enqueue_CAN_mcu_status(CAN_message_t &msg); + void enqueue_CAN_mcu_status(); /* Tick SysClock to send CAN at 10Hz */ - void tick( - const SysTick_s &tick, - CAR_STATE fsm_state, - bool inv_has_error, - bool software_is_ok, - // TCMux return - bool buzzer_is_on, - // Pedal system return - bool pack_charge_is_critical, - bool button_is_pressed - ); + // void tick( + // const SysTick_s &tick, + // CAR_STATE fsm_state, + // bool inv_has_error, + // bool software_is_ok, + // // TCMux return + // bool buzzer_is_on, + // // Pedal system return + // bool pack_charge_is_critical, + // bool button_is_pressed + // ); }; diff --git a/lib/interfaces/include/ORBIS_BR10.h b/lib/interfaces/include/ORBIS_BR10.h index 26720e14f..d1ed3937a 100644 --- a/lib/interfaces/include/ORBIS_BR10.h +++ b/lib/interfaces/include/ORBIS_BR10.h @@ -17,7 +17,7 @@ const uint16_t ORBIS_BR10_BITMASK_DETAILED_DIST_FAR = (0b1 << 6); const uint16_t ORBIS_BR10_BITMASK_DETAILED_DIST_NEAR = (0b1 << 7); const uint16_t ORBIS_BR10_BITMASK_DETAILED = (ORBIS_BR10_BITMASK_DETAILED_COUNTER_ERROR | ORBIS_BR10_BITMASK_DETAILED_SPEED_HIGH | ORBIS_BR10_BITMASK_DETAILED_TEMP_RANGE | ORBIS_BR10_BITMASK_DETAILED_DIST_FAR | ORBIS_BR10_BITMASK_DETAILED_DIST_NEAR); -class OrbisBR10 : SteeringEncoderInterface +class OrbisBR10 : public SteeringEncoderInterface { private: // Data diff --git a/lib/interfaces/include/SteeringEncoderInterface.h b/lib/interfaces/include/SteeringEncoderInterface.h index 602f98f02..98035968e 100644 --- a/lib/interfaces/include/SteeringEncoderInterface.h +++ b/lib/interfaces/include/SteeringEncoderInterface.h @@ -21,13 +21,13 @@ class SteeringEncoderInterface public: // Functions /// @brief Commands the underlying steering sensor to sample and hold the result - void sample(); + virtual void sample(); /// @brief Calculate steering angle and whether result is in sensor's defined bounds. DOES NOT SAMPLE. /// @return Calculated steering angle in degrees, upperSteeringStatus_s - SteeringEncoderConversion_s convert(); + virtual SteeringEncoderConversion_s convert(); /// @brief Set the upper steering sensor's offset. 0 degrees should be centered. /// @param newOffset - void setOffset(float newOffset); + virtual void setOffset(float newOffset); }; #endif /* __UPPERSTEERINGSENSOR_H__ */ diff --git a/lib/interfaces/include/TelemetryInterface.h b/lib/interfaces/include/TelemetryInterface.h index 717a15e6f..73508f046 100644 --- a/lib/interfaces/include/TelemetryInterface.h +++ b/lib/interfaces/include/TelemetryInterface.h @@ -1,9 +1,11 @@ #ifndef __TELEMETRY_INTERFACE_H__ #define __TELEMETRY_INTERFACE_H__ +#include "stdint.h" #include "FlexCAN_T4.h" #include "HyTech_CAN.h" -#include "hytech.h" +// #include "hytech.h" +#include "MessageQueueDefine.h" #include "SysClock.h" #include "AnalogSensorsInterface.h" #include "SteeringEncoderInterface.h" @@ -18,12 +20,11 @@ class TelemetryInterface MCU_rear_potentiometers mcu_rear_potentiometers_; MCU_analog_readings mcu_analog_readings_; /* CAN Tx buffer */ - // CANBufferType *msg_queue_; + CANBufferType *msg_queue_; public: - TelemetryInterface(){}; - // TelemetryInterface(message_queue *msg_output_queue): - // msg_queue_(msg_output_queue) {}; + TelemetryInterface(CANBufferType *msg_output_queue): + msg_queue_(msg_output_queue) {}; /* Update CAN messages (main loop) */ // Interfaces @@ -60,8 +61,8 @@ class TelemetryInterface void tick( const SysTick_s &tick, const AnalogConversionPacket_s<8> &adc1, - const AnalogConversionPacket_s<4> &adc2, - const AnalogConversionPacket_s<4> &adc3, + const AnalogConversionPacket_s<8> &adc2, + const AnalogConversionPacket_s<8> &adc3, const SteeringEncoderConversion_s &encoder ); diff --git a/lib/interfaces/src/MCUInterface.cpp b/lib/interfaces/src/MCUInterface.cpp index cd8e8ac4e..a291f2206 100644 --- a/lib/interfaces/src/MCUInterface.cpp +++ b/lib/interfaces/src/MCUInterface.cpp @@ -76,15 +76,17 @@ bool MCUInterface::imd_ok_is_high() { /* Send CAN message */ // MCU status -// void MCUInterface::enqueue_CAN_mcu_status() { +void MCUInterface::enqueue_CAN_mcu_status() { -// CAN_message_t msg; -// mcu_status_.write(msg.buf); -// msg.id = ID_MCU_STATUS; -// msg.len = sizeof(mcu_status_); + CAN_message_t msg; + mcu_status_.write(msg.buf); + msg.id = ID_MCU_STATUS; + msg.len = sizeof(mcu_status_); + uint8_t buf[sizeof(CAN_message_t)]; + memmove(buf, &msg, sizeof(msg)); -// msg_queue_->push_back(msg, sizeof(CAN_message_t)); -// } + msg_queue_->push_back(buf, sizeof(CAN_message_t)); +} /* Update MCU_status CAN */ // MCUInterface @@ -103,12 +105,12 @@ void MCUInterface::update_mcu_status_CAN() { // Main loop // State machine -void MCUInterface::update_mcu_status_CAN_fsm(CAR_STATE fsm_state) { - // State machine returns struct in main loop - // fsm.get_state() - // might not be compatible anymore, using new states - // mcu_status_.set_state(fsm_state); -} +// void MCUInterface::update_mcu_status_CAN_fsm(CAR_STATE fsm_state) { +// // State machine returns struct in main loop +// // fsm.get_state() +// // might not be compatible anymore, using new states +// // mcu_status_.set_state(fsm_state); +// } //DriveTrain void MCUInterface::update_mcu_status_CAN_drivetrain(bool has_error) { // Drivetrain returns struct in main loop @@ -138,7 +140,8 @@ void MCUInterface::update_mcu_status_CAN_TCMux() { void MCUInterface::update_mcu_status_CAN_dashboard(bool is_pressed) { // DashboardInterface (?) returns struct in main loop // dash.lauchControlButtonPressed() - // mcu_status_.toggle_launch_ctrl_active(is_pressed); + if (is_pressed) + mcu_status_.toggle_launch_ctrl_active(); } // BuzzerSystem void MCUInterface::update_mcu_status_CAN_buzzer(bool is_on) { diff --git a/lib/interfaces/src/TelemetryInterface.cpp b/lib/interfaces/src/TelemetryInterface.cpp index 8af093aea..ad6197080 100644 --- a/lib/interfaces/src/TelemetryInterface.cpp +++ b/lib/interfaces/src/TelemetryInterface.cpp @@ -8,8 +8,10 @@ void TelemetryInterface::enqueue_CAN_mcu_pedal_readings() { mcu_pedal_readings_.write(msg.buf); msg.id = ID_MCU_PEDAL_READINGS; msg.len = sizeof(mcu_pedal_readings_); + uint8_t buf[sizeof(CAN_message_t)]; + memmove(buf, &msg, sizeof(msg)); - // msg_queue_->push_back(msg, sizeof(CAN_message_t)); + msg_queue_->push_back(buf, sizeof(CAN_message_t)); } // Loadcell readings void TelemetryInterface::enqueue_CAN_mcu_load_cells() { @@ -18,8 +20,10 @@ void TelemetryInterface::enqueue_CAN_mcu_load_cells() { mcu_load_cells_.write(msg.buf); msg.id = ID_MCU_LOAD_CELLS; msg.len = sizeof(mcu_load_cells_); + uint8_t buf[sizeof(CAN_message_t)]; + memmove(buf, &msg, sizeof(msg)); - // msg_queue_->push_back(msg, sizeof(CAN_message_t)); + msg_queue_->push_back(buf, sizeof(CAN_message_t)); } // Suspension potentiometers // Front @@ -29,8 +33,10 @@ void TelemetryInterface::enqueue_CAN_mcu_front_potentiometers() { mcu_front_potentiometers_.write(msg.buf); msg.id = ID_MCU_FRONT_POTS; msg.len = sizeof(mcu_front_potentiometers_); + uint8_t buf[sizeof(CAN_message_t)]; + memmove(buf, &msg, sizeof(msg)); - // msg_queue_->push_back(msg, sizeof(CAN_message_t)); + msg_queue_->push_back(buf, sizeof(CAN_message_t)); } // Rear void TelemetryInterface::enqueue_CAN_mcu_rear_potentiometers() { @@ -39,8 +45,10 @@ void TelemetryInterface::enqueue_CAN_mcu_rear_potentiometers() { mcu_rear_potentiometers_.write(msg.buf); msg.id = ID_MCU_REAR_POTS; msg.len = sizeof(mcu_rear_potentiometers_); + uint8_t buf[sizeof(CAN_message_t)]; + memmove(buf, &msg, sizeof(msg)); - // msg_queue_->push_back(msg, sizeof(CAN_message_t)); + msg_queue_->push_back(buf, sizeof(CAN_message_t)); } // Analog sensor readings void TelemetryInterface::enqueue_CAN_mcu_analog_readings() { @@ -49,8 +57,10 @@ void TelemetryInterface::enqueue_CAN_mcu_analog_readings() { mcu_analog_readings_.write(msg.buf); msg.id = ID_MCU_ANALOG_READINGS; msg.len = sizeof(mcu_analog_readings_); + uint8_t buf[sizeof(CAN_message_t)]; + memmove(buf, &msg, sizeof(msg)); - // msg_queue_->push_back(msg, sizeof(CAN_message_t)); + msg_queue_->push_back(buf, sizeof(CAN_message_t)); } /* Update CAN messages */ @@ -102,8 +112,8 @@ void TelemetryInterface::update_analog_readings_CAN_msg(const SteeringEncoderCon /* Tick SysClock */ void TelemetryInterface::tick(const SysTick_s &tick, const AnalogConversionPacket_s<8> &adc1, - const AnalogConversionPacket_s<4> &adc2, - const AnalogConversionPacket_s<4> &adc3, + const AnalogConversionPacket_s<8> &adc2, + const AnalogConversionPacket_s<8> &adc3, const SteeringEncoderConversion_s &encoder) { if (tick.triggers.trigger50) { diff --git a/lib/systems/include/SteeringSystem.h b/lib/systems/include/SteeringSystem.h index ce3cf8a39..6fc7fef68 100644 --- a/lib/systems/include/SteeringSystem.h +++ b/lib/systems/include/SteeringSystem.h @@ -31,11 +31,11 @@ struct SteeringSystemData_s class SteeringSystem { private: - SteeringEncoderInterface& primarySensor_; + SteeringEncoderInterface *primarySensor_; SteeringEncoderConversion_s primaryConversion_; SteeringSystemData_s data_; public: - SteeringSystem(SteeringEncoderInterface &primarySensor) : primarySensor_(primarySensor) {} + SteeringSystem(SteeringEncoderInterface *primarySensor) : primarySensor_(primarySensor) {} /// @brief Computes steering angle and status of the steering system. /// @param secondaryAngle The computed steering angle as reported by the secondary steering sensor. diff --git a/lib/systems/src/SteeringSystem.cpp b/lib/systems/src/SteeringSystem.cpp index 51765f32e..924603ef0 100644 --- a/lib/systems/src/SteeringSystem.cpp +++ b/lib/systems/src/SteeringSystem.cpp @@ -8,8 +8,8 @@ void SteeringSystem::tick(const SysTick_s &tick, const AnalogConversion_s &secon if (tick.triggers.trigger100) { // Poll upper steering sensor - primarySensor_.sample(); - primaryConversion_ = primarySensor_.convert(); + primarySensor_->sample(); + primaryConversion_ = primarySensor_->convert(); // Compute internal state diff --git a/src/main.cpp b/src/main.cpp index 16d2d247f..c5da9f596 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -50,8 +50,8 @@ OrbisBR10 steering1(STEERING_SERIAL); DashboardInterface dashboard(&CAN2_txBuffer); AMSInterface ams_interface(SOFTWARE_OK); WatchdogInterface wd_interface(WATCHDOG_INPUT); -// MCUInterface main_ecu(&CAN3_txBuffer); -// TelemetryInterface telem_interface(&CAN3_txBuffer); +MCUInterface main_ecu(&CAN3_txBuffer); +TelemetryInterface telem_interface(&CAN3_txBuffer); using InverterInterfaceType = InverterInterface; InverterInterfaceType fl_inv(&CAN2_txBuffer, ID_MC1_SETPOINTS_COMMAND); @@ -65,7 +65,7 @@ SysClock sys_clock; BuzzerController buzzer(BUZZER_ON_INTERVAL); SafetySystem safety_system(&ams_interface, &wd_interface); // Tie ams and wd interface to safety system (by pointers) PedalsSystem pedals; -// SteeringSystem steering_system(steering1); // Unify member reference and pointers? tied by reference in this case +SteeringSystem steering_system(&steering1); // Unify member reference and pointers? tied by reference in this case using DrivetrainSystemType = DrivetrainSystem; auto drivetrain = DrivetrainSystemType({&fl_inv, &fr_inv, &rl_inv, &rr_inv}, INVERTER_ENABLING_TIMEOUT_INTERVAL); // Tie inverter interfaces to drivetrain system (by pointers) /* @@ -89,6 +89,7 @@ SysTick_s curr_tick; /* Function declarations */ /* CAN functions */ +void update_and_enqueue_all_CAN(); /* External value readings */ void sample_all_external_readings(); /* Process all readings */ @@ -100,9 +101,9 @@ void setup() { curr_tick = sys_clock.tick(micros()); /* Initialize interface */ - // main_ecu.init(); // pin mode, initial shutdown circuit readings, - wd_interface.init(curr_tick); // pin mode, initialize wd kick time - ams_interface.init(curr_tick); // pin mode, initialize last heartbeat time + main_ecu.init(); // initial shutdown circuit readings, + wd_interface.init(curr_tick); // initialize wd kick time + ams_interface.init(curr_tick); // initialize last heartbeat time /* Initialize system */ safety_system.init(); // write software_ok high, write wd_input high, set software ok state true @@ -130,7 +131,7 @@ void loop() { process_all_value_readings(); /* Update and enqueue CAN messages */ - // update_and_enqueue_all_CAN(); + update_and_enqueue_all_CAN(); /* Inverter procedure before entering state machine */ // Drivetrain check if inverters have error @@ -146,7 +147,7 @@ void loop() { void init_all_CAN() { // Inverter CAN line INV_CAN.begin(); - INV_CAN.setBaudRate(500000); + INV_CAN.setBaudRate(INV_CAN_BAUDRATE); INV_CAN.setMaxMB(16); INV_CAN.enableFIFO(); INV_CAN.enableFIFOInterrupt(); @@ -155,7 +156,7 @@ void init_all_CAN() { // Telemetry CAN line TELEM_CAN.begin(); - TELEM_CAN.setBaudRate(500000); + TELEM_CAN.setBaudRate(TELEM_CAN_BAUDRATE); TELEM_CAN.setMaxMB(16); TELEM_CAN.enableFIFO(); TELEM_CAN.enableFIFOInterrupt(); @@ -179,25 +180,29 @@ void update_and_enqueue_all_CAN() { // ams_interface.pack_charge_is_critical(), // dash.lauchControlButtonPressed()); // Telemetry - // telem_interface.tick(curr_tick, - // ADC1.get(), - // ADC2.get(), - // ADC3.get(), - // steering1.convert()); + telem_interface.tick(curr_tick, + ADC1.get(), + ADC2.get(), // Add MCP3204 functionality for corner board + ADC3.get(), // Add implementation to get() + steering1.convert()); } void sample_all_external_readings() { // Tick all adcs - // ADC1.tick(); - // ADC2.tick(); - // ADC3.tick(); + ADC1.tick(curr_tick); + ADC2.tick(curr_tick); + ADC3.tick(curr_tick); // Tick steering system - // steering1.tick(); + steering_system.tick(curr_tick, ADC1.channels[MCU15_STEERING_CHANNEL].convert()); // Read shutdown circuits - // main_ecu.read_mcu_status(); + main_ecu.read_mcu_status(); } void process_all_value_readings() { - // pedals.tick(); + pedals.tick(curr_tick, + ADC1.channels[MCU15_ACCEL1_CHANNEL].convert(), + ADC1.channels[MCU15_ACCEL2_CHANNEL].convert(), + ADC1.channels[MCU15_BRAKE1_CHANNEL].convert(), + ADC1.channels[MCU15_BRAKE2_CHANNEL].convert()); }