diff --git a/lib/interfaces/include/DrivebrainETHInterface.h b/lib/interfaces/include/DrivebrainETHInterface.h new file mode 100644 index 000000000..0ba8f0ea5 --- /dev/null +++ b/lib/interfaces/include/DrivebrainETHInterface.h @@ -0,0 +1,21 @@ +#ifndef __DRIVEBRAINETHINTERFACE_H__ +#define __DRIVEBRAINETHINTERFACE_H__ +#include "hytech_msgs.pb.h" +#include "DrivebrainData.h" + +class DrivebrainETHInterface +{ + public: + DrivebrainETHInterface() = default; + + void receive_pb_msg(const hytech_msgs_MCUData& msg_in); + + DrivebrainData_s get_latest_data() { return _latest_data; } + + private: + + DrivebrainData_s _latest_data = {}; + +}; + +#endif // __DRIVEBRAINETHINTERFACE_H__ \ No newline at end of file diff --git a/lib/interfaces/include/ProtobufMsgInterface.h b/lib/interfaces/include/ProtobufMsgInterface.hpp similarity index 59% rename from lib/interfaces/include/ProtobufMsgInterface.h rename to lib/interfaces/include/ProtobufMsgInterface.hpp index 4b24cf586..61f2b36fc 100644 --- a/lib/interfaces/include/ProtobufMsgInterface.h +++ b/lib/interfaces/include/ProtobufMsgInterface.hpp @@ -6,37 +6,39 @@ #include "pb_common.h" #include "circular_buffer.h" #include "NativeEthernet.h" +#include // #include "InterfaceParams.h" struct ETHInterfaces { }; -using recv_function_t = void (*)(const uint8_t* buffer, size_t packet_size, ETHInterfaces& interfaces); +// using recv_function_t = void (*)(const uint8_t* buffer, size_t packet_size, ETHInterfaces& interfaces); // this should be usable with arbitrary functions idk something -template -void handle_ethernet_socket_receive(EthernetUDP* socket, recv_function_t recv_function, ETHInterfaces& interfaces) +template +void handle_ethernet_socket_receive(EthernetUDP *socket, std::function recv_function, eth_interface &interface, const pb_msgdesc_t *desc_pointer) { int packet_size = socket->parsePacket(); - if(packet_size > 0) + if (packet_size > 0) { Serial.println("packet size"); Serial.println(packet_size); uint8_t buffer[buffer_size]; size_t read_bytes = socket->read(buffer, sizeof(buffer)); socket->read(buffer, UDP_TX_PACKET_MAX_SIZE); - recv_function(buffer, read_bytes, interfaces); + recv_function(buffer, read_bytes, interface, desc_pointer); } } template -bool handle_ethernet_socket_send_pb(IPAddress addr, uint16_t port, EthernetUDP* socket, const pb_struct& msg, const pb_msgdesc_t* msg_desc) +bool handle_ethernet_socket_send_pb(IPAddress addr, uint16_t port, EthernetUDP *socket, const pb_struct &msg, const pb_msgdesc_t *msg_desc) { socket->beginPacket(addr, port); uint8_t buffer[buffer_size]; pb_ostream_t stream = pb_ostream_from_buffer(buffer, sizeof(buffer)); - if (!pb_encode(&stream, msg_desc, &msg)) { + if (!pb_encode(&stream, msg_desc, &msg)) + { // You can handle error more explicitly by looking at stream.errmsg return false; } @@ -46,19 +48,15 @@ bool handle_ethernet_socket_send_pb(IPAddress addr, uint16_t port, EthernetUDP* return true; } - -template -std::pair recv_pb_stream_msg(const uint8_t *buffer, size_t packet_size, ETHInterfaces& interfaces, const pb_msgdesc_t * desc_pointer) +template +void recv_pb_stream_msg(const uint8_t *buffer, size_t packet_size, eth_interface &interface, const pb_msgdesc_t *desc_pointer) { pb_istream_t stream = pb_istream_from_buffer(buffer, packet_size); pb_msg_type msg = {}; if (pb_decode(&stream, desc_pointer, &msg)) { - return {msg, true}; + interface.receive_pb_msg(msg); } - return {msg, }; } - - #endif \ No newline at end of file diff --git a/lib/interfaces/library.json b/lib/interfaces/library.json index 202cee41a..359a0f9e7 100644 --- a/lib/interfaces/library.json +++ b/lib/interfaces/library.json @@ -5,7 +5,8 @@ "dependencies": { "can_lib": "*", "shared_data": "*", - "CASE_lib": "*" + "CASE_lib": "*", + "nanopb": "*" }, "frameworks": "*", "platforms": "*" diff --git a/lib/interfaces/src/DrivebrainETHInterface.cpp b/lib/interfaces/src/DrivebrainETHInterface.cpp new file mode 100644 index 000000000..fe4bf359e --- /dev/null +++ b/lib/interfaces/src/DrivebrainETHInterface.cpp @@ -0,0 +1,8 @@ +#include "DrivebrainETHInterface.h" + +#include +void DrivebrainETHInterface::receive_pb_msg(const hytech_msgs_MCUData& msg_in) +{ + Serial.println("msg recvd"); + _latest_data = {}; +} \ No newline at end of file diff --git a/lib/systems/include/CASESystem.h b/lib/systems/include/CASESystem.h index 5e5c962f2..737fb011f 100644 --- a/lib/systems/include/CASESystem.h +++ b/lib/systems/include/CASESystem.h @@ -7,7 +7,7 @@ #include "DrivetrainSystem.h" #include "SteeringSystem.h" #include "MCUStateMachine.h" -#include "ProtobufMsgInterface.h" +#include "ProtobufMsgInterface.hpp" // #include "ParameterInterface.h" struct CASEConfiguration diff --git a/platformio.ini b/platformio.ini index 0b3a02629..5c535e22e 100644 --- a/platformio.ini +++ b/platformio.ini @@ -4,7 +4,8 @@ lib_deps_shared = git+ssh://git@github.com/hytech-racing/CASE_lib.git#v49 https://github.com/hytech-racing/shared_firmware_types.git#feb3b83 https://github.com/hytech-racing/shared_firmware_systems.git#af96a63 - + https://github.com/hytech-racing/HT_proto/releases/download/2024-09-15T16_39_42/hytech_msgs_pb_lib.tar.gz + [env:test_env] platform = native test_framework = googletest @@ -14,7 +15,7 @@ build_src_filter = build_unflags = -std=gnu++11 build_flags = - -std=c++17 + -std=c++17 -g -D TESTING_SYSTEMS lib_ignore = @@ -57,6 +58,7 @@ upload_protocol = teensy-cli extra_scripts = pre:extra_script.py lib_deps = ${common.lib_deps_shared} + Nanopb SPI https://github.com/hytech-racing/shared_firmware_interfaces.git#shared_types https://github.com/tonton81/FlexCAN_T4 diff --git a/src/main.cpp b/src/main.cpp index 767eb25e9..01bdcf7e0 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -7,9 +7,10 @@ #include "HyTech_CAN.h" #include "MCU_rev15_defs.h" // #include "NativeEthernet.h" - +#include "pb.h" // /* Interfaces */ - +#include "DrivebrainETHInterface.h" +#include "ProtobufMsgInterface.hpp" #include "HytechCANInterface.h" #include "ThermistorInterface.h" #include "Teensy_ADC.h" @@ -34,7 +35,6 @@ #include "DrivetrainSystem.h" #include "PedalsSystem.h" #include "DrivebrainController.h" -// #include "TorqueControllerMux.h" #include "TorqueControllerMux.h" #include "CASESystem.h" @@ -42,7 +42,10 @@ #include "MCUStateMachine.h" #include "HT08_CASE.h" +#include "InterfaceParams.h" #include "PrintLogger.h" + +#include "hytech_msgs.pb.h" /* PARAMETER STRUCTS */ @@ -322,6 +325,7 @@ MCUStateMachine fsm(&buzzer, &drivetrain, &dashboard, &pedals_system // */ DrivebrainInterface db_interface(&CAN3_txBuffer); +DrivebrainETHInterface db_eth_interface; CANInterfaces CAN_receive_interfaces = {&inv.fl, &inv.fr, &inv.rl, &inv.rr, &vn_interface, &dashboard, &ams_interface, &sab_interface, &db_interface}; /* @@ -348,9 +352,9 @@ void setup() // initialize CAN communication init_all_CAN_devices(); - // Ethernet.begin(EthParams::default_MCU_MAC_address, EthParams::default_MCU_ip); - // protobuf_send_socket.begin(EthParams::default_protobuf_send_port); - // protobuf_recv_socket.begin(EthParams::default_protobuf_recv_port); + Ethernet.begin(EthParams::default_MCU_MAC_address, EthParams::default_MCU_ip); + protobuf_send_socket.begin(EthParams::default_protobuf_send_port); + protobuf_recv_socket.begin(EthParams::default_protobuf_recv_port); SPI.begin(); a1.init(); @@ -412,7 +416,7 @@ void loop() // get latest tick from sys clock SysTick_s curr_tick = sys_clock.tick(micros()); - // handle_ethernet_interface_comms(); + handle_ethernet_interface_comms(); // process received CAN messages process_ring_buffer(CAN2_rxBuffer, CAN_receive_interfaces, curr_tick.millis); @@ -687,5 +691,9 @@ void handle_ethernet_interface_comms() // via the union message. this is a little bit cursed ngl. // TODO un fuck this and make it more sane // Serial.println("bruh"); - // handle_ethernet_socket_receive(&protobuf_recv_socket, &recv_pb_stream_union_msg, ethernet_interfaces); + + + std::function recv_boi = &recv_pb_stream_msg; + handle_ethernet_socket_receive<1024, hytech_msgs_MCUData>(&protobuf_recv_socket, recv_boi, db_eth_interface, hytech_msgs_MCUData_fields); + } \ No newline at end of file