Skip to content

Commit

Permalink
seemingly working ethernet integration
Browse files Browse the repository at this point in the history
  • Loading branch information
RCMast3r committed Sep 17, 2024
1 parent c02d1f1 commit 200da81
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 37 deletions.
2 changes: 1 addition & 1 deletion include/InterfaceParams.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace EthParams
{0x04, 0xe9, 0xe5, 0x10, 0x1f, 0x22};

const IPAddress default_MCU_ip(192, 168, 1, 30);
const IPAddress default_TCU_ip(192, 168, 1, 68);
const IPAddress default_TCU_ip(192, 168, 1, 69);

const uint16_t default_protobuf_send_port = 2001;
const uint16_t default_protobuf_recv_port = 2000;
Expand Down
17 changes: 8 additions & 9 deletions lib/interfaces/include/DrivebrainETHInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,19 @@
#define __DRIVEBRAINETHINTERFACE_H__
#include "hytech_msgs.pb.h"
#include "DrivebrainData.h"

#include "SharedDataTypes.h"
class DrivebrainETHInterface
{
public:
DrivebrainETHInterface() = default;

void receive_pb_msg(const hytech_msgs_MCUData& msg_in);

DrivebrainData_s get_latest_data() { return _latest_data; }
public:
DrivebrainETHInterface() = default;

private:
void receive_pb_msg(const hytech_msgs_MCUCommandData &msg_in, unsigned long curr_millis);

DrivebrainData_s _latest_data = {};
hytech_msgs_MCUOutputData make_db_msg(const SharedCarState_s &shared_state);
DrivebrainData_s get_latest_data() { return _latest_data; }

private:
DrivebrainData_s _latest_data = {};
};

#endif // __DRIVEBRAINETHINTERFACE_H__
13 changes: 5 additions & 8 deletions lib/interfaces/include/ProtobufMsgInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,18 @@ struct ETHInterfaces
{
};

// 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, 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)
void handle_ethernet_socket_receive(const SysTick_s& tick, EthernetUDP *socket, std::function<void(unsigned long, 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)
{
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, interface, desc_pointer);
recv_function(tick.millis, buffer, read_bytes, interface, desc_pointer);
}
}

Expand All @@ -49,13 +46,13 @@ bool handle_ethernet_socket_send_pb(IPAddress addr, uint16_t port, EthernetUDP *
}

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)
void recv_pb_stream_msg(unsigned long curr_millis, 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))
{
interface.receive_pb_msg(msg);
interface.receive_pb_msg(msg, curr_millis);
}
}

Expand Down
31 changes: 27 additions & 4 deletions lib/interfaces/src/DrivebrainETHInterface.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,31 @@
#include "DrivebrainETHInterface.h"

#include "SharedDataTypes.h"
#include <Arduino.h>
void DrivebrainETHInterface::receive_pb_msg(const hytech_msgs_MCUData& msg_in)

hytech_msgs_MCUOutputData DrivebrainETHInterface::make_db_msg(const SharedCarState_s &shared_state)
{
hytech_msgs_MCUOutputData out;
out.accel_percent = shared_state.pedals_data.accelPercent;
out.brake_percent = shared_state.pedals_data.brakePercent;
out.rpm_data = {shared_state.drivetrain_data.measuredSpeeds[0],
shared_state.drivetrain_data.measuredSpeeds[1],
shared_state.drivetrain_data.measuredSpeeds[2],
shared_state.drivetrain_data.measuredSpeeds[3]};
return out;
}

void DrivebrainETHInterface::receive_pb_msg(const hytech_msgs_MCUCommandData &msg_in, unsigned long curr_millis)
{
Serial.println("msg recvd");
_latest_data = {};
// Serial.println("msg recvd in here");
veh_vec<float> nm_lim;
nm_lim.construct(msg_in.torque_limit_nm.FL, msg_in.torque_limit_nm.FR, msg_in.torque_limit_nm.RL, msg_in.torque_limit_nm.RR);

veh_vec<float> speed_set;
speed_set.construct(msg_in.desired_rpms.FL, msg_in.desired_rpms.FR, msg_in.desired_rpms.RL, msg_in.desired_rpms.RR);
// Serial.println(msg_in.desired_rpms.FL);

_latest_data.torque_limits_nm = nm_lim;
_latest_data.speed_setpoints_rpm = speed_set;
_latest_data.last_torque_lim_receive_time_millis = curr_millis;
_latest_data.last_speed_setpoint_receive_time_millis = curr_millis;
}
10 changes: 5 additions & 5 deletions lib/shared_data/include/Utility.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ struct veh_vec
T RL;
T RR;

