Skip to content

Commit

Permalink
Fixed compile errors. Can build on teensy41, test on test_interfaces env
Browse files Browse the repository at this point in the history
  • Loading branch information
jhwang04 committed Jul 9, 2024
1 parent daa6799 commit 4c11918
Show file tree
Hide file tree
Showing 10 changed files with 130 additions and 227 deletions.
46 changes: 0 additions & 46 deletions lib/interfaces/include/AMSInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,52 +120,6 @@ class AMSInterface
*/
void tick(const SysTick_s &tick);

/**
* Initializes the charge member variable from the voltage of the minimum cell using the VOLTAGE_LOOKUP_TABLE.
*
* @pre The bms_voltages_ member variable MUST be initialized before this function can be called.
* @return The charge, in coulombs, that the charge member variable is initialized to.
*/
float initialize_charge();

/**
* Calculates SoC based on the energy meter CAN message. Calling calculate_SoC_em()
* will update the SoC_ and charge_ member variables.
*
* @param The current tick that the calculate_SoC_em function should use for integration.
*
* @pre The last_tick_ member variable must be updated correctly.
* @post The charge_ field has the updated charge, and the SoC field contains an updated percentage.
*/
void calculate_SoC_em(const SysTick_s &tick);

/**
* Calculates SoC based on the ACU_SHUNT_MEASUREMENTS CAN message. Calling calculate_SoC_acu()
* will update the SoC_ and charge_ member variables.
*
* @param The current tick that the calculate_SoC_em function should use for integration.
*
* @pre The last_tick_ member variable must be updated correctly.
* @post The charge_ field has the updated charge, and the SoC field contains an updated percentage.
*/
void calculate_SoC_acu(const SysTick_s &tick);

/**
* Retrieves the value of the SoC member variable. This function does NOT recalculate
* the SoC_ variable, it only returns the value that is stored.
*
* @return the current value stored in the SoC_ member variable.
*/
float get_SoC() {return SoC_;}

/**
* This is AMSInterface's tick() function. It behaves correctly regardless of the
* since the functions calculate the elapsed time between the given tick and the stored last_tick_.
*
* @param tick The current system tick.
*/
void tick(const SysTick_s &tick);

//RETRIEVE CAN MESSAGES//

