Skip to content

Commit

Permalink
ready to test on car
Browse files Browse the repository at this point in the history
  • Loading branch information
RCMast3r committed Sep 17, 2024
1 parent 77b1e2b commit 8d4a7f1
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 26 deletions.
21 changes: 21 additions & 0 deletions lib/interfaces/include/DrivebrainETHInterface.h
Original file line number Diff line number Diff line change
@@ -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__
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,39 @@
#include "pb_common.h"
#include "circular_buffer.h"
#include "NativeEthernet.h"
#include <functional>
// #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<size_t buffer_size>
void handle_ethernet_socket_receive(EthernetUDP* socket, recv_function_t recv_function, ETHInterfaces& interfaces)
template <size_t buffer_size, typename pb_msg_type, class eth_interface>
void handle_ethernet_socket_receive(EthernetUDP *socket, std::function<void(const uint8_t *, size_t, eth_interface &, const pb_msgdesc_t *)> 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 <typename pb_struct, size_t buffer_size>
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;
}
Expand All @@ -46,19 +48,15 @@ bool handle_ethernet_socket_send_pb(IPAddress addr, uint16_t port, EthernetUDP*
return true;
}


template <typename pb_msg_type>
std::pair<pb_msg_type, bool> recv_pb_stream_msg(const uint8_t *buffer, size_t packet_size, ETHInterfaces& interfaces, const pb_msgdesc_t * desc_pointer)
template <typename pb_msg_type, class eth_interface>
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
3 changes: 2 additions & 1 deletion lib/interfaces/library.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"dependencies": {
"can_lib": "*",
"shared_data": "*",
"CASE_lib": "*"
"CASE_lib": "*",
"nanopb": "*"
},
"frameworks": "*",
"platforms": "*"
Expand Down
8 changes: 8 additions & 0 deletions lib/interfaces/src/DrivebrainETHInterface.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include "DrivebrainETHInterface.h"

#include <Arduino.h>
void DrivebrainETHInterface::receive_pb_msg(const hytech_msgs_MCUData& msg_in)
{
Serial.println("msg recvd");
_latest_data = {};
}
2 changes: 1 addition & 1 deletion lib/systems/include/CASESystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include "DrivetrainSystem.h"
#include "SteeringSystem.h"
#include "MCUStateMachine.h"
#include "ProtobufMsgInterface.h"
#include "ProtobufMsgInterface.hpp"
// #include "ParameterInterface.h"

struct CASEConfiguration
Expand Down
6 changes: 4 additions & 2 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 =
Expand Down Expand Up @@ -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
Expand Down
24 changes: 16 additions & 8 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -34,15 +35,17 @@
#include "DrivetrainSystem.h"
#include "PedalsSystem.h"
#include "DrivebrainController.h"
// #include "TorqueControllerMux.h"
#include "TorqueControllerMux.h"

#include "CASESystem.h"
// /* State machine */
#include "MCUStateMachine.h"
#include "HT08_CASE.h"

#include "InterfaceParams.h"
#include "PrintLogger.h"

#include "hytech_msgs.pb.h"
/*
PARAMETER STRUCTS
*/
Expand Down Expand Up @@ -322,6 +325,7 @@ MCUStateMachine<DriveSys_t> fsm(&buzzer, &drivetrain, &dashboard, &pedals_system
// */

DrivebrainInterface db_interface(&CAN3_txBuffer);
DrivebrainETHInterface db_eth_interface;
CANInterfaces<CircularBufferType> CAN_receive_interfaces = {&inv.fl, &inv.fr, &inv.rl, &inv.rr, &vn_interface, &dashboard, &ams_interface, &sab_interface, &db_interface};

/*
Expand All @@ -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();
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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<void(const uint8_t *, size_t, DrivebrainETHInterface &, const pb_msgdesc_t *)> recv_boi = &recv_pb_stream_msg<hytech_msgs_MCUData, DrivebrainETHInterface>;
handle_ethernet_socket_receive<1024, hytech_msgs_MCUData>(&protobuf_recv_socket, recv_boi, db_eth_interface, hytech_msgs_MCUData_fields);

}

0 comments on commit 8d4a7f1

Please sign in to comment.