From e84c2947282cf72024c5665b272ac7108fbce354 Mon Sep 17 00:00:00 2001 From: Ben Hall Date: Mon, 29 Apr 2024 19:18:44 -0400 Subject: [PATCH 01/18] adding stuff in --- lib/systems/include/ParameterSystem.h | 56 +++++++++++++++++++++++++++ src/main.cpp | 3 +- 2 files changed, 57 insertions(+), 2 deletions(-) create mode 100644 lib/systems/include/ParameterSystem.h diff --git a/lib/systems/include/ParameterSystem.h b/lib/systems/include/ParameterSystem.h new file mode 100644 index 000000000..a792903cc --- /dev/null +++ b/lib/systems/include/ParameterSystem.h @@ -0,0 +1,56 @@ +#pragma once +#include + +namespace Parameters { + +class FloatParameter { +public: + virtual float get() const = 0; + virtual void set(float value) = 0; + virtual ~FloatParameter() {} +}; + +class BoolParameter { +public: + virtual bool get() const = 0; + virtual void set(bool value) = 0; + virtual ~BoolParameter() {} +}; + +class MaxSpeed : public FloatParameter { + float value; +public: + MaxSpeed() : value(120.0) {} + float get() const override { return value; } + void set(float v) override { value = v; } +}; + +class Threshold : public FloatParameter { + float value; +public: + Threshold() : value(0.75) {} + float get() const override { return value; } + void set(float v) override { value = v; } +}; + +class IsActive : public BoolParameter { + bool value; +public: + IsActive() : value(false) {} + bool get() const override { return value; } + void set(bool v) override { value = v; } +}; + +MaxSpeed MaxSpeedInstance; +Threshold ThresholdInstance; +std::unordered_map floatLookupMap = { + {"b1fc2577", &MaxSpeedInstance}, + {"0da627ad", &ThresholdInstance}, +}; + +IsActive IsActiveInstance; +std::unordered_map boolLookupMap = { + {"57401e59", &IsActiveInstance}, +}; + +} \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index ec9079834..c4e0e23a8 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,7 +1,7 @@ /* Include files */ /* System Includes*/ #include - +#include "ParameterSystem.h" /* Libraries */ #include "FlexCAN_T4.h" #include "HyTech_CAN.h" @@ -282,7 +282,6 @@ void setup() /* Init Systems */ - safety_system.init(); // Drivetrain set all inverters disabled From ada45c234fc12114ad9d0aeb8728c74202e0d73d Mon Sep 17 00:00:00 2001 From: Ben Hall Date: Mon, 29 Apr 2024 23:14:41 -0400 Subject: [PATCH 02/18] adding in current progress --- lib/interfaces/include/EthernetInterface.h | 23 ++++++++ lib/library.json | 52 ------------------ lib/systems/include/ParameterSystem.h | 64 ++++------------------ lib/systems/library.json | 3 +- platformio.ini | 9 +++ proto/ht_msgs.proto | 21 +++++++ 6 files changed, 67 insertions(+), 105 deletions(-) create mode 100644 lib/interfaces/include/EthernetInterface.h delete mode 100644 lib/library.json create mode 100644 proto/ht_msgs.proto diff --git a/lib/interfaces/include/EthernetInterface.h b/lib/interfaces/include/EthernetInterface.h new file mode 100644 index 000000000..cd06774b9 --- /dev/null +++ b/lib/interfaces/include/EthernetInterface.h @@ -0,0 +1,23 @@ +#ifndef ETHERNETINTERFACE +#define ETHERNETINTERFACE +#include + +#include "circular_buffer.h" + + + +class EthernetInterface +{ +public: + EthernetInterface() {}; + void begin(int port); + /// @brief sends all udp packets in queue + void handle_sending(); + /// @brief receives all udp packets + void handle_recvs(); + +private: + EthernetUDP udp_ethernet_; +} + +#endif \ No newline at end of file diff --git a/lib/library.json b/lib/library.json deleted file mode 100644 index 61921be1f..000000000 --- a/lib/library.json +++ /dev/null @@ -1,52 +0,0 @@ -{ - "name": "CASE_lib", - "version": "1.0.0", - "build": { - "flags": [ - "-Istate_machine", - "-Istate_machine/include", - "-Iinterfaces", - "-Iinterfaces/include", - "-Iinterfaces/src", - "-Imock_interfaces", - "-Isystems", - "-Isystems/src", - "-Isystems/include", - "-Ishared_data", - "-Ishared_data/include", - "-ICASE_lib", - "-ICASE_lib/R2023b", - "-ICASE_lib/R2023b/simulink", - "-ICASE_lib/R2023b/simulink/include", - "-ICASE_lib/R2023b/rtw", - "-ICASE_lib/R2023b/rtw/c", - "-ICASE_lib/R2023b/rtw/c/src", - "-ICASE_lib/CASE", - "-ICASE_lib/CASE/HT08_CONTROL_SYSTEM_ert_rtw", - "-ICASE_lib/CASE/slprj", - "-ICASE_lib/CASE/slprj/ert", - "-ICASE_lib/CASE/slprj/ert/BasicVehicleMath", - "-ICASE_lib/CASE/slprj/ert/LAUNCH_CONTROL", - "-ICASE_lib/CASE/slprj/ert/NORMAL_FORCE_TV", - "-ICASE_lib/CASE/slprj/ert/PID_TV", - "-ICASE_lib/CASE/slprj/ert/POWER_LIMIT", - "-ICASE_lib/CASE/slprj/ert/_sharedutils", - "-ILateral Models", - "-ILateral Models/HT08_CONTROL_SYSTEM_ert_rtw", - "-ILateral Models/slprj", - "-ILateral Models/slprj/ert", - "-ILateral Models/slprj/ert/BasicVehicleMath", - "-ILateral Models/slprj/ert/LAUNCH_CONTROL", - "-ILateral Models/slprj/ert/NORMAL_FORCE_TV", - "-ILateral Models/slprj/ert/PID_TV", - "-ILateral Models/slprj/ert/POWER_LIMIT", - "-ILateral Models/slprj/ert/_sharedutils", - "-IR2023b", - "-IR2023b/simulink", - "-IR2023b/simulink/include", - "-IR2023b/rtw", - "-IR2023b/rtw/c", - "-IR2023b/rtw/c/src" - ] - } -} \ No newline at end of file diff --git a/lib/systems/include/ParameterSystem.h b/lib/systems/include/ParameterSystem.h index a792903cc..7359e8e52 100644 --- a/lib/systems/include/ParameterSystem.h +++ b/lib/systems/include/ParameterSystem.h @@ -1,56 +1,16 @@ -#pragma once -#include +#ifndef PARAMETERSYSTEM +#define PARAMETERSYSTEM +#include "Parameters.h" -namespace Parameters { +template +void setParameter(T& paramType, float value) { + paramType.set(value); +} -class FloatParameter { -public: - virtual float get() const = 0; - virtual void set(float value) = 0; - virtual ~FloatParameter() {} -}; +template +auto getParameter(T& paramType){ + return paramType.get(); +} -class BoolParameter { -public: - virtual bool get() const = 0; - virtual void set(bool value) = 0; - virtual ~BoolParameter() {} -}; -class MaxSpeed : public FloatParameter { - float value; -public: - MaxSpeed() : value(120.0) {} - float get() const override { return value; } - void set(float v) override { value = v; } -}; - -class Threshold : public FloatParameter { - float value; -public: - Threshold() : value(0.75) {} - float get() const override { return value; } - void set(float v) override { value = v; } -}; - -class IsActive : public BoolParameter { - bool value; -public: - IsActive() : value(false) {} - bool get() const override { return value; } - void set(bool v) override { value = v; } -}; - -MaxSpeed MaxSpeedInstance; -Threshold ThresholdInstance; -std::unordered_map floatLookupMap = { - {"b1fc2577", &MaxSpeedInstance}, - {"0da627ad", &ThresholdInstance}, -}; - -IsActive IsActiveInstance; -std::unordered_map boolLookupMap = { - {"57401e59", &IsActiveInstance}, -}; - -} \ No newline at end of file +#endif \ No newline at end of file diff --git a/lib/systems/library.json b/lib/systems/library.json index 50a076984..f2cc84cc5 100644 --- a/lib/systems/library.json +++ b/lib/systems/library.json @@ -7,7 +7,8 @@ "shared-interfaces-lib": "*", "shared-systems-lib": "*", "CASE_lib": "*", - "shared_data": "*" + "shared_data": "*", + "HT_params_lib": "*" }, "frameworks": "*", "platforms": "*" diff --git a/platformio.ini b/platformio.ini index d69ab7739..646d73437 100644 --- a/platformio.ini +++ b/platformio.ini @@ -11,6 +11,7 @@ lib_ignore = test_ignore= test_interfaces* lib_deps= + https://github.com/hytech-racing/HT_params/releases/download/3/HT_params_lib.tar.gz git+ssh://git@github.com/hytech-racing/CASE_lib.git#v39 https://github.com/hytech-racing/shared_firmware_systems.git @@ -42,16 +43,24 @@ board = teensy41 framework = arduino upload_protocol = teensy-cli extra_scripts = pre:extra_script.py +custom_nanopb_protos = + + + lib_deps = SPI + Nanopb + ssilverman/QNEthernet@^0.27.0 https://github.com/hytech-racing/shared_firmware_interfaces.git https://github.com/hytech-racing/shared_firmware_systems.git https://github.com/RCMast3r/spi_libs https://github.com/tonton81/FlexCAN_T4 https://github.com/RCMast3r/hytech_can#testing_new_inv_ids https://github.com/hytech-racing/HT_CAN/releases/download/80/can_lib.tar.gz + https://github.com/hytech-racing/HT_params/releases/download/3/HT_params_lib.tar.gz + https://github.com/tonton81/Circular_Buffer git+ssh://git@github.com/hytech-racing/CASE_lib.git#v39 + [env:test_can_on_teensy] lib_ignore = mock_interfaces diff --git a/proto/ht_msgs.proto b/proto/ht_msgs.proto new file mode 100644 index 000000000..fe52c40a8 --- /dev/null +++ b/proto/ht_msgs.proto @@ -0,0 +1,21 @@ +syntax = "proto3"; + +package ht_eth; + +message config +{ + string id = 1; + oneof config_val { + bool bool_val = 2; + float float_val = 3; + int32 int_val = 4; + } +} + +message CASE_output +{ + float vehm_fl_slip = 1; + float vehm_fr_slip = 2; + float vehm_rl_slip = 3; + float vehm_rr_slip = 4; +} \ No newline at end of file From bf380618f2aace0a7a1150d01adbd116f8eda8e8 Mon Sep 17 00:00:00 2001 From: walkermburns Date: Mon, 29 Apr 2024 23:40:48 -0400 Subject: [PATCH 03/18] Cooking on EthernetInterface --- lib/interfaces/include/EthernetInterface.h | 15 +++++++++++++++ lib/interfaces/src/EthernetInterface.cpp | 11 +++++++++++ platformio.ini | 1 + 3 files changed, 27 insertions(+) create mode 100644 lib/interfaces/include/EthernetInterface.h create mode 100644 lib/interfaces/src/EthernetInterface.cpp diff --git a/lib/interfaces/include/EthernetInterface.h b/lib/interfaces/include/EthernetInterface.h new file mode 100644 index 000000000..3dea930f8 --- /dev/null +++ b/lib/interfaces/include/EthernetInterface.h @@ -0,0 +1,15 @@ +#ifndef __ETHERNETINTERFACE_H__ +#define __ETHERNETINTERFACE_H__ + +#include +#include "QNEthernet.h" + +using namespace qindesign::network; + +EthernetUDP udp; + +uint8_t mac_address[6]; + +void init_ethernet_device(); + +#endif \ No newline at end of file diff --git a/lib/interfaces/src/EthernetInterface.cpp b/lib/interfaces/src/EthernetInterface.cpp new file mode 100644 index 000000000..2c95e824c --- /dev/null +++ b/lib/interfaces/src/EthernetInterface.cpp @@ -0,0 +1,11 @@ +#include "EthernetInterface.h" + +void init_ethernet_device() +{ + Ethernet.macAddress(mac_address); + + if (!Ethernet.begin()) + { + Serial.println("Failed to start Ethernet"); + } +} \ No newline at end of file diff --git a/platformio.ini b/platformio.ini index d69ab7739..2137484d4 100644 --- a/platformio.ini +++ b/platformio.ini @@ -48,6 +48,7 @@ lib_deps = https://github.com/hytech-racing/shared_firmware_systems.git https://github.com/RCMast3r/spi_libs https://github.com/tonton81/FlexCAN_T4 + https://github.com/ssilverman/QNEthernet.git https://github.com/RCMast3r/hytech_can#testing_new_inv_ids https://github.com/hytech-racing/HT_CAN/releases/download/80/can_lib.tar.gz git+ssh://git@github.com/hytech-racing/CASE_lib.git#v39 From 21df00adde7c769a4dacc6d054e7a043aa89594d Mon Sep 17 00:00:00 2001 From: walkermburns Date: Mon, 29 Apr 2024 23:45:09 -0400 Subject: [PATCH 04/18] fixing merges --- lib/interfaces/include/EthernetInterface.h | 24 ++++++++-------------- 1 file changed, 8 insertions(+), 16 deletions(-) diff --git a/lib/interfaces/include/EthernetInterface.h b/lib/interfaces/include/EthernetInterface.h index cd06774b9..afc70eab1 100644 --- a/lib/interfaces/include/EthernetInterface.h +++ b/lib/interfaces/include/EthernetInterface.h @@ -1,23 +1,15 @@ #ifndef ETHERNETINTERFACE #define ETHERNETINTERFACE -#include - -#include "circular_buffer.h" - +#include +#include -class EthernetInterface -{ -public: - EthernetInterface() {}; - void begin(int port); - /// @brief sends all udp packets in queue - void handle_sending(); - /// @brief receives all udp packets - void handle_recvs(); +EthernetUDP udp_ethernet; -private: - EthernetUDP udp_ethernet_; -} +void init_ethernet_device(); +/// @brief sends all udp packets in queue +void handle_sending(); +/// @brief receives all udp packets +void handle_recvs(); #endif \ No newline at end of file From 43c11a44684deba1b7477fdfbed12a6d074a2c9d Mon Sep 17 00:00:00 2001 From: Ben Hall Date: Tue, 30 Apr 2024 00:48:05 -0400 Subject: [PATCH 05/18] cooking on parameter server --- lib/interfaces/include/EthernetInterface.h | 10 ++++- lib/interfaces/include/ParamMsgInterface.h | 26 +++++++++++ lib/interfaces/include/ParamMsgInterface.tpp | 3 ++ lib/interfaces/src/EthernetInterface.cpp | 15 ++++--- lib/systems/include/ParameterSystem.h | 47 ++++++++++++++++++-- platformio.ini | 3 +- proto/ht_msgs.proto | 2 +- src/main.cpp | 1 - test/test_systems/main.cpp | 2 +- test/test_systems/param_system_test.h | 20 +++++++++ 10 files changed, 112 insertions(+), 17 deletions(-) create mode 100644 lib/interfaces/include/ParamMsgInterface.h create mode 100644 lib/interfaces/include/ParamMsgInterface.tpp create mode 100644 test/test_systems/param_system_test.h diff --git a/lib/interfaces/include/EthernetInterface.h b/lib/interfaces/include/EthernetInterface.h index afc70eab1..8e9184cd9 100644 --- a/lib/interfaces/include/EthernetInterface.h +++ b/lib/interfaces/include/EthernetInterface.h @@ -2,9 +2,10 @@ #define ETHERNETINTERFACE #include -#include +#include "ParamMsgInterface.h" +// #include -EthernetUDP udp_ethernet; +// EthernetUDP udp_ethernet; void init_ethernet_device(); /// @brief sends all udp packets in queue @@ -12,4 +13,9 @@ void handle_sending(); /// @brief receives all udp packets void handle_recvs(); +template +struct ETHInterfaces +{ + ParamMsgInterface *param_msg_interface; +}; #endif \ No newline at end of file diff --git a/lib/interfaces/include/ParamMsgInterface.h b/lib/interfaces/include/ParamMsgInterface.h new file mode 100644 index 000000000..25bdae9fb --- /dev/null +++ b/lib/interfaces/include/ParamMsgInterface.h @@ -0,0 +1,26 @@ +#ifndef PARAMMSGINTERFACE +#define PARAMMSGINTERFACE +#include "ht_msgs.pb.h" +#include "pb_encode.h" +#include "pb_decode.h" + +// needs to hold all of the parameter update messages (technically we can receive multiple in one loop tick) + +/// @brief +/// @tparam message_queue +template +class ParamMsgInterface +{ + +public: + ParamMsgInterface(){}; + + void receive_config_msg(ht_eth_config & config_msg); + +private: + // TODO this will get used later when we want to get all of the parameter values? + // message_queue *msg_out_queue_; + message_queue *msg_in_queue_; +}; + +#endif \ No newline at end of file diff --git a/lib/interfaces/include/ParamMsgInterface.tpp b/lib/interfaces/include/ParamMsgInterface.tpp new file mode 100644 index 000000000..4863308e0 --- /dev/null +++ b/lib/interfaces/include/ParamMsgInterface.tpp @@ -0,0 +1,3 @@ +#pragma once +#include "ParamMsgInterface.h" + diff --git a/lib/interfaces/src/EthernetInterface.cpp b/lib/interfaces/src/EthernetInterface.cpp index 2c95e824c..11d2cff1b 100644 --- a/lib/interfaces/src/EthernetInterface.cpp +++ b/lib/interfaces/src/EthernetInterface.cpp @@ -2,10 +2,13 @@ void init_ethernet_device() { - Ethernet.macAddress(mac_address); + // Ethernet.macAddress(mac_address); + + // if (!Ethernet.begin()) + // { + // Serial.println("Failed to start Ethernet"); + // } +} + + - if (!Ethernet.begin()) - { - Serial.println("Failed to start Ethernet"); - } -} \ No newline at end of file diff --git a/lib/systems/include/ParameterSystem.h b/lib/systems/include/ParameterSystem.h index 7359e8e52..4d455687d 100644 --- a/lib/systems/include/ParameterSystem.h +++ b/lib/systems/include/ParameterSystem.h @@ -1,16 +1,55 @@ #ifndef PARAMETERSYSTEM #define PARAMETERSYSTEM #include "Parameters.h" +#include "ParamMsgInterface.h" -template -void setParameter(T& paramType, float value) { +#include + +// A helper type trait to check if a type is one of a list of types +template +struct is_one_of : std::disjunction...> +{ +}; + +template +void set_parameter(T ¶mType, value_type value) +{ + static_assert(is_one_of::value, "set_parameter can only be used with int, bool, or float"); paramType.set(value); } -template -auto getParameter(T& paramType){ +template +auto get_parameter(T ¶mType) +{ return paramType.get(); } +void update_parameters_from_interface() +{ + +} + +/// @brief templated function used to set parameter based on unique id +/// @tparam param_type the value type (float, bool or int) +/// @tparam param_base_type the base pointer for that is being used in the unordered map that will be updated +/// @param name id of the param +/// @param out_map map that will be updated +/// @param value the value the id will be set to +template +void id_param_set_(const char *name, std::unordered_map &out_map, param_type value) +{ + out_map.at(name)->set(value); +} +/// @brief templated function used to get parameter based on unique id +/// @tparam param_type the value type (float, bool or int) +/// @tparam param_base_type the base pointer for that is being used in the unordered map +/// @param name the id of the param being retrieved +/// @param map the map to retrieve from +/// @return the value +template +auto id_param_get_(const char *name, const std::unordered_map &map) +{ + return map.get(name)->get(); +} #endif \ No newline at end of file diff --git a/platformio.ini b/platformio.ini index 14bceea85..fa885d404 100644 --- a/platformio.ini +++ b/platformio.ini @@ -45,7 +45,7 @@ upload_protocol = teensy-cli extra_scripts = pre:extra_script.py custom_nanopb_protos = + - + lib_deps = SPI Nanopb @@ -58,7 +58,6 @@ lib_deps = https://github.com/RCMast3r/hytech_can#testing_new_inv_ids https://github.com/hytech-racing/HT_CAN/releases/download/80/can_lib.tar.gz https://github.com/hytech-racing/HT_params/releases/download/3/HT_params_lib.tar.gz - https://github.com/tonton81/Circular_Buffer git+ssh://git@github.com/hytech-racing/CASE_lib.git#v39 diff --git a/proto/ht_msgs.proto b/proto/ht_msgs.proto index fe52c40a8..280bf9683 100644 --- a/proto/ht_msgs.proto +++ b/proto/ht_msgs.proto @@ -13,7 +13,7 @@ message config } message CASE_output -{ +{ float vehm_fl_slip = 1; float vehm_fr_slip = 2; float vehm_rl_slip = 3; diff --git a/src/main.cpp b/src/main.cpp index c4e0e23a8..72d090d1b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -237,7 +237,6 @@ void drivetrain_reset(); void setup() { - // initialize CAN communication init_all_CAN_devices(); diff --git a/test/test_systems/main.cpp b/test/test_systems/main.cpp index 4ac9fd06a..2c947a336 100644 --- a/test/test_systems/main.cpp +++ b/test/test_systems/main.cpp @@ -6,11 +6,11 @@ #include "drivetrain_system_test.h" #include "safety_system_test.h" #include "test_CASE.h" +#include "param_system_test.h" int main(int argc, char **argv) { - testing::InitGoogleMock(&argc, argv); if (RUN_ALL_TESTS()) ; diff --git a/test/test_systems/param_system_test.h b/test/test_systems/param_system_test.h new file mode 100644 index 000000000..7e7c30b03 --- /dev/null +++ b/test/test_systems/param_system_test.h @@ -0,0 +1,20 @@ +#ifndef PARAMS_SYSTEM_TEST +#define PARAMS_SYSTEM_TEST + +#include "ParameterSystem.h" + +// TODO may wanna do this with a fixed param lib, but for now the regular +// param lib should be fine +TEST(ParamSystemTest, test_set_params) { + int val = 20; + set_parameter(Parameters::CASE_TORQUE_MAXInstance, val); + EXPECT_EQ(Parameters::CASE_TORQUE_MAXInstance.get(), val); +} + +TEST(ParamSystemTest, test_set_get_params) { + int val = 20; + set_parameter(Parameters::CASE_TORQUE_MAXInstance, val); + EXPECT_EQ(get_parameter(Parameters::CASE_TORQUE_MAXInstance), val); +} + +#endif \ No newline at end of file From 9d2b83b4def4a948b304554a572c1c0af7714701 Mon Sep 17 00:00:00 2001 From: Ben Hall Date: Tue, 30 Apr 2024 13:22:05 -0400 Subject: [PATCH 06/18] working on getting protobuf messages handled --- lib/interfaces/include/EthernetInterface.h | 47 +++++++++++++++++++++- lib/interfaces/include/ParamMsgInterface.h | 25 +++++++++--- proto/ht_msgs.proto | 8 ++++ src/main.cpp | 1 - 4 files changed, 74 insertions(+), 7 deletions(-) diff --git a/lib/interfaces/include/EthernetInterface.h b/lib/interfaces/include/EthernetInterface.h index 8e9184cd9..fcdffeb1d 100644 --- a/lib/interfaces/include/EthernetInterface.h +++ b/lib/interfaces/include/EthernetInterface.h @@ -2,9 +2,14 @@ #define ETHERNETINTERFACE #include +#include "ht_msgs.pb.h" +#include "pb_encode.h" +#include "pb_decode.h" +#include "pb_common.h" #include "ParamMsgInterface.h" -// #include +#include +uint8_t union_port_ethernet_buffer[512]; // EthernetUDP udp_ethernet; void init_ethernet_device(); @@ -13,6 +18,46 @@ void handle_sending(); /// @brief receives all udp packets void handle_recvs(); +template +void append_to_interface_queue(interface * msg_interface_queue, msg_struct msg) +{ + uint8_t buf[sizeof(msg)]; + memmove(buf, &msg, sizeof(msg)) + msg_interface_queue->push_back(buf, sizeof(msg_struct)); +} + +void handle_pb_stream_union_msg(pb_istream_t *stream) +{ + ht_eth_big_union msg = ht_eth_big_union_init_zero; + if(pb_decode(stream, ht_eth_big_union_fields, &msg)) + { + switch(msg.which_msg) + { + case ht_eth_big_union_config_msg_tag: + ht_eth_config cfg_msg = msg.msg.config_msg; + uint8_t buf[sizeof(cfg_msg)]; + memmove(buf, &cfg_msg, sizeof(cfg_msg)) + param_msg_interface->push_back(buf, sizeof(ht_eth_config)); + break; + } + } +} + +void process_ethernet_port_buffer() +{ + // get the number of msgs in the queue to be de-serialized + while(union_port.available()) + { + int size = union_port.parsePacket(); + if(size > 0) + { + union_port.read(union_port_ethernet_buffer, size); + pb_istream_t stream = pb_istream_from_buffer(union_port_ethernet_buffer, size); + handle_pb_stream_union_msg(&stream); + } + } +} + template struct ETHInterfaces { diff --git a/lib/interfaces/include/ParamMsgInterface.h b/lib/interfaces/include/ParamMsgInterface.h index 25bdae9fb..7c983ea99 100644 --- a/lib/interfaces/include/ParamMsgInterface.h +++ b/lib/interfaces/include/ParamMsgInterface.h @@ -3,21 +3,36 @@ #include "ht_msgs.pb.h" #include "pb_encode.h" #include "pb_decode.h" +#include // needs to hold all of the parameter update messages (technically we can receive multiple in one loop tick) -/// @brief -/// @tparam message_queue +/// @brief +/// @tparam message_queue template -class ParamMsgInterface +class EthMsgInterface { public: - ParamMsgInterface(){}; + EthMsgInterface(){}; - void receive_config_msg(ht_eth_config & config_msg); + void receive_union_msg(pb_istream_t *stream) + { + ht_eth_big_union msg = ht_eth_big_union_init_zero; + if(pb_decode(stream, ht_eth_big_union_fields, &msg)) + { + + } + else { + return 1; + } + + } private: + + void receive_config_msg_(const ht_eth_config &config_msg); + void // TODO this will get used later when we want to get all of the parameter values? // message_queue *msg_out_queue_; message_queue *msg_in_queue_; diff --git a/proto/ht_msgs.proto b/proto/ht_msgs.proto index 280bf9683..6b9eec857 100644 --- a/proto/ht_msgs.proto +++ b/proto/ht_msgs.proto @@ -18,4 +18,12 @@ message CASE_output float vehm_fr_slip = 2; float vehm_rl_slip = 3; float vehm_rr_slip = 4; +} + +message big_union +{ + oneof msg { + config config_msg = 1; + CASE_output CASE_output_msg = 2; + } } \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 72d090d1b..ee9acf746 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -294,7 +294,6 @@ void setup() void loop() { - // Serial.println("test"); // get latest tick from sys clock SysTick_s curr_tick = sys_clock.tick(micros()); From 1b1d6204c2ccfc32f4b9c9a21303cb79220afd81 Mon Sep 17 00:00:00 2001 From: Ben Hall Date: Tue, 30 Apr 2024 14:03:39 -0400 Subject: [PATCH 07/18] pb ethernet receiving docs --- ETHERNET.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 ETHERNET.md diff --git a/ETHERNET.md b/ETHERNET.md new file mode 100644 index 000000000..c43db2b89 --- /dev/null +++ b/ETHERNET.md @@ -0,0 +1,17 @@ +```mermaid +flowchart TD + ethernet[ethernet port] --> qnethernet[encoded ethernet data packets] + qnethernet --> port[protobuf union msgs port] + port --> ht_eth[HT ethernet interface] + ht_eth --> union_dec[union msgs splitter] + union_dec --> int1[interface 1 message buffer] + union_dec --> int2[interface 2 message buffer] + union_dec --> intN[interface N message buffer] +``` + +## explanation + +### receiving +Packets stream over ethernet and hit the ethernet port itself. The ethernet library has an internal queue for the UDP packets received. We know that a specific port id (say 4521) all messages will be a protobuf union msg that can contain only one of the types of messages that we will be sending (config control, TCU status, CASE msgs, etc.) and the union decoder method in the ethernet interface itself will handle this. The ethernet shall be able to receive multiple messages in one loop (of a limited number) and in between iterations of the loop the ethernet driver will hold the un-parsed messages in it's queue. + +The union message decoder will handle parsing of all of the messages in the queue and will be able to determine what message the union pb packet holds. If all messages of a specific type are needed, The message decoder will then add the decoded particular protobuf message struct to the message queue in the respective interface that way if multiple messages of the same type appear in one loop iteration they can all be processed by the underlying system / interface. If the interface just needs the latest version of a message each loop, the decoder will just update the message instance in its respective interface. From 458728950403ab1d413db0a25b21555defa4e095 Mon Sep 17 00:00:00 2001 From: walkermburns Date: Tue, 30 Apr 2024 14:58:21 -0400 Subject: [PATCH 08/18] Initial UDPSocket template --- lib/interfaces/include/EthernetInterface.h | 49 +++++++++++++++++++--- lib/interfaces/src/EthernetInterface.cpp | 21 +++++++--- 2 files changed, 58 insertions(+), 12 deletions(-) diff --git a/lib/interfaces/include/EthernetInterface.h b/lib/interfaces/include/EthernetInterface.h index fcdffeb1d..21a4ac76e 100644 --- a/lib/interfaces/include/EthernetInterface.h +++ b/lib/interfaces/include/EthernetInterface.h @@ -2,6 +2,7 @@ #define ETHERNETINTERFACE #include +#include #include "ht_msgs.pb.h" #include "pb_encode.h" #include "pb_decode.h" @@ -10,13 +11,49 @@ #include uint8_t union_port_ethernet_buffer[512]; -// EthernetUDP udp_ethernet; -void init_ethernet_device(); -/// @brief sends all udp packets in queue -void handle_sending(); -/// @brief receives all udp packets -void handle_recvs(); +IPAddress ip_addr; +uint8_t mac_address[6]; +bool connection_successful = false; + +void init_ethernet_device(IPAddress ip); + +/* + The UDPSocket class holds a reference to the + individual EthernetUDP socket. It is constructed + using a port which the UDP socket will use + when it is initialized. + Importantly, this must be done after the ethernet + device is initialized with init_ethernet_device(); +*/ +class UDPSocket +{ +private: + EthernetUDP udp_; + const uint16_t port_; +public: + UDPSocket(uint16_t port): port_(port) {}; + + /* Must be called in setup() to communicate with socket */ + void initialize_socket() {udp_.begin(port_)} + + /** + * @brief Sends a udp packet from the provided buffer + * @param data a constant uint8_t pointer to the data buffer + * @param len the length of the data in the buffer to send + * @return Returns a bool for if the buffer was sent successfully + */ + bool send(const uint8_t *data, int len); + + /** + * @brief Receives a udp packet, will return the size of packets received + * and move the data to the location of the buffer pointer passed in + * @param data a uint8_t pointer to the data buffer to fill + * @return Returns the size of the received packet, or -1 when there + * are no more packets available on the socket + */ + int receive(uint8_t *data); +} template void append_to_interface_queue(interface * msg_interface_queue, msg_struct msg) diff --git a/lib/interfaces/src/EthernetInterface.cpp b/lib/interfaces/src/EthernetInterface.cpp index 11d2cff1b..3a8bceebf 100644 --- a/lib/interfaces/src/EthernetInterface.cpp +++ b/lib/interfaces/src/EthernetInterface.cpp @@ -1,14 +1,23 @@ #include "EthernetInterface.h" -void init_ethernet_device() +void init_ethernet_device(IPAddress ip) { - // Ethernet.macAddress(mac_address); + ip_addr = ip; - // if (!Ethernet.begin()) - // { - // Serial.println("Failed to start Ethernet"); - // } + Ethernet.macAddress(mac_address); + + if (!Ethernet.begin(mac_address, ip)) + { + Serial.println("Failed to start Ethernet"); + } else {connection_successful = true;} } +bool UDPSocket::send(const uint8_t *data, int len) +{ +} +int UDPSocket::receive(uint8_t *data) +{ + +} \ No newline at end of file From d7a191ac7cde72280ef6f61e6ac65cfb0a6c2e3d Mon Sep 17 00:00:00 2001 From: Ben Hall Date: Tue, 30 Apr 2024 16:46:14 -0400 Subject: [PATCH 09/18] removing BS --- lib/interfaces/include/EthernetInterface.h | 66 ------------------- lib/interfaces/include/ParamMsgInterface.h | 41 ------------ lib/interfaces/include/ParamMsgInterface.tpp | 3 - lib/interfaces/include/ParameterInterface.h | 27 ++++++++ lib/interfaces/include/ProtobufMsgInterface.h | 31 +++++++++ lib/interfaces/src/EthernetInterface.cpp | 2 +- lib/systems/include/ParameterSystem.h | 55 ---------------- lib/systems/library.json | 3 +- platformio.ini | 2 - proto/ht_msgs.proto | 12 ++-- src/main.cpp | 2 +- test/test_interfaces/test_ethernet.h | 0 12 files changed, 66 insertions(+), 178 deletions(-) delete mode 100644 lib/interfaces/include/EthernetInterface.h delete mode 100644 lib/interfaces/include/ParamMsgInterface.h delete mode 100644 lib/interfaces/include/ParamMsgInterface.tpp create mode 100644 lib/interfaces/include/ParameterInterface.h create mode 100644 lib/interfaces/include/ProtobufMsgInterface.h delete mode 100644 lib/systems/include/ParameterSystem.h create mode 100644 test/test_interfaces/test_ethernet.h diff --git a/lib/interfaces/include/EthernetInterface.h b/lib/interfaces/include/EthernetInterface.h deleted file mode 100644 index fcdffeb1d..000000000 --- a/lib/interfaces/include/EthernetInterface.h +++ /dev/null @@ -1,66 +0,0 @@ -#ifndef ETHERNETINTERFACE -#define ETHERNETINTERFACE - -#include -#include "ht_msgs.pb.h" -#include "pb_encode.h" -#include "pb_decode.h" -#include "pb_common.h" -#include "ParamMsgInterface.h" -#include - -uint8_t union_port_ethernet_buffer[512]; -// EthernetUDP udp_ethernet; - -void init_ethernet_device(); -/// @brief sends all udp packets in queue -void handle_sending(); -/// @brief receives all udp packets -void handle_recvs(); - -template -void append_to_interface_queue(interface * msg_interface_queue, msg_struct msg) -{ - uint8_t buf[sizeof(msg)]; - memmove(buf, &msg, sizeof(msg)) - msg_interface_queue->push_back(buf, sizeof(msg_struct)); -} - -void handle_pb_stream_union_msg(pb_istream_t *stream) -{ - ht_eth_big_union msg = ht_eth_big_union_init_zero; - if(pb_decode(stream, ht_eth_big_union_fields, &msg)) - { - switch(msg.which_msg) - { - case ht_eth_big_union_config_msg_tag: - ht_eth_config cfg_msg = msg.msg.config_msg; - uint8_t buf[sizeof(cfg_msg)]; - memmove(buf, &cfg_msg, sizeof(cfg_msg)) - param_msg_interface->push_back(buf, sizeof(ht_eth_config)); - break; - } - } -} - -void process_ethernet_port_buffer() -{ - // get the number of msgs in the queue to be de-serialized - while(union_port.available()) - { - int size = union_port.parsePacket(); - if(size > 0) - { - union_port.read(union_port_ethernet_buffer, size); - pb_istream_t stream = pb_istream_from_buffer(union_port_ethernet_buffer, size); - handle_pb_stream_union_msg(&stream); - } - } -} - -template -struct ETHInterfaces -{ - ParamMsgInterface *param_msg_interface; -}; -#endif \ No newline at end of file diff --git a/lib/interfaces/include/ParamMsgInterface.h b/lib/interfaces/include/ParamMsgInterface.h deleted file mode 100644 index 7c983ea99..000000000 --- a/lib/interfaces/include/ParamMsgInterface.h +++ /dev/null @@ -1,41 +0,0 @@ -#ifndef PARAMMSGINTERFACE -#define PARAMMSGINTERFACE -#include "ht_msgs.pb.h" -#include "pb_encode.h" -#include "pb_decode.h" -#include - -// needs to hold all of the parameter update messages (technically we can receive multiple in one loop tick) - -/// @brief -/// @tparam message_queue -template -class EthMsgInterface -{ - -public: - EthMsgInterface(){}; - - void receive_union_msg(pb_istream_t *stream) - { - ht_eth_big_union msg = ht_eth_big_union_init_zero; - if(pb_decode(stream, ht_eth_big_union_fields, &msg)) - { - - } - else { - return 1; - } - - } - -private: - - void receive_config_msg_(const ht_eth_config &config_msg); - void - // TODO this will get used later when we want to get all of the parameter values? - // message_queue *msg_out_queue_; - message_queue *msg_in_queue_; -}; - -#endif \ No newline at end of file diff --git a/lib/interfaces/include/ParamMsgInterface.tpp b/lib/interfaces/include/ParamMsgInterface.tpp deleted file mode 100644 index 4863308e0..000000000 --- a/lib/interfaces/include/ParamMsgInterface.tpp +++ /dev/null @@ -1,3 +0,0 @@ -#pragma once -#include "ParamMsgInterface.h" - diff --git a/lib/interfaces/include/ParameterInterface.h b/lib/interfaces/include/ParameterInterface.h new file mode 100644 index 000000000..ca495f541 --- /dev/null +++ b/lib/interfaces/include/ParameterInterface.h @@ -0,0 +1,27 @@ +#ifndef PARAMETERINTERFACE +#define PARAMETERINTERFACE + +#include "ht_msgs.pb.h" + +// yes, i know this is a singleton. im prototyping rn. +class ParameterInterface +{ +public: + ParameterInterface() + { + config_ = {}; + } + void update_config(const ht_eth_config &config) + { + config_ = config; + } + const ht_eth_config &get_config() + { + return config_; + } + +private: + ht_eth_config config_; +}; + +#endif \ No newline at end of file diff --git a/lib/interfaces/include/ProtobufMsgInterface.h b/lib/interfaces/include/ProtobufMsgInterface.h new file mode 100644 index 000000000..a29f08d48 --- /dev/null +++ b/lib/interfaces/include/ProtobufMsgInterface.h @@ -0,0 +1,31 @@ +#ifndef PROTOBUFMSGINTERFACE +#define PROTOBUFMSGINTERFACE + +#include "ht_msgs.pb.h" +#include "pb_encode.h" +#include "pb_decode.h" +#include "pb_common.h" +#include "ParameterInterface.h" + +struct ETHInterfaces +{ + ParameterInterface* param_interface; +}; + +void handle_pb_stream_union_msg(pb_istream_t *stream, ETHInterfaces& interfaces) +{ + ht_eth_big_union msg = ht_eth_big_union_init_zero; + if (pb_decode(stream, ht_eth_big_union_fields, &msg)) + { + switch (msg.which_msg) + { + case ht_eth_big_union_config_msg_tag: + interfaces.param_interface->update_config(msg.msg.config_msg); + break; + default: + break; + } + } +} + +#endif \ No newline at end of file diff --git a/lib/interfaces/src/EthernetInterface.cpp b/lib/interfaces/src/EthernetInterface.cpp index 11d2cff1b..6880f7906 100644 --- a/lib/interfaces/src/EthernetInterface.cpp +++ b/lib/interfaces/src/EthernetInterface.cpp @@ -1,4 +1,4 @@ -#include "EthernetInterface.h" +// #include "EthernetInterface.h" void init_ethernet_device() { diff --git a/lib/systems/include/ParameterSystem.h b/lib/systems/include/ParameterSystem.h deleted file mode 100644 index 4d455687d..000000000 --- a/lib/systems/include/ParameterSystem.h +++ /dev/null @@ -1,55 +0,0 @@ -#ifndef PARAMETERSYSTEM -#define PARAMETERSYSTEM -#include "Parameters.h" -#include "ParamMsgInterface.h" - -#include - -// A helper type trait to check if a type is one of a list of types -template -struct is_one_of : std::disjunction...> -{ -}; - -template -void set_parameter(T ¶mType, value_type value) -{ - static_assert(is_one_of::value, "set_parameter can only be used with int, bool, or float"); - paramType.set(value); -} - -template -auto get_parameter(T ¶mType) -{ - return paramType.get(); -} - -void update_parameters_from_interface() -{ - -} - -/// @brief templated function used to set parameter based on unique id -/// @tparam param_type the value type (float, bool or int) -/// @tparam param_base_type the base pointer for that is being used in the unordered map that will be updated -/// @param name id of the param -/// @param out_map map that will be updated -/// @param value the value the id will be set to -template -void id_param_set_(const char *name, std::unordered_map &out_map, param_type value) -{ - out_map.at(name)->set(value); -} -/// @brief templated function used to get parameter based on unique id -/// @tparam param_type the value type (float, bool or int) -/// @tparam param_base_type the base pointer for that is being used in the unordered map -/// @param name the id of the param being retrieved -/// @param map the map to retrieve from -/// @return the value -template -auto id_param_get_(const char *name, const std::unordered_map &map) -{ - return map.get(name)->get(); -} - -#endif \ No newline at end of file diff --git a/lib/systems/library.json b/lib/systems/library.json index f2cc84cc5..50a076984 100644 --- a/lib/systems/library.json +++ b/lib/systems/library.json @@ -7,8 +7,7 @@ "shared-interfaces-lib": "*", "shared-systems-lib": "*", "CASE_lib": "*", - "shared_data": "*", - "HT_params_lib": "*" + "shared_data": "*" }, "frameworks": "*", "platforms": "*" diff --git a/platformio.ini b/platformio.ini index fa885d404..6db6e6e06 100644 --- a/platformio.ini +++ b/platformio.ini @@ -11,7 +11,6 @@ lib_ignore = test_ignore= test_interfaces* lib_deps= - https://github.com/hytech-racing/HT_params/releases/download/3/HT_params_lib.tar.gz git+ssh://git@github.com/hytech-racing/CASE_lib.git#v39 https://github.com/hytech-racing/shared_firmware_systems.git @@ -57,7 +56,6 @@ lib_deps = https://github.com/ssilverman/QNEthernet.git https://github.com/RCMast3r/hytech_can#testing_new_inv_ids https://github.com/hytech-racing/HT_CAN/releases/download/80/can_lib.tar.gz - https://github.com/hytech-racing/HT_params/releases/download/3/HT_params_lib.tar.gz git+ssh://git@github.com/hytech-racing/CASE_lib.git#v39 diff --git a/proto/ht_msgs.proto b/proto/ht_msgs.proto index 6b9eec857..bbd65be4c 100644 --- a/proto/ht_msgs.proto +++ b/proto/ht_msgs.proto @@ -3,13 +3,11 @@ syntax = "proto3"; package ht_eth; message config -{ - string id = 1; - oneof config_val { - bool bool_val = 2; - float float_val = 3; - int32 int_val = 4; - } +{ + float amk_max_rpm = 1; + float case_torque_max = 2; + float front_brake_bias = 3; + bool tcs_active = 4; } message CASE_output diff --git a/src/main.cpp b/src/main.cpp index ee9acf746..05321f60a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,7 +1,7 @@ /* Include files */ /* System Includes*/ #include -#include "ParameterSystem.h" +#include "ParameterInterface.h" /* Libraries */ #include "FlexCAN_T4.h" #include "HyTech_CAN.h" diff --git a/test/test_interfaces/test_ethernet.h b/test/test_interfaces/test_ethernet.h new file mode 100644 index 000000000..e69de29bb From b20435b8e032acdb15a84cbe9ebcfbd5cb1f5ce2 Mon Sep 17 00:00:00 2001 From: walkermburns Date: Tue, 30 Apr 2024 17:44:20 -0400 Subject: [PATCH 10/18] Updating ethernet interface --- lib/interfaces/include/EthernetInterface.h | 108 +++++---------------- lib/interfaces/src/EthernetInterface.cpp | 23 +++-- 2 files changed, 34 insertions(+), 97 deletions(-) diff --git a/lib/interfaces/include/EthernetInterface.h b/lib/interfaces/include/EthernetInterface.h index 21a4ac76e..225867bd4 100644 --- a/lib/interfaces/include/EthernetInterface.h +++ b/lib/interfaces/include/EthernetInterface.h @@ -1,103 +1,41 @@ -#ifndef ETHERNETINTERFACE -#define ETHERNETINTERFACE +#ifndef ETHERNETINTERFACE_H +#define ETHERNETINTERFACE_H -#include #include -#include "ht_msgs.pb.h" -#include "pb_encode.h" -#include "pb_decode.h" -#include "pb_common.h" -#include "ParamMsgInterface.h" #include -uint8_t union_port_ethernet_buffer[512]; +namespace qn = qindesign::network; -IPAddress ip_addr; -uint8_t mac_address[6]; -bool connection_successful = false; - -void init_ethernet_device(IPAddress ip); - -/* - The UDPSocket class holds a reference to the - individual EthernetUDP socket. It is constructed - using a port which the UDP socket will use - when it is initialized. - Importantly, this must be done after the ethernet - device is initialized with init_ethernet_device(); -*/ -class UDPSocket +namespace EthParams { -private: - EthernetUDP udp_; - const uint16_t port_; -public: - UDPSocket(uint16_t port): port_(port) {}; + extern IPAddress ip_addr_; - /* Must be called in setup() to communicate with socket */ - void initialize_socket() {udp_.begin(port_)} + const IPAddress default_netmask(255, 255, 255, 0); + const IPAddress default_gateway(192, 168, 0, 1); - /** - * @brief Sends a udp packet from the provided buffer - * @param data a constant uint8_t pointer to the data buffer - * @param len the length of the data in the buffer to send - * @return Returns a bool for if the buffer was sent successfully - */ - bool send(const uint8_t *data, int len); - - /** - * @brief Receives a udp packet, will return the size of packets received - * and move the data to the location of the buffer pointer passed in - * @param data a uint8_t pointer to the data buffer to fill - * @return Returns the size of the received packet, or -1 when there - * are no more packets available on the socket - */ - int receive(uint8_t *data); + const default_buffer_size = 512; } -template -void append_to_interface_queue(interface * msg_interface_queue, msg_struct msg) -{ - uint8_t buf[sizeof(msg)]; - memmove(buf, &msg, sizeof(msg)) - msg_interface_queue->push_back(buf, sizeof(msg_struct)); -} +void init_ethernet_device(IPAddress ip); -void handle_pb_stream_union_msg(pb_istream_t *stream) -{ - ht_eth_big_union msg = ht_eth_big_union_init_zero; - if(pb_decode(stream, ht_eth_big_union_fields, &msg)) - { - switch(msg.which_msg) - { - case ht_eth_big_union_config_msg_tag: - ht_eth_config cfg_msg = msg.msg.config_msg; - uint8_t buf[sizeof(cfg_msg)]; - memmove(buf, &cfg_msg, sizeof(cfg_msg)) - param_msg_interface->push_back(buf, sizeof(ht_eth_config)); - break; - } - } -} +const uint16_t pb_port = 2000; -void process_ethernet_port_buffer() +class EthernetInterface { - // get the number of msgs in the queue to be de-serialized - while(union_port.available()) +private: + uint8_t ethernet_rx_buffer[EthParams::default_buffer_size]; + + qn::EthernetUDP protobuf_socket; + +public: + EthernetInterface(uint16_t buffer_size) {} + + void initialize_sockets() { - int size = union_port.parsePacket(); - if(size > 0) - { - union_port.read(union_port_ethernet_buffer, size); - pb_istream_t stream = pb_istream_from_buffer(union_port_ethernet_buffer, size); - handle_pb_stream_union_msg(&stream); - } + protobuf_socket.begin(pb_port); } -} -template -struct ETHInterfaces -{ - ParamMsgInterface *param_msg_interface; + void receive(); + void send(); }; #endif \ No newline at end of file diff --git a/lib/interfaces/src/EthernetInterface.cpp b/lib/interfaces/src/EthernetInterface.cpp index 3a8bceebf..961086ce1 100644 --- a/lib/interfaces/src/EthernetInterface.cpp +++ b/lib/interfaces/src/EthernetInterface.cpp @@ -1,23 +1,22 @@ #include "EthernetInterface.h" +IPAddress ip_addr_; + void init_ethernet_device(IPAddress ip) { - ip_addr = ip; - - Ethernet.macAddress(mac_address); - - if (!Ethernet.begin(mac_address, ip)) - { - Serial.println("Failed to start Ethernet"); - } else {connection_successful = true;} + ip_addr_ = ip; + qn::Ethernet.begin(ip, EthParams::default_netmask, EthParams::default_gateway); } -bool UDPSocket::send(const uint8_t *data, int len) +void EthernetInterface::send() { - + int num = 3; + IPAddress ip_2(192, 168, 0, 24); + memcpy(ethernet_rx_buffer, &num, sizeof(num)); + protobuf_socket.send(ip_2, pb_port, ethernet_rx_buffer, sizeof(ethernet_rx_buffer)); } -int UDPSocket::receive(uint8_t *data) +void EthernetInterface::receive() { - + } \ No newline at end of file From 62a1d978efad632146276fa984d21e9481d3a276 Mon Sep 17 00:00:00 2001 From: walkermburns Date: Tue, 30 Apr 2024 17:47:28 -0400 Subject: [PATCH 11/18] fixing ethernetinterface compile errors --- lib/interfaces/include/EthernetInterface.h | 2 +- lib/interfaces/src/EthernetInterface.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/interfaces/include/EthernetInterface.h b/lib/interfaces/include/EthernetInterface.h index 225867bd4..a36a6b8e1 100644 --- a/lib/interfaces/include/EthernetInterface.h +++ b/lib/interfaces/include/EthernetInterface.h @@ -13,7 +13,7 @@ namespace EthParams const IPAddress default_netmask(255, 255, 255, 0); const IPAddress default_gateway(192, 168, 0, 1); - const default_buffer_size = 512; + const uint16_t default_buffer_size = 512; } void init_ethernet_device(IPAddress ip); diff --git a/lib/interfaces/src/EthernetInterface.cpp b/lib/interfaces/src/EthernetInterface.cpp index c864f2ff1..961086ce1 100644 --- a/lib/interfaces/src/EthernetInterface.cpp +++ b/lib/interfaces/src/EthernetInterface.cpp @@ -1,4 +1,4 @@ -// #include "EthernetInterface.h" +#include "EthernetInterface.h" IPAddress ip_addr_; From 11f49d68002a5bb6b73bda34658e0e86ed6e3d06 Mon Sep 17 00:00:00 2001 From: walkermburns Date: Tue, 30 Apr 2024 18:15:38 -0400 Subject: [PATCH 12/18] Rewriting send() function in EthernetInterface --- lib/interfaces/include/EthernetInterface.h | 9 +++++++-- lib/interfaces/src/EthernetInterface.cpp | 10 +++++----- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/lib/interfaces/include/EthernetInterface.h b/lib/interfaces/include/EthernetInterface.h index a36a6b8e1..65cc37c63 100644 --- a/lib/interfaces/include/EthernetInterface.h +++ b/lib/interfaces/include/EthernetInterface.h @@ -10,6 +10,11 @@ namespace EthParams { extern IPAddress ip_addr_; + const IPAddress default_MCU_ip(192, 168, 1, 30); + const IPAddress default_TCU_ip(192, 168, 1, 69); + + const uint16_t default_protobuf_port = 2000; + const IPAddress default_netmask(255, 255, 255, 0); const IPAddress default_gateway(192, 168, 0, 1); @@ -32,10 +37,10 @@ class EthernetInterface void initialize_sockets() { - protobuf_socket.begin(pb_port); + protobuf_socket.begin(EthParams::default_protobuf_port); } + void send(uint8_t *buf, uint16_t len); void receive(); - void send(); }; #endif \ No newline at end of file diff --git a/lib/interfaces/src/EthernetInterface.cpp b/lib/interfaces/src/EthernetInterface.cpp index 961086ce1..01745fc27 100644 --- a/lib/interfaces/src/EthernetInterface.cpp +++ b/lib/interfaces/src/EthernetInterface.cpp @@ -5,18 +5,18 @@ IPAddress ip_addr_; void init_ethernet_device(IPAddress ip) { ip_addr_ = ip; + qn::Ethernet.begin(ip, EthParams::default_netmask, EthParams::default_gateway); } -void EthernetInterface::send() +void EthernetInterface::send(uint8_t *buf, uint16_t len) { - int num = 3; - IPAddress ip_2(192, 168, 0, 24); - memcpy(ethernet_rx_buffer, &num, sizeof(num)); - protobuf_socket.send(ip_2, pb_port, ethernet_rx_buffer, sizeof(ethernet_rx_buffer)); + protobuf_socket.send(EthParams::default_TCU_ip, pb_port, buf, len); } void EthernetInterface::receive() { + + } \ No newline at end of file From 3b39475eb73290ecfb63afd1ed04bc2294204689 Mon Sep 17 00:00:00 2001 From: walkermburns Date: Tue, 30 Apr 2024 18:16:36 -0400 Subject: [PATCH 13/18] removed buffer size from EthernetInterface constructor --- lib/interfaces/include/EthernetInterface.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/interfaces/include/EthernetInterface.h b/lib/interfaces/include/EthernetInterface.h index 65cc37c63..d8b8459e2 100644 --- a/lib/interfaces/include/EthernetInterface.h +++ b/lib/interfaces/include/EthernetInterface.h @@ -33,7 +33,7 @@ class EthernetInterface qn::EthernetUDP protobuf_socket; public: - EthernetInterface(uint16_t buffer_size) {} + EthernetInterface() {} void initialize_sockets() { From cb7c7d86d4f9e9938f0291231b932d660898e62f Mon Sep 17 00:00:00 2001 From: Ben Hall Date: Tue, 30 Apr 2024 18:37:04 -0400 Subject: [PATCH 14/18] adding stuff in --- lib/interfaces/include/ProtobufMsgInterface.h | 20 ++++++++++++++++++- lib/interfaces/src/ProtobufMsgInterface.cpp | 3 +++ platformio.ini | 2 ++ 3 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 lib/interfaces/src/ProtobufMsgInterface.cpp diff --git a/lib/interfaces/include/ProtobufMsgInterface.h b/lib/interfaces/include/ProtobufMsgInterface.h index a29f08d48..41f6b3b09 100644 --- a/lib/interfaces/include/ProtobufMsgInterface.h +++ b/lib/interfaces/include/ProtobufMsgInterface.h @@ -6,13 +6,17 @@ #include "pb_decode.h" #include "pb_common.h" #include "ParameterInterface.h" +#include "circular_buffer.h" + + +extern Circular_Buffer pb_tx_buffer; struct ETHInterfaces { ParameterInterface* param_interface; }; -void handle_pb_stream_union_msg(pb_istream_t *stream, ETHInterfaces& interfaces) +void recv_pb_stream_union_msg(pb_istream_t *stream, ETHInterfaces& interfaces) { ht_eth_big_union msg = ht_eth_big_union_init_zero; if (pb_decode(stream, ht_eth_big_union_fields, &msg)) @@ -28,4 +32,18 @@ void handle_pb_stream_union_msg(pb_istream_t *stream, ETHInterfaces& interfaces) } } +template +void send_pb_stream_msg(Circular_Buffer& buffer, EthernetInterface *eth_int) +{ + while (buffer.available()) + { + msg_type msg; + uint8_t buf[sizeof(msg_type)]; + buffer.pop_front(buf, sizeof(msg_type)); + memmove(&msg, buf, sizeof(msg)); + // eth_int->send(); + } +} + + #endif \ No newline at end of file diff --git a/lib/interfaces/src/ProtobufMsgInterface.cpp b/lib/interfaces/src/ProtobufMsgInterface.cpp new file mode 100644 index 000000000..1618771dd --- /dev/null +++ b/lib/interfaces/src/ProtobufMsgInterface.cpp @@ -0,0 +1,3 @@ +#include "ProtobufMsgInterface.h" + +Circular_Buffer pb_tx_buffer; diff --git a/platformio.ini b/platformio.ini index 6db6e6e06..f96130b6b 100644 --- a/platformio.ini +++ b/platformio.ini @@ -12,6 +12,7 @@ test_ignore= test_interfaces* lib_deps= git+ssh://git@github.com/hytech-racing/CASE_lib.git#v39 + https://github.com/tonton81/Circular_Buffer.git https://github.com/hytech-racing/shared_firmware_systems.git [env:teensy41] @@ -49,6 +50,7 @@ lib_deps = SPI Nanopb ssilverman/QNEthernet@^0.27.0 + https://github.com/tonton81/Circular_Buffer.git https://github.com/hytech-racing/shared_firmware_interfaces.git https://github.com/hytech-racing/shared_firmware_systems.git https://github.com/RCMast3r/spi_libs From 3ea8ab21ed02469d4cdec3db31f8bd7a5c598639 Mon Sep 17 00:00:00 2001 From: walkermburns Date: Tue, 30 Apr 2024 22:32:02 -0400 Subject: [PATCH 15/18] Moving Ethernet UDP socket into main and Using NativeEthernet instead --- include/MCU_rev15_defs.h | 17 ++++++++ lib/interfaces/include/EthernetInterface.h | 46 ---------------------- lib/interfaces/src/EthernetInterface.cpp | 22 ----------- platformio.ini | 5 ++- src/main.cpp | 12 ++++++ 5 files changed, 32 insertions(+), 70 deletions(-) delete mode 100644 lib/interfaces/include/EthernetInterface.h delete mode 100644 lib/interfaces/src/EthernetInterface.cpp diff --git a/include/MCU_rev15_defs.h b/include/MCU_rev15_defs.h index 1dedb0ce6..3ed446cd0 100644 --- a/include/MCU_rev15_defs.h +++ b/include/MCU_rev15_defs.h @@ -2,6 +2,23 @@ #define __MCU15_H__ #include "PedalsSystem.h" +#include "NativeEthernet.h" + +namespace EthParams +{ + uint8_t default_MCU_MAC_address[6] = + {0x04, 0xe9, 0xe5, 0x10, 0x1f, 0x22}; + + const IPAddress default_MCU_ip(192, 168, 1, 30); + const IPAddress default_TCU_ip(192, 168, 1, 69); + + const uint16_t default_protobuf_port = 2000; + + const IPAddress default_netmask(255, 255, 255, 0); + const IPAddress default_gateway(192, 168, 0, 1); + + const uint16_t default_buffer_size = 512; +} // pindefs const int ADC1_CS = 34; diff --git a/lib/interfaces/include/EthernetInterface.h b/lib/interfaces/include/EthernetInterface.h deleted file mode 100644 index d8b8459e2..000000000 --- a/lib/interfaces/include/EthernetInterface.h +++ /dev/null @@ -1,46 +0,0 @@ -#ifndef ETHERNETINTERFACE_H -#define ETHERNETINTERFACE_H - -#include -#include - -namespace qn = qindesign::network; - -namespace EthParams -{ - extern IPAddress ip_addr_; - - const IPAddress default_MCU_ip(192, 168, 1, 30); - const IPAddress default_TCU_ip(192, 168, 1, 69); - - const uint16_t default_protobuf_port = 2000; - - const IPAddress default_netmask(255, 255, 255, 0); - const IPAddress default_gateway(192, 168, 0, 1); - - const uint16_t default_buffer_size = 512; -} - -void init_ethernet_device(IPAddress ip); - -const uint16_t pb_port = 2000; - -class EthernetInterface -{ -private: - uint8_t ethernet_rx_buffer[EthParams::default_buffer_size]; - - qn::EthernetUDP protobuf_socket; - -public: - EthernetInterface() {} - - void initialize_sockets() - { - protobuf_socket.begin(EthParams::default_protobuf_port); - } - - void send(uint8_t *buf, uint16_t len); - void receive(); -}; -#endif \ No newline at end of file diff --git a/lib/interfaces/src/EthernetInterface.cpp b/lib/interfaces/src/EthernetInterface.cpp deleted file mode 100644 index 01745fc27..000000000 --- a/lib/interfaces/src/EthernetInterface.cpp +++ /dev/null @@ -1,22 +0,0 @@ -#include "EthernetInterface.h" - -IPAddress ip_addr_; - -void init_ethernet_device(IPAddress ip) -{ - ip_addr_ = ip; - - qn::Ethernet.begin(ip, EthParams::default_netmask, EthParams::default_gateway); -} - -void EthernetInterface::send(uint8_t *buf, uint16_t len) -{ - protobuf_socket.send(EthParams::default_TCU_ip, pb_port, buf, len); -} - -void EthernetInterface::receive() -{ - - - -} \ No newline at end of file diff --git a/platformio.ini b/platformio.ini index f96130b6b..019844b66 100644 --- a/platformio.ini +++ b/platformio.ini @@ -49,13 +49,14 @@ custom_nanopb_protos = lib_deps = SPI Nanopb - ssilverman/QNEthernet@^0.27.0 https://github.com/tonton81/Circular_Buffer.git https://github.com/hytech-racing/shared_firmware_interfaces.git https://github.com/hytech-racing/shared_firmware_systems.git https://github.com/RCMast3r/spi_libs https://github.com/tonton81/FlexCAN_T4 - https://github.com/ssilverman/QNEthernet.git + https://github.com/vjmuzik/NativeEthernet + https://github.com/vjmuzik/FNET + https://github.com/RCMast3r/hytech_can#testing_new_inv_ids https://github.com/hytech-racing/HT_CAN/releases/download/80/can_lib.tar.gz git+ssh://git@github.com/hytech-racing/CASE_lib.git#v39 diff --git a/src/main.cpp b/src/main.cpp index 05321f60a..38dd9fbeb 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -6,6 +6,8 @@ #include "FlexCAN_T4.h" #include "HyTech_CAN.h" #include "MCU_rev15_defs.h" +#include "NativeEthernet.h" +#include "NativeEthernetUDP.h" // /* Interfaces */ #include "HytechCANInterface.h" @@ -85,6 +87,8 @@ const PedalsParams brake_params = { DATA SOURCES */ +EthernetUDP protobuf_socket; + /* Two CAN lines on Main ECU rev15 */ FlexCAN_T4 INV_CAN; // Inverter CAN (now both are on same line) FlexCAN_T4 TELEM_CAN; // telemetry CAN (basically everything except inverters) @@ -240,6 +244,14 @@ void setup() // initialize CAN communication init_all_CAN_devices(); + Ethernet.begin(EthParams::default_MCU_MAC_address, EthParams::default_MCU_ip); + protobuf_socket.begin(EthParams::default_protobuf_port); + + /* Do this to send message VVV */ + // protobuf_socket.beginPacket(EthParams::default_TCU_ip, EthParams::default_protobuf_port); + // protobuf_socket.write(buf, len); + // protobuf_socker.endPacket(); + SPI.begin(); a1.init(); a2.init(); From cd4dbd315fc3935fd40e5dff922ef7620d8d3e24 Mon Sep 17 00:00:00 2001 From: Ben Hall Date: Wed, 1 May 2024 04:25:27 -0400 Subject: [PATCH 16/18] she lives! --- lib/interfaces/include/ParameterInterface.h | 6 +-- lib/interfaces/include/ProtobufMsgInterface.h | 49 +++++++++++-------- lib/interfaces/src/ProtobufMsgInterface.cpp | 3 -- lib/systems/include/TorqueControllers.h | 2 +- platformio.ini | 45 +++++++++++++++-- proto/ht_msgs.proto | 31 +++++------- src/main.cpp | 3 +- test/test_interfaces/run_embedded_tests.cpp | 22 +++++---- test/test_interfaces/test_ethernet.h | 32 ++++++++++++ 9 files changed, 129 insertions(+), 64 deletions(-) delete mode 100644 lib/interfaces/src/ProtobufMsgInterface.cpp diff --git a/lib/interfaces/include/ParameterInterface.h b/lib/interfaces/include/ParameterInterface.h index ca495f541..c75ea7f69 100644 --- a/lib/interfaces/include/ParameterInterface.h +++ b/lib/interfaces/include/ParameterInterface.h @@ -11,17 +11,17 @@ class ParameterInterface { config_ = {}; } - void update_config(const ht_eth_config &config) + void update_config(const config &config) { config_ = config; } - const ht_eth_config &get_config() + const config &get_config() { return config_; } private: - ht_eth_config config_; + config config_; }; #endif \ No newline at end of file diff --git a/lib/interfaces/include/ProtobufMsgInterface.h b/lib/interfaces/include/ProtobufMsgInterface.h index 41f6b3b09..b5c074c76 100644 --- a/lib/interfaces/include/ProtobufMsgInterface.h +++ b/lib/interfaces/include/ProtobufMsgInterface.h @@ -7,24 +7,45 @@ #include "pb_common.h" #include "ParameterInterface.h" #include "circular_buffer.h" +#include "NativeEthernet.h" +#include "MCU_rev15_defs.h" -extern Circular_Buffer pb_tx_buffer; - struct ETHInterfaces { ParameterInterface* param_interface; }; -void recv_pb_stream_union_msg(pb_istream_t *stream, 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 +void handle_ethernet_socket_receive(EthernetUDP* socket, recv_function_t recv_function, ETHInterfaces& interfaces) +{ + int packet_size = socket->parsePacket(); + if(packet_size > 0) + { + // Serial.println("packet size"); + // Serial.println(packet_size); + uint8_t buffer[EthParams::default_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); + } +} + +void recv_pb_stream_union_msg(const uint8_t *buffer, size_t packet_size, ETHInterfaces& interfaces) { - ht_eth_big_union msg = ht_eth_big_union_init_zero; - if (pb_decode(stream, ht_eth_big_union_fields, &msg)) + pb_istream_t stream = pb_istream_from_buffer(buffer, packet_size); + // Serial.println("running the function"); + HT_ETH_Union msg = HT_ETH_Union_init_zero; + if (pb_decode(&stream, HT_ETH_Union_fields, &msg)) { - switch (msg.which_msg) + // Serial.println("decoded!"); + + switch (msg.which_type_union) { - case ht_eth_big_union_config_msg_tag: - interfaces.param_interface->update_config(msg.msg.config_msg); + case HT_ETH_Union_config__tag: + interfaces.param_interface->update_config(msg.type_union.config_); break; default: break; @@ -32,18 +53,6 @@ void recv_pb_stream_union_msg(pb_istream_t *stream, ETHInterfaces& interfaces) } } -template -void send_pb_stream_msg(Circular_Buffer& buffer, EthernetInterface *eth_int) -{ - while (buffer.available()) - { - msg_type msg; - uint8_t buf[sizeof(msg_type)]; - buffer.pop_front(buf, sizeof(msg_type)); - memmove(&msg, buf, sizeof(msg)); - // eth_int->send(); - } -} #endif \ No newline at end of file diff --git a/lib/interfaces/src/ProtobufMsgInterface.cpp b/lib/interfaces/src/ProtobufMsgInterface.cpp deleted file mode 100644 index 1618771dd..000000000 --- a/lib/interfaces/src/ProtobufMsgInterface.cpp +++ /dev/null @@ -1,3 +0,0 @@ -#include "ProtobufMsgInterface.h" - -Circular_Buffer pb_tx_buffer; diff --git a/lib/systems/include/TorqueControllers.h b/lib/systems/include/TorqueControllers.h index e5dcc4fba..18ed91e28 100644 --- a/lib/systems/include/TorqueControllers.h +++ b/lib/systems/include/TorqueControllers.h @@ -15,7 +15,7 @@ #include "accel_lookup.h" #include "TorqueControllersData.h" -#include "PID_TV.h" + /* CONTROLLER CONSTANTS */ diff --git a/platformio.ini b/platformio.ini index 019844b66..738fdbee3 100644 --- a/platformio.ini +++ b/platformio.ini @@ -30,7 +30,9 @@ build_src_filter = -<**/*.cpp> + build_unflags = -std=gnu++11 -build_flags = -std=c++17 +build_flags = + -std=c++17 + -D TEENSY_OPT_SMALLEST_CODE check_tool = clangtidy check_src_filters = + @@ -49,14 +51,11 @@ custom_nanopb_protos = lib_deps = SPI Nanopb - https://github.com/tonton81/Circular_Buffer.git https://github.com/hytech-racing/shared_firmware_interfaces.git https://github.com/hytech-racing/shared_firmware_systems.git - https://github.com/RCMast3r/spi_libs https://github.com/tonton81/FlexCAN_T4 https://github.com/vjmuzik/NativeEthernet https://github.com/vjmuzik/FNET - https://github.com/RCMast3r/hytech_can#testing_new_inv_ids https://github.com/hytech-racing/HT_CAN/releases/download/80/can_lib.tar.gz git+ssh://git@github.com/hytech-racing/CASE_lib.git#v39 @@ -89,6 +88,42 @@ upload_protocol = teensy-cli lib_deps = https://github.com/tonton81/FlexCAN_T4 https://github.com/RCMast3r/hytech_can - https://github.com/hytech-racing/HT_CAN/releases/latest/download/can_lib.tar.gz + https://github.com/hytech-racing/HT_CAN/releases/download/80/can_lib.tar.gz + +[env:test_ethernet_on_teensy] +lib_ignore = + mock_interfaces + mock_systems +; including only the current main file for compiling to keep old main still around for now while +; refactoring +; build_src_filter = +; -<**/*.cpp> +test_ignore= + test_systems* +build_src_filter = + -<**/*.cpp> + + +build_unflags = -std=gnu++11 +build_flags = + -std=c++17 + -D TEENSY_OPT_SMALLEST_CODE +platform = teensy +board = teensy41 +framework = arduino +upload_protocol = teensy-cli +custom_nanopb_protos = + + +lib_deps = + SPI + Nanopb + https://github.com/hytech-racing/shared_firmware_interfaces.git + https://github.com/hytech-racing/shared_firmware_systems.git + https://github.com/tonton81/FlexCAN_T4 + https://github.com/vjmuzik/NativeEthernet + https://github.com/vjmuzik/FNET + https://github.com/RCMast3r/hytech_can#testing_new_inv_ids + https://github.com/hytech-racing/HT_CAN/releases/download/80/can_lib.tar.gz + git+ssh://git@github.com/hytech-racing/CASE_lib.git#v39 + diff --git a/proto/ht_msgs.proto b/proto/ht_msgs.proto index bbd65be4c..f8c04a76c 100644 --- a/proto/ht_msgs.proto +++ b/proto/ht_msgs.proto @@ -1,27 +1,18 @@ syntax = "proto3"; -package ht_eth; - -message config -{ - float amk_max_rpm = 1; - float case_torque_max = 2; - float front_brake_bias = 3; - bool tcs_active = 4; -} - -message CASE_output +message CASE_msg { float vehm_fl_slip = 1; - float vehm_fr_slip = 2; - float vehm_rl_slip = 3; - float vehm_rr_slip = 4; +} +message config { + float CASE_TORQUE_MAX = 1; + float FRONT_BRAKE_BIAS = 2; + bool TCS_ACTIVE = 3; } -message big_union -{ - oneof msg { - config config_msg = 1; - CASE_output CASE_output_msg = 2; - } +message HT_ETH_Union { + oneof type_union { + CASE_msg case_msg_ = 1; + config config_ = 2; + } } \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 38dd9fbeb..01e13810d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -7,7 +7,6 @@ #include "HyTech_CAN.h" #include "MCU_rev15_defs.h" #include "NativeEthernet.h" -#include "NativeEthernetUDP.h" // /* Interfaces */ #include "HytechCANInterface.h" @@ -246,7 +245,7 @@ void setup() Ethernet.begin(EthParams::default_MCU_MAC_address, EthParams::default_MCU_ip); protobuf_socket.begin(EthParams::default_protobuf_port); - + /* Do this to send message VVV */ // protobuf_socket.beginPacket(EthParams::default_TCU_ip, EthParams::default_protobuf_port); // protobuf_socket.write(buf, len); diff --git a/test/test_interfaces/run_embedded_tests.cpp b/test/test_interfaces/run_embedded_tests.cpp index 083fd0eaa..7351a3fb3 100644 --- a/test/test_interfaces/run_embedded_tests.cpp +++ b/test/test_interfaces/run_embedded_tests.cpp @@ -1,15 +1,16 @@ #include -#include - -#include "AMS_interface_test.h" -#include "dashboard_interface_test.h" -#include "Watchdog_interface_test.h" +// #include +// #include "AMS_interface_test.h" +// #include "dashboard_interface_test.h" +// #include "Watchdog_interface_test.h" +#include "test_ethernet.h" // #include "Telemetry_interface_test.h" void setUp(void) { // init_can_interface(); + init_ethernet_device(); } void tearDown(void) @@ -21,12 +22,13 @@ int runUnityTests(void) { UNITY_BEGIN(); /* TEST DASHBOARD */ - RUN_TEST(test_dashboard_unpacking_can_message); - RUN_TEST(test_dashboard_circular_buffer); + RUN_TEST(test_ethernet); + // RUN_TEST(test_dashboard_unpacking_can_message); + // RUN_TEST(test_dashboard_circular_buffer); /* TEST AMS */ - RUN_TEST(test_AMS_heartbeat); + // RUN_TEST(test_AMS_heartbeat); /* TEST WATCHDOG */ - RUN_TEST(test_watchdog_kick); + // RUN_TEST(test_watchdog_kick); // testing can interface // RUN_TEST(test_can_interface_send) // RUN_TEST(test_can_interface_send_and_receive_raw) @@ -38,7 +40,7 @@ int runUnityTests(void) void setup() { - delay(500); + delay(200); runUnityTests(); } diff --git a/test/test_interfaces/test_ethernet.h b/test/test_interfaces/test_ethernet.h index e69de29bb..6aea50213 100644 --- a/test/test_interfaces/test_ethernet.h +++ b/test/test_interfaces/test_ethernet.h @@ -0,0 +1,32 @@ +#ifndef TEST_ETHERNET +#define TEST_ETHERNET + +#include +#include + +#include "ProtobufMsgInterface.h" +#include "MCU_rev15_defs.h" + +EthernetUDP protobuf_socket; +ParameterInterface params; +ETHInterfaces ethernet_interfaces = {¶ms}; + +void init_ethernet_device() +{ + Ethernet.begin(EthParams::default_MCU_MAC_address, EthParams::default_MCU_ip); + protobuf_socket.begin(EthParams::default_protobuf_port); +} + +void test_ethernet() +{ + while(1) + { + handle_ethernet_socket_receive(&protobuf_socket, &recv_pb_stream_union_msg, ethernet_interfaces); + Serial.println(params.get_config().CASE_TORQUE_MAX); + // Serial.println("asdf"); + } + + TEST_ASSERT_EQUAL(1, 1); +} + +#endif \ No newline at end of file From c9da1cf6252baece6ac2653ee36758ec4053acb5 Mon Sep 17 00:00:00 2001 From: Ben Hall Date: Wed, 1 May 2024 23:10:03 -0400 Subject: [PATCH 17/18] actually working in the test pog --- include/MCU_rev15_defs.h | 3 +- lib/interfaces/include/ParameterInterface.h | 29 +++++++++--- lib/interfaces/include/ProtobufMsgInterface.h | 24 +++++++++- platformio.ini | 12 ++--- proto/ht_msgs.proto | 18 -------- src/main.cpp | 6 ++- src/main_ethernet_test.cpp | 44 +++++++++++++++++++ test/test_interfaces/run_embedded_tests.cpp | 4 +- test/test_interfaces/test_ethernet.h | 32 -------------- 9 files changed, 101 insertions(+), 71 deletions(-) delete mode 100644 proto/ht_msgs.proto create mode 100644 src/main_ethernet_test.cpp delete mode 100644 test/test_interfaces/test_ethernet.h diff --git a/include/MCU_rev15_defs.h b/include/MCU_rev15_defs.h index 3ed446cd0..c2ff57e93 100644 --- a/include/MCU_rev15_defs.h +++ b/include/MCU_rev15_defs.h @@ -12,7 +12,8 @@ namespace EthParams const IPAddress default_MCU_ip(192, 168, 1, 30); const IPAddress default_TCU_ip(192, 168, 1, 69); - const uint16_t default_protobuf_port = 2000; + const uint16_t default_protobuf_send_port = 2000; + const uint16_t default_protobuf_recv_port = 2001; const IPAddress default_netmask(255, 255, 255, 0); const IPAddress default_gateway(192, 168, 0, 1); diff --git a/lib/interfaces/include/ParameterInterface.h b/lib/interfaces/include/ParameterInterface.h index c75ea7f69..639e5e2e2 100644 --- a/lib/interfaces/include/ParameterInterface.h +++ b/lib/interfaces/include/ParameterInterface.h @@ -1,27 +1,42 @@ #ifndef PARAMETERINTERFACE #define PARAMETERINTERFACE - -#include "ht_msgs.pb.h" +#include "MCUStateMachine.h" +#include "ht_eth.pb.h" // yes, i know this is a singleton. im prototyping rn. class ParameterInterface { public: - ParameterInterface() + ParameterInterface(CAR_STATE& car_state_ref): current_car_state_(car_state_ref), params_need_sending_(false), config_({}) {} + + void update_config(const config &config) { - config_ = {}; + if(static_cast(current_car_state_) < 5 ){ + config_ = config; + } + } - void update_config(const config &config) + + void set_params_need_sending() + { + params_need_sending_ = true; + } + void reset_params_need_sending() { - config_ = config; + params_need_sending_ = false; } - const config &get_config() + bool params_need_sending() { return params_need_sending_; } + + config get_config() { return config_; } private: + const CAR_STATE& current_car_state_; + bool params_need_sending_ = false; config config_; + }; #endif \ No newline at end of file diff --git a/lib/interfaces/include/ProtobufMsgInterface.h b/lib/interfaces/include/ProtobufMsgInterface.h index b5c074c76..3002c23bf 100644 --- a/lib/interfaces/include/ProtobufMsgInterface.h +++ b/lib/interfaces/include/ProtobufMsgInterface.h @@ -1,7 +1,7 @@ #ifndef PROTOBUFMSGINTERFACE #define PROTOBUFMSGINTERFACE -#include "ht_msgs.pb.h" +#include "ht_eth.pb.h" #include "pb_encode.h" #include "pb_decode.h" #include "pb_common.h" @@ -14,6 +14,7 @@ struct ETHInterfaces { ParameterInterface* param_interface; + }; using recv_function_t = void (*)(const uint8_t* buffer, size_t packet_size, ETHInterfaces& interfaces); @@ -33,6 +34,24 @@ void handle_ethernet_socket_receive(EthernetUDP* socket, recv_function_t recv_fu } } +template +bool handle_ethernet_socket_send_pb(EthernetUDP* socket, const pb_struct& msg, const pb_msgdesc_t* msg_desc) +{ + socket->beginPacket(EthParams::default_TCU_ip, EthParams::default_protobuf_send_port); + + uint8_t buffer[EthParams::default_buffer_size]; + pb_ostream_t stream = pb_ostream_from_buffer(buffer, sizeof(buffer)); + if (!pb_encode(&stream, msg_desc, &msg)) { + // You can handle error more explicitly by looking at stream.errmsg + return false; + } + auto message_length = stream.bytes_written; + socket->write(buffer, message_length); + socket->endPacket(); + return true; + +} + void recv_pb_stream_union_msg(const uint8_t *buffer, size_t packet_size, ETHInterfaces& interfaces) { pb_istream_t stream = pb_istream_from_buffer(buffer, packet_size); @@ -47,6 +66,9 @@ void recv_pb_stream_union_msg(const uint8_t *buffer, size_t packet_size, ETHInte case HT_ETH_Union_config__tag: interfaces.param_interface->update_config(msg.type_union.config_); break; + case HT_ETH_Union_get_config__tag: + interfaces.param_interface->set_params_need_sending(); + break; default: break; } diff --git a/platformio.ini b/platformio.ini index 738fdbee3..0ea5d03af 100644 --- a/platformio.ini +++ b/platformio.ini @@ -45,12 +45,9 @@ board = teensy41 framework = arduino upload_protocol = teensy-cli extra_scripts = pre:extra_script.py -custom_nanopb_protos = - + lib_deps = SPI - Nanopb https://github.com/hytech-racing/shared_firmware_interfaces.git https://github.com/hytech-racing/shared_firmware_systems.git https://github.com/tonton81/FlexCAN_T4 @@ -59,7 +56,8 @@ lib_deps = https://github.com/RCMast3r/hytech_can#testing_new_inv_ids https://github.com/hytech-racing/HT_CAN/releases/download/80/can_lib.tar.gz git+ssh://git@github.com/hytech-racing/CASE_lib.git#v39 - + Nanopb + https://github.com/hytech-racing/HT_params/releases/download/2024-05-02T01_39_59/ht_eth_pb_lib.tar.gz [env:test_can_on_teensy] lib_ignore = @@ -103,7 +101,7 @@ test_ignore= test_systems* build_src_filter = -<**/*.cpp> - + + + build_unflags = -std=gnu++11 build_flags = -std=c++17 @@ -112,12 +110,10 @@ platform = teensy board = teensy41 framework = arduino upload_protocol = teensy-cli -custom_nanopb_protos = - + - lib_deps = SPI Nanopb + https://github.com/hytech-racing/HT_params/releases/download/2024-05-02T01_39_59/ht_eth_pb_lib.tar.gz https://github.com/hytech-racing/shared_firmware_interfaces.git https://github.com/hytech-racing/shared_firmware_systems.git https://github.com/tonton81/FlexCAN_T4 diff --git a/proto/ht_msgs.proto b/proto/ht_msgs.proto deleted file mode 100644 index f8c04a76c..000000000 --- a/proto/ht_msgs.proto +++ /dev/null @@ -1,18 +0,0 @@ -syntax = "proto3"; - -message CASE_msg -{ - float vehm_fl_slip = 1; -} -message config { - float CASE_TORQUE_MAX = 1; - float FRONT_BRAKE_BIAS = 2; - bool TCS_ACTIVE = 3; -} - -message HT_ETH_Union { - oneof type_union { - CASE_msg case_msg_ = 1; - config config_ = 2; - } -} \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 01e13810d..17af2e8e3 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -86,7 +86,8 @@ const PedalsParams brake_params = { DATA SOURCES */ -EthernetUDP protobuf_socket; +EthernetUDP protobuf_send_socket; +EthernetUDP protobuf_recv_socket; /* Two CAN lines on Main ECU rev15 */ FlexCAN_T4 INV_CAN; // Inverter CAN (now both are on same line) @@ -244,7 +245,8 @@ void setup() init_all_CAN_devices(); Ethernet.begin(EthParams::default_MCU_MAC_address, EthParams::default_MCU_ip); - protobuf_socket.begin(EthParams::default_protobuf_port); + protobuf_send_socket.begin(EthParams::default_protobuf_send_port); + protobuf_recv_socket.begin(EthParams::default_protobuf_recv_port); /* Do this to send message VVV */ // protobuf_socket.beginPacket(EthParams::default_TCU_ip, EthParams::default_protobuf_port); diff --git a/src/main_ethernet_test.cpp b/src/main_ethernet_test.cpp new file mode 100644 index 000000000..c93536216 --- /dev/null +++ b/src/main_ethernet_test.cpp @@ -0,0 +1,44 @@ +#include + + +#include "ProtobufMsgInterface.h" +#include "MCU_rev15_defs.h" + +EthernetUDP protobuf_send_socket; +EthernetUDP protobuf_recv_socket; +CAR_STATE state = CAR_STATE::STARTUP; +ParameterInterface params(state); +ETHInterfaces ethernet_interfaces = {¶ms}; + +void init_ethernet_device() +{ + 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); +} + +void test_ethernet() +{ + + handle_ethernet_socket_receive(&protobuf_recv_socket, &recv_pb_stream_union_msg, ethernet_interfaces); + if(params.params_need_sending()) + { + + auto config = params.get_config(); + if(!handle_ethernet_socket_send_pb(&protobuf_send_socket, config, config_fields)){ + } + params.reset_params_need_sending(); + } + +} + +void setup() +{ + init_ethernet_device(); +} + +void loop() +{ + test_ethernet(); + // Serial.println("loopin"); +} \ No newline at end of file diff --git a/test/test_interfaces/run_embedded_tests.cpp b/test/test_interfaces/run_embedded_tests.cpp index 7351a3fb3..5eb8cc1f7 100644 --- a/test/test_interfaces/run_embedded_tests.cpp +++ b/test/test_interfaces/run_embedded_tests.cpp @@ -4,7 +4,7 @@ // #include "AMS_interface_test.h" // #include "dashboard_interface_test.h" // #include "Watchdog_interface_test.h" -#include "test_ethernet.h" +// #include "test_ethernet.h" // #include "Telemetry_interface_test.h" void setUp(void) @@ -22,7 +22,7 @@ int runUnityTests(void) { UNITY_BEGIN(); /* TEST DASHBOARD */ - RUN_TEST(test_ethernet); + // RUN_TEST(test_ethernet); // RUN_TEST(test_dashboard_unpacking_can_message); // RUN_TEST(test_dashboard_circular_buffer); /* TEST AMS */ diff --git a/test/test_interfaces/test_ethernet.h b/test/test_interfaces/test_ethernet.h deleted file mode 100644 index 6aea50213..000000000 --- a/test/test_interfaces/test_ethernet.h +++ /dev/null @@ -1,32 +0,0 @@ -#ifndef TEST_ETHERNET -#define TEST_ETHERNET - -#include -#include - -#include "ProtobufMsgInterface.h" -#include "MCU_rev15_defs.h" - -EthernetUDP protobuf_socket; -ParameterInterface params; -ETHInterfaces ethernet_interfaces = {¶ms}; - -void init_ethernet_device() -{ - Ethernet.begin(EthParams::default_MCU_MAC_address, EthParams::default_MCU_ip); - protobuf_socket.begin(EthParams::default_protobuf_port); -} - -void test_ethernet() -{ - while(1) - { - handle_ethernet_socket_receive(&protobuf_socket, &recv_pb_stream_union_msg, ethernet_interfaces); - Serial.println(params.get_config().CASE_TORQUE_MAX); - // Serial.println("asdf"); - } - - TEST_ASSERT_EQUAL(1, 1); -} - -#endif \ No newline at end of file From 14a3ea8d06db36d732974f931ca6a23af90e27a4 Mon Sep 17 00:00:00 2001 From: Ben Hall Date: Thu, 2 May 2024 01:41:36 -0400 Subject: [PATCH 18/18] cooking --- lib/interfaces/include/ParameterInterface.h | 19 +++++---- lib/interfaces/include/ProtobufMsgInterface.h | 4 +- lib/systems/include/CASESystem.h | 1 + src/main.cpp | 39 ++++++++++++++----- src/main_ethernet_test.cpp | 1 - 5 files changed, 42 insertions(+), 22 deletions(-) diff --git a/lib/interfaces/include/ParameterInterface.h b/lib/interfaces/include/ParameterInterface.h index 639e5e2e2..109423627 100644 --- a/lib/interfaces/include/ParameterInterface.h +++ b/lib/interfaces/include/ParameterInterface.h @@ -4,11 +4,16 @@ #include "ht_eth.pb.h" // yes, i know this is a singleton. im prototyping rn. +// TODO review if I can just give this a pointer to an ethernet port class ParameterInterface { public: - ParameterInterface(CAR_STATE& car_state_ref): current_car_state_(car_state_ref), params_need_sending_(false), config_({}) {} + ParameterInterface(): current_car_state_(CAR_STATE::STARTUP), params_need_sending_(false), config_({}) {} + void update_car_state(const CAR_STATE& state) + { + current_car_state_ = state; + } void update_config(const config &config) { if(static_cast(current_car_state_) < 5 ){ @@ -16,7 +21,10 @@ class ParameterInterface } } - + config get_config() + { + return config_; + } void set_params_need_sending() { params_need_sending_ = true; @@ -26,14 +34,9 @@ class ParameterInterface params_need_sending_ = false; } bool params_need_sending() { return params_need_sending_; } - - config get_config() - { - return config_; - } private: - const CAR_STATE& current_car_state_; + CAR_STATE current_car_state_; bool params_need_sending_ = false; config config_; diff --git a/lib/interfaces/include/ProtobufMsgInterface.h b/lib/interfaces/include/ProtobufMsgInterface.h index 3002c23bf..455d27745 100644 --- a/lib/interfaces/include/ProtobufMsgInterface.h +++ b/lib/interfaces/include/ProtobufMsgInterface.h @@ -14,7 +14,6 @@ struct ETHInterfaces { ParameterInterface* param_interface; - }; using recv_function_t = void (*)(const uint8_t* buffer, size_t packet_size, ETHInterfaces& interfaces); @@ -49,13 +48,12 @@ bool handle_ethernet_socket_send_pb(EthernetUDP* socket, const pb_struct& msg, c socket->write(buffer, message_length); socket->endPacket(); return true; - } +// void recv_pb_stream_union_msg(const uint8_t *buffer, size_t packet_size, ETHInterfaces& interfaces) { pb_istream_t stream = pb_istream_from_buffer(buffer, packet_size); - // Serial.println("running the function"); HT_ETH_Union msg = HT_ETH_Union_init_zero; if (pb_decode(&stream, HT_ETH_Union_fields, &msg)) { diff --git a/lib/systems/include/CASESystem.h b/lib/systems/include/CASESystem.h index 8cf556e1a..48261b1d8 100644 --- a/lib/systems/include/CASESystem.h +++ b/lib/systems/include/CASESystem.h @@ -7,6 +7,7 @@ #include "DrivetrainSystem.h" #include "SteeringSystem.h" #include "MCUStateMachine.h" +#include "ProtobufMsgInterface.h" struct CASEConfiguration { diff --git a/src/main.cpp b/src/main.cpp index 17af2e8e3..83a96a28b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -107,6 +107,8 @@ OrbisBR10 steering1(&Serial5); // /* // INTERFACES // */ +ParameterInterface param_interface; +ETHInterfaces ethernet_interfaces = {¶m_interface}; VNInterface vn_interface(&CAN3_txBuffer); DashboardInterface dashboard(&CAN3_txBuffer); AMSInterface ams_interface(8); @@ -235,6 +237,8 @@ void tick_all_systems(const SysTick_s ¤t_system_tick); /* Reset inverters */ void drivetrain_reset(); +void handle_ethernet_interface_comms(); + /* SETUP */ @@ -247,7 +251,7 @@ void setup() 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); - + /* Do this to send message VVV */ // protobuf_socket.beginPacket(EthParams::default_TCU_ip, EthParams::default_protobuf_port); // protobuf_socket.write(buf, len); @@ -309,6 +313,8 @@ 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); @@ -329,6 +335,9 @@ void loop() // tick state machine fsm.tick_state_machine(curr_tick.millis); + // give the state of the car to the param interface + param_interface.update_car_state(fsm.get_state()); + // tick safety system safety_system.software_shutdown(curr_tick); @@ -449,15 +458,6 @@ void tick_all_systems(const SysTick_s ¤t_system_tick) { // tick pedals system - // Serial.println("accel1"); - // Serial.println(a1.get().conversions[MCU15_ACCEL1_CHANNEL].raw); - // Serial.println("accel2"); - // Serial.println(a1.get().conversions[MCU15_ACCEL2_CHANNEL].raw); - // Serial.println("brake1"); - // Serial.println(a1.get().conversions[MCU15_BRAKE1_CHANNEL].raw); - // Serial.println("brake2"); - // Serial.println(a1.get().conversions[MCU15_BRAKE2_CHANNEL].raw); - pedals_system.tick( current_system_tick, a1.get().conversions[MCU15_ACCEL1_CHANNEL], @@ -502,3 +502,22 @@ void tick_all_systems(const SysTick_s ¤t_system_tick) vn_interface.get_vn_struct(), controller_output); } + +void handle_ethernet_interface_comms() +{ + // 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 + handle_ethernet_socket_receive(&protobuf_recv_socket, &recv_pb_stream_union_msg, ethernet_interfaces); + + // this is just kinda here i know. + if (param_interface.params_need_sending()) + { + auto config = param_interface.get_config(); + if (!handle_ethernet_socket_send_pb(&protobuf_send_socket, config, config_fields)) + { + // TODO this means that something bad has happend + } + param_interface.reset_params_need_sending(); + } +} \ No newline at end of file diff --git a/src/main_ethernet_test.cpp b/src/main_ethernet_test.cpp index c93536216..9c92421fa 100644 --- a/src/main_ethernet_test.cpp +++ b/src/main_ethernet_test.cpp @@ -23,7 +23,6 @@ void test_ethernet() handle_ethernet_socket_receive(&protobuf_recv_socket, &recv_pb_stream_union_msg, ethernet_interfaces); if(params.params_need_sending()) { - auto config = params.get_config(); if(!handle_ethernet_socket_send_pb(&protobuf_send_socket, config, config_fields)){ }