/**
Expand Down
1 change: 0 additions & 1 deletion lib/interfaces/include/MessageQueueDefine.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

#include "FlexCAN_T4.h"

// does this need to be 3 params like our original definitions of the buffers?
using CANBufferType = Circular_Buffer<uint8_t, (uint32_t)128, sizeof(CAN_message_t)>;

#endif
2 changes: 1 addition & 1 deletion lib/interfaces/src/AMSInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ void AMSInterface::retrieve_voltage_CAN(CAN_message_t &can_msg) {
if (!has_received_bms_voltage_)
{
has_received_bms_voltage_ = true;
timestamp_start = last_tick_.millis;
timestamp_start_ = last_tick_.millis;
}

}
Expand Down
2 changes: 1 addition & 1 deletion lib/mock_interfaces/AMSInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const float DEFAULT_INIT_VOLTAGE = 3.5;
const float DEFAULT_TEMP_ALPHA = 0.8;
const float DEFAULT_VOLTAGE_ALPHA = 0.8;

#define CANBufferType int // Placeholder define for the mock interface
// #define CANBufferType int // Placeholder define for the mock interface

class AMSInterface
{
Expand Down
8 changes: 8 additions & 0 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,16 @@ upload_protocol = teensy-cli
extra_scripts = pre:extra_script.py
lib_ldf_mode = deep+
lib_deps =
SPI
Nanopb
https://github.com/vjmuzik/NativeEthernet.git
https://github.com/tonton81/FlexCAN_T4#b928bcb
https://github.com/RCMast3r/hytech_can#4ffbba4
https://github.com/hytech-racing/HT_params/releases/download/2024-05-07T06_59_33/ht_eth_pb_lib.tar.gz
https://github.com/hytech-racing/shared_firmware_interfaces.git#feature/thermistor-template
https://github.com/hytech-racing/shared_firmware_systems.git#af96a63
https://github.com/RCMast3r/spi_libs#2214fee
https://github.com/hytech-racing/HT_CAN/releases/latest/download/can_lib.tar.gz
git+ssh://git@github.com/hytech-racing/CASE_lib.git#v49


4 changes: 0 additions & 4 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -554,10 +554,6 @@ void tick_all_interfaces(const SysTick_s &current_system_tick)
if (t.trigger50) // 50Hz
{
steering1.sample();
<<<<<<< HEAD

=======
>>>>>>> master
ams_interface.tick(current_system_tick);
}

Expand Down
3 changes: 1 addition & 2 deletions src/main_ethernet_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@

EthernetUDP protobuf_send_socket;
EthernetUDP protobuf_recv_socket;
CAR_STATE state = CAR_STATE::STARTUP;
ParameterInterface params(state);
ParameterInterface params = ParameterInterface();
ETHInterfaces ethernet_interfaces = {&params};

void init_ethernet_device()
Expand Down
10 changes: 5 additions & 5 deletions src/test_can_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ FlexCAN_T4<CAN2, RX_SIZE_256, TX_SIZE_16> TEST_CAN2; // Inverter CAN (now both a

CAN_message_t msg;

using CircularBufferType = Circular_Buffer<uint8_t, (uint32_t)16, sizeof(CAN_message_t)>;
using CircularBufferType = CANBufferType;
using InverterInterfaceType = InverterInterface<CircularBufferType>;
InverterInterfaceType fl_inv(&CAN2_txBuffer, ID_MC1_SETPOINTS_COMMAND);
InverterInterfaceType fr_inv(&CAN2_txBuffer, ID_MC2_SETPOINTS_COMMAND);
InverterInterfaceType rl_inv(&CAN2_txBuffer, ID_MC3_SETPOINTS_COMMAND);
InverterInterfaceType rr_inv(&CAN2_txBuffer, ID_MC4_SETPOINTS_COMMAND);
InverterInterfaceType fl_inv = InverterInterfaceType(&CAN2_txBuffer, MC1_SETPOINTS_COMMAND_CANID);
InverterInterfaceType fr_inv = InverterInterfaceType(&CAN2_txBuffer, MC2_SETPOINTS_COMMAND_CANID);
InverterInterfaceType rl_inv = InverterInterfaceType(&CAN2_txBuffer, MC3_SETPOINTS_COMMAND_CANID);
InverterInterfaceType rr_inv = InverterInterfaceType(&CAN2_txBuffer, MC4_SETPOINTS_COMMAND_CANID);

CANInterfaces<CircularBufferType> CAN_interfaces = {&fl_inv, &fr_inv, &rl_inv, &rr_inv, 0, 0, 0};

Expand Down
228 changes: 114 additions & 114 deletions test/test_interfaces/dashboard_interface_test.h
Original file line number Diff line number Diff line change
@@ -1,137 +1,137 @@
// #include <Arduino.h>
// #include <unity.h>

// #include "hytech.h"
// #include "DashboardInterface.h"

// Circular_Buffer <uint8_t, (uint32_t)16, sizeof(CAN_message_t)> dash_CAN_buffer;

// /* CHAT GPT FUNCTIONS */
// void printMemoryHex(void* ptr, size_t length) {
// uint8_t* bytePtr = (uint8_t*)ptr;
// for (size_t i = 0; i < length; ++i) {
// if (bytePtr[i] < 0x10) {
// Serial.print("0"); // Add leading zero for values less than 0x10
// }
// Serial.print(bytePtr[i], HEX);
// Serial.print(' ');
// }
// Serial.println();
// }


// void test_dashboard_unpacking_can_message(void)
// {
// DashboardInterface dash_interface(&dash_CAN_buffer);