void construct(T FL, T FR, T RL, T RR)
void construct(T _FL, T _FR, T _RL, T _RR)
{
FL = FL;
FR = FR;
RL = RL;
RR = RR;
FL = _FL;
FR = _FR;
RL = _RL;
RR = _RR;
}

/// @brief copy values to array in FL, FR, RL, RR order
Expand Down
2 changes: 1 addition & 1 deletion lib/systems/include/DrivebrainController.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef __DRIVEBRAINCONTROLLER_H__
#define __DRIVEBRAINCONTROLLER_H__

#include "DrivebrainInterface.h"

#include "TorqueControllers.h"
#include "DrivebrainData.h"
#include "BaseController.h"
Expand Down
2 changes: 1 addition & 1 deletion lib/systems/src/DrivebrainController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ TorqueControllerOutput_s DrivebrainController::evaluate(const SharedCarState_s &

auto sys_tick = state.systick;
auto db_input = state.db_data;

bool speed_setpoint_too_latent = (::abs((int)(sys_tick.millis - db_input.last_speed_setpoint_receive_time_millis)) > (int)_params.allowed_latency);
bool torque_setpoint_too_latent = (::abs((int)(sys_tick.millis - db_input.last_torque_lim_receive_time_millis)) > (int)_params.allowed_latency);

Expand Down
2 changes: 1 addition & 1 deletion platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ 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
https://github.com/hytech-racing/HT_proto/releases/download/2024-09-17T03_08_13/hytech_msgs_pb_lib.tar.gz

[env:test_env]
platform = native
Expand Down
22 changes: 15 additions & 7 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ void tick_all_systems(const SysTick_s &current_system_tick);
/* Reset inverters */
void drivetrain_reset();

void handle_ethernet_interface_comms();
void handle_ethernet_interface_comms(const SysTick_s& systick, const hytech_msgs_MCUOutputData& out_msg);

/*
SETUP
Expand Down Expand Up @@ -416,7 +416,6 @@ void loop()
// get latest tick from sys clock
SysTick_s curr_tick = sys_clock.tick(micros());

handle_ethernet_interface_comms();

// process received CAN messages
process_ring_buffer(CAN2_rxBuffer, CAN_receive_interfaces, curr_tick.millis);
Expand All @@ -435,9 +434,13 @@ void loop()
load_cell_interface.getLoadCellForces(),
pedals_system.getPedalsSystemData(),
vn_interface.get_vn_struct(),
db_interface.get_latest_db_data(),
db_eth_interface.get_latest_data(),
torque_controller_mux.get_tc_mux_status());

hytech_msgs_MCUOutputData out_eth_msg = db_eth_interface.make_db_msg(car_state_inst);

handle_ethernet_interface_comms(curr_tick, out_eth_msg);

tick_all_systems(curr_tick);

// inverter procedure before entering state machine
Expand All @@ -458,7 +461,8 @@ void loop()
send_all_CAN_msgs(CAN3_txBuffer, &TELEM_CAN);

// Basic debug prints
if (curr_tick.triggers.trigger5)
// if (curr_tick.triggers.trigger5)
if (false)
{
Serial.print("Steering system reported angle (deg): ");
Serial.println(steering_system.getSteeringSystemData().angle);
Expand Down Expand Up @@ -685,15 +689,19 @@ void tick_all_systems(const SysTick_s &current_system_tick)

}

void handle_ethernet_interface_comms()
void handle_ethernet_interface_comms(const SysTick_s& systick, const hytech_msgs_MCUOutputData& out_msg)
{
// function that will handle receiving and distributing of all messages to all ethernet interfaces
// via the union message. this is a little bit cursed ngl.
// TODO un fuck this and make it more sane
// Serial.println("bruh");


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);
std::function<void(unsigned long, const uint8_t *, size_t, DrivebrainETHInterface &, const pb_msgdesc_t *)> recv_boi = &recv_pb_stream_msg<hytech_msgs_MCUCommandData, DrivebrainETHInterface>;
handle_ethernet_socket_receive<1024, hytech_msgs_MCUCommandData>(systick, &protobuf_recv_socket, recv_boi, db_eth_interface, hytech_msgs_MCUCommandData_fields);

if(systick.triggers.trigger500)
{
handle_ethernet_socket_send_pb<hytech_msgs_MCUOutputData, 1024>(EthParams::default_TCU_ip, EthParams::default_protobuf_send_port, &protobuf_send_socket, out_msg, hytech_msgs_MCUOutputData_fields);
}
}

0 comments on commit 200da81

Please sign in to comment.