// // Chat GPT code that iterates through a truth table
// for (int i = 0; i < (1 << 10); ++i) { // 2^10 possible combinations
// DASHBOARD_STATE_t current_state;
// current_state.start_button = (uint8_t)((i & (1 << 9)) >> 9);
// current_state.mark_button = (uint8_t)((i & (1 << 8)) >> 8);
// current_state.mode_button = (uint8_t)((i & (1 << 7)) >> 7);
// current_state.motor_controller_cycle_button = (uint8_t)((i & (1 << 6)) >> 6);
// current_state.launch_ctrl_button = (uint8_t)((i & (1 << 5)) >> 5);
// current_state.torque_mode_button = (uint8_t)((i & (1 << 4)) >> 4);
// current_state.led_dimmer_button = (uint8_t)((i & (1 << 3)) >> 3);
// current_state.left_shifter_button = (uint8_t)((i & (1 << 2)) >> 2);
// current_state.right_shifter_button = (uint8_t)((i & (1 << 1)) >> 1);
// current_state.shutdown_h_above_threshold = (uint8_t)(i & 1);
#include <Arduino.h>
#include <unity.h>

#include "hytech.h"
#include "DashboardInterface.h"

CANBufferType dash_CAN_buffer;

/* CHAT GPT FUNCTIONS */
void printMemoryHex(void* ptr, size_t length) {
uint8_t* bytePtr = (uint8_t*)ptr;
for (size_t i = 0; i < length; ++i) {
if (bytePtr[i] < 0x10) {
Serial.print("0"); // Add leading zero for values less than 0x10
}
Serial.print(bytePtr[i], HEX);
Serial.print(' ');
}
Serial.println();
}


void test_dashboard_unpacking_can_message(void)
{
DashboardInterface dash_interface(&dash_CAN_buffer);

// Chat GPT code that iterates through a truth table
for (int i = 0; i < (1 << 10); ++i) { // 2^10 possible combinations
DASHBOARD_STATE_t current_state;
current_state.start_button = (uint8_t)((i & (1 << 9)) >> 9);
current_state.mark_button = (uint8_t)((i & (1 << 8)) >> 8);
current_state.mode_button = (uint8_t)((i & (1 << 7)) >> 7);
current_state.motor_controller_cycle_button = (uint8_t)((i & (1 << 6)) >> 6);
current_state.launch_ctrl_button = (uint8_t)((i & (1 << 5)) >> 5);
current_state.torque_mode_button = (uint8_t)((i & (1 << 4)) >> 4);
current_state.led_dimmer_button = (uint8_t)((i & (1 << 3)) >> 3);
current_state.left_shifter_button = (uint8_t)((i & (1 << 2)) >> 2);
current_state.right_shifter_button = (uint8_t)((i & (1 << 1)) >> 1);
current_state.shutdown_h_above_threshold = (uint8_t)(i & 1);

// CAN_message_t msg;
// msg.id= Pack_DASHBOARD_STATE_hytech(&current_state, msg.buf, &msg.len, (uint8_t*) &msg.flags.extended);
CAN_message_t msg;
msg.id= Pack_DASHBOARD_STATE_hytech(&current_state, msg.buf, &msg.len, (uint8_t*) &msg.flags.extended);

// dash_interface.read(msg);
dash_interface.read(msg);

// TEST_ASSERT_EQUAL(current_state.start_button, dash_interface.startButtonPressed() );
// TEST_ASSERT_EQUAL(current_state.mark_button, dash_interface.specialButtonPressed() );
// TEST_ASSERT_EQUAL(current_state.mode_button , dash_interface.torqueModeButtonPressed() );
// TEST_ASSERT_EQUAL(current_state.motor_controller_cycle_button, dash_interface.inverterResetButtonPressed() );
// TEST_ASSERT_EQUAL(current_state.launch_ctrl_button , dash_interface.launchControlButtonPressed() );
// TEST_ASSERT_EQUAL(current_state.torque_mode_button , dash_interface.torqueLoadingButtonPressed() );
// TEST_ASSERT_EQUAL(current_state.led_dimmer_button , dash_interface.nightModeButtonPressed() );
// TEST_ASSERT_EQUAL(current_state.left_shifter_button, dash_interface.leftShifterButtonPressed() );
// TEST_ASSERT_EQUAL(current_state.right_shifter_button , dash_interface.rightShifterButtonPressed() );
// TEST_ASSERT_EQUAL(current_state.shutdown_h_above_threshold , dash_interface.shutdownHAboveThreshold() );
TEST_ASSERT_EQUAL(current_state.start_button, dash_interface.startButtonPressed() );
TEST_ASSERT_EQUAL(current_state.mark_button, dash_interface.specialButtonPressed() );
TEST_ASSERT_EQUAL(current_state.mode_button , dash_interface.torqueModeButtonPressed() );
TEST_ASSERT_EQUAL(current_state.motor_controller_cycle_button, dash_interface.inverterResetButtonPressed() );
TEST_ASSERT_EQUAL(current_state.launch_ctrl_button , dash_interface.launchControlButtonPressed() );
TEST_ASSERT_EQUAL(current_state.torque_mode_button , dash_interface.torqueLoadingButtonPressed() );
TEST_ASSERT_EQUAL(current_state.led_dimmer_button , dash_interface.nightModeButtonPressed() );
TEST_ASSERT_EQUAL(current_state.left_shifter_button, dash_interface.leftShifterButtonPressed() );
TEST_ASSERT_EQUAL(current_state.right_shifter_button , dash_interface.rightShifterButtonPressed() );
TEST_ASSERT_EQUAL(current_state.shutdown_h_above_threshold , dash_interface.shutdownHAboveThreshold() );

// }
}



// }

// void test_dashboard_circular_buffer(void)
// {

// Circular_Buffer <uint8_t, (uint32_t)16, sizeof(CAN_message_t)> dash_CAN_buffer_2;
// DashboardInterface dash_interface(&dash_CAN_buffer_2);

// uint8_t LED[12] = {};

// // for (uint64_t i = 0; i < (1 << 48); ++i) { // 2^48 possible combinations
// // for (int j = 0; j < 12; ++j) {
// // int color_index = (i >> (j * 2)) & 0b11; // Extract 2 bits for each LED
// // switch (color_index) {
// // case 0:
// // LED[j] = static_cast<uint8_t>(LEDColors_e::OFF);
// // break;
// // case 1:
// // LED[j] = static_cast<uint8_t>(LEDColors_e::ON);
// // break;
// // case 2:
// // LED[j] = static_cast<uint8_t>(LEDColors_e::YELLOW);
// // break;
// // case 3:
// // LED[j] = static_cast<uint8_t>(LEDColors_e::RED);
// // break;
// // }
// // }
}

void test_dashboard_circular_buffer(void)
{

CANBufferType dash_CAN_buffer_2;
DashboardInterface dash_interface(&dash_CAN_buffer_2);

uint8_t LED[12] = {};

// for (uint64_t i = 0; i < (1 << 48); ++i) { // 2^48 possible combinations
// for (int j = 0; j < 12; ++j) {
// int color_index = (i >> (j * 2)) & 0b11; // Extract 2 bits for each LED
// switch (color_index) {
// case 0:
// LED[j] = static_cast<uint8_t>(LEDColors_e::OFF);
// break;
// case 1:
// LED[j] = static_cast<uint8_t>(LEDColors_e::ON);
// break;
// case 2:
// LED[j] = static_cast<uint8_t>(LEDColors_e::YELLOW);
// break;
// case 3:
// LED[j] = static_cast<uint8_t>(LEDColors_e::RED);
// break;
// }
// }

// // // Use LED array as needed
// // // For example, you can print the LED colors:
// // for (int k = 0; k < 12; ++k) {
// // Serial.print(LED[k]);
// // Serial.print(" ");
// // }
// // Serial.println();
// // }
// // Use LED array as needed
// // For example, you can print the LED colors:
// for (int k = 0; k < 12; ++k) {
// Serial.print(LED[k]);
// Serial.print(" ");
// }
// Serial.println();
// }

// LED[0] = static_cast<uint8_t>(LEDColors_e::RED);
// LED[1] = static_cast<uint8_t>(LEDColors_e::YELLOW);
LED[0] = static_cast<uint8_t>(LEDColors_e::RED);
LED[1] = static_cast<uint8_t>(LEDColors_e::YELLOW);


// dash_interface.setLED(DashLED_e::BOTS_LED, LEDColors_e::RED);
// dash_interface.setLED(DashLED_e::LAUNCH_CONTROL_LED, LEDColors_e::YELLOW);
dash_interface.setLED(DashLED_e::BOTS_LED, LEDColors_e::RED);
dash_interface.setLED(DashLED_e::LAUNCH_CONTROL_LED, LEDColors_e::YELLOW);

// // need to make _data in dash interface public:
// // TODO: investigate using friend class
// // TEST_ASSERT_EQUAL_UINT8_ARRAY(LED, dash_interface._data.LED, sizeof(LED));
// need to make _data in dash interface public:
// TODO: investigate using friend class
// TEST_ASSERT_EQUAL_UINT8_ARRAY(LED, dash_interface._data.LED, sizeof(LED));

// CAN_message_t packed_message = dash_interface.write();
CAN_message_t packed_message = dash_interface.write();

// uint8_t buffer[sizeof(CAN_message_t)];
// dash_CAN_buffer_2.pop_front(buffer, sizeof(CAN_message_t));
uint8_t buffer[sizeof(CAN_message_t)];
dash_CAN_buffer_2.pop_front(buffer, sizeof(CAN_message_t));

// delay(1000);
delay(1000);

// Serial.println("Printing packed message:");
// printMemoryHex(&packed_message, sizeof(CAN_message_t));
// Serial.println("Printing popped message");
// printMemoryHex(&buffer, sizeof(CAN_message_t));
Serial.println("Printing packed message:");
printMemoryHex(&packed_message, sizeof(CAN_message_t));
Serial.println("Printing popped message");
printMemoryHex(&buffer, sizeof(CAN_message_t));

// CAN_message_t msg;
// memmove(&msg, buffer, sizeof(CAN_message_t));
CAN_message_t msg;
memmove(&msg, buffer, sizeof(CAN_message_t));

// Serial.println("Printing moved message:");
// printMemoryHex(&msg, sizeof(CAN_message_t));
Serial.println("Printing moved message:");
printMemoryHex(&msg, sizeof(CAN_message_t));

// TEST_ASSERT_EQUAL_MEMORY(&packed_message, &msg, sizeof(CAN_message_t));
TEST_ASSERT_EQUAL_MEMORY(&packed_message, &msg, sizeof(CAN_message_t));

// DASHBOARD_MCU_STATE_t mcu_state;
DASHBOARD_MCU_STATE_t mcu_state;

// Unpack_DASHBOARD_MCU_STATE_hytech(&mcu_state, msg.buf, NULL);
Unpack_DASHBOARD_MCU_STATE_hytech(&mcu_state, msg.buf, NULL);

// TEST_ASSERT_EQUAL(LEDColors_e::RED, mcu_state.bots_led);
// TEST_ASSERT_EQUAL(LEDColors_e::YELLOW, mcu_state.launch_control_led);
TEST_ASSERT_EQUAL(LEDColors_e::RED, mcu_state.bots_led);
TEST_ASSERT_EQUAL(LEDColors_e::YELLOW, mcu_state.launch_control_led);

// }
}
Loading

0 comments on commit 4c11918

Please sign in to comment.