From 33927553e2d755b6e375701fca9e8ae4d4ba8171 Mon Sep 17 00:00:00 2001 From: Ikubimu Date: Thu, 26 Jun 2025 17:41:01 +0200 Subject: [PATCH 01/25] something fix or not --- Core/Inc/VCU/Actuators/Pinout.hpp | 2 +- Core/Inc/VCU/Communcations/Ethernet.hpp | 1 + Core/Inc/VCU/VCU.hpp | 2 +- Core/Src/VCU/Communications/Ethernet.cpp | 94 +++++++++++++++--------- Core/Src/VCU/VCU.cpp | 49 ++++++------ 5 files changed, 88 insertions(+), 60 deletions(-) diff --git a/Core/Inc/VCU/Actuators/Pinout.hpp b/Core/Inc/VCU/Actuators/Pinout.hpp index aa392e9..9843d98 100644 --- a/Core/Inc/VCU/Actuators/Pinout.hpp +++ b/Core/Inc/VCU/Actuators/Pinout.hpp @@ -9,7 +9,7 @@ #define REED1_PIN PD11 #define REED2_PIN PD10 -#define REED3_PIN PG4 +#define REED3_PIN PG2 #define REED4_PIN PG3 #define REED5_PIN PD8 #define REED6_PIN PD9 diff --git a/Core/Inc/VCU/Communcations/Ethernet.hpp b/Core/Inc/VCU/Communcations/Ethernet.hpp index 0695953..6d6663f 100644 --- a/Core/Inc/VCU/Communcations/Ethernet.hpp +++ b/Core/Inc/VCU/Communcations/Ethernet.hpp @@ -136,6 +136,7 @@ class Ethernet{ void update(); bool connected(); void initialize_state_orders(); + void send_packets(); private: diff --git a/Core/Inc/VCU/VCU.hpp b/Core/Inc/VCU/VCU.hpp index 8694df1..34332a1 100644 --- a/Core/Inc/VCU/VCU.hpp +++ b/Core/Inc/VCU/VCU.hpp @@ -29,7 +29,7 @@ class VCU{ static StateMachine GeneralStateMachine; static StateMachine OperationalStateMachine; - Communications::Ethernet ethernet; + Communications::Ethernet *ethernet; void initialize_state_machines(); diff --git a/Core/Src/VCU/Communications/Ethernet.cpp b/Core/Src/VCU/Communications/Ethernet.cpp index d3d9954..0b307ec 100644 --- a/Core/Src/VCU/Communications/Ethernet.cpp +++ b/Core/Src/VCU/Communications/Ethernet.cpp @@ -1,5 +1,7 @@ #include "VCU/Communcations/Ethernet.hpp" +static bool flag_send_packets = false; + namespace Communications { StateMachine* Ethernet::GeneralStateMachine = nullptr; @@ -28,13 +30,18 @@ namespace Communications { Flags_ready Ethernet::flags_ready{}; -Ethernet::Ethernet(StateMachine* GeneralStateMachine, StateMachine* OperationalStateMachine,Actuators::Actuators* actuators, Actuators::Brakes* brakes) { +Ethernet::Ethernet(StateMachine* GeneralStateMachine, StateMachine* OperationalStateMachine,Actuators::Actuators* actuators, Actuators::Brakes* brakes) +: +Control_station(VCU_IP,local_port), +packets_endpoint(VCU_IP, udp_controlstation_port,control_station_ip, udp_controlstation_port) + + { this->GeneralStateMachine = GeneralStateMachine; this->OperationalStateMachine = OperationalStateMachine; this->Actuators = actuators; this->Brakes = brakes; - Control_station=ServerSocket(VCU_IP,local_port); + // Control_station=ServerSocket(VCU_IP,local_port); //LO comento para probar sin tener todo conectado // Socket_PCU = Socket(VCU_IP,pcu_port,PCU_IP,local_port); // Socket_HVSCU = Socket(VCU_IP,hvscu_port,HVSCU_IP,local_port); @@ -42,7 +49,7 @@ Ethernet::Ethernet(StateMachine* GeneralStateMachine, StateMachine* OperationalS // Socket_LCU = Socket(VCU_IP,lcu_port,LCU_IP,local_port); // Socket_BLCU = Socket(VCU_IP,blcu_port,BLCU_IP,local_port); - packets_endpoint =DatagramSocket(VCU_IP, udp_controlstation_port,control_station_ip, udp_port); + // packets_endpoint =DatagramSocket(VCU_IP, udp_controlstation_port,control_station_ip, udp_port); Socket_to_board[Boards::PCU] = &Socket_PCU; Socket_to_board[Boards::HVSCU] = &Socket_HVSCU; @@ -64,15 +71,23 @@ Ethernet::Ethernet(StateMachine* GeneralStateMachine, StateMachine* OperationalS Tapes_enable = new HeapPacket(Packets_id::Tapes_enable, &brakes->Tape_enabled); Time::register_low_precision_alarm(16, [&]() { - packets_endpoint.send_packet(*Reeds); - packets_endpoint.send_packet(*flow); - packets_endpoint.send_packet(*Regulator); - packets_endpoint.send_packet(*Pressure); - // packets_endpoint.send_packet(*Tapes); + + flag_send_packets = true; }); } + +void Ethernet::send_packets() +{ + + packets_endpoint.send_packet(*Reeds); + packets_endpoint.send_packet(*flow); + packets_endpoint.send_packet(*Regulator); + packets_endpoint.send_packet(*Pressure); + // packets_endpoint.send_packet(*Tapes); +} + void Ethernet::recieve_order(Boards board, HeapStateOrder* Order,Orders_id id){//No hace falta mandar la placa if(Order->remote_ip==nullptr){ ErrorHandler("Order ip is null"); @@ -95,40 +110,47 @@ void Ethernet::recieve_order(Boards board, HeapStateOrder* Order,Orders_id id){/ } void Ethernet::update(){ - for(auto& pending : id_to_pending){ - if(pending.second.control_station){ - Socket_to_board[id_to_orders[pending.first].Board]->send_order(*id_to_orders[pending.first].order); - id_to_timeout[pending.first]=Time::set_timeout(5000,[&](){ - //InfoWarning::InfoWarningTrigger("Timeout for order to control station"); - if(id_to_timeout.contains(pending.first)){ - Time::cancel_timeout(id_to_timeout[pending.first]);//No se si esto es valido - id_to_timeout.erase(pending.first); - } - }); + // for(auto& pending : id_to_pending){ + // if(pending.second.control_station){ + // Socket_to_board[id_to_orders[pending.first].Board]->send_order(*id_to_orders[pending.first].order); + // id_to_timeout[pending.first]=Time::set_timeout(5000,[&](){ + // //InfoWarning::InfoWarningTrigger("Timeout for order to control station"); + // if(id_to_timeout.contains(pending.first)){ + // Time::cancel_timeout(id_to_timeout[pending.first]);//No se si esto es valido + // id_to_timeout.erase(pending.first); + // } + // }); - } - else if(pending.second.board){ - if(id_to_timeout.contains(pending.first)){ - Time::cancel_timeout(id_to_timeout[pending.first]); - id_to_timeout.erase(pending.first); - auto it = id_to_flag.find(pending.first); - if (it != id_to_flag.end()) { - *(it->second.first) = it->second.second; - } - } - } - } + // } + // else if(pending.second.board){ + // if(id_to_timeout.contains(pending.first)){ + // Time::cancel_timeout(id_to_timeout[pending.first]); + // id_to_timeout.erase(pending.first); + // auto it = id_to_flag.find(pending.first); + // if (it != id_to_flag.end()) { + // *(it->second.first) = it->second.second; + // } + // } + // } + // } + + if(flag_send_packets) + { + flag_send_packets = false; + send_packets(); + } } bool Ethernet::connected(){ //igual meter lo de la blcu - return Socket_PCU.is_connected() && - Socket_HVSCU.is_connected() && - Socket_BMSL.is_connected() && - Socket_LCU.is_connected() && - Socket_BLCU.is_connected() && - Control_station.is_connected(); + // return Socket_PCU.is_connected() && + // Socket_HVSCU.is_connected() && + // Socket_BMSL.is_connected() && + // Socket_LCU.is_connected() && + // Socket_BLCU.is_connected() && + // Control_station.is_connected(); + return Control_station.is_connected(); } void Ethernet::initialize_state_orders() { diff --git a/Core/Src/VCU/VCU.cpp b/Core/Src/VCU/VCU.cpp index 5e37236..35b8fc5 100644 --- a/Core/Src/VCU/VCU.cpp +++ b/Core/Src/VCU/VCU.cpp @@ -34,21 +34,26 @@ VCU::VCU() Pinout::reed8_pin, Pinout::Tape_pin ) - , ethernet(&GeneralStateMachine, &OperationalStateMachine, &Actuators, &Brakes) + // , ethernet(&GeneralStateMachine, &OperationalStateMachine, &Actuators, &Brakes) { - initialize_state_machines(); - ethernet.initialize_state_orders(); - STLIB::start(ethernet.local_mac,ethernet.VCU_IP, "255.255.0.0","192.168.1.1",UART::uart2); + + + STLIB::start("00:80:E1:11:02:00", ethernet->VCU_IP, "255.255.0.0"); Actuators.init(); + ethernet = new Communications::Ethernet(&GeneralStateMachine, &OperationalStateMachine, &Actuators, &Brakes); + initialize_state_machines(); + //ethernet->initialize_state_orders(); Brakes.init(); - Time::register_low_precision_alarm(16, [&]() { - Brakes.read_reeds(); - Actuators.read_regulators(); - Actuators.read_pressure(); - Actuators.read_flow(); - Actuators.read_sdc(); - }); + // Time::register_low_precision_alarm(16, [&]() { + // Brakes.read_reeds(); + // Actuators.read_regulators(); + // Actuators.read_pressure(); + // Actuators.read_flow(); + // Actuators.read_sdc(); + // }); + + } @@ -66,15 +71,15 @@ void VCU::initialize_state_machines(){ GeneralStateMachine.add_transition(GeneralStates::Connecting, GeneralStates::Operational, [&](){ - return ethernet.connected(); + return ethernet->connected(); }); GeneralStateMachine.add_transition(GeneralStates::Operational, GeneralStates::Fault, [&](){ - return !ethernet.connected(); + return !ethernet->connected(); }); GeneralStateMachine.add_transition(GeneralStates::Connecting, GeneralStates::Fault, [&](){ - return !ethernet.connected(); // y algo mas para que no se vaya a fault al principio + return !ethernet->connected(); // y algo mas para que no se vaya a fault al principio }); GeneralStateMachine.add_transition(GeneralStates::Operational, GeneralStates::Fault, [&](){ @@ -110,15 +115,15 @@ void VCU::initialize_state_machines(){ //el sdc tambien manda a fault? OperationalStateMachine.add_transition(OperationalStates::Idle, OperationalStates::Energyzed, [&](){ - return ethernet.requested_close_contactors; + return ethernet->requested_close_contactors; }); OperationalStateMachine.add_transition(OperationalStates::Idle, OperationalStates::EndOfRun, [&](){ - return ethernet.requested_end_of_run; + return ethernet->requested_end_of_run; }); OperationalStateMachine.add_transition(OperationalStates::Energyzed, OperationalStates::Idle, [&](){ - return ethernet.requested_open_contactors; + return ethernet->requested_open_contactors; }); @@ -131,18 +136,18 @@ void VCU::initialize_state_machines(){ }); OperationalStateMachine.add_enter_action([&]() { - ethernet.requested_close_contactors = false; + ethernet->requested_close_contactors = false; }, OperationalStates::Energyzed); OperationalStateMachine.add_enter_action([&]() { - ethernet.requested_end_of_run = false; + ethernet->requested_end_of_run = false; }, OperationalStates::EndOfRun); OperationalStateMachine.add_enter_action([&]() { - ethernet.requested_open_contactors = false; + ethernet->requested_open_contactors = false; }, OperationalStates::Idle); @@ -150,13 +155,13 @@ void VCU::initialize_state_machines(){ ProtectionManager::link_state_machine(GeneralStateMachine, GeneralStates::Fault); ProtectionManager::add_standard_protections(); - ethernet.initialize_state_orders(); + //ethernet->initialize_state_orders(); } void VCU::update(){ STLIB::update(); - ethernet.update(); + ethernet->update(); } From 58bcb5bf53f3d1e29d5d4ca5248a302aa6dd41b8 Mon Sep 17 00:00:00 2001 From: jdmarmen Date: Fri, 27 Jun 2025 03:14:27 +0200 Subject: [PATCH 02/25] Working code refactor without orders and packets --- .vscode/launch.json | 1 - .vscode/settings.json | 76 +++++- Core/Inc/Code_generation/JSON_ADE | 2 +- Core/Inc/Communications/Packets/.gitignore | 0 Core/Inc/VCU/Actuators.hpp | 60 +++++ Core/Inc/VCU/Actuators/Actuators.hpp | 90 ------- Core/Inc/VCU/Actuators/Brakes.hpp | 66 ----- Core/Inc/VCU/Actuators/Leds.hpp | 29 -- Core/Inc/VCU/Actuators/Pinout.hpp | 73 ----- Core/Inc/VCU/Brakes.hpp | 46 ++++ Core/Inc/VCU/Comms.hpp | 53 ++++ Core/Inc/VCU/Communcations/Ethernet.hpp | 297 --------------------- Core/Inc/VCU/Leds.hpp | 24 ++ Core/Inc/VCU/Pinout.hpp | 35 +++ Core/Inc/VCU/VCU.hpp | 48 +--- Core/Inc/state_machine.hpp | 130 +++++++++ Core/Src/Runes/generated_metadata.cpp | 4 +- Core/Src/VCU/Actuators.cpp | 79 ++++++ Core/Src/VCU/Actuators/Actuators.cpp | 61 ----- Core/Src/VCU/Actuators/Brakes.cpp | 43 --- Core/Src/VCU/Actuators/Leds.cpp | 35 --- Core/Src/VCU/Actuators/Pinout.cpp | 37 --- Core/Src/VCU/Brakes.cpp | 57 ++++ Core/Src/VCU/Comms.cpp | 17 ++ Core/Src/VCU/Communications/Ethernet.cpp | 172 ------------ Core/Src/VCU/Leds.cpp | 37 +++ Core/Src/VCU/VCU.cpp | 172 +----------- Core/Src/main.cpp | 12 +- 28 files changed, 644 insertions(+), 1112 deletions(-) delete mode 100644 Core/Inc/Communications/Packets/.gitignore create mode 100644 Core/Inc/VCU/Actuators.hpp delete mode 100644 Core/Inc/VCU/Actuators/Actuators.hpp delete mode 100644 Core/Inc/VCU/Actuators/Brakes.hpp delete mode 100644 Core/Inc/VCU/Actuators/Leds.hpp delete mode 100644 Core/Inc/VCU/Actuators/Pinout.hpp create mode 100644 Core/Inc/VCU/Brakes.hpp create mode 100644 Core/Inc/VCU/Comms.hpp delete mode 100644 Core/Inc/VCU/Communcations/Ethernet.hpp create mode 100644 Core/Inc/VCU/Leds.hpp create mode 100644 Core/Inc/VCU/Pinout.hpp create mode 100644 Core/Inc/state_machine.hpp create mode 100644 Core/Src/VCU/Actuators.cpp delete mode 100644 Core/Src/VCU/Actuators/Actuators.cpp delete mode 100644 Core/Src/VCU/Actuators/Brakes.cpp delete mode 100644 Core/Src/VCU/Actuators/Leds.cpp delete mode 100644 Core/Src/VCU/Actuators/Pinout.cpp create mode 100644 Core/Src/VCU/Brakes.cpp create mode 100644 Core/Src/VCU/Comms.cpp delete mode 100644 Core/Src/VCU/Communications/Ethernet.cpp create mode 100644 Core/Src/VCU/Leds.cpp diff --git a/.vscode/launch.json b/.vscode/launch.json index 6071427..86fba4c 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -21,7 +21,6 @@ "decoders": [{ "type": "console", "label": "ITM", "port": 0 }] }, "preLaunchTask": "CMake: build", - "stm32cubeprogrammer":"/home/danielcanto/STMicroelectronics/STM32Cube/STM32CubeProgrammer/bin", }, { "type": "cortex-debug", diff --git a/.vscode/settings.json b/.vscode/settings.json index 8fb1bf5..81f2607 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -22,6 +22,80 @@ "vector": "cpp", "deque": "cpp", "string": "cpp", - "span": "cpp" + "span": "cpp", + "any": "cpp", + "barrier": "cpp", + "bit": "cpp", + "bitset": "cpp", + "cctype": "cpp", + "cfenv": "cpp", + "charconv": "cpp", + "cinttypes": "cpp", + "clocale": "cpp", + "cmath": "cpp", + "codecvt": "cpp", + "compare": "cpp", + "complex": "cpp", + "concepts": "cpp", + "coroutine": "cpp", + "csetjmp": "cpp", + "csignal": "cpp", + "cstdarg": "cpp", + "cstddef": "cpp", + "cstdint": "cpp", + "cstdio": "cpp", + "cstdlib": "cpp", + "cstring": "cpp", + "ctime": "cpp", + "cuchar": "cpp", + "cwchar": "cpp", + "cwctype": "cpp", + "forward_list": "cpp", + "list": "cpp", + "map": "cpp", + "set": "cpp", + "unordered_map": "cpp", + "unordered_set": "cpp", + "exception": "cpp", + "expected": "cpp", + "algorithm": "cpp", + "iterator": "cpp", + "memory_resource": "cpp", + "numeric": "cpp", + "optional": "cpp", + "random": "cpp", + "ratio": "cpp", + "regex": "cpp", + "source_location": "cpp", + "string_view": "cpp", + "system_error": "cpp", + "type_traits": "cpp", + "fstream": "cpp", + "future": "cpp", + "initializer_list": "cpp", + "iomanip": "cpp", + "iosfwd": "cpp", + "iostream": "cpp", + "latch": "cpp", + "limits": "cpp", + "mutex": "cpp", + "new": "cpp", + "numbers": "cpp", + "ostream": "cpp", + "ranges": "cpp", + "scoped_allocator": "cpp", + "semaphore": "cpp", + "shared_mutex": "cpp", + "spanstream": "cpp", + "sstream": "cpp", + "stacktrace": "cpp", + "stdexcept": "cpp", + "stop_token": "cpp", + "streambuf": "cpp", + "syncstream": "cpp", + "thread": "cpp", + "typeindex": "cpp", + "typeinfo": "cpp", + "valarray": "cpp" } } diff --git a/Core/Inc/Code_generation/JSON_ADE b/Core/Inc/Code_generation/JSON_ADE index c9f7efc..0fdf9c8 160000 --- a/Core/Inc/Code_generation/JSON_ADE +++ b/Core/Inc/Code_generation/JSON_ADE @@ -1 +1 @@ -Subproject commit c9f7efc38aa0c225b2a55a8daa74911e25ca3c93 +Subproject commit 0fdf9c8976a7cafb4f1125666f28d063046a1a75 diff --git a/Core/Inc/Communications/Packets/.gitignore b/Core/Inc/Communications/Packets/.gitignore deleted file mode 100644 index e69de29..0000000 diff --git a/Core/Inc/VCU/Actuators.hpp b/Core/Inc/VCU/Actuators.hpp new file mode 100644 index 0000000..0ada712 --- /dev/null +++ b/Core/Inc/VCU/Actuators.hpp @@ -0,0 +1,60 @@ +#pragma once +#include "ST-LIB.hpp" + +class Actuators { + private: + PWM Regulator_out1; // por caracterizar + PWM Regulator_out2; + + DigitalSensor Regulator_in1; + DigitalSensor Regulator_in2; + + PWM Pump_c1; + PWM Pump_c2; + + DigitalSensor flow1_input; + DigitalSensor flow2_input; + + DigitalSensor SDC_sensor; // creo que tocará hacerlo exti + + LinearSensor PresionAlta; + LinearSensor PresionRegulador; // baja + LinearSensor PresionFrenos; // baja + LinearSensor PresionCapsula; // por caracterizar + + public: + enum Pump { PUMP_UNIDADES = 0, PUMP_PLACAS = 1 }; + + enum Regulator { REGULATOR_1 = 0, REGULATOR_2 = 1 }; + + Regulator selected_regulator; + Pump selected_pump; + float selected_regulator_pressure; + + PinState regulator_1_input; + PinState regulator_2_input; + + uint32_t selected_pump_duty; + + PinState flow1; + PinState flow2; + + PinState Sdc; + + float pressure_1 = 0; + float pressure_2 = 0; + float pressure_3 = 0; + float pressure_4 = 0; + + Actuators(); + + void init(); + void set_regulator_1(float value); + void set_regulator_2(float value); + void set_pump_1(uint32_t value); + void set_pump_2(uint32_t value); + void read_regulators(); + void read_pressure(); + void read_flow(); + void read_sdc(); +}; \ No newline at end of file diff --git a/Core/Inc/VCU/Actuators/Actuators.hpp b/Core/Inc/VCU/Actuators/Actuators.hpp deleted file mode 100644 index 4546212..0000000 --- a/Core/Inc/VCU/Actuators/Actuators.hpp +++ /dev/null @@ -1,90 +0,0 @@ -#pragma once -#include "ST-LIB.hpp" - -namespace Actuators { - - enum Pump{ - PUMP_UNIDADES = 0, - PUMP_PLACAS = 1 - }; - - enum Regulator{ - REGULATOR_1 = 0, - REGULATOR_2 = 1 - }; - class Actuators{ - private: - - PWM Regulator_out1;//por caracterizar - PWM Regulator_out2; - - DigitalSensor Regulator_in1; - DigitalSensor Regulator_in2; - - PWM Pump_c1; - PWM Pump_c2; - - DigitalSensor flow1_input; - DigitalSensor flow2_input; - - DigitalSensor SDC_sensor;//creo que tocará hacerlo exti - - LinearSensor PresionAlta; - LinearSensor PresionRegulador;//baja - LinearSensor PresionFrenos;//baja - LinearSensor PresionCapsula;// por caracterizar - - - public: - Regulator selected_regulator; - Pump selected_pump; - float selected_regulator_pressure; - - PinState regulator_1_input; - PinState regulator_2_input; - - uint32_t selected_pump_duty; - - PinState flow1; - PinState flow2; - - PinState Sdc; - - float pressure_1 = 0; - float pressure_2 = 0; - float pressure_3 = 0; - float pressure_4 = 0; - - Actuators(Pin& regulator_1_in_pin, Pin& regulator_2_in_pin, Pin& regulator_1_out_pin, Pin& regulator_2_out_pin, - Pin& pump_c1_pin, Pin& pump_c2_pin, Pin& flow1_pin, Pin& flow2_pin,Pin& sdc_sensor_pin, - Pin& pressure_alta_pin, Pin& pressure_regulador_pin, Pin& pressure_frenos_pin, Pin& pressure_capsula_pin) : - Regulator_out1(regulator_1_out_pin), - Regulator_out2(regulator_2_out_pin), - Regulator_in1(regulator_1_in_pin,®ulator_1_input), - Regulator_in2(regulator_2_in_pin,®ulator_2_input), - Pump_c1(pump_c1_pin), - Pump_c2(pump_c2_pin), - flow1_input(flow1_pin,&flow1), - flow2_input(flow2_pin,&flow2), - SDC_sensor(sdc_sensor_pin,&Sdc), - PresionAlta(pressure_alta_pin,0.00763,0.318,&pressure_1), - PresionRegulador(pressure_regulador_pin,0.256,0.375,&pressure_2), - PresionFrenos(pressure_frenos_pin,0.256,0.375,&pressure_3), - PresionCapsula(pressure_capsula_pin,1,0,&pressure_4)//Falta por caracterizar - { - - } - - void init(); - void set_regulator_1(float value); - void set_regulator_2(float value); - void set_pump_1(uint32_t value); - void set_pump_2(uint32_t value); - void read_regulators(); - void read_pressure(); - void read_flow(); - void read_sdc(); - - - }; -} \ No newline at end of file diff --git a/Core/Inc/VCU/Actuators/Brakes.hpp b/Core/Inc/VCU/Actuators/Brakes.hpp deleted file mode 100644 index 6f0e10e..0000000 --- a/Core/Inc/VCU/Actuators/Brakes.hpp +++ /dev/null @@ -1,66 +0,0 @@ -#pragma once -#include "ST-LIB.hpp" - -namespace Actuators { -class Brakes { - private: - //Faltan todo lo que es tapes - DigitalOutput Actuator_out; - - DigitalSensor reed1_input; - DigitalSensor reed2_input; - DigitalSensor reed3_input; - DigitalSensor reed4_input; - DigitalSensor reed5_input; - DigitalSensor reed6_input; - DigitalSensor reed7_input; - DigitalSensor reed8_input; - - // DigitalSensor Tape_input; - - - public: - bool Active_brakes=true; - bool breaks_first_time = true; - - PinState reed1= PinState::OFF; - PinState reed2= PinState::OFF; - PinState reed3= PinState::OFF; - PinState reed4= PinState::OFF; - PinState reed5= PinState::OFF; - PinState reed6= PinState::OFF; - PinState reed7= PinState::OFF; - PinState reed8= PinState::OFF; - - bool All_reeds = false; - - bool Tape_enabled = true; - DigitalOutput Tape_output; - - - PinState Tape_state = PinState::OFF; - - - - Brakes(Pin& actuator_pin, Pin& reed1_pin, Pin& reed2_pin, Pin& reed3_pin, Pin& reed4_pin, - Pin& reed5_pin, Pin& reed6_pin, Pin& reed7_pin, Pin& reed8_pin, - /* Pin& Tapes_input_pin*/Pin&Tapes_output_pin) - : Actuator_out(actuator_pin), - reed1_input(reed1_pin, &reed1), - reed2_input(reed2_pin, &reed2), - reed3_input(reed3_pin, &reed3), - reed4_input(reed4_pin, &reed4), - reed5_input(reed5_pin, &reed5), - reed6_input(reed6_pin, &reed6), - reed7_input(reed7_pin, &reed7), - reed8_input(reed8_pin, &reed8), - // Tape_input(Tapes_input_pin, &Tape_state), - Tape_output(Tapes_output_pin) - { - } - void init(); - void brake(); - void unbrake(); - void read_reeds(); -}; -} \ No newline at end of file diff --git a/Core/Inc/VCU/Actuators/Leds.hpp b/Core/Inc/VCU/Actuators/Leds.hpp deleted file mode 100644 index ed1cdc1..0000000 --- a/Core/Inc/VCU/Actuators/Leds.hpp +++ /dev/null @@ -1,29 +0,0 @@ -#pragma once -#include "ST-LIB.hpp" - -namespace Actuators { - - class Leds { - private: - static constexpr uint32_t LINK_PERIOD_MS{500}; - static constexpr uint8_t UNDEFINED_ID{std::numeric_limits::max()}; - - DigitalOutput LedOperational; - DigitalOutput LedFault; - DigitalOutput LedCan; //Ver que hacer con estos leds - DigitalOutput LedSleep; - DigitalOutput LedFlash; - - uint8_t blinking_action_id{UNDEFINED_ID}; - - public: - Leds(Pin& operational_pin, Pin& fault_pin, Pin& can_pin, Pin& sleep_pin, Pin& flash_pin) - : LedOperational(operational_pin), LedFault(fault_pin), LedCan(can_pin), LedSleep(sleep_pin), LedFlash(flash_pin) {} - - void leds_connecting(); - void leds_operational(); - void leds_fault(); - - }; - -}; diff --git a/Core/Inc/VCU/Actuators/Pinout.hpp b/Core/Inc/VCU/Actuators/Pinout.hpp deleted file mode 100644 index 9843d98..0000000 --- a/Core/Inc/VCU/Actuators/Pinout.hpp +++ /dev/null @@ -1,73 +0,0 @@ -#pragma once -#include "ST-LIB.hpp" - -#define LED_OPERATIONAL_PIN PG8 -#define LED_FAULT_PIN PG7 -#define LED_CAN_PIN PG6 -#define LED_SLEEP_PIN PG4 -#define LED_FLASH_PIN PG5 - -#define REED1_PIN PD11 -#define REED2_PIN PD10 -#define REED3_PIN PG2 -#define REED4_PIN PG3 -#define REED5_PIN PD8 -#define REED6_PIN PD9 -#define REED7_PIN PB15 -#define REED8_PIN PB14 - -#define REGULATOR_1_IN_PIN PA4 -#define REGULATOR_2_IN_PIN PD12 -#define REGULATOR_1_OUT_PIN PB9 -#define REGULATOR_2_OUT_PIN PD13 - -#define BRAKE_PIN PE7 -#define SDC_PIN PA12 -#define PUMP_C1_PIN PE9 -#define PUMP_C2_PIN PE11 -#define TAPE_E_PIN PG1 -#define FLOW1_PIN PE13 -#define FLOW2_PIN PE14 -#define TAPE_PIN PE0 - -#define PRESION_ALTA_PIN PB0 -#define PRESION_REGULADOR_PIN PB1 -#define PRESION_FRENOS_PIN PF11 -#define PRESION_CAPSULA_PIN PF12 - -namespace Pinout { - extern Pin& led_operational_pin; - extern Pin& led_fault_pin; - extern Pin& led_can_pin; - extern Pin& led_sleep_pin; - extern Pin& led_flash_pin; - - extern Pin& reed1_pin; - extern Pin& reed2_pin; - extern Pin& reed3_pin; - extern Pin& reed4_pin; - extern Pin& reed5_pin; - extern Pin& reed6_pin; - extern Pin& reed7_pin; - extern Pin& reed8_pin; - - extern Pin& regulator_1_in_pin; - extern Pin& regulator_2_in_pin; - extern Pin& regulator_1_out_pin; - extern Pin& regulator_2_out_pin; - - extern Pin& Brake_pin; - extern Pin& SDC_pin; - extern Pin& Pump_c1_pin; - extern Pin& Pump_c2_pin; - extern Pin& TapeE_pin; - extern Pin& Flow1_pin; - extern Pin& Flow2_pin; - extern Pin& Tape_pin; - - extern Pin& Presion_alta_pin; - extern Pin& Presion_regulador_pin; - extern Pin& Presion_frenos_pin; - extern Pin& Presion_capsula_pin; - -}; \ No newline at end of file diff --git a/Core/Inc/VCU/Brakes.hpp b/Core/Inc/VCU/Brakes.hpp new file mode 100644 index 0000000..62837d4 --- /dev/null +++ b/Core/Inc/VCU/Brakes.hpp @@ -0,0 +1,46 @@ +#pragma once +#include "ST-LIB.hpp" + +class Brakes { + private: + // Faltan todo lo que es tapes + DigitalOutput Actuator_out; + + DigitalSensor reed1_input; + DigitalSensor reed2_input; + DigitalSensor reed3_input; + DigitalSensor reed4_input; + DigitalSensor reed5_input; + DigitalSensor reed6_input; + DigitalSensor reed7_input; + DigitalSensor reed8_input; + + // DigitalSensor Tape_input; + + public: + bool Active_brakes = true; + bool breaks_first_time = true; + + PinState reed1 = PinState::OFF; + PinState reed2 = PinState::OFF; + PinState reed3 = PinState::OFF; + PinState reed4 = PinState::OFF; + PinState reed5 = PinState::OFF; + PinState reed6 = PinState::OFF; + PinState reed7 = PinState::OFF; + PinState reed8 = PinState::OFF; + + bool All_reeds = false; + + bool Tape_enabled = true; + DigitalOutput Tape_output; + + PinState Tape_state = PinState::OFF; + + Brakes(); + + void init(); + void brake(); + void unbrake(); + void read_reeds(); +}; \ No newline at end of file diff --git a/Core/Inc/VCU/Comms.hpp b/Core/Inc/VCU/Comms.hpp new file mode 100644 index 0000000..a259b6d --- /dev/null +++ b/Core/Inc/VCU/Comms.hpp @@ -0,0 +1,53 @@ +#pragma once +#include "ST-LIB.hpp" + +class Comms { + public: + + enum Orders_id : uint16_t { + Open_contactors = 1000, + Close_contactors = 1001, + Unbrake = 201, + Brake = 200, + Potencia_refri_id = 202, + Set_regulator_id = 203, + Enable_tapes_id = 204, + Disable_tapes_id = 205, + EndOfRun_id = 1002, + Levitation_active = 1003, + Propulsion_active = 1004, + Charging_LV_battery = 1005, + Enable_booster = 1006, + Levitation_inactive = 1007, + Propulsion_inactive = 1008, + Charging_LV_battery_inactive = 1009, + Disable_booster = 1010 + }; + + enum Packets_id : uint16_t { + Flow = 250, + Reeds = 251, + Regulator = 252, + Pressure = 253, + Tapes_input = 254, + Tapes_enable = 255 + }; + + // -----------------IP's/Ports----------------- + static constexpr std::string CONTROL_SATION_IP = "192.168.0.9"; + static constexpr std::string VCU_IP = "192.168.1.3"; + + static const uint16_t LOCAL_PORT = 50500; + static const uint16_t CONTROL_STATION_UDP_PORT = 50400; + + + // -----------------Sockets----------------- + static inline ServerSocket* control_station_tcp{}; + static inline DatagramSocket* control_station_udp{}; + + // -----------------Functions----------------- + static void init(); + static void add_packets(); + static void add_orders(); + +}; \ No newline at end of file diff --git a/Core/Inc/VCU/Communcations/Ethernet.hpp b/Core/Inc/VCU/Communcations/Ethernet.hpp deleted file mode 100644 index 6d6663f..0000000 --- a/Core/Inc/VCU/Communcations/Ethernet.hpp +++ /dev/null @@ -1,297 +0,0 @@ -#pragma once -#include "ST-LIB.hpp" -#include "VCU/Actuators/Actuators.hpp" -#include "VCU/Actuators/Brakes.hpp" - -namespace Communications { - -enum GeneralStates { - Connecting, - Operational, - Fault, -}; - -enum OperationalStates { - Idle, - Precharge, - EndOfRun, - Energyzed, - Ready, -}; - -struct Flags_udp { - uint8_t prueba;//Aqui van las flags -}; -//IDs inventadas excepto las de id con 200 -enum Orders_id : uint16_t { - Open_contactors = 1000, - Close_contactors = 1001, - Unbrake =201, - Brake =200, - Potencia_refri_id =202, - Set_regulator_id =203, - Enable_tapes_id =204, - Disable_tapes_id =205, - EndOfRun_id = 1002, - Levitation_active = 1003, - Propulsion_active = 1004, - Charging_LV_battery = 1005, - Enable_booster = 1006, - Levitation_inactive = 1007, - Propulsion_inactive = 1008, - Charging_LV_battery_inactive = 1009, - Disable_booster = 1010 -}; - -enum Packets_id: uint16_t{ - Flow =250, - Reeds =251, - Regulator =252, - Pressure =253, - Tapes_input =254, - Tapes_enable =255 -}; - -enum class Boards : uint8_t { - PCU, - HVSCU, - BMSL, - LCU, - BLCU, - BCU -}; - -struct Flags_ready{ - bool requested_levitation_active = false; - bool requested_propulsion_active = false; - bool requested_charging_LV_battery = false; - bool requested_enable_booster = false; -}; - -struct PendingOrder { - bool control_station; - bool board; -}; - -struct BoardOrder { - Boards Board; - HeapStateOrder* order; -}; - -class Ethernet{ - - public: - static Flags_ready flags_ready; - inline static bool requested_open_contactors = false; - inline static bool requested_close_contactors=false; - inline static bool requested_end_of_run=false; - - - - inline static const string VCU_IP{"192.168.1.3"}; //Habra que ver las ips - inline static const string PCU_IP{"192.168.1.4"}; - inline static const string HVSCU_IP{"192.168.1.5"}; - inline static const string BMSL_IP{"192.168.1.6"}; - inline static const string LCU_IP{"192.168.1.7"}; - inline static const string BLCU_IP{"192.168.1.8"}; - inline static const string control_station_ip{"192.168.0.9"}; - - inline static const MAC local_mac{"00:80:E1:11:02:00"}; - - inline static const uint16_t local_port{50500};//Habra que ver tambien los puertos - inline static const uint16_t controlstation_port{50555}; - inline static const uint16_t pcu_port{50501}; - inline static const uint16_t hvscu_port{50502}; - inline static const uint16_t bmsl_port{50503}; - inline static const uint16_t lcu_port{50504}; - inline static const uint16_t blcu_port{50505}; - - inline static const uint16_t udp_controlstation_port{50400}; - inline static const uint16_t udp_port{50401}; - // inline static const uint16_t pcu_udp_port{50402}; - // inline static const uint16_t hvcu_udp_port{50403}; - // inline static const uint16_t bmsl_udp_port{50404}; - // inline static const uint16_t lcu_udp_port{50405}; - // inline static const uint16_t blcu_udp_port{50406}; - - - Socket Socket_PCU; - Socket Socket_HVSCU; - Socket Socket_BMSL; - Socket Socket_LCU; - Socket Socket_BLCU; - - ServerSocket Control_station; - - DatagramSocket packets_endpoint; - // DatagramSocket PCU_UDP; - // DatagramSocket HVSU_UDP; - // DatagramSocket BMSL_UDP; - // DatagramSocket LCU_UDP; - // DatagramSocket BLCU_UDP; - - - - Ethernet(StateMachine* GeneralStateMachine, StateMachine* OperationalStateMachine,Actuators::Actuators* actuators, Actuators::Brakes* brakes); - void update(); - bool connected(); - void initialize_state_orders(); - void send_packets(); - - private: - - static void recieve_order(Boards board, HeapStateOrder* Order,Orders_id id); - - static StateMachine* GeneralStateMachine; - static StateMachine* OperationalStateMachine; - static Actuators::Actuators* Actuators; - static Actuators::Brakes* Brakes; - - //Heap Orders: - static HeapOrder* Potencia_refri; - static HeapOrder* Set_Regulator; - static HeapOrder* Enable_tapes; - static HeapOrder* Disable_tapes; - - //State orders: - static HeapStateOrder* Open_Contactors; - static HeapStateOrder* Close_Contactors; - static HeapStateOrder* Unbrake; - static HeapStateOrder* Brake; - static HeapStateOrder* EndOfRun; - - static HeapStateOrder* Levitation_Active; - static HeapStateOrder* Propulsion_Active; - static HeapStateOrder* Charging_LV_Battery; - static HeapStateOrder* Enable_Booster; - static HeapStateOrder* Levitation_Inactive; - static HeapStateOrder* Propulsion_Inactive; - static HeapStateOrder* Charging_LV_Battery_Inactive; - static HeapStateOrder* Disable_booster; - - - static void on_brake(){ - Brakes->brake(); - } - static void on_unbrake(){ - Brakes->unbrake(); - } - static void on_end_of_run(){ - requested_end_of_run = true; - } - - static void on_potencia_refri(){ - if(Actuators->selected_pump == Actuators::Pump::PUMP_UNIDADES) { - Actuators->set_pump_1(Actuators->selected_pump_duty); - } else if (Actuators->selected_pump == Actuators::Pump::PUMP_PLACAS) { - Actuators->set_pump_2(Actuators->selected_pump_duty); - } - } - - static void on_Set_regulator(){ - if(Actuators->selected_regulator_pressure>6){ - Actuators->selected_regulator_pressure =6; - //InfoWarning::InfoWarningTrigger("Cannot exceed 6 bar on regulator pressure"); - } - if(Actuators->selected_regulator == Actuators::Regulator::REGULATOR_1){ - Actuators->set_regulator_1(Actuators->selected_regulator_pressure); - }else if(Actuators->selected_regulator == Actuators::Regulator::REGULATOR_2){ - Actuators->set_regulator_2(Actuators->selected_regulator_pressure); - } - } - - static void on_Enable_tapes(){ - Brakes->Tape_enabled = true; - Brakes->Tape_output.turn_on(); - } - - static void on_Disable_tapes(){ - Brakes->Tape_enabled = false; - Brakes->Tape_output.turn_off(); - } - //Funciones de las flags, y las que se mandan a otras placas, cambiar y tal al gusto: - static void on_open_contactors(){recieve_order(Boards::HVSCU, Open_Contactors, Orders_id::Open_contactors);} - static void on_close_contactors(){recieve_order(Boards::HVSCU, Close_Contactors, Orders_id::Close_contactors);} - static void on_levitation_active() { recieve_order(Boards::LCU, Levitation_Active, Orders_id::Levitation_active); }//No hace falta mandar la placa - static void on_propulsion_active() { recieve_order(Boards::PCU, Propulsion_Active, Orders_id::Propulsion_active); } - static void on_charging_LV_battery() { recieve_order(Boards::BMSL, Charging_LV_Battery, Orders_id::Charging_LV_battery); } - static void on_enable_booster() { recieve_order(Boards::BCU, Enable_Booster, Orders_id::Enable_booster); } - static void on_levitation_inactive() { recieve_order(Boards::LCU, Levitation_Inactive, Orders_id::Levitation_inactive); } - static void on_propulsion_inactive() { recieve_order(Boards::PCU, Propulsion_Inactive, Orders_id::Propulsion_inactive); } - static void on_charging_LV_battery_inactive() { recieve_order(Boards::BMSL, Charging_LV_Battery_Inactive, Orders_id::Charging_LV_battery_inactive); } - static void on_disable_booster() { recieve_order(Boards::BCU, Disable_booster, Orders_id::Disable_booster); } - - - inline static std::unordered_map Socket_to_board{}; - inline static std::unordered_map Board_to_ip{ - {Boards::PCU, PCU_IP}, - {Boards::HVSCU, HVSCU_IP}, - {Boards::BMSL, BMSL_IP}, - {Boards::LCU, LCU_IP}, - {Boards::BLCU, BLCU_IP} - }; - inline static std::unordered_map id_to_pending{ - {Orders_id::Levitation_active, {false, false}}, - {Orders_id::Propulsion_active, {false, false}}, - {Orders_id::Charging_LV_battery, {false, false}}, - {Orders_id::Enable_booster, {false, false}}, - {Orders_id::Open_contactors, {false, false}}, - // {Orders_id::Brake, {false, false}}, - {Orders_id::Close_contactors, {false, false}}, - // {Orders_id::Unbrake, {false, false}}, - {Orders_id::Levitation_inactive, {false, false}}, - {Orders_id::Propulsion_inactive, {false, false}}, - {Orders_id::Charging_LV_battery_inactive, {false, false}}, - {Orders_id::Disable_booster, {false, false}} - }; - inline static std::unordered_map> id_to_flag{ - {Orders_id::Levitation_active, {&flags_ready.requested_levitation_active, true}}, - {Orders_id::Propulsion_active, {&flags_ready.requested_propulsion_active, true}}, - {Orders_id::Charging_LV_battery, {&flags_ready.requested_charging_LV_battery, true}}, - {Orders_id::Enable_booster, {&flags_ready.requested_enable_booster, true}}, - {Orders_id::Open_contactors, {&requested_open_contactors, true}}, - // {Orders_id::Brake, {&requested_brake, true}}, - {Orders_id::Levitation_inactive, {&flags_ready.requested_levitation_active, false}}, - {Orders_id::Propulsion_inactive, {&flags_ready.requested_propulsion_active, false}}, - {Orders_id::Charging_LV_battery_inactive, {&flags_ready.requested_charging_LV_battery, false}}, - {Orders_id::Disable_booster, {&flags_ready.requested_enable_booster, false}}, - {Orders_id::Close_contactors, {&requested_close_contactors, true}}, - // {Orders_id::Unbrake, {&requested_unbrake, true}} - // {Orders_id::EndOfRun_id, {&requested_end_of_run, true}} - }; - inline static std::unordered_map id_to_orders{ - {Orders_id::Levitation_active, {Boards::LCU, Levitation_Active}}, - {Orders_id::Propulsion_active, {Boards::PCU, Propulsion_Active}}, - {Orders_id::Charging_LV_battery, {Boards::BMSL, Charging_LV_Battery}}, - {Orders_id::Enable_booster, {Boards::BCU, Enable_Booster}}, - {Orders_id::Open_contactors, {Boards::HVSCU, Open_Contactors}}, - // {Orders_id::Brake, {Boards::, Brake}}, - {Orders_id::Close_contactors, {Boards::HVSCU, Close_Contactors}}, - // {Orders_id::Unbrake, {Boards::PCU, Unbrake}}, - {Orders_id::Levitation_inactive, {Boards::LCU, Levitation_Inactive}}, - {Orders_id::Propulsion_inactive, {Boards::PCU, Propulsion_Inactive}}, - {Orders_id::Charging_LV_battery_inactive, {Boards::BMSL, Charging_LV_Battery_Inactive}}, - {Orders_id::Disable_booster, {Boards::BCU, Disable_booster}} - }; - inline static std::unordered_map id_to_timeout{}; - - HeapPacket* Reeds = nullptr; - HeapPacket* flow = nullptr; - HeapPacket* Regulator = nullptr; - HeapPacket* Pressure = nullptr; - // HeapPacket* Tapes = nullptr; - HeapPacket* Tapes_enable = nullptr; - - // static std::vector packets{}; //Lo que mando a la gui - // struct OrderTriggers{ - // Boards board; - // bool* flag; - // HeapOrder* order; - - // }; - // static std::vector order_triggers; - - -}; -} // namespace Communications - diff --git a/Core/Inc/VCU/Leds.hpp b/Core/Inc/VCU/Leds.hpp new file mode 100644 index 0000000..2326dd4 --- /dev/null +++ b/Core/Inc/VCU/Leds.hpp @@ -0,0 +1,24 @@ +#pragma once +#include "ST-LIB.hpp" + + +class Leds { + private: + static constexpr uint32_t LINK_PERIOD_MS{500}; + static constexpr uint8_t UNDEFINED_ID{std::numeric_limits::max()}; + + DigitalOutput LedOperational; + DigitalOutput LedFault; + DigitalOutput LedCan; //Ver que hacer con estos leds + DigitalOutput LedSleep; + DigitalOutput LedFlash; + + uint8_t blinking_action_id{UNDEFINED_ID}; + + public: + Leds(); + void leds_connecting(); + void leds_operational(); + void leds_fault(); + +}; diff --git a/Core/Inc/VCU/Pinout.hpp b/Core/Inc/VCU/Pinout.hpp new file mode 100644 index 0000000..210df22 --- /dev/null +++ b/Core/Inc/VCU/Pinout.hpp @@ -0,0 +1,35 @@ +#pragma once + +#define LED_OPERATIONAL_PIN PG8 +#define LED_FAULT_PIN PG7 +#define LED_CAN_PIN PG6 +#define LED_SLEEP_PIN PG4 +#define LED_FLASH_PIN PG5 + +#define REED1_PIN PD11 +#define REED2_PIN PD10 +#define REED3_PIN PG2 +#define REED4_PIN PG3 +#define REED5_PIN PD8 +#define REED6_PIN PD9 +#define REED7_PIN PB15 +#define REED8_PIN PB14 + +#define REGULATOR_1_IN_PIN PA4 +#define REGULATOR_2_IN_PIN PD12 +#define REGULATOR_1_OUT_PIN PB9 +#define REGULATOR_2_OUT_PIN PD13 + +#define BRAKE_PIN PE7 +#define SDC_PIN PA12 +#define PUMP_C1_PIN PE9 +#define PUMP_C2_PIN PE11 +#define TAPE_E_PIN PG1 +#define FLOW1_PIN PE13 +#define FLOW2_PIN PE14 +#define TAPE_PIN PE0 + +#define PRESION_ALTA_PIN PB0 +#define PRESION_REGULADOR_PIN PB1 +#define PRESION_FRENOS_PIN PF11 +#define PRESION_CAPSULA_PIN PF12 \ No newline at end of file diff --git a/Core/Inc/VCU/VCU.hpp b/Core/Inc/VCU/VCU.hpp index 34332a1..481aa07 100644 --- a/Core/Inc/VCU/VCU.hpp +++ b/Core/Inc/VCU/VCU.hpp @@ -1,43 +1,13 @@ #pragma once -#include "ST-LIB.hpp" -#include "VCU/Communcations/Ethernet.hpp" -#include "VCU/Actuators/Leds.hpp" -#include "VCU/Actuators/Pinout.hpp" -#include "VCU/Actuators/Actuators.hpp" -#include "VCU/Actuators/Brakes.hpp" +#include "state_machine.hpp" +#include "Comms.hpp" -enum GeneralStates { - Connecting, - Operational, - Fault, -}; - -enum OperationalStates { - Idle, - EndOfRun, - Energyzed, - Ready, -}; - -class VCU{ - private: - - Actuators::Leds leds; - Actuators::Actuators Actuators; - Actuators::Brakes Brakes; - - static StateMachine GeneralStateMachine; - static StateMachine OperationalStateMachine; - - Communications::Ethernet *ethernet; - - - void initialize_state_machines(); - - - public: - VCU(); - void update(); - +class VCU { + public: + static inline VCU_SM* state_machine{}; + static inline uint8_t* VCU_state{}; + static void init(); + static void start(); + static void update(); }; \ No newline at end of file diff --git a/Core/Inc/state_machine.hpp b/Core/Inc/state_machine.hpp new file mode 100644 index 0000000..688a5c3 --- /dev/null +++ b/Core/Inc/state_machine.hpp @@ -0,0 +1,130 @@ +#pragma once +#include "VCU/Comms.hpp" +#include "VCU/Actuators.hpp" +#include "VCU/Brakes.hpp" +#include "VCU/Leds.hpp" +#include "ST-LIB.hpp" + +using namespace std::chrono_literals; + +class VCU_SM { + public: + enum GeneralStates { + Connecting, + Operational, + Fault, + }; + + enum OperationalStates { + Idle, + EndOfRun, + Energyzed, + Ready, + }; + + StateMachine GeneralStateMachine; + StateMachine OperationalStateMachine; + + Leds leds; + Actuators actuators; + Brakes brakes; + + VCU_SM() { + GeneralStateMachine = StateMachine(GeneralStates::Connecting); + GeneralStateMachine.add_state(GeneralStates::Operational); + GeneralStateMachine.add_state(GeneralStates::Fault); + + OperationalStateMachine = StateMachine(OperationalStates::Idle); + OperationalStateMachine.add_state(OperationalStates::EndOfRun); + OperationalStateMachine.add_state(OperationalStates::Energyzed); + OperationalStateMachine.add_state(OperationalStates::Ready); + + GeneralStateMachine.add_transition( + GeneralStates::Connecting, GeneralStates::Operational, + [&]() { return Comms::control_station_tcp->is_connected(); }); + + GeneralStateMachine.add_transition( + GeneralStates::Operational, GeneralStates::Fault, + [&]() { return !Comms::control_station_tcp->is_connected(); }); + + /* GeneralStateMachine.add_transition( + GeneralStates::Connecting, GeneralStates::Fault, [&]() { + return !ethernet->connected(); // y algo mas para que no se + // vaya a fault al principio + }); + + GeneralStateMachine.add_transition( + GeneralStates::Operational, GeneralStates::Fault, [&]() { + return ((Brakes.All_reeds && Brakes.Active_brakes) && + (!Brakes.breaks_first_time)); + }); + + GeneralStateMachine.add_transition( + GeneralStates::Connecting, GeneralStates::Fault, [&]() { + return ((Brakes.All_reeds && Brakes.Active_brakes) && + (!Brakes.breaks_first_time)); + }); + + GeneralStateMachine.add_transition(GeneralStates::Operational, + GeneralStates::Fault, + [&]() { return (!Actuators.Sdc); }); + + GeneralStateMachine.add_transition(GeneralStates::Connecting, + GeneralStates::Fault, + [&]() { return (!Actuators.Sdc); }); + + GeneralStateMachine.add_enter_action([&]() { leds.leds_connecting(); }, + GeneralStates::Connecting); + + GeneralStateMachine.add_enter_action([&]() { leds.leds_operational(); }, + GeneralStates::Operational); + + GeneralStateMachine.add_enter_action( + [&]() { + leds.leds_fault(); + HAL_Delay(100); + Brakes.brake(); + }, + GeneralStates::Fault); + + // el sdc tambien manda a fault? + + OperationalStateMachine.add_transition( + OperationalStates::Idle, OperationalStates::Energyzed, + [&]() { return ethernet->requested_close_contactors; }); + + OperationalStateMachine.add_transition( + OperationalStates::Idle, OperationalStates::EndOfRun, + [&]() { return ethernet->requested_end_of_run; }); + + OperationalStateMachine.add_transition( + OperationalStates::Energyzed, OperationalStates::Idle, [&]() { + return ethernet->requested_open_contactors; + }); + + OperationalStateMachine.add_transition( + OperationalStates::Ready, OperationalStates::Energyzed, + [&]() { return Brakes.Active_brakes; }); + + OperationalStateMachine.add_transition( + OperationalStates::Energyzed, OperationalStates::Ready, + [&]() { return (!Brakes.Active_brakes); }); + + OperationalStateMachine.add_enter_action( + [&]() { ethernet->requested_close_contactors = false; }, + OperationalStates::Energyzed); + + OperationalStateMachine.add_enter_action( + [&]() { ethernet->requested_end_of_run = false; }, + OperationalStates::EndOfRun); + + OperationalStateMachine.add_enter_action( + [&]() { ethernet->requested_open_contactors = false; }, + OperationalStates::Idle); + + ProtectionManager::link_state_machine(GeneralStateMachine, + GeneralStates::Fault); + ProtectionManager::add_standard_protections(); + // ethernet->initialize_state_orders(); */ + } +}; \ No newline at end of file diff --git a/Core/Src/Runes/generated_metadata.cpp b/Core/Src/Runes/generated_metadata.cpp index 93949eb..ada8947 100644 --- a/Core/Src/Runes/generated_metadata.cpp +++ b/Core/Src/Runes/generated_metadata.cpp @@ -5,11 +5,11 @@ extern "C"{ const char DESCRIPTION[255] __attribute__((section(".metadata_pool")))= "****************" // placeholder for beggining - "20250625T162222" // DateTime using ISO-8601 format + "20250627T030038" // DateTime using ISO-8601 format " " // alignment "f8e647d0" // STLIB commit "--------" // ADJ commit - "375961a2" // Board commit + "33927553" // Board commit // the '=' is used for unparsing ; } \ No newline at end of file diff --git a/Core/Src/VCU/Actuators.cpp b/Core/Src/VCU/Actuators.cpp new file mode 100644 index 0000000..cb8db42 --- /dev/null +++ b/Core/Src/VCU/Actuators.cpp @@ -0,0 +1,79 @@ +#include "VCU/Actuators.hpp" + +#include "VCU/Pinout.hpp" + +Actuators::Actuators() { + Regulator_out1 = PWM(REGULATOR_1_OUT_PIN); + Regulator_out2 = PWM(REGULATOR_2_OUT_PIN); + + Regulator_in1 = DigitalSensor(REGULATOR_1_IN_PIN, ®ulator_1_input); + Regulator_in2 = DigitalSensor(REGULATOR_2_IN_PIN, ®ulator_2_input); + + Pump_c1 = PWM(PUMP_C1_PIN); + Pump_c2 = PWM(PUMP_C2_PIN); + + flow1_input = DigitalSensor(FLOW1_PIN, &flow1); + flow2_input = DigitalSensor(FLOW2_PIN, &flow2); + SDC_sensor = DigitalSensor(SDC_PIN, &Sdc); + + PresionAlta = + LinearSensor(PRESION_ALTA_PIN, 0.00763, 0.318, &pressure_1); + PresionRegulador = + LinearSensor(PRESION_REGULADOR_PIN, 0.256, 0.375, &pressure_2); + PresionFrenos = + LinearSensor(PRESION_FRENOS_PIN, 0.256, 0.375, &pressure_3); + PresionCapsula = LinearSensor( + PRESION_CAPSULA_PIN, 1, 0, &pressure_4); // Falta por caracterizar +} + +void Actuators::init() { + Pump_c1.set_frequency(10000); + Pump_c1.set_duty_cycle(0); + Pump_c1.turn_on(); + + Pump_c2.set_frequency(10000); + Pump_c2.set_duty_cycle(0); + Pump_c2.turn_on(); + + Regulator_out1.set_frequency(30000); + Regulator_out1.set_duty_cycle(0); + Regulator_out1.turn_on(); + + Regulator_out2.set_frequency(30000); + Regulator_out2.set_duty_cycle(0); + Regulator_out2.turn_on(); +} + +void Actuators::set_regulator_1(float value) { + float regulator_value = static_cast( + value * (100.0 / 6)); // FAlta hacer la caracterizacion pero echale que + // al 100 deberia sacar 6 bares + Regulator_out1.set_duty_cycle(regulator_value); +} + +void Actuators::set_regulator_2(float value) { + float regulator_value = static_cast(value * (100.0 / 6)); + Regulator_out2.set_duty_cycle(regulator_value); +} + +void Actuators::set_pump_1(uint32_t value) { Pump_c1.set_duty_cycle(value); } +void Actuators::set_pump_2(uint32_t value) { Pump_c2.set_duty_cycle(value); } + +void Actuators::read_regulators() { + Regulator_in1.read(); + Regulator_in2.read(); +} + +void Actuators::read_pressure() { + PresionAlta.read(); + PresionRegulador.read(); + PresionFrenos.read(); + // PresionCapsula.read(); +} + +void Actuators::read_flow() { + flow1_input.read(); + flow2_input.read(); +} + +void Actuators::read_sdc() { SDC_sensor.read(); } \ No newline at end of file diff --git a/Core/Src/VCU/Actuators/Actuators.cpp b/Core/Src/VCU/Actuators/Actuators.cpp deleted file mode 100644 index 269d3cc..0000000 --- a/Core/Src/VCU/Actuators/Actuators.cpp +++ /dev/null @@ -1,61 +0,0 @@ -#include "VCU/Actuators/Actuators.hpp" - -namespace Actuators{ - - - void Actuators::init(){ - Pump_c1.set_frequency(10000); - Pump_c1.set_duty_cycle(0); - Pump_c1.turn_on(); - - Pump_c2.set_frequency(10000); - Pump_c2.set_duty_cycle(0); - Pump_c2.turn_on(); - - Regulator_out1.set_frequency(30000); - Regulator_out1.set_duty_cycle(0); - Regulator_out1.turn_on(); - - Regulator_out2.set_frequency(30000); - Regulator_out2.set_duty_cycle(0); - Regulator_out2.turn_on(); - } - - void Actuators::set_regulator_1(float value){ - float regulator_value = static_cast(value * (100.0 / 6));//FAlta hacer la caracterizacion pero echale que al 100 deberia sacar 6 bares - Regulator_out1.set_duty_cycle(regulator_value); - } - - void Actuators::set_regulator_2(float value){ - float regulator_value = static_cast(value * (100.0 / 6)); - Regulator_out2.set_duty_cycle(regulator_value); - } - - void Actuators::set_pump_1(uint32_t value){ - Pump_c1.set_duty_cycle(value); - } - void Actuators::set_pump_2(uint32_t value){ - Pump_c2.set_duty_cycle(value); - } - - void Actuators::read_regulators() { - Regulator_in1.read(); - Regulator_in2.read(); - } - - void Actuators::read_pressure(){ - PresionAlta.read(); - PresionRegulador.read(); - PresionFrenos.read(); - // PresionCapsula.read(); - } - - void Actuators::read_flow(){ - flow1_input.read(); - flow2_input.read(); - } - - void Actuators::read_sdc() { - SDC_sensor.read(); - } -} \ No newline at end of file diff --git a/Core/Src/VCU/Actuators/Brakes.cpp b/Core/Src/VCU/Actuators/Brakes.cpp deleted file mode 100644 index 3cc2745..0000000 --- a/Core/Src/VCU/Actuators/Brakes.cpp +++ /dev/null @@ -1,43 +0,0 @@ -#include "VCU/Actuators/Brakes.hpp" - -namespace Actuators { - - void Brakes::init(){ - Actuator_out.turn_on(); - Tape_output.turn_on();//Hardcodeado para desabilitar tapes creo, o al reves xd - } - - void Brakes::brake(){ - if(breaks_first_time){ - breaks_first_time = false; - - } - Actuator_out.turn_on(); - Active_brakes = true; - } - - void Brakes::unbrake(){ - Actuator_out.turn_off(); - Active_brakes = false; - } - - void Brakes::read_reeds() { - reed1_input.read(); - reed2_input.read(); - reed3_input.read(); - reed4_input.read(); - reed5_input.read(); - reed6_input.read(); - reed7_input.read(); - reed8_input.read(); - - All_reeds = reed1 && - reed2 && - reed3 && - reed4 && - reed5 && - reed6 && - reed7 && - reed8; - } -} \ No newline at end of file diff --git a/Core/Src/VCU/Actuators/Leds.cpp b/Core/Src/VCU/Actuators/Leds.cpp deleted file mode 100644 index d7aeaa8..0000000 --- a/Core/Src/VCU/Actuators/Leds.cpp +++ /dev/null @@ -1,35 +0,0 @@ -#include "VCU/Actuators/Leds.hpp" - -namespace Actuators { - - void Leds::leds_connecting() { - if (blinking_action_id != UNDEFINED_ID) { - Time::unregister_low_precision_alarm(blinking_action_id); - blinking_action_id = UNDEFINED_ID; - } - blinking_action_id = Time::register_low_precision_alarm(LINK_PERIOD_MS,[this]() { - LedOperational.toggle(); - }); - } - - void Leds::leds_operational() { - if (blinking_action_id != UNDEFINED_ID) { - Time::unregister_low_precision_alarm(blinking_action_id); - blinking_action_id = UNDEFINED_ID; - } - LedOperational.turn_on(); - } - - void Leds::leds_fault() { - if (blinking_action_id != UNDEFINED_ID) { - Time::unregister_low_precision_alarm(blinking_action_id); - blinking_action_id = UNDEFINED_ID; - } - blinking_action_id = Time::register_low_precision_alarm(LINK_PERIOD_MS,[this]() { - LedFault.toggle(); - }); - } - - - -}; \ No newline at end of file diff --git a/Core/Src/VCU/Actuators/Pinout.cpp b/Core/Src/VCU/Actuators/Pinout.cpp deleted file mode 100644 index 0863f58..0000000 --- a/Core/Src/VCU/Actuators/Pinout.cpp +++ /dev/null @@ -1,37 +0,0 @@ -#include "VCU/Actuators/Pinout.hpp" - -namespace Pinout { -Pin& led_operational_pin(LED_OPERATIONAL_PIN); -Pin& led_fault_pin(LED_FAULT_PIN); -Pin& led_can_pin(LED_CAN_PIN); -Pin& led_sleep_pin(LED_SLEEP_PIN); -Pin& led_flash_pin(LED_FLASH_PIN); - -Pin& reed1_pin(REED1_PIN); -Pin& reed2_pin(REED2_PIN); -Pin& reed3_pin(REED3_PIN); -Pin& reed4_pin(REED4_PIN); -Pin& reed5_pin(REED5_PIN); -Pin& reed6_pin(REED6_PIN); -Pin& reed7_pin(REED7_PIN); -Pin& reed8_pin(REED8_PIN); - -Pin& regulator_1_in_pin(REGULATOR_1_IN_PIN); -Pin& regulator_2_in_pin(REGULATOR_2_IN_PIN); -Pin& regulator_1_out_pin(REGULATOR_1_OUT_PIN); -Pin& regulator_2_out_pin(REGULATOR_2_OUT_PIN); - -Pin& Brake_pin(BRAKE_PIN); -Pin& SDC_pin(SDC_PIN); -Pin& Pump_c1_pin(PUMP_C1_PIN); -Pin& Pump_c2_pin(PUMP_C2_PIN); -Pin& TapeE_pin(TAPE_E_PIN); -Pin& Flow1_pin(FLOW1_PIN); -Pin& Flow2_pin(FLOW2_PIN); -Pin& Tape_pin(TAPE_PIN); - -Pin& Presion_alta_pin(PRESION_ALTA_PIN); -Pin& Presion_regulador_pin(PRESION_REGULADOR_PIN); -Pin& Presion_frenos_pin(PRESION_FRENOS_PIN); -Pin& Presion_capsula_pin(PRESION_CAPSULA_PIN); -} \ No newline at end of file diff --git a/Core/Src/VCU/Brakes.cpp b/Core/Src/VCU/Brakes.cpp new file mode 100644 index 0000000..7f6d624 --- /dev/null +++ b/Core/Src/VCU/Brakes.cpp @@ -0,0 +1,57 @@ +#include "VCU/Brakes.hpp" +#include "VCU/Pinout.hpp" + +Brakes::Brakes(){ + Actuator_out = DigitalOutput(BRAKE_PIN); + + reed1_input = DigitalSensor(REED1_PIN, &reed1); + reed2_input = DigitalSensor(REED2_PIN, &reed2); + reed3_input = DigitalSensor(REED3_PIN, &reed3); + reed4_input = DigitalSensor(REED4_PIN, &reed4); + reed5_input = DigitalSensor(REED5_PIN, &reed5); + reed6_input = DigitalSensor(REED6_PIN, &reed6); + reed7_input = DigitalSensor(REED7_PIN, &reed7); + reed8_input = DigitalSensor(REED8_PIN, &reed8); + + // Tape_input = DigitalSensor(TAPE_INPUT_PIN, &Tape_state); + Tape_output = DigitalOutput(TAPE_PIN); +} + +void Brakes::init(){ + Actuator_out.turn_on(); + Tape_output.turn_on();//Hardcodeado para desabilitar tapes creo, o al reves xd +} + +void Brakes::brake(){ + if(breaks_first_time){ + breaks_first_time = false; + + } + Actuator_out.turn_on(); + Active_brakes = true; +} + +void Brakes::unbrake(){ + Actuator_out.turn_off(); + Active_brakes = false; +} + +void Brakes::read_reeds() { + reed1_input.read(); + reed2_input.read(); + reed3_input.read(); + reed4_input.read(); + reed5_input.read(); + reed6_input.read(); + reed7_input.read(); + reed8_input.read(); + + All_reeds = reed1 && + reed2 && + reed3 && + reed4 && + reed5 && + reed6 && + reed7 && + reed8; +} diff --git a/Core/Src/VCU/Comms.cpp b/Core/Src/VCU/Comms.cpp new file mode 100644 index 0000000..7990afc --- /dev/null +++ b/Core/Src/VCU/Comms.cpp @@ -0,0 +1,17 @@ +#include "VCU/Comms.hpp" + +void Comms::init(){ + control_station_tcp = new ServerSocket(IPV4(VCU_IP), LOCAL_PORT); + control_station_udp = new DatagramSocket(IPV4(VCU_IP), CONTROL_STATION_UDP_PORT,IPV4(CONTROL_SATION_IP), CONTROL_STATION_UDP_PORT); + + //add_packets(); + //add_orders(); +} + +void Comms::add_packets(){ + +} + +void Comms::add_orders(){ + +} \ No newline at end of file diff --git a/Core/Src/VCU/Communications/Ethernet.cpp b/Core/Src/VCU/Communications/Ethernet.cpp deleted file mode 100644 index 0b307ec..0000000 --- a/Core/Src/VCU/Communications/Ethernet.cpp +++ /dev/null @@ -1,172 +0,0 @@ -#include "VCU/Communcations/Ethernet.hpp" - -static bool flag_send_packets = false; - -namespace Communications { - - StateMachine* Ethernet::GeneralStateMachine = nullptr; - StateMachine* Ethernet::OperationalStateMachine = nullptr; - Actuators::Actuators* Ethernet::Actuators = nullptr; - Actuators::Brakes* Ethernet::Brakes = nullptr; - HeapOrder* Ethernet::Potencia_refri = nullptr; - HeapOrder* Ethernet::Set_Regulator = nullptr; - HeapOrder* Ethernet::Enable_tapes = nullptr; - HeapOrder* Ethernet::Disable_tapes = nullptr; - - HeapStateOrder* Ethernet::Open_Contactors = nullptr; - HeapStateOrder* Ethernet::Close_Contactors = nullptr; - HeapStateOrder* Ethernet::Unbrake = nullptr; - HeapStateOrder* Ethernet::Brake = nullptr; - HeapStateOrder* Ethernet::EndOfRun = nullptr; - HeapStateOrder* Ethernet::Levitation_Active = nullptr; - HeapStateOrder* Ethernet::Propulsion_Active = nullptr; - HeapStateOrder* Ethernet::Charging_LV_Battery = nullptr; - HeapStateOrder* Ethernet::Enable_Booster = nullptr; - HeapStateOrder* Ethernet::Levitation_Inactive = nullptr; - HeapStateOrder* Ethernet::Propulsion_Inactive = nullptr; - HeapStateOrder* Ethernet::Charging_LV_Battery_Inactive = nullptr; - HeapStateOrder* Ethernet::Disable_booster = nullptr; - - Flags_ready Ethernet::flags_ready{}; - - -Ethernet::Ethernet(StateMachine* GeneralStateMachine, StateMachine* OperationalStateMachine,Actuators::Actuators* actuators, Actuators::Brakes* brakes) -: -Control_station(VCU_IP,local_port), -packets_endpoint(VCU_IP, udp_controlstation_port,control_station_ip, udp_controlstation_port) - - { - this->GeneralStateMachine = GeneralStateMachine; - this->OperationalStateMachine = OperationalStateMachine; - this->Actuators = actuators; - this->Brakes = brakes; - - // Control_station=ServerSocket(VCU_IP,local_port); - //LO comento para probar sin tener todo conectado - // Socket_PCU = Socket(VCU_IP,pcu_port,PCU_IP,local_port); - // Socket_HVSCU = Socket(VCU_IP,hvscu_port,HVSCU_IP,local_port); - // Socket_BMSL = Socket(VCU_IP,bmsl_port,BMSL_IP,local_port); - // Socket_LCU = Socket(VCU_IP,lcu_port,LCU_IP,local_port); - // Socket_BLCU = Socket(VCU_IP,blcu_port,BLCU_IP,local_port); - - // packets_endpoint =DatagramSocket(VCU_IP, udp_controlstation_port,control_station_ip, udp_port); - - Socket_to_board[Boards::PCU] = &Socket_PCU; - Socket_to_board[Boards::HVSCU] = &Socket_HVSCU; - Socket_to_board[Boards::BMSL] = &Socket_BMSL; - Socket_to_board[Boards::LCU] = &Socket_LCU; - Socket_to_board[Boards::BLCU] = &Socket_BLCU; - //falta la bcu - - Potencia_refri = new HeapOrder(Orders_id::Potencia_refri_id, &on_potencia_refri, &this->Actuators->selected_pump_duty, &this->Actuators->selected_pump); - Set_Regulator = new HeapOrder(Orders_id::Set_regulator_id, &on_Set_regulator, &this->Actuators->selected_regulator_pressure, &this->Actuators->selected_regulator); - Enable_tapes = new HeapOrder(Orders_id::Enable_tapes_id, &on_Enable_tapes); - Disable_tapes = new HeapOrder(Orders_id::Disable_tapes_id, &on_Disable_tapes); - - Reeds = new HeapPacket(Packets_id::Reeds, &brakes->reed1, &brakes->reed2, &brakes->reed3, &brakes->reed4, &brakes->reed5, &brakes->reed6, &brakes->reed7, &brakes->reed8,&brakes->All_reeds); - flow = new HeapPacket(Packets_id::Flow, &actuators->flow1, &actuators->flow2); - Regulator = new HeapPacket(Packets_id::Regulator, &actuators->regulator_1_input, &actuators->regulator_2_input); - Pressure = new HeapPacket(Packets_id::Pressure, &actuators->pressure_1, &actuators->pressure_2, &actuators->pressure_3, &actuators->pressure_4); - // Tapes = new HeapPacket(1354, &brakes->Tape_state); - Tapes_enable = new HeapPacket(Packets_id::Tapes_enable, &brakes->Tape_enabled); - - Time::register_low_precision_alarm(16, [&]() { - - flag_send_packets = true; - - }); -} - - -void Ethernet::send_packets() -{ - - packets_endpoint.send_packet(*Reeds); - packets_endpoint.send_packet(*flow); - packets_endpoint.send_packet(*Regulator); - packets_endpoint.send_packet(*Pressure); - // packets_endpoint.send_packet(*Tapes); -} - -void Ethernet::recieve_order(Boards board, HeapStateOrder* Order,Orders_id id){//No hace falta mandar la placa - if(Order->remote_ip==nullptr){ - ErrorHandler("Order ip is null"); - return; - } - string remote_ip =*(Order->remote_ip); - if(remote_ip==control_station_ip){//en pruebas esto!! - if(id_to_pending.contains(id)){ - id_to_pending[id].control_station= true; - return; - } - }if(remote_ip==Board_to_ip[board]){ - if(id_to_pending.contains(id)){ - id_to_pending[id].board= true; - return; - } - }else{ - ErrorHandler("Order recieved from unknow ip"); - } -} - -void Ethernet::update(){ - // for(auto& pending : id_to_pending){ - // if(pending.second.control_station){ - // Socket_to_board[id_to_orders[pending.first].Board]->send_order(*id_to_orders[pending.first].order); - // id_to_timeout[pending.first]=Time::set_timeout(5000,[&](){ - // //InfoWarning::InfoWarningTrigger("Timeout for order to control station"); - // if(id_to_timeout.contains(pending.first)){ - // Time::cancel_timeout(id_to_timeout[pending.first]);//No se si esto es valido - // id_to_timeout.erase(pending.first); - // } - // }); - - // } - // else if(pending.second.board){ - // if(id_to_timeout.contains(pending.first)){ - // Time::cancel_timeout(id_to_timeout[pending.first]); - // id_to_timeout.erase(pending.first); - // auto it = id_to_flag.find(pending.first); - // if (it != id_to_flag.end()) { - // *(it->second.first) = it->second.second; - // } - // } - // } - // } - - if(flag_send_packets) - { - flag_send_packets = false; - send_packets(); - } -} - - -bool Ethernet::connected(){ - //igual meter lo de la blcu - // return Socket_PCU.is_connected() && - // Socket_HVSCU.is_connected() && - // Socket_BMSL.is_connected() && - // Socket_LCU.is_connected() && - // Socket_BLCU.is_connected() && - // Control_station.is_connected(); - return Control_station.is_connected(); -} - -void Ethernet::initialize_state_orders() { - Open_Contactors = new HeapStateOrder(Orders_id::Open_contactors, &Ethernet::on_open_contactors, *GeneralStateMachine, GeneralStates::Operational); - Close_Contactors = new HeapStateOrder(Orders_id::Close_contactors, &Ethernet::on_close_contactors, *GeneralStateMachine, GeneralStates::Operational); - Unbrake = new HeapStateOrder(Orders_id::Unbrake, &Ethernet::on_unbrake, *GeneralStateMachine, GeneralStates::Operational); - Brake = new HeapStateOrder(Orders_id::Brake, &Ethernet::on_brake, *GeneralStateMachine, GeneralStates::Operational); - EndOfRun = new HeapStateOrder(Orders_id::EndOfRun_id, &Ethernet::on_brake, *OperationalStateMachine, OperationalStates::EndOfRun); - - Levitation_Active = new HeapStateOrder(Orders_id::Levitation_active, &Ethernet::on_levitation_active, *OperationalStateMachine, OperationalStates::Ready); - Propulsion_Active = new HeapStateOrder(Orders_id::Propulsion_active, &Ethernet::on_propulsion_active, *OperationalStateMachine, OperationalStates::Ready); - Charging_LV_Battery = new HeapStateOrder(Orders_id::Charging_LV_battery, &Ethernet::on_charging_LV_battery, *OperationalStateMachine, OperationalStates::Ready); - Enable_Booster = new HeapStateOrder(Orders_id::Enable_booster, &Ethernet::on_enable_booster, *OperationalStateMachine, OperationalStates::Ready); - Levitation_Inactive = new HeapStateOrder(Orders_id::Levitation_inactive, &Ethernet::on_levitation_inactive, *OperationalStateMachine, OperationalStates::Ready); - Propulsion_Inactive = new HeapStateOrder(Orders_id::Propulsion_inactive, &Ethernet::on_propulsion_inactive, *OperationalStateMachine, OperationalStates::Ready); - Charging_LV_Battery_Inactive = new HeapStateOrder(Orders_id::Charging_LV_battery_inactive, &Ethernet::on_charging_LV_battery_inactive, *OperationalStateMachine, OperationalStates::Ready); - Disable_booster = new HeapStateOrder(Orders_id::Disable_booster, &Ethernet::on_disable_booster, *OperationalStateMachine, OperationalStates::Ready); -} -} \ No newline at end of file diff --git a/Core/Src/VCU/Leds.cpp b/Core/Src/VCU/Leds.cpp new file mode 100644 index 0000000..4dafbcd --- /dev/null +++ b/Core/Src/VCU/Leds.cpp @@ -0,0 +1,37 @@ +#include "VCU/Leds.hpp" + +#include "VCU/Pinout.hpp" + +Leds::Leds() { + LedOperational = DigitalOutput(LED_OPERATIONAL_PIN); + LedFault = DigitalOutput(LED_FAULT_PIN); + LedCan = DigitalOutput(LED_CAN_PIN); + LedSleep = DigitalOutput(LED_SLEEP_PIN); + LedFlash = DigitalOutput(LED_FLASH_PIN); +} + +void Leds::leds_connecting() { + if (blinking_action_id != UNDEFINED_ID) { + Time::unregister_low_precision_alarm(blinking_action_id); + blinking_action_id = UNDEFINED_ID; + } + blinking_action_id = Time::register_low_precision_alarm( + LINK_PERIOD_MS, [this]() { LedOperational.toggle(); }); +} + +void Leds::leds_operational() { + if (blinking_action_id != UNDEFINED_ID) { + Time::unregister_low_precision_alarm(blinking_action_id); + blinking_action_id = UNDEFINED_ID; + } + LedOperational.turn_on(); +} + +void Leds::leds_fault() { + if (blinking_action_id != UNDEFINED_ID) { + Time::unregister_low_precision_alarm(blinking_action_id); + blinking_action_id = UNDEFINED_ID; + } + blinking_action_id = Time::register_low_precision_alarm( + LINK_PERIOD_MS, [this]() { LedFault.toggle(); }); +} \ No newline at end of file diff --git a/Core/Src/VCU/VCU.cpp b/Core/Src/VCU/VCU.cpp index 35b8fc5..0fd2f7f 100644 --- a/Core/Src/VCU/VCU.cpp +++ b/Core/Src/VCU/VCU.cpp @@ -1,167 +1,23 @@ #include "VCU/VCU.hpp" +void VCU::init(){ + state_machine = new VCU_SM(); + VCU_state = &state_machine->GeneralStateMachine.current_state; - -StateMachine VCU::GeneralStateMachine; -StateMachine VCU::OperationalStateMachine; - -VCU::VCU() - : leds(Pinout::led_operational_pin, Pinout::led_fault_pin, Pinout::led_can_pin, Pinout::led_sleep_pin, Pinout::led_flash_pin) - , Actuators( - Pinout::regulator_1_in_pin, - Pinout::regulator_2_in_pin, - Pinout::regulator_1_out_pin, - Pinout::regulator_2_out_pin, - Pinout::Pump_c1_pin, - Pinout::Pump_c2_pin, - Pinout::Flow1_pin, - Pinout::Flow2_pin, - Pinout::SDC_pin, - Pinout::Presion_alta_pin, - Pinout::Presion_regulador_pin, - Pinout::Presion_frenos_pin, - Pinout::Presion_capsula_pin - ) - , Brakes( - Pinout::Brake_pin, - Pinout::reed1_pin, - Pinout::reed2_pin, - Pinout::reed3_pin, - Pinout::reed4_pin, - Pinout::reed5_pin, - Pinout::reed6_pin, - Pinout::reed7_pin, - Pinout::reed8_pin, - Pinout::Tape_pin - ) - // , ethernet(&GeneralStateMachine, &OperationalStateMachine, &Actuators, &Brakes) -{ - - - STLIB::start("00:80:E1:11:02:00", ethernet->VCU_IP, "255.255.0.0"); - Actuators.init(); - ethernet = new Communications::Ethernet(&GeneralStateMachine, &OperationalStateMachine, &Actuators, &Brakes); - initialize_state_machines(); - //ethernet->initialize_state_orders(); - Brakes.init(); - - // Time::register_low_precision_alarm(16, [&]() { - // Brakes.read_reeds(); - // Actuators.read_regulators(); - // Actuators.read_pressure(); - // Actuators.read_flow(); - // Actuators.read_sdc(); - // }); - - - -} - - - -void VCU::initialize_state_machines(){ - GeneralStateMachine= StateMachine(GeneralStates::Connecting); - GeneralStateMachine.add_state(GeneralStates::Operational); - GeneralStateMachine.add_state(GeneralStates::Fault); - - OperationalStateMachine=StateMachine(OperationalStates::Idle); - OperationalStateMachine.add_state(OperationalStates::EndOfRun); - OperationalStateMachine.add_state(OperationalStates::Energyzed); - OperationalStateMachine.add_state(OperationalStates::Ready); - - - GeneralStateMachine.add_transition(GeneralStates::Connecting, GeneralStates::Operational, [&](){ - return ethernet->connected(); - }); - - GeneralStateMachine.add_transition(GeneralStates::Operational, GeneralStates::Fault, [&](){ - return !ethernet->connected(); - }); - - GeneralStateMachine.add_transition(GeneralStates::Connecting, GeneralStates::Fault, [&](){ - return !ethernet->connected(); // y algo mas para que no se vaya a fault al principio - }); - - GeneralStateMachine.add_transition(GeneralStates::Operational, GeneralStates::Fault, [&](){ - return ((Brakes.All_reeds&& Brakes.Active_brakes )&& (!Brakes.breaks_first_time)); - }); - - GeneralStateMachine.add_transition(GeneralStates::Connecting, GeneralStates::Fault, [&](){ - return ((Brakes.All_reeds && Brakes.Active_brakes )&& (!Brakes.breaks_first_time)); - }); - - GeneralStateMachine.add_transition(GeneralStates::Operational, GeneralStates::Fault, [&](){ - return (!Actuators.Sdc); - }); - - GeneralStateMachine.add_transition(GeneralStates::Connecting, GeneralStates::Fault, [&](){ - return (!Actuators.Sdc); - }); - - GeneralStateMachine.add_enter_action([&](){ - leds.leds_connecting(); - }, GeneralStates::Connecting); - - GeneralStateMachine.add_enter_action([&](){ - leds.leds_operational(); - }, GeneralStates::Operational); - - GeneralStateMachine.add_enter_action([&](){ - leds.leds_fault(); - HAL_Delay(100); - Brakes.brake(); - }, GeneralStates::Fault); - -//el sdc tambien manda a fault? - - OperationalStateMachine.add_transition(OperationalStates::Idle, OperationalStates::Energyzed, [&](){ - return ethernet->requested_close_contactors; - }); - - OperationalStateMachine.add_transition(OperationalStates::Idle, OperationalStates::EndOfRun, [&](){ - return ethernet->requested_end_of_run; - }); - - OperationalStateMachine.add_transition(OperationalStates::Energyzed, OperationalStates::Idle, [&](){ - return ethernet->requested_open_contactors; - - }); - - OperationalStateMachine.add_transition(OperationalStates::Ready, OperationalStates::Energyzed, [&](){ - return Brakes.Active_brakes; - }); - - OperationalStateMachine.add_transition(OperationalStates::Energyzed, OperationalStates::Ready, [&](){ - return (!Brakes.Active_brakes); - }); - - OperationalStateMachine.add_enter_action([&]() { - ethernet->requested_close_contactors = false; - }, OperationalStates::Energyzed); - - - - OperationalStateMachine.add_enter_action([&]() { - ethernet->requested_end_of_run = false; - }, OperationalStates::EndOfRun); - - - OperationalStateMachine.add_enter_action([&]() { - ethernet->requested_open_contactors = false; - }, OperationalStates::Idle); - - - - - ProtectionManager::link_state_machine(GeneralStateMachine, GeneralStates::Fault); ProtectionManager::add_standard_protections(); - //ethernet->initialize_state_orders(); + ProtectionManager::initialize(); + ProtectionManager::link_state_machine(state_machine->GeneralStateMachine, VCU_SM::GeneralStates::Fault); + ProtectionManager::set_id(Boards::ID::VCU); } -void VCU::update(){ - STLIB::update(); - ethernet->update(); - +void VCU::start(){ + state_machine->actuators.init(); + state_machine->brakes.init(); + Comms::init(); } +void VCU::update(){ + state_machine->GeneralStateMachine.check_transitions(); + ProtectionManager::check_protections(); +} \ No newline at end of file diff --git a/Core/Src/main.cpp b/Core/Src/main.cpp index 9241b44..2c75f3f 100644 --- a/Core/Src/main.cpp +++ b/Core/Src/main.cpp @@ -7,17 +7,15 @@ #include "ST-LIB.hpp" #include "VCU/VCU.hpp" - - int main(void) { -#ifdef SIM_ON - SharedMemory::start(); -#endif - VCU vcu; + VCU::init(); + STLIB::start("00:00:00:00:01:03","192.168.1.3"); + VCU::start(); while (1) { - vcu.update(); + STLIB::update(); + VCU::update(); } } From 8f37276a26678088d16084639b031f4e1b075430 Mon Sep 17 00:00:00 2001 From: jdmarmen Date: Fri, 27 Jun 2025 19:23:39 +0200 Subject: [PATCH 03/25] Modified some transitions --- Core/Inc/state_machine.hpp | 44 +++++++++++---------------- Core/Src/Runes/generated_metadata.cpp | 4 +-- 2 files changed, 19 insertions(+), 29 deletions(-) diff --git a/Core/Inc/state_machine.hpp b/Core/Inc/state_machine.hpp index 688a5c3..28e12e6 100644 --- a/Core/Inc/state_machine.hpp +++ b/Core/Inc/state_machine.hpp @@ -1,9 +1,9 @@ #pragma once -#include "VCU/Comms.hpp" +#include "ST-LIB.hpp" #include "VCU/Actuators.hpp" #include "VCU/Brakes.hpp" +#include "VCU/Comms.hpp" #include "VCU/Leds.hpp" -#include "ST-LIB.hpp" using namespace std::chrono_literals; @@ -47,31 +47,25 @@ class VCU_SM { GeneralStates::Operational, GeneralStates::Fault, [&]() { return !Comms::control_station_tcp->is_connected(); }); - /* GeneralStateMachine.add_transition( - GeneralStates::Connecting, GeneralStates::Fault, [&]() { - return !ethernet->connected(); // y algo mas para que no se - // vaya a fault al principio - }); - GeneralStateMachine.add_transition( GeneralStates::Operational, GeneralStates::Fault, [&]() { - return ((Brakes.All_reeds && Brakes.Active_brakes) && - (!Brakes.breaks_first_time)); + return ((brakes.All_reeds && brakes.Active_brakes) && + (!brakes.breaks_first_time)); }); GeneralStateMachine.add_transition( GeneralStates::Connecting, GeneralStates::Fault, [&]() { - return ((Brakes.All_reeds && Brakes.Active_brakes) && - (!Brakes.breaks_first_time)); + return ((brakes.All_reeds && brakes.Active_brakes) && + (!brakes.breaks_first_time)); }); GeneralStateMachine.add_transition(GeneralStates::Operational, GeneralStates::Fault, - [&]() { return (!Actuators.Sdc); }); + [&]() { return (!actuators.Sdc); }); GeneralStateMachine.add_transition(GeneralStates::Connecting, GeneralStates::Fault, - [&]() { return (!Actuators.Sdc); }); + [&]() { return (!actuators.Sdc); }); GeneralStateMachine.add_enter_action([&]() { leds.leds_connecting(); }, GeneralStates::Connecting); @@ -83,13 +77,13 @@ class VCU_SM { [&]() { leds.leds_fault(); HAL_Delay(100); - Brakes.brake(); + brakes.brake(); }, GeneralStates::Fault); // el sdc tambien manda a fault? - OperationalStateMachine.add_transition( + /* OperationalStateMachine.add_transition( OperationalStates::Idle, OperationalStates::Energyzed, [&]() { return ethernet->requested_close_contactors; }); @@ -98,19 +92,18 @@ class VCU_SM { [&]() { return ethernet->requested_end_of_run; }); OperationalStateMachine.add_transition( - OperationalStates::Energyzed, OperationalStates::Idle, [&]() { - return ethernet->requested_open_contactors; - }); + OperationalStates::Energyzed, OperationalStates::Idle, + [&]() { return ethernet->requested_open_contactors; }); */ OperationalStateMachine.add_transition( OperationalStates::Ready, OperationalStates::Energyzed, - [&]() { return Brakes.Active_brakes; }); + [&]() { return brakes.Active_brakes; }); OperationalStateMachine.add_transition( OperationalStates::Energyzed, OperationalStates::Ready, - [&]() { return (!Brakes.Active_brakes); }); + [&]() { return (!brakes.Active_brakes); }); - OperationalStateMachine.add_enter_action( + /* OperationalStateMachine.add_enter_action( [&]() { ethernet->requested_close_contactors = false; }, OperationalStates::Energyzed); @@ -120,11 +113,8 @@ class VCU_SM { OperationalStateMachine.add_enter_action( [&]() { ethernet->requested_open_contactors = false; }, - OperationalStates::Idle); + OperationalStates::Idle); */ - ProtectionManager::link_state_machine(GeneralStateMachine, - GeneralStates::Fault); - ProtectionManager::add_standard_protections(); - // ethernet->initialize_state_orders(); */ + // ethernet->initialize_state_orders(); } }; \ No newline at end of file diff --git a/Core/Src/Runes/generated_metadata.cpp b/Core/Src/Runes/generated_metadata.cpp index ada8947..e21748f 100644 --- a/Core/Src/Runes/generated_metadata.cpp +++ b/Core/Src/Runes/generated_metadata.cpp @@ -5,11 +5,11 @@ extern "C"{ const char DESCRIPTION[255] __attribute__((section(".metadata_pool")))= "****************" // placeholder for beggining - "20250627T030038" // DateTime using ISO-8601 format + "20250627T032134" // DateTime using ISO-8601 format " " // alignment "f8e647d0" // STLIB commit "--------" // ADJ commit - "33927553" // Board commit + "58bcb5bf" // Board commit // the '=' is used for unparsing ; } \ No newline at end of file From 51295b5f7a2e999b01836ea0130062dd983f7d4c Mon Sep 17 00:00:00 2001 From: jdmarmen Date: Sat, 28 Jun 2025 00:58:12 +0200 Subject: [PATCH 04/25] Finished Comms.hpp implementation --- Core/Inc/Code_generation/JSON_ADE | 2 +- Core/Inc/VCU/Comms.hpp | 203 +++++++++++++++++++++++++- Core/Src/Runes/generated_metadata.cpp | 4 +- 3 files changed, 202 insertions(+), 7 deletions(-) diff --git a/Core/Inc/Code_generation/JSON_ADE b/Core/Inc/Code_generation/JSON_ADE index 0fdf9c8..c9f7efc 160000 --- a/Core/Inc/Code_generation/JSON_ADE +++ b/Core/Inc/Code_generation/JSON_ADE @@ -1 +1 @@ -Subproject commit 0fdf9c8976a7cafb4f1125666f28d063046a1a75 +Subproject commit c9f7efc38aa0c225b2a55a8daa74911e25ca3c93 diff --git a/Core/Inc/VCU/Comms.hpp b/Core/Inc/VCU/Comms.hpp index a259b6d..c0d24f4 100644 --- a/Core/Inc/VCU/Comms.hpp +++ b/Core/Inc/VCU/Comms.hpp @@ -3,8 +3,9 @@ class Comms { public: - - enum Orders_id : uint16_t { + // -----------------Enums----------------- + enum class Orders_id : uint16_t { + // IDs inventadas excepto las de id con 200 Open_contactors = 1000, Close_contactors = 1001, Unbrake = 201, @@ -24,7 +25,7 @@ class Comms { Disable_booster = 1010 }; - enum Packets_id : uint16_t { + enum class Packets_id : uint16_t { Flow = 250, Reeds = 251, Regulator = 252, @@ -33,21 +34,215 @@ class Comms { Tapes_enable = 255 }; + enum class Boards : uint8_t { PCU, HVSCU, BMSL, LCU, BLCU, BCU }; + + // -----------------Structs----------------- + struct Flags_ready { + bool requested_levitation_active; + bool requested_propulsion_active; + bool requested_charging_LV_battery; + bool requested_enable_booster; + + Flags_ready() + : requested_levitation_active(false), + requested_propulsion_active(false), + requested_charging_LV_battery(false), + requested_enable_booster(false) {} + }; + + struct PendingOrder { + bool control_station; + bool board; + }; + + struct BoardOrder { + Boards Board; + HeapStateOrder* order; + }; + + // -----------------Flags----------------- + static inline Flags_ready flags_ready; + static inline bool requested_open_contactors{}; + static inline bool requested_close_contactors{}; + static inline bool requested_end_of_run{}; + // -----------------IP's/Ports----------------- static constexpr std::string CONTROL_SATION_IP = "192.168.0.9"; + static constexpr std::string VCU_IP = "192.168.1.3"; + static constexpr std::string PCU_IP = "192.168.1.3"; + static constexpr std::string HVSCU_IP = "192.168.1.3"; + static constexpr std::string BMSL_IP = "192.168.1.3"; + static constexpr std::string LCU_IP = "192.168.1.3"; + static constexpr std::string BLCU_IP = "192.168.1.3"; static const uint16_t LOCAL_PORT = 50500; static const uint16_t CONTROL_STATION_UDP_PORT = 50400; + static const uint16_t UDP_PORT = 50401; + + static const uint16_t PCU_PORT = 50501; + static const uint16_t PCU_UDP_PORT = 50402; + + static const uint16_t HVSCU_PORT = 50502; + static const uint16_t HVSCU_UDP_PORT = 50403; + + static const uint16_t BMSL_PORT = 50503; + static const uint16_t BMSL_UDP_PORT = 50404; + static const uint16_t LCU_PORT = 50504; + static const uint16_t LCU_UDP_PORT = 50405; + + static const uint16_t BLCU_PORT = 50505; + static const uint16_t BLCU_UDP_PORT = 50406; // -----------------Sockets----------------- static inline ServerSocket* control_station_tcp{}; static inline DatagramSocket* control_station_udp{}; + static inline Socket pcu_tcp{}; + static inline DatagramSocket pcu_udp{}; + + static inline Socket hvscu_tcp{}; + static inline DatagramSocket hvscu_udp{}; + + static inline Socket bmsl_tcp{}; + static inline DatagramSocket bmsl_udp{}; + + static inline Socket lcu_tcp{}; + static inline DatagramSocket lcu_udp{}; + + static inline Socket blcu_tcp{}; + static inline DatagramSocket blcu_udp{}; + + // -----------------Packets----------------- + static inline HeapPacket* Reeds{}; + static inline HeapPacket* flow{}; + static inline HeapPacket* Regulator{}; + static inline HeapPacket* Pressure{}; + // static inline HeapPacket* Tapes{}; + static inline HeapPacket* Tapes_enable{}; + + /* static std::vector packets{}; // Lo que mando a la gui + struct OrderTriggers { + Boards board; + bool* flag; + HeapOrder* order; + }; + static std::vector order_triggers; */ + + // -----------------Orders----------------- + static inline HeapOrder* Potencia_refri{}; + static inline HeapOrder* Set_Regulator{}; + static inline HeapOrder* Enable_tapes{}; + static inline HeapOrder* Disable_tapes{}; + + static inline HeapStateOrder* Open_Contactors{}; + static inline HeapStateOrder* Close_Contactors{}; + static inline HeapStateOrder* Unbrake{}; + static inline HeapStateOrder* Brake{}; + static inline HeapStateOrder* EndOfRun{}; + + static inline HeapStateOrder* Levitation_Active{}; + static inline HeapStateOrder* Propulsion_Active{}; + static inline HeapStateOrder* Charging_LV_Battery{}; + static inline HeapStateOrder* Enable_Booster{}; + static inline HeapStateOrder* Levitation_Inactive{}; + static inline HeapStateOrder* Propulsion_Inactive{}; + static inline HeapStateOrder* Charging_LV_Battery_Inactive{}; + static inline HeapStateOrder* Disable_booster{}; + // -----------------Functions----------------- static void init(); static void add_packets(); static void add_orders(); - + + void update(); + bool connected(); + + static void on_brake(); + static void on_unbrake(); + static void on_end_of_run(); + static void on_potencia_refri(); + static void on_Set_regulator(); + static void on_Enable_tapes(); + static void on_Disable_tapes(); + + // Funciones de las flags, y las que se mandan a otras placas, cambiar y tal + // al gusto: + static void on_open_contactors(); + static void on_close_contactors(); + static void on_levitation_active(); + static void on_propulsion_active(); + static void on_charging_LV_battery(); + static void on_enable_booster(); + static void on_levitation_inactive(); + static void on_propulsion_inactive(); + static void on_charging_LV_battery_inactive(); + static void on_disable_booster(); + + // -----------------Unordered maps----------------- + static inline std::unordered_map Socket_to_board{}; + + static inline std::unordered_map Board_to_ip{ + {Boards::PCU, PCU_IP}, + {Boards::HVSCU, HVSCU_IP}, + {Boards::BMSL, BMSL_IP}, + {Boards::LCU, LCU_IP}, + {Boards::BLCU, BLCU_IP}}; + + static inline std::unordered_map id_to_pending{ + {Orders_id::Levitation_active, {false, false}}, + {Orders_id::Propulsion_active, {false, false}}, + {Orders_id::Charging_LV_battery, {false, false}}, + {Orders_id::Enable_booster, {false, false}}, + {Orders_id::Open_contactors, {false, false}}, + // {Orders_id::Brake, {false, false}}, + {Orders_id::Close_contactors, {false, false}}, + // {Orders_id::Unbrake, {false, false}}, + {Orders_id::Levitation_inactive, {false, false}}, + {Orders_id::Propulsion_inactive, {false, false}}, + {Orders_id::Charging_LV_battery_inactive, {false, false}}, + {Orders_id::Disable_booster, {false, false}}}; + + static inline std::unordered_map> + id_to_flag{ + {Orders_id::Levitation_active, + {&flags_ready.requested_levitation_active, true}}, + {Orders_id::Propulsion_active, + {&flags_ready.requested_propulsion_active, true}}, + {Orders_id::Charging_LV_battery, + {&flags_ready.requested_charging_LV_battery, true}}, + {Orders_id::Enable_booster, + {&flags_ready.requested_enable_booster, true}}, + {Orders_id::Open_contactors, {&requested_open_contactors, true}}, + // {Orders_id::Brake, {&requested_brake, true}}, + {Orders_id::Levitation_inactive, + {&flags_ready.requested_levitation_active, false}}, + {Orders_id::Propulsion_inactive, + {&flags_ready.requested_propulsion_active, false}}, + {Orders_id::Charging_LV_battery_inactive, + {&flags_ready.requested_charging_LV_battery, false}}, + {Orders_id::Disable_booster, + {&flags_ready.requested_enable_booster, false}}, + {Orders_id::Close_contactors, {&requested_close_contactors, true}}, + // {Orders_id::Unbrake, {&requested_unbrake, true}} + // {Orders_id::EndOfRun_id, {&requested_end_of_run, true}} + }; + + static inline std::unordered_map id_to_orders{ + {Orders_id::Levitation_active, {Boards::LCU, Levitation_Active}}, + {Orders_id::Propulsion_active, {Boards::PCU, Propulsion_Active}}, + {Orders_id::Charging_LV_battery, {Boards::BMSL, Charging_LV_Battery}}, + {Orders_id::Enable_booster, {Boards::BCU, Enable_Booster}}, + {Orders_id::Open_contactors, {Boards::HVSCU, Open_Contactors}}, + // {Orders_id::Brake, {Boards::, Brake}}, + {Orders_id::Close_contactors, {Boards::HVSCU, Close_Contactors}}, + // {Orders_id::Unbrake, {Boards::PCU, Unbrake}}, + {Orders_id::Levitation_inactive, {Boards::LCU, Levitation_Inactive}}, + {Orders_id::Propulsion_inactive, {Boards::PCU, Propulsion_Inactive}}, + {Orders_id::Charging_LV_battery_inactive, + {Boards::BMSL, Charging_LV_Battery_Inactive}}, + {Orders_id::Disable_booster, {Boards::BCU, Disable_booster}}}; + + static inline std::unordered_map id_to_timeout{}; }; \ No newline at end of file diff --git a/Core/Src/Runes/generated_metadata.cpp b/Core/Src/Runes/generated_metadata.cpp index e21748f..b6dc924 100644 --- a/Core/Src/Runes/generated_metadata.cpp +++ b/Core/Src/Runes/generated_metadata.cpp @@ -5,11 +5,11 @@ extern "C"{ const char DESCRIPTION[255] __attribute__((section(".metadata_pool")))= "****************" // placeholder for beggining - "20250627T032134" // DateTime using ISO-8601 format + "20250628T005557" // DateTime using ISO-8601 format " " // alignment "f8e647d0" // STLIB commit "--------" // ADJ commit - "58bcb5bf" // Board commit + "8f37276a" // Board commit // the '=' is used for unparsing ; } \ No newline at end of file From 638d27774511898b5cf7feed5c25483bc5b2255e Mon Sep 17 00:00:00 2001 From: jdmarmen Date: Sat, 28 Jun 2025 01:53:46 +0200 Subject: [PATCH 05/25] First working order --- Core/Inc/Code_generation/JSON_ADE | 2 +- Core/Inc/VCU/Comms.hpp | 10 +++++++++ Core/Inc/state_machine.hpp | 31 +++++++++++---------------- Core/Src/Runes/generated_metadata.cpp | 4 ++-- Core/Src/VCU/Comms.cpp | 30 ++++++++++++++++++++------ Core/Src/VCU/VCU.cpp | 11 +++++++--- 6 files changed, 56 insertions(+), 32 deletions(-) diff --git a/Core/Inc/Code_generation/JSON_ADE b/Core/Inc/Code_generation/JSON_ADE index c9f7efc..3c8eb13 160000 --- a/Core/Inc/Code_generation/JSON_ADE +++ b/Core/Inc/Code_generation/JSON_ADE @@ -1 +1 @@ -Subproject commit c9f7efc38aa0c225b2a55a8daa74911e25ca3c93 +Subproject commit 3c8eb13ebea7dfe525e84a12da96c44f961268a5 diff --git a/Core/Inc/VCU/Comms.hpp b/Core/Inc/VCU/Comms.hpp index c0d24f4..fd8fcb3 100644 --- a/Core/Inc/VCU/Comms.hpp +++ b/Core/Inc/VCU/Comms.hpp @@ -1,8 +1,17 @@ #pragma once #include "ST-LIB.hpp" +#include "VCU/Actuators.hpp" +#include "VCU/Brakes.hpp" +#include "VCU/Comms.hpp" +#include "VCU/Leds.hpp" class Comms { public: + + static inline Leds* leds; + static inline Actuators* actuators; + static inline Brakes* brakes; + // -----------------Enums----------------- enum class Orders_id : uint16_t { // IDs inventadas excepto las de id con 200 @@ -153,6 +162,7 @@ class Comms { // -----------------Functions----------------- static void init(); + static void start(); static void add_packets(); static void add_orders(); diff --git a/Core/Inc/state_machine.hpp b/Core/Inc/state_machine.hpp index 28e12e6..71b072d 100644 --- a/Core/Inc/state_machine.hpp +++ b/Core/Inc/state_machine.hpp @@ -1,9 +1,6 @@ #pragma once #include "ST-LIB.hpp" -#include "VCU/Actuators.hpp" -#include "VCU/Brakes.hpp" #include "VCU/Comms.hpp" -#include "VCU/Leds.hpp" using namespace std::chrono_literals; @@ -25,10 +22,6 @@ class VCU_SM { StateMachine GeneralStateMachine; StateMachine OperationalStateMachine; - Leds leds; - Actuators actuators; - Brakes brakes; - VCU_SM() { GeneralStateMachine = StateMachine(GeneralStates::Connecting); GeneralStateMachine.add_state(GeneralStates::Operational); @@ -49,35 +42,35 @@ class VCU_SM { GeneralStateMachine.add_transition( GeneralStates::Operational, GeneralStates::Fault, [&]() { - return ((brakes.All_reeds && brakes.Active_brakes) && - (!brakes.breaks_first_time)); + return ((Comms::brakes->All_reeds && Comms::brakes->Active_brakes) && + (!Comms::brakes->breaks_first_time)); }); GeneralStateMachine.add_transition( GeneralStates::Connecting, GeneralStates::Fault, [&]() { - return ((brakes.All_reeds && brakes.Active_brakes) && - (!brakes.breaks_first_time)); + return ((Comms::brakes->All_reeds && Comms::brakes->Active_brakes) && + (!Comms::brakes->breaks_first_time)); }); GeneralStateMachine.add_transition(GeneralStates::Operational, GeneralStates::Fault, - [&]() { return (!actuators.Sdc); }); + [&]() { return (!Comms::actuators->Sdc); }); GeneralStateMachine.add_transition(GeneralStates::Connecting, GeneralStates::Fault, - [&]() { return (!actuators.Sdc); }); + [&]() { return (!Comms::actuators->Sdc); }); - GeneralStateMachine.add_enter_action([&]() { leds.leds_connecting(); }, + GeneralStateMachine.add_enter_action([&]() { Comms::leds->leds_connecting(); }, GeneralStates::Connecting); - GeneralStateMachine.add_enter_action([&]() { leds.leds_operational(); }, + GeneralStateMachine.add_enter_action([&]() { Comms::leds->leds_operational(); }, GeneralStates::Operational); GeneralStateMachine.add_enter_action( [&]() { - leds.leds_fault(); + Comms::leds->leds_fault(); HAL_Delay(100); - brakes.brake(); + Comms::brakes->brake(); }, GeneralStates::Fault); @@ -97,11 +90,11 @@ class VCU_SM { OperationalStateMachine.add_transition( OperationalStates::Ready, OperationalStates::Energyzed, - [&]() { return brakes.Active_brakes; }); + [&]() { return Comms::brakes->Active_brakes; }); OperationalStateMachine.add_transition( OperationalStates::Energyzed, OperationalStates::Ready, - [&]() { return (!brakes.Active_brakes); }); + [&]() { return (!Comms::brakes->Active_brakes); }); /* OperationalStateMachine.add_enter_action( [&]() { ethernet->requested_close_contactors = false; }, diff --git a/Core/Src/Runes/generated_metadata.cpp b/Core/Src/Runes/generated_metadata.cpp index b6dc924..129cbfb 100644 --- a/Core/Src/Runes/generated_metadata.cpp +++ b/Core/Src/Runes/generated_metadata.cpp @@ -5,11 +5,11 @@ extern "C"{ const char DESCRIPTION[255] __attribute__((section(".metadata_pool")))= "****************" // placeholder for beggining - "20250628T005557" // DateTime using ISO-8601 format + "20250628T014926" // DateTime using ISO-8601 format " " // alignment "f8e647d0" // STLIB commit "--------" // ADJ commit - "8f37276a" // Board commit + "51295b5f" // Board commit // the '=' is used for unparsing ; } \ No newline at end of file diff --git a/Core/Src/VCU/Comms.cpp b/Core/Src/VCU/Comms.cpp index 7990afc..c8e1a7d 100644 --- a/Core/Src/VCU/Comms.cpp +++ b/Core/Src/VCU/Comms.cpp @@ -1,17 +1,33 @@ #include "VCU/Comms.hpp" -void Comms::init(){ - control_station_tcp = new ServerSocket(IPV4(VCU_IP), LOCAL_PORT); - control_station_udp = new DatagramSocket(IPV4(VCU_IP), CONTROL_STATION_UDP_PORT,IPV4(CONTROL_SATION_IP), CONTROL_STATION_UDP_PORT); +void Comms::on_potencia_refri() { + if (actuators->selected_pump == Actuators::Pump::PUMP_UNIDADES) { + actuators->set_pump_1(actuators->selected_pump_duty); + } else if (actuators->selected_pump == Actuators::Pump::PUMP_PLACAS) { + actuators->set_pump_2(actuators->selected_pump_duty); + } +} - //add_packets(); - //add_orders(); +void Comms::init() { + leds = new Leds(); + actuators = new Actuators(); + brakes = new Brakes(); } -void Comms::add_packets(){ +void Comms::start() { + control_station_tcp = new ServerSocket(IPV4(VCU_IP), LOCAL_PORT); + control_station_udp = + new DatagramSocket(IPV4(VCU_IP), CONTROL_STATION_UDP_PORT, + IPV4(CONTROL_SATION_IP), CONTROL_STATION_UDP_PORT); + add_packets(); + add_orders(); } -void Comms::add_orders(){ +void Comms::add_packets() {} +void Comms::add_orders() { + Potencia_refri = new HeapOrder( + static_cast(Orders_id::Potencia_refri_id), &on_potencia_refri, + &actuators->selected_pump_duty, &actuators->selected_pump); } \ No newline at end of file diff --git a/Core/Src/VCU/VCU.cpp b/Core/Src/VCU/VCU.cpp index 0fd2f7f..a07443b 100644 --- a/Core/Src/VCU/VCU.cpp +++ b/Core/Src/VCU/VCU.cpp @@ -9,12 +9,17 @@ void VCU::init(){ ProtectionManager::link_state_machine(state_machine->GeneralStateMachine, VCU_SM::GeneralStates::Fault); ProtectionManager::set_id(Boards::ID::VCU); + Comms::init(); } void VCU::start(){ - state_machine->actuators.init(); - state_machine->brakes.init(); - Comms::init(); + //state_machine->actuators.init(); + //state_machine->brakes.init(); + + Comms::actuators->init(); + Comms::brakes->init(); + + Comms::start(); } void VCU::update(){ From afb72f2ee16a0a90470df22ff125dcc44f039523 Mon Sep 17 00:00:00 2001 From: jdmarmen Date: Fri, 4 Jul 2025 19:58:58 +0200 Subject: [PATCH 06/25] Added udp packet sending logic --- Core/Inc/Code_generation/JSON_ADE | 2 +- Core/Inc/VCU/Comms.hpp | 4 +++ Core/Inc/VCU/VCU.hpp | 7 +++-- Core/Inc/state_machine.hpp | 38 ++++++++++++++++++--------- Core/Src/Runes/generated_metadata.cpp | 4 +-- Core/Src/VCU/Comms.cpp | 10 ++++++- Core/Src/VCU/VCU.cpp | 11 +++++++- 7 files changed, 57 insertions(+), 19 deletions(-) diff --git a/Core/Inc/Code_generation/JSON_ADE b/Core/Inc/Code_generation/JSON_ADE index 3c8eb13..1517110 160000 --- a/Core/Inc/Code_generation/JSON_ADE +++ b/Core/Inc/Code_generation/JSON_ADE @@ -1 +1 @@ -Subproject commit 3c8eb13ebea7dfe525e84a12da96c44f961268a5 +Subproject commit 15171102587e02560b6b18b10fe1fb316385b86a diff --git a/Core/Inc/VCU/Comms.hpp b/Core/Inc/VCU/Comms.hpp index fd8fcb3..ad6fc6a 100644 --- a/Core/Inc/VCU/Comms.hpp +++ b/Core/Inc/VCU/Comms.hpp @@ -35,6 +35,7 @@ class Comms { }; enum class Packets_id : uint16_t { + States = 249, Flow = 250, Reeds = 251, Regulator = 252, @@ -74,6 +75,7 @@ class Comms { static inline bool requested_open_contactors{}; static inline bool requested_close_contactors{}; static inline bool requested_end_of_run{}; + static inline bool packet_sending{}; // -----------------IP's/Ports----------------- static constexpr std::string CONTROL_SATION_IP = "192.168.0.9"; @@ -124,6 +126,7 @@ class Comms { static inline DatagramSocket blcu_udp{}; // -----------------Packets----------------- + static inline HeapPacket* states{}; static inline HeapPacket* Reeds{}; static inline HeapPacket* flow{}; static inline HeapPacket* Regulator{}; @@ -165,6 +168,7 @@ class Comms { static void start(); static void add_packets(); static void add_orders(); + static void send_packets(); void update(); bool connected(); diff --git a/Core/Inc/VCU/VCU.hpp b/Core/Inc/VCU/VCU.hpp index 481aa07..f16520c 100644 --- a/Core/Inc/VCU/VCU.hpp +++ b/Core/Inc/VCU/VCU.hpp @@ -1,11 +1,14 @@ #pragma once -#include "state_machine.hpp" #include "Comms.hpp" +#include "state_machine.hpp" class VCU { + static void send_packets(); + public: static inline VCU_SM* state_machine{}; - static inline uint8_t* VCU_state{}; + static inline uint8_t* general_state{}; + static inline uint8_t* operational_state{}; static void init(); static void start(); diff --git a/Core/Inc/state_machine.hpp b/Core/Inc/state_machine.hpp index 71b072d..9db4d62 100644 --- a/Core/Inc/state_machine.hpp +++ b/Core/Inc/state_machine.hpp @@ -42,29 +42,33 @@ class VCU_SM { GeneralStateMachine.add_transition( GeneralStates::Operational, GeneralStates::Fault, [&]() { - return ((Comms::brakes->All_reeds && Comms::brakes->Active_brakes) && + return ((Comms::brakes->All_reeds && + Comms::brakes->Active_brakes) && (!Comms::brakes->breaks_first_time)); }); GeneralStateMachine.add_transition( GeneralStates::Connecting, GeneralStates::Fault, [&]() { - return ((Comms::brakes->All_reeds && Comms::brakes->Active_brakes) && + return ((Comms::brakes->All_reeds && + Comms::brakes->Active_brakes) && (!Comms::brakes->breaks_first_time)); }); - GeneralStateMachine.add_transition(GeneralStates::Operational, - GeneralStates::Fault, - [&]() { return (!Comms::actuators->Sdc); }); + GeneralStateMachine.add_transition( + GeneralStates::Operational, GeneralStates::Fault, + [&]() { return (!Comms::actuators->Sdc); }); - GeneralStateMachine.add_transition(GeneralStates::Connecting, - GeneralStates::Fault, - [&]() { return (!Comms::actuators->Sdc); }); + GeneralStateMachine.add_transition( + GeneralStates::Connecting, GeneralStates::Fault, + [&]() { return (!Comms::actuators->Sdc); }); - GeneralStateMachine.add_enter_action([&]() { Comms::leds->leds_connecting(); }, - GeneralStates::Connecting); + GeneralStateMachine.add_enter_action( + [&]() { Comms::leds->leds_connecting(); }, + GeneralStates::Connecting); - GeneralStateMachine.add_enter_action([&]() { Comms::leds->leds_operational(); }, - GeneralStates::Operational); + GeneralStateMachine.add_enter_action( + [&]() { Comms::leds->leds_operational(); }, + GeneralStates::Operational); GeneralStateMachine.add_enter_action( [&]() { @@ -109,5 +113,15 @@ class VCU_SM { OperationalStates::Idle); */ // ethernet->initialize_state_orders(); + + GeneralStateMachine.add_low_precision_cyclic_action( + [&]() { Comms::packet_sending = true; }, 100ms, + GeneralStates::Connecting); + GeneralStateMachine.add_low_precision_cyclic_action( + [&]() { Comms::packet_sending = true; }, 100ms, + GeneralStates::Operational); + GeneralStateMachine.add_low_precision_cyclic_action( + [&]() { Comms::packet_sending = true; }, 100ms, + GeneralStates::Fault); } }; \ No newline at end of file diff --git a/Core/Src/Runes/generated_metadata.cpp b/Core/Src/Runes/generated_metadata.cpp index 129cbfb..869caa6 100644 --- a/Core/Src/Runes/generated_metadata.cpp +++ b/Core/Src/Runes/generated_metadata.cpp @@ -5,11 +5,11 @@ extern "C"{ const char DESCRIPTION[255] __attribute__((section(".metadata_pool")))= "****************" // placeholder for beggining - "20250628T014926" // DateTime using ISO-8601 format + "20250704T195126" // DateTime using ISO-8601 format " " // alignment "f8e647d0" // STLIB commit "--------" // ADJ commit - "51295b5f" // Board commit + "638d2777" // Board commit // the '=' is used for unparsing ; } \ No newline at end of file diff --git a/Core/Src/VCU/Comms.cpp b/Core/Src/VCU/Comms.cpp index c8e1a7d..48a7669 100644 --- a/Core/Src/VCU/Comms.cpp +++ b/Core/Src/VCU/Comms.cpp @@ -1,4 +1,5 @@ #include "VCU/Comms.hpp" +#include "VCU/VCU.hpp" void Comms::on_potencia_refri() { if (actuators->selected_pump == Actuators::Pump::PUMP_UNIDADES) { @@ -24,10 +25,17 @@ void Comms::start() { add_orders(); } -void Comms::add_packets() {} +void Comms::add_packets() { + states = new HeapPacket(static_cast(Packets_id::States), + VCU::general_state, VCU::operational_state); +} void Comms::add_orders() { Potencia_refri = new HeapOrder( static_cast(Orders_id::Potencia_refri_id), &on_potencia_refri, &actuators->selected_pump_duty, &actuators->selected_pump); +} + +void Comms::send_packets(){ + control_station_udp->send_packet(*states); } \ No newline at end of file diff --git a/Core/Src/VCU/VCU.cpp b/Core/Src/VCU/VCU.cpp index a07443b..70c52b3 100644 --- a/Core/Src/VCU/VCU.cpp +++ b/Core/Src/VCU/VCU.cpp @@ -2,7 +2,8 @@ void VCU::init(){ state_machine = new VCU_SM(); - VCU_state = &state_machine->GeneralStateMachine.current_state; + general_state = &state_machine->GeneralStateMachine.current_state; + operational_state = &state_machine->OperationalStateMachine.current_state; ProtectionManager::add_standard_protections(); ProtectionManager::initialize(); @@ -22,7 +23,15 @@ void VCU::start(){ Comms::start(); } +void VCU::send_packets(){ + if(Comms::packet_sending){ + Comms::send_packets(); + Comms::packet_sending = false; + } +} + void VCU::update(){ state_machine->GeneralStateMachine.check_transitions(); + send_packets(); ProtectionManager::check_protections(); } \ No newline at end of file From 6afef347fed5c4edae27d4f4839875b979929a54 Mon Sep 17 00:00:00 2001 From: jdmarmen Date: Sat, 5 Jul 2025 10:27:49 +0200 Subject: [PATCH 07/25] Added packets --- Core/Inc/Code_generation/JSON_ADE | 2 +- Core/Inc/VCU/Comms.hpp | 16 +++--- Core/Inc/VCU/VCU.hpp | 1 + Core/Inc/state_machine.hpp | 10 ++++ Core/Src/Runes/generated_metadata.cpp | 4 +- Core/Src/VCU/Comms.cpp | 82 ++++++++++++++++++++++++++- Core/Src/VCU/VCU.cpp | 8 +++ 7 files changed, 111 insertions(+), 12 deletions(-) diff --git a/Core/Inc/Code_generation/JSON_ADE b/Core/Inc/Code_generation/JSON_ADE index 1517110..ee80a0a 160000 --- a/Core/Inc/Code_generation/JSON_ADE +++ b/Core/Inc/Code_generation/JSON_ADE @@ -1 +1 @@ -Subproject commit 15171102587e02560b6b18b10fe1fb316385b86a +Subproject commit ee80a0a5374eff94d846830615976a414575a4a0 diff --git a/Core/Inc/VCU/Comms.hpp b/Core/Inc/VCU/Comms.hpp index ad6fc6a..31ab3fe 100644 --- a/Core/Inc/VCU/Comms.hpp +++ b/Core/Inc/VCU/Comms.hpp @@ -76,6 +76,7 @@ class Comms { static inline bool requested_close_contactors{}; static inline bool requested_end_of_run{}; static inline bool packet_sending{}; + static inline bool reading_sensors{}; // -----------------IP's/Ports----------------- static constexpr std::string CONTROL_SATION_IP = "192.168.0.9"; @@ -127,12 +128,12 @@ class Comms { // -----------------Packets----------------- static inline HeapPacket* states{}; - static inline HeapPacket* Reeds{}; + static inline HeapPacket* reeds{}; static inline HeapPacket* flow{}; - static inline HeapPacket* Regulator{}; - static inline HeapPacket* Pressure{}; + static inline HeapPacket* regulator{}; + static inline HeapPacket* pressure{}; // static inline HeapPacket* Tapes{}; - static inline HeapPacket* Tapes_enable{}; + static inline HeapPacket* tapes_enabled{}; /* static std::vector packets{}; // Lo que mando a la gui struct OrderTriggers { @@ -167,11 +168,12 @@ class Comms { static void init(); static void start(); static void add_packets(); - static void add_orders(); static void send_packets(); + static void add_orders(); + static void add_state_orders(); - void update(); - bool connected(); + static void update(); + static bool connected(); static void on_brake(); static void on_unbrake(); diff --git a/Core/Inc/VCU/VCU.hpp b/Core/Inc/VCU/VCU.hpp index f16520c..7b032c1 100644 --- a/Core/Inc/VCU/VCU.hpp +++ b/Core/Inc/VCU/VCU.hpp @@ -4,6 +4,7 @@ class VCU { static void send_packets(); + static void read_sensors(); public: static inline VCU_SM* state_machine{}; diff --git a/Core/Inc/state_machine.hpp b/Core/Inc/state_machine.hpp index 9db4d62..d09dea3 100644 --- a/Core/Inc/state_machine.hpp +++ b/Core/Inc/state_machine.hpp @@ -123,5 +123,15 @@ class VCU_SM { GeneralStateMachine.add_low_precision_cyclic_action( [&]() { Comms::packet_sending = true; }, 100ms, GeneralStates::Fault); + + GeneralStateMachine.add_low_precision_cyclic_action( + [&]() { Comms::reading_sensors = true; }, 100ms, + GeneralStates::Connecting); + GeneralStateMachine.add_low_precision_cyclic_action( + [&]() { Comms::reading_sensors = true; }, 100ms, + GeneralStates::Operational); + GeneralStateMachine.add_low_precision_cyclic_action( + [&]() { Comms::reading_sensors = true; }, 100ms, + GeneralStates::Fault); } }; \ No newline at end of file diff --git a/Core/Src/Runes/generated_metadata.cpp b/Core/Src/Runes/generated_metadata.cpp index 869caa6..9fbf321 100644 --- a/Core/Src/Runes/generated_metadata.cpp +++ b/Core/Src/Runes/generated_metadata.cpp @@ -5,11 +5,11 @@ extern "C"{ const char DESCRIPTION[255] __attribute__((section(".metadata_pool")))= "****************" // placeholder for beggining - "20250704T195126" // DateTime using ISO-8601 format + "20250705T102558" // DateTime using ISO-8601 format " " // alignment "f8e647d0" // STLIB commit "--------" // ADJ commit - "638d2777" // Board commit + "afb72f2e" // Board commit // the '=' is used for unparsing ; } \ No newline at end of file diff --git a/Core/Src/VCU/Comms.cpp b/Core/Src/VCU/Comms.cpp index 48a7669..e30bdd7 100644 --- a/Core/Src/VCU/Comms.cpp +++ b/Core/Src/VCU/Comms.cpp @@ -1,4 +1,5 @@ #include "VCU/Comms.hpp" + #include "VCU/VCU.hpp" void Comms::on_potencia_refri() { @@ -9,6 +10,34 @@ void Comms::on_potencia_refri() { } } +void Comms::on_Set_regulator() { + if (actuators->selected_regulator_pressure > 6) { + actuators->selected_regulator_pressure = 6; + // InfoWarning::InfoWarningTrigger("Cannot exceed 6 bar on regulator + // pressure"); + } + if (actuators->selected_regulator == Actuators::Regulator::REGULATOR_1) { + actuators->set_regulator_1(actuators->selected_regulator_pressure); + } else if (actuators->selected_regulator == + Actuators::Regulator::REGULATOR_2) { + actuators->set_regulator_2(actuators->selected_regulator_pressure); + } +} + +void Comms::on_Enable_tapes() { + brakes->Tape_enabled = true; + brakes->Tape_output.turn_on(); +} + +void Comms::on_Disable_tapes() { + brakes->Tape_enabled = false; + brakes->Tape_output.turn_off(); +} + +void Comms::on_brake() { brakes->brake(); } + +void Comms::on_unbrake() { brakes->unbrake(); } + void Comms::init() { leds = new Leds(); actuators = new Actuators(); @@ -28,14 +57,63 @@ void Comms::start() { void Comms::add_packets() { states = new HeapPacket(static_cast(Packets_id::States), VCU::general_state, VCU::operational_state); + reeds = new HeapPacket(static_cast(Packets_id::Reeds), + &brakes->reed1, &brakes->reed2, &brakes->reed3, + &brakes->reed4, &brakes->reed5, &brakes->reed6, + &brakes->reed7, &brakes->reed8, &brakes->All_reeds); + flow = new HeapPacket(static_cast(Packets_id::Flow), + &actuators->flow1, &actuators->flow2); + regulator = new HeapPacket(static_cast(Packets_id::Regulator), + &actuators->regulator_1_input, + &actuators->regulator_2_input); + pressure = new HeapPacket(static_cast(Packets_id::Pressure), + &actuators->pressure_1, &actuators->pressure_2, + &actuators->pressure_3, &actuators->pressure_4); + tapes_enabled = new HeapPacket( + static_cast(Packets_id::Tapes_enable), &brakes->Tape_enabled); } void Comms::add_orders() { Potencia_refri = new HeapOrder( static_cast(Orders_id::Potencia_refri_id), &on_potencia_refri, &actuators->selected_pump_duty, &actuators->selected_pump); + Set_Regulator = new HeapOrder( + static_cast(Orders_id::Set_regulator_id), &on_Set_regulator, + &actuators->selected_regulator_pressure, + &actuators->selected_regulator); + Enable_tapes = new HeapOrder( + static_cast(Orders_id::Enable_tapes_id), &on_Enable_tapes); + Disable_tapes = new HeapOrder( + static_cast(Orders_id::Disable_tapes_id), &on_Disable_tapes); + + add_state_orders(); +} + +void Comms::add_state_orders() { + StateOrder::set_socket(*control_station_tcp); + Brake = + new HeapStateOrder(static_cast(Orders_id::Brake), &on_brake, + VCU::state_machine->GeneralStateMachine, + VCU_SM::GeneralStates::Operational); + Unbrake = + new HeapStateOrder(static_cast(Orders_id::Unbrake), + &on_unbrake, VCU::state_machine->GeneralStateMachine, + VCU_SM::GeneralStates::Operational); +} + +void Comms::send_packets() { + control_station_udp->send_packet(*states); + control_station_udp->send_packet(*reeds); + control_station_udp->send_packet(*flow); + control_station_udp->send_packet(*regulator); + control_station_udp->send_packet(*pressure); + control_station_udp->send_packet(*tapes_enabled); } -void Comms::send_packets(){ - control_station_udp->send_packet(*states); +void Comms::update(){ + actuators->read_regulators(); + actuators->read_pressure(); + actuators->read_flow(); + actuators->read_sdc(); + brakes->read_reeds(); } \ No newline at end of file diff --git a/Core/Src/VCU/VCU.cpp b/Core/Src/VCU/VCU.cpp index 70c52b3..1bfac7d 100644 --- a/Core/Src/VCU/VCU.cpp +++ b/Core/Src/VCU/VCU.cpp @@ -30,8 +30,16 @@ void VCU::send_packets(){ } } +void VCU::read_sensors(){ + if(Comms::reading_sensors){ + Comms::update(); + Comms::reading_sensors = false; + } +} + void VCU::update(){ state_machine->GeneralStateMachine.check_transitions(); + read_sensors(); send_packets(); ProtectionManager::check_protections(); } \ No newline at end of file From 690dd0e7a93b0edeff091d088d460e82dba68da8 Mon Sep 17 00:00:00 2001 From: jdmarmen Date: Sat, 5 Jul 2025 17:31:12 +0200 Subject: [PATCH 08/25] Changed some stuff from tapes + added missing comms functions --- Core/Inc/VCU/Brakes.hpp | 7 +++++-- Core/Inc/VCU/Comms.hpp | 13 ++++++++++--- Core/Inc/state_machine.hpp | 19 +++++++++++-------- Core/Src/Runes/generated_metadata.cpp | 4 ++-- Core/Src/VCU/Brakes.cpp | 8 +++++++- Core/Src/VCU/Comms.cpp | 7 ++++--- 6 files changed, 39 insertions(+), 19 deletions(-) diff --git a/Core/Inc/VCU/Brakes.hpp b/Core/Inc/VCU/Brakes.hpp index 62837d4..fdafd16 100644 --- a/Core/Inc/VCU/Brakes.hpp +++ b/Core/Inc/VCU/Brakes.hpp @@ -17,6 +17,8 @@ class Brakes { // DigitalSensor Tape_input; + DigitalSensor tape_emergency_input; + public: bool Active_brakes = true; bool breaks_first_time = true; @@ -32,10 +34,10 @@ class Brakes { bool All_reeds = false; - bool Tape_enabled = true; + PinState tape_emergency = PinState::ON; DigitalOutput Tape_output; - PinState Tape_state = PinState::OFF; + PinState Tape_status = PinState::OFF; Brakes(); @@ -43,4 +45,5 @@ class Brakes { void brake(); void unbrake(); void read_reeds(); + void read_tape_emergency(); }; \ No newline at end of file diff --git a/Core/Inc/VCU/Comms.hpp b/Core/Inc/VCU/Comms.hpp index 31ab3fe..acb654a 100644 --- a/Core/Inc/VCU/Comms.hpp +++ b/Core/Inc/VCU/Comms.hpp @@ -51,12 +51,14 @@ class Comms { bool requested_levitation_active; bool requested_propulsion_active; bool requested_charging_LV_battery; + bool requested_horizontal_levitation_active; bool requested_enable_booster; Flags_ready() : requested_levitation_active(false), requested_propulsion_active(false), requested_charging_LV_battery(false), + requested_horizontal_levitation_active(false), requested_enable_booster(false) {} }; @@ -177,7 +179,6 @@ class Comms { static void on_brake(); static void on_unbrake(); - static void on_end_of_run(); static void on_potencia_refri(); static void on_Set_regulator(); static void on_Enable_tapes(); @@ -185,17 +186,23 @@ class Comms { // Funciones de las flags, y las que se mandan a otras placas, cambiar y tal // al gusto: - static void on_open_contactors(); - static void on_close_contactors(); static void on_levitation_active(); static void on_propulsion_active(); static void on_charging_LV_battery(); + static void on_horizontal_levitation_active(); static void on_enable_booster(); + static void on_levitation_inactive(); static void on_propulsion_inactive(); static void on_charging_LV_battery_inactive(); + static void on_horizontal_levitation_inactive(); static void on_disable_booster(); + static void on_open_contactors(); + static void on_close_contactors(); + + static void on_end_of_run(); + // -----------------Unordered maps----------------- static inline std::unordered_map Socket_to_board{}; diff --git a/Core/Inc/state_machine.hpp b/Core/Inc/state_machine.hpp index d09dea3..31265e1 100644 --- a/Core/Inc/state_machine.hpp +++ b/Core/Inc/state_machine.hpp @@ -12,12 +12,7 @@ class VCU_SM { Fault, }; - enum OperationalStates { - Idle, - EndOfRun, - Energyzed, - Ready, - }; + enum OperationalStates { Idle, EndOfRun, Energyzed, Ready, Demonstration }; StateMachine GeneralStateMachine; StateMachine OperationalStateMachine; @@ -62,6 +57,13 @@ class VCU_SM { GeneralStates::Connecting, GeneralStates::Fault, [&]() { return (!Comms::actuators->Sdc); }); + GeneralStateMachine.add_transition( + GeneralStates::Operational, GeneralStates::Fault, [&]() { + bool emergency_tape = + static_cast(Comms::brakes->tape_emergency); + return !emergency_tape; + }); + GeneralStateMachine.add_enter_action( [&]() { Comms::leds->leds_connecting(); }, GeneralStates::Connecting); @@ -73,8 +75,7 @@ class VCU_SM { GeneralStateMachine.add_enter_action( [&]() { Comms::leds->leds_fault(); - HAL_Delay(100); - Comms::brakes->brake(); + Time::set_timeout(100, [&]() { Comms::brakes->brake(); }); }, GeneralStates::Fault); @@ -114,6 +115,8 @@ class VCU_SM { // ethernet->initialize_state_orders(); + //-----------------CYCLYC ACTIONS----------------------- + GeneralStateMachine.add_low_precision_cyclic_action( [&]() { Comms::packet_sending = true; }, 100ms, GeneralStates::Connecting); diff --git a/Core/Src/Runes/generated_metadata.cpp b/Core/Src/Runes/generated_metadata.cpp index 9fbf321..3e63a7f 100644 --- a/Core/Src/Runes/generated_metadata.cpp +++ b/Core/Src/Runes/generated_metadata.cpp @@ -5,11 +5,11 @@ extern "C"{ const char DESCRIPTION[255] __attribute__((section(".metadata_pool")))= "****************" // placeholder for beggining - "20250705T102558" // DateTime using ISO-8601 format + "20250705T173014" // DateTime using ISO-8601 format " " // alignment "f8e647d0" // STLIB commit "--------" // ADJ commit - "afb72f2e" // Board commit + "6afef347" // Board commit // the '=' is used for unparsing ; } \ No newline at end of file diff --git a/Core/Src/VCU/Brakes.cpp b/Core/Src/VCU/Brakes.cpp index 7f6d624..5ab294f 100644 --- a/Core/Src/VCU/Brakes.cpp +++ b/Core/Src/VCU/Brakes.cpp @@ -14,7 +14,9 @@ Brakes::Brakes(){ reed8_input = DigitalSensor(REED8_PIN, &reed8); // Tape_input = DigitalSensor(TAPE_INPUT_PIN, &Tape_state); - Tape_output = DigitalOutput(TAPE_PIN); + Tape_output = DigitalOutput(TAPE_E_PIN); + + tape_emergency_input = DigitalSensor(TAPE_PIN, &tape_emergency); } void Brakes::init(){ @@ -55,3 +57,7 @@ void Brakes::read_reeds() { reed7 && reed8; } + +void Brakes::read_tape_emergency() { + tape_emergency_input.read(); +} diff --git a/Core/Src/VCU/Comms.cpp b/Core/Src/VCU/Comms.cpp index e30bdd7..9ae1c13 100644 --- a/Core/Src/VCU/Comms.cpp +++ b/Core/Src/VCU/Comms.cpp @@ -25,12 +25,12 @@ void Comms::on_Set_regulator() { } void Comms::on_Enable_tapes() { - brakes->Tape_enabled = true; + brakes->Tape_status = PinState::ON; brakes->Tape_output.turn_on(); } void Comms::on_Disable_tapes() { - brakes->Tape_enabled = false; + brakes->Tape_status = PinState::OFF; brakes->Tape_output.turn_off(); } @@ -70,7 +70,7 @@ void Comms::add_packets() { &actuators->pressure_1, &actuators->pressure_2, &actuators->pressure_3, &actuators->pressure_4); tapes_enabled = new HeapPacket( - static_cast(Packets_id::Tapes_enable), &brakes->Tape_enabled); + static_cast(Packets_id::Tapes_enable), &brakes->Tape_status); } void Comms::add_orders() { @@ -116,4 +116,5 @@ void Comms::update(){ actuators->read_flow(); actuators->read_sdc(); brakes->read_reeds(); + brakes->read_tape_emergency(); } \ No newline at end of file From 86310c8bbdf3a49ca0c122ace9653a3bcef6ff6a Mon Sep 17 00:00:00 2001 From: jdmarmen Date: Sat, 5 Jul 2025 18:44:13 +0200 Subject: [PATCH 09/25] Changed brakes and tapes stuff --- Core/Inc/Code_generation/JSON_ADE | 2 +- Core/Inc/VCU/Actuators.hpp | 4 +- Core/Inc/VCU/Brakes.hpp | 4 +- Core/Inc/VCU/Comms.hpp | 58 +++++++++++----------- Core/Inc/state_machine.hpp | 5 +- Core/Src/Runes/generated_metadata.cpp | 4 +- Core/Src/VCU/Brakes.cpp | 10 ++-- Core/Src/VCU/Comms.cpp | 70 +++++++++++++++++++++------ 8 files changed, 104 insertions(+), 53 deletions(-) diff --git a/Core/Inc/Code_generation/JSON_ADE b/Core/Inc/Code_generation/JSON_ADE index ee80a0a..524859b 160000 --- a/Core/Inc/Code_generation/JSON_ADE +++ b/Core/Inc/Code_generation/JSON_ADE @@ -1 +1 @@ -Subproject commit ee80a0a5374eff94d846830615976a414575a4a0 +Subproject commit 524859b32bab8407de932cb08dc53a417d686806 diff --git a/Core/Inc/VCU/Actuators.hpp b/Core/Inc/VCU/Actuators.hpp index 0ada712..535ea52 100644 --- a/Core/Inc/VCU/Actuators.hpp +++ b/Core/Inc/VCU/Actuators.hpp @@ -30,6 +30,8 @@ class Actuators { Regulator selected_regulator; Pump selected_pump; float selected_regulator_pressure; + float regulator_1_pressure; + float regulator_2_pressure; PinState regulator_1_input; PinState regulator_2_input; @@ -39,7 +41,7 @@ class Actuators { PinState flow1; PinState flow2; - PinState Sdc; + PinState Sdc = PinState::ON; float pressure_1 = 0; float pressure_2 = 0; diff --git a/Core/Inc/VCU/Brakes.hpp b/Core/Inc/VCU/Brakes.hpp index fdafd16..c342636 100644 --- a/Core/Inc/VCU/Brakes.hpp +++ b/Core/Inc/VCU/Brakes.hpp @@ -35,9 +35,9 @@ class Brakes { bool All_reeds = false; PinState tape_emergency = PinState::ON; - DigitalOutput Tape_output; + DigitalOutput tape_enable_output; - PinState Tape_status = PinState::OFF; + PinState tape_enable_status = PinState::OFF; Brakes(); diff --git a/Core/Inc/VCU/Comms.hpp b/Core/Inc/VCU/Comms.hpp index acb654a..6268c9a 100644 --- a/Core/Inc/VCU/Comms.hpp +++ b/Core/Inc/VCU/Comms.hpp @@ -40,8 +40,8 @@ class Comms { Reeds = 251, Regulator = 252, Pressure = 253, - Tapes_input = 254, - Tapes_enable = 255 + Tapes = 254, + Sdc = 255 }; enum class Boards : uint8_t { PCU, HVSCU, BMSL, LCU, BLCU, BCU }; @@ -90,43 +90,43 @@ class Comms { static constexpr std::string LCU_IP = "192.168.1.3"; static constexpr std::string BLCU_IP = "192.168.1.3"; - static const uint16_t LOCAL_PORT = 50500; - static const uint16_t CONTROL_STATION_UDP_PORT = 50400; - static const uint16_t UDP_PORT = 50401; + static const uint32_t LOCAL_PORT = 50500; + static const uint32_t CONTROL_STATION_UDP_PORT = 50400; + static const uint32_t UDP_PORT = 50401; - static const uint16_t PCU_PORT = 50501; - static const uint16_t PCU_UDP_PORT = 50402; + static const uint32_t PCU_PORT = 50501; + static const uint32_t PCU_UDP_PORT = 50402; - static const uint16_t HVSCU_PORT = 50502; - static const uint16_t HVSCU_UDP_PORT = 50403; + static const uint32_t HVSCU_PORT = 50502; + static const uint32_t HVSCU_UDP_PORT = 50403; - static const uint16_t BMSL_PORT = 50503; - static const uint16_t BMSL_UDP_PORT = 50404; + static const uint32_t BMSL_PORT = 50503; + static const uint32_t BMSL_UDP_PORT = 50404; - static const uint16_t LCU_PORT = 50504; - static const uint16_t LCU_UDP_PORT = 50405; + static const uint32_t LCU_PORT = 50504; + static const uint32_t LCU_UDP_PORT = 50405; - static const uint16_t BLCU_PORT = 50505; - static const uint16_t BLCU_UDP_PORT = 50406; + static const uint32_t BLCU_PORT = 50505; + static const uint32_t BLCU_UDP_PORT = 50406; // -----------------Sockets----------------- static inline ServerSocket* control_station_tcp{}; static inline DatagramSocket* control_station_udp{}; - static inline Socket pcu_tcp{}; - static inline DatagramSocket pcu_udp{}; + static inline Socket* pcu_tcp{}; + static inline DatagramSocket* pcu_udp{}; - static inline Socket hvscu_tcp{}; - static inline DatagramSocket hvscu_udp{}; + static inline Socket* hvscu_tcp{}; + static inline DatagramSocket* hvscu_udp{}; - static inline Socket bmsl_tcp{}; - static inline DatagramSocket bmsl_udp{}; + static inline Socket* bmsl_tcp{}; + static inline DatagramSocket* bmsl_udp{}; - static inline Socket lcu_tcp{}; - static inline DatagramSocket lcu_udp{}; + static inline Socket* lcu_tcp{}; + static inline DatagramSocket* lcu_udp{}; - static inline Socket blcu_tcp{}; - static inline DatagramSocket blcu_udp{}; + static inline Socket* blcu_tcp{}; + static inline DatagramSocket* blcu_udp{}; // -----------------Packets----------------- static inline HeapPacket* states{}; @@ -135,7 +135,8 @@ class Comms { static inline HeapPacket* regulator{}; static inline HeapPacket* pressure{}; // static inline HeapPacket* Tapes{}; - static inline HeapPacket* tapes_enabled{}; + static inline HeapPacket* tapes{}; + static inline HeapPacket* sdc{}; /* static std::vector packets{}; // Lo que mando a la gui struct OrderTriggers { @@ -174,6 +175,7 @@ class Comms { static void add_orders(); static void add_state_orders(); + static void read_sensors(); static void update(); static bool connected(); @@ -204,7 +206,9 @@ class Comms { static void on_end_of_run(); // -----------------Unordered maps----------------- - static inline std::unordered_map Socket_to_board{}; + static inline std::unordered_map Socket_to_board{ + {Boards::LCU, lcu_tcp} + }; static inline std::unordered_map Board_to_ip{ {Boards::PCU, PCU_IP}, diff --git a/Core/Inc/state_machine.hpp b/Core/Inc/state_machine.hpp index 31265e1..7e9eaac 100644 --- a/Core/Inc/state_machine.hpp +++ b/Core/Inc/state_machine.hpp @@ -33,7 +33,8 @@ class VCU_SM { GeneralStateMachine.add_transition( GeneralStates::Operational, GeneralStates::Fault, - [&]() { return !Comms::control_station_tcp->is_connected(); }); + [&]() { + return !Comms::control_station_tcp->is_connected(); }); GeneralStateMachine.add_transition( GeneralStates::Operational, GeneralStates::Fault, [&]() { @@ -61,7 +62,7 @@ class VCU_SM { GeneralStates::Operational, GeneralStates::Fault, [&]() { bool emergency_tape = static_cast(Comms::brakes->tape_emergency); - return !emergency_tape; + return !emergency_tape && Comms::brakes->tape_enable_status == PinState::ON; }); GeneralStateMachine.add_enter_action( diff --git a/Core/Src/Runes/generated_metadata.cpp b/Core/Src/Runes/generated_metadata.cpp index 3e63a7f..cefe605 100644 --- a/Core/Src/Runes/generated_metadata.cpp +++ b/Core/Src/Runes/generated_metadata.cpp @@ -5,11 +5,11 @@ extern "C"{ const char DESCRIPTION[255] __attribute__((section(".metadata_pool")))= "****************" // placeholder for beggining - "20250705T173014" // DateTime using ISO-8601 format + "20250705T184131" // DateTime using ISO-8601 format " " // alignment "f8e647d0" // STLIB commit "--------" // ADJ commit - "6afef347" // Board commit + "690dd0e7" // Board commit // the '=' is used for unparsing ; } \ No newline at end of file diff --git a/Core/Src/VCU/Brakes.cpp b/Core/Src/VCU/Brakes.cpp index 5ab294f..276335c 100644 --- a/Core/Src/VCU/Brakes.cpp +++ b/Core/Src/VCU/Brakes.cpp @@ -14,14 +14,14 @@ Brakes::Brakes(){ reed8_input = DigitalSensor(REED8_PIN, &reed8); // Tape_input = DigitalSensor(TAPE_INPUT_PIN, &Tape_state); - Tape_output = DigitalOutput(TAPE_E_PIN); + tape_enable_output = DigitalOutput(TAPE_E_PIN); tape_emergency_input = DigitalSensor(TAPE_PIN, &tape_emergency); } void Brakes::init(){ Actuator_out.turn_on(); - Tape_output.turn_on();//Hardcodeado para desabilitar tapes creo, o al reves xd + tape_enable_output.turn_on();//Hardcodeado para desabilitar tapes creo, o al reves xd } void Brakes::brake(){ @@ -29,12 +29,14 @@ void Brakes::brake(){ breaks_first_time = false; } - Actuator_out.turn_on(); + // TO DO: SET UP REGULATOR PRESSURE TO 6 bars + + Actuator_out.turn_off(); Active_brakes = true; } void Brakes::unbrake(){ - Actuator_out.turn_off(); + Actuator_out.turn_on(); Active_brakes = false; } diff --git a/Core/Src/VCU/Comms.cpp b/Core/Src/VCU/Comms.cpp index 9ae1c13..3cd30c3 100644 --- a/Core/Src/VCU/Comms.cpp +++ b/Core/Src/VCU/Comms.cpp @@ -17,21 +17,25 @@ void Comms::on_Set_regulator() { // pressure"); } if (actuators->selected_regulator == Actuators::Regulator::REGULATOR_1) { - actuators->set_regulator_1(actuators->selected_regulator_pressure); + actuators->regulator_1_pressure = + actuators->selected_regulator_pressure; + actuators->set_regulator_1(actuators->regulator_1_pressure); } else if (actuators->selected_regulator == Actuators::Regulator::REGULATOR_2) { - actuators->set_regulator_2(actuators->selected_regulator_pressure); + actuators->regulator_2_pressure = + actuators->selected_regulator_pressure; + actuators->set_regulator_2(actuators->regulator_2_pressure); } } void Comms::on_Enable_tapes() { - brakes->Tape_status = PinState::ON; - brakes->Tape_output.turn_on(); + brakes->tape_enable_status = PinState::ON; + brakes->tape_enable_output.turn_on(); } void Comms::on_Disable_tapes() { - brakes->Tape_status = PinState::OFF; - brakes->Tape_output.turn_off(); + brakes->tape_enable_status = PinState::OFF; + brakes->tape_enable_output.turn_off(); } void Comms::on_brake() { brakes->brake(); } @@ -50,6 +54,10 @@ void Comms::start() { new DatagramSocket(IPV4(VCU_IP), CONTROL_STATION_UDP_PORT, IPV4(CONTROL_SATION_IP), CONTROL_STATION_UDP_PORT); + + + lcu_tcp = new Socket(IPV4(VCU_IP),LOCAL_PORT, IPV4(LCU_IP), LCU_PORT); + add_packets(); add_orders(); } @@ -64,13 +72,16 @@ void Comms::add_packets() { flow = new HeapPacket(static_cast(Packets_id::Flow), &actuators->flow1, &actuators->flow2); regulator = new HeapPacket(static_cast(Packets_id::Regulator), - &actuators->regulator_1_input, - &actuators->regulator_2_input); + &actuators->regulator_1_pressure, + &actuators->regulator_2_pressure); pressure = new HeapPacket(static_cast(Packets_id::Pressure), &actuators->pressure_1, &actuators->pressure_2, &actuators->pressure_3, &actuators->pressure_4); - tapes_enabled = new HeapPacket( - static_cast(Packets_id::Tapes_enable), &brakes->Tape_status); + tapes = new HeapPacket( + static_cast(Packets_id::Tapes), &brakes->tape_enable_status,&brakes->tape_emergency); + + sdc = new HeapPacket( + static_cast(Packets_id::Sdc), &actuators->Sdc); } void Comms::add_orders() { @@ -101,20 +112,51 @@ void Comms::add_state_orders() { VCU_SM::GeneralStates::Operational); } -void Comms::send_packets() { - control_station_udp->send_packet(*states); +void Comms::send_packets() { + control_station_udp->send_packet(*states); control_station_udp->send_packet(*reeds); control_station_udp->send_packet(*flow); control_station_udp->send_packet(*regulator); control_station_udp->send_packet(*pressure); - control_station_udp->send_packet(*tapes_enabled); + control_station_udp->send_packet(*tapes); } -void Comms::update(){ +void Comms::read_sensors() { actuators->read_regulators(); actuators->read_pressure(); actuators->read_flow(); actuators->read_sdc(); brakes->read_reeds(); brakes->read_tape_emergency(); +} + +void Comms::update() { + read_sensors(); + + for (auto& pending : id_to_pending) { + if (pending.second.control_station) { + Socket_to_board[id_to_orders[pending.first].Board]->send_order( + *id_to_orders[pending.first].order); + id_to_timeout[pending.first] = Time::set_timeout(5000, [&]() { + // InfoWarning::InfoWarningTrigger("Timeout for order to control + // station"); + if (id_to_timeout.contains(pending.first)) { + Time::cancel_timeout( + id_to_timeout[pending.first]); // No se si esto es + // valido + id_to_timeout.erase(pending.first); + } + }); + + } else if (pending.second.board) { + if (id_to_timeout.contains(pending.first)) { + Time::cancel_timeout(id_to_timeout[pending.first]); + id_to_timeout.erase(pending.first); + auto it = id_to_flag.find(pending.first); + if (it != id_to_flag.end()) { + *(it->second.first) = it->second.second; + } + } + } + } } \ No newline at end of file From 992c46c1530998db003349aee7d0c3a46130c8b1 Mon Sep 17 00:00:00 2001 From: jdmarmen Date: Tue, 8 Jul 2025 09:35:19 +0200 Subject: [PATCH 10/25] Added order logic for hvscu, lcu and bcu --- Core/Inc/Code_generation/JSON_ADE | 2 +- Core/Inc/VCU/Actuators.hpp | 1 + Core/Inc/VCU/Comms.hpp | 314 +++++++++++++------------ Core/Inc/state_machine.hpp | 75 +++--- Core/Src/Runes/generated_metadata.cpp | 4 +- Core/Src/VCU/Comms.cpp | 324 ++++++++++++++++++++++---- Core/Src/VCU/VCU.cpp | 8 +- 7 files changed, 488 insertions(+), 240 deletions(-) diff --git a/Core/Inc/Code_generation/JSON_ADE b/Core/Inc/Code_generation/JSON_ADE index 524859b..124edb1 160000 --- a/Core/Inc/Code_generation/JSON_ADE +++ b/Core/Inc/Code_generation/JSON_ADE @@ -1 +1 @@ -Subproject commit 524859b32bab8407de932cb08dc53a417d686806 +Subproject commit 124edb1996a162f5ca8542c233725324355ed82c diff --git a/Core/Inc/VCU/Actuators.hpp b/Core/Inc/VCU/Actuators.hpp index 535ea52..6c5ab70 100644 --- a/Core/Inc/VCU/Actuators.hpp +++ b/Core/Inc/VCU/Actuators.hpp @@ -42,6 +42,7 @@ class Actuators { PinState flow2; PinState Sdc = PinState::ON; + bool contactors_closed = false; float pressure_1 = 0; float pressure_2 = 0; diff --git a/Core/Inc/VCU/Comms.hpp b/Core/Inc/VCU/Comms.hpp index 6268c9a..b59c750 100644 --- a/Core/Inc/VCU/Comms.hpp +++ b/Core/Inc/VCU/Comms.hpp @@ -12,85 +12,127 @@ class Comms { static inline Actuators* actuators; static inline Brakes* brakes; + static inline uint32_t order_demonstration_bitfield{0}; + static constexpr uint8_t levitation_bit{0}; + static constexpr uint8_t propulsion_bit{1}; + static constexpr uint8_t charging_lv_bit{2}; + static constexpr uint8_t charging_hv_bit{3}; + static constexpr uint8_t horizontal_levitation_bit{4}; + static constexpr uint8_t booster_bit{5}; + static constexpr uint8_t brake_bit{6}; + static constexpr uint8_t close_contactors_bit{7}; + static constexpr uint8_t end_of_run_bit{8}; + + static inline uint8_t hvscu_state{}; + static inline uint8_t lcu_v_state{}; + static inline uint8_t lcu_h_state{}; + // -----------------Enums----------------- enum class Orders_id : uint16_t { - // IDs inventadas excepto las de id con 200 - Open_contactors = 1000, - Close_contactors = 1001, - Unbrake = 201, - Brake = 200, - Potencia_refri_id = 202, - Set_regulator_id = 203, - Enable_tapes_id = 204, - Disable_tapes_id = 205, - EndOfRun_id = 1002, - Levitation_active = 1003, - Propulsion_active = 1004, - Charging_LV_battery = 1005, - Enable_booster = 1006, - Levitation_inactive = 1007, - Propulsion_inactive = 1008, - Charging_LV_battery_inactive = 1009, - Disable_booster = 1010 + Potencia_refri = 98, + Set_Regulator = 99, + Enable_tapes = 100, + Disable_tapes = 101, + + Levitation = 102, + Propulsion = 103, + Charging_lv = 104, + Charging_hv = 105, + Horizontal_levitation = 106, + Booster = 1790, + Brake = 108, + Close_contactors = 900, + End_of_run = 110, + Stop_levitation = 111, + Stop_propulsion = 112, + Stop_charging_lv = 113, + Stop_charging_hv = 114, + Stop_horizontal_levitation = 115, + Stop_booster = 1789, + Unbrake = 117, + Open_contactors = 118, + Forward_booster = 1788, + Emergency_stop = 119 }; - enum class Packets_id : uint16_t { + enum class Packets_id : uint8_t { States = 249, Flow = 250, Reeds = 251, Regulator = 252, Pressure = 253, Tapes = 254, - Sdc = 255 + Sdc = 255, + + hvscu_state = 62, + lcu_state = 63 }; - enum class Boards : uint8_t { PCU, HVSCU, BMSL, LCU, BLCU, BCU }; - - // -----------------Structs----------------- - struct Flags_ready { - bool requested_levitation_active; - bool requested_propulsion_active; - bool requested_charging_LV_battery; - bool requested_horizontal_levitation_active; - bool requested_enable_booster; - - Flags_ready() - : requested_levitation_active(false), - requested_propulsion_active(false), - requested_charging_LV_battery(false), - requested_horizontal_levitation_active(false), - requested_enable_booster(false) {} + enum class HVSCU_states : uint8_t { + HVSCU_Closed = 2, }; - struct PendingOrder { - bool control_station; - bool board; + enum class LCU_states : uint8_t { + LCU_Levitation = 3, + LCU_Stop_Levitation = 0 }; - struct BoardOrder { - Boards Board; - HeapStateOrder* order; + enum class Booster_states : uint8_t { + Booster_Disabled = 0, + Booster_Enabled = 1, }; // -----------------Flags----------------- - static inline Flags_ready flags_ready; - static inline bool requested_open_contactors{}; - static inline bool requested_close_contactors{}; - static inline bool requested_end_of_run{}; static inline bool packet_sending{}; static inline bool reading_sensors{}; + // Orders + + static inline bool levitation_flag{}; + static inline bool propulsion_flag{}; + static inline bool charging_lv_flag{}; + static inline bool charging_hv_flag{}; + static inline bool horizontal_levitation_flag{}; + static inline bool booster_flag{}; + + static inline bool brake_flag{}; + static inline bool close_contactors_flag{}; + static inline bool end_of_run_flag{}; + + static inline bool stop_levitation_flag{}; + static inline bool stop_propulsion_flag{}; + static inline bool stop_charging_lv_flag{}; + static inline bool stop_charging_hv_flag{}; + static inline bool stop_horizontal_levitation_flag{}; + static inline bool stop_booster_flag{}; + + static inline bool unbrake_flag{}; + static inline bool open_contactors_flag{}; + + // Orders sent to boards + + static inline bool close_contactors_sent{}; + static inline bool levitation_sent{}; + static inline bool stop_levitation_sent{}; + static inline bool booster_sent{}; + static inline bool stop_booster_sent{}; + + // -----------------Order variables----------------- + static inline float levitation_distance{}; + // -----------------IP's/Ports----------------- - static constexpr std::string CONTROL_SATION_IP = "192.168.0.9"; + static constexpr std::string CONTROL_STATION_IP = "192.168.0.9"; static constexpr std::string VCU_IP = "192.168.1.3"; static constexpr std::string PCU_IP = "192.168.1.3"; - static constexpr std::string HVSCU_IP = "192.168.1.3"; + static constexpr std::string HVSCU_IP = "192.168.1.7"; static constexpr std::string BMSL_IP = "192.168.1.3"; - static constexpr std::string LCU_IP = "192.168.1.3"; + static constexpr std::string LCU_IP = "192.168.1.4"; + static constexpr std::string BCU_IP = "192.168.2.17"; static constexpr std::string BLCU_IP = "192.168.1.3"; - static const uint32_t LOCAL_PORT = 50500; + static const uint32_t REMOTE_PORT = 50500; + static const uint32_t JUANS_REMOTE_PORT = 50501; static const uint32_t CONTROL_STATION_UDP_PORT = 50400; static const uint32_t UDP_PORT = 50401; @@ -106,7 +148,9 @@ class Comms { static const uint32_t LCU_PORT = 50504; static const uint32_t LCU_UDP_PORT = 50405; - static const uint32_t BLCU_PORT = 50505; + static const uint32_t BCU_PORT = 50505; + + static const uint32_t BLCU_PORT = 50506; static const uint32_t BLCU_UDP_PORT = 50406; // -----------------Sockets----------------- @@ -125,6 +169,9 @@ class Comms { static inline Socket* lcu_tcp{}; static inline DatagramSocket* lcu_udp{}; + static inline Socket* bcu_tcp{}; + static inline DatagramSocket* bcu_udp{}; + static inline Socket* blcu_tcp{}; static inline DatagramSocket* blcu_udp{}; @@ -134,17 +181,12 @@ class Comms { static inline HeapPacket* flow{}; static inline HeapPacket* regulator{}; static inline HeapPacket* pressure{}; - // static inline HeapPacket* Tapes{}; static inline HeapPacket* tapes{}; static inline HeapPacket* sdc{}; - /* static std::vector packets{}; // Lo que mando a la gui - struct OrderTriggers { - Boards board; - bool* flag; - HeapOrder* order; - }; - static std::vector order_triggers; */ + // Remote packets + static inline HeapPacket* hvscu_state_packet{}; + static inline HeapPacket* lcu_state_packet{}; // -----------------Orders----------------- static inline HeapOrder* Potencia_refri{}; @@ -152,20 +194,32 @@ class Comms { static inline HeapOrder* Enable_tapes{}; static inline HeapOrder* Disable_tapes{}; - static inline HeapStateOrder* Open_Contactors{}; - static inline HeapStateOrder* Close_Contactors{}; static inline HeapStateOrder* Unbrake{}; static inline HeapStateOrder* Brake{}; - static inline HeapStateOrder* EndOfRun{}; - static inline HeapStateOrder* Levitation_Active{}; - static inline HeapStateOrder* Propulsion_Active{}; - static inline HeapStateOrder* Charging_LV_Battery{}; - static inline HeapStateOrder* Enable_Booster{}; - static inline HeapStateOrder* Levitation_Inactive{}; - static inline HeapStateOrder* Propulsion_Inactive{}; - static inline HeapStateOrder* Charging_LV_Battery_Inactive{}; - static inline HeapStateOrder* Disable_booster{}; + static inline HeapOrder* levitation{}; + static inline HeapOrder* propulsion{}; + static inline HeapOrder* charging_lv{}; + static inline HeapOrder* charging_hv{}; + static inline HeapOrder* horizontal_levitation{}; + static inline HeapOrder* booster{}; + + static inline HeapOrder* brake{}; + static inline HeapOrder* close_contactors{}; + static inline HeapOrder* end_of_run{}; + + static inline HeapOrder* stop_levitation{}; + static inline HeapOrder* stop_propulsion{}; + static inline HeapOrder* stop_charging_lv{}; + static inline HeapOrder* stop_charging_hv{}; + static inline HeapOrder* stop_horizontal_levitation{}; + static inline HeapOrder* stop_booster{}; + + static inline HeapOrder* unbrake{}; + static inline HeapOrder* open_contactors{}; + + static inline HeapOrder* forward_booster_order{}; + static inline HeapOrder* emergency_stop{}; // -----------------Functions----------------- static void init(); @@ -173,11 +227,10 @@ class Comms { static void add_packets(); static void send_packets(); static void add_orders(); - static void add_state_orders(); static void read_sensors(); static void update(); - static bool connected(); + static void check_orders(); static void on_brake(); static void on_unbrake(); @@ -186,90 +239,35 @@ class Comms { static void on_Enable_tapes(); static void on_Disable_tapes(); - // Funciones de las flags, y las que se mandan a otras placas, cambiar y tal - // al gusto: - static void on_levitation_active(); - static void on_propulsion_active(); - static void on_charging_LV_battery(); - static void on_horizontal_levitation_active(); - static void on_enable_booster(); - - static void on_levitation_inactive(); - static void on_propulsion_inactive(); - static void on_charging_LV_battery_inactive(); - static void on_horizontal_levitation_inactive(); - static void on_disable_booster(); - - static void on_open_contactors(); - static void on_close_contactors(); - - static void on_end_of_run(); - - // -----------------Unordered maps----------------- - static inline std::unordered_map Socket_to_board{ - {Boards::LCU, lcu_tcp} - }; - - static inline std::unordered_map Board_to_ip{ - {Boards::PCU, PCU_IP}, - {Boards::HVSCU, HVSCU_IP}, - {Boards::BMSL, BMSL_IP}, - {Boards::LCU, LCU_IP}, - {Boards::BLCU, BLCU_IP}}; - - static inline std::unordered_map id_to_pending{ - {Orders_id::Levitation_active, {false, false}}, - {Orders_id::Propulsion_active, {false, false}}, - {Orders_id::Charging_LV_battery, {false, false}}, - {Orders_id::Enable_booster, {false, false}}, - {Orders_id::Open_contactors, {false, false}}, - // {Orders_id::Brake, {false, false}}, - {Orders_id::Close_contactors, {false, false}}, - // {Orders_id::Unbrake, {false, false}}, - {Orders_id::Levitation_inactive, {false, false}}, - {Orders_id::Propulsion_inactive, {false, false}}, - {Orders_id::Charging_LV_battery_inactive, {false, false}}, - {Orders_id::Disable_booster, {false, false}}}; - - static inline std::unordered_map> - id_to_flag{ - {Orders_id::Levitation_active, - {&flags_ready.requested_levitation_active, true}}, - {Orders_id::Propulsion_active, - {&flags_ready.requested_propulsion_active, true}}, - {Orders_id::Charging_LV_battery, - {&flags_ready.requested_charging_LV_battery, true}}, - {Orders_id::Enable_booster, - {&flags_ready.requested_enable_booster, true}}, - {Orders_id::Open_contactors, {&requested_open_contactors, true}}, - // {Orders_id::Brake, {&requested_brake, true}}, - {Orders_id::Levitation_inactive, - {&flags_ready.requested_levitation_active, false}}, - {Orders_id::Propulsion_inactive, - {&flags_ready.requested_propulsion_active, false}}, - {Orders_id::Charging_LV_battery_inactive, - {&flags_ready.requested_charging_LV_battery, false}}, - {Orders_id::Disable_booster, - {&flags_ready.requested_enable_booster, false}}, - {Orders_id::Close_contactors, {&requested_close_contactors, true}}, - // {Orders_id::Unbrake, {&requested_unbrake, true}} - // {Orders_id::EndOfRun_id, {&requested_end_of_run, true}} - }; - - static inline std::unordered_map id_to_orders{ - {Orders_id::Levitation_active, {Boards::LCU, Levitation_Active}}, - {Orders_id::Propulsion_active, {Boards::PCU, Propulsion_Active}}, - {Orders_id::Charging_LV_battery, {Boards::BMSL, Charging_LV_Battery}}, - {Orders_id::Enable_booster, {Boards::BCU, Enable_Booster}}, - {Orders_id::Open_contactors, {Boards::HVSCU, Open_Contactors}}, - // {Orders_id::Brake, {Boards::, Brake}}, - {Orders_id::Close_contactors, {Boards::HVSCU, Close_Contactors}}, - // {Orders_id::Unbrake, {Boards::PCU, Unbrake}}, - {Orders_id::Levitation_inactive, {Boards::LCU, Levitation_Inactive}}, - {Orders_id::Propulsion_inactive, {Boards::PCU, Propulsion_Inactive}}, - {Orders_id::Charging_LV_battery_inactive, - {Boards::BMSL, Charging_LV_Battery_Inactive}}, - {Orders_id::Disable_booster, {Boards::BCU, Disable_booster}}}; - - static inline std::unordered_map id_to_timeout{}; + // Callbacks + + static void levitation_callback(); + static void propulsion_callback(); + static void charging_lv_callback(); + static void charging_hv_callback(); + static void horizontal_levitation_callback(); + static void booster_callback(); + + static void brake_callback(); + static void close_contactors_callback(); + static void end_of_run_callback(); + + static void stop_levitation_callback(); + static void stop_propulsion_callback(); + static void stop_charging_lv_callback(); + static void stop_charging_hv_callback(); + static void stop_horizontal_levitation_callback(); + static void stop_booster_callback(); + + static void unbrake_callback(); + static void open_contactors_callback(); + + static void emergency_stop_callback(); + + // Check orders + static void check_close_contactors_order(); + static void check_levitation_order(); + static void check_stop_levitation_order(); + static void check_booster_order(); + static void check_stop_booster_order(); }; \ No newline at end of file diff --git a/Core/Inc/state_machine.hpp b/Core/Inc/state_machine.hpp index 7e9eaac..fe82ef7 100644 --- a/Core/Inc/state_machine.hpp +++ b/Core/Inc/state_machine.hpp @@ -26,15 +26,24 @@ class VCU_SM { OperationalStateMachine.add_state(OperationalStates::EndOfRun); OperationalStateMachine.add_state(OperationalStates::Energyzed); OperationalStateMachine.add_state(OperationalStates::Ready); + OperationalStateMachine.add_state(OperationalStates::Demonstration); + + GeneralStateMachine.add_state_machine(OperationalStateMachine, + GeneralStates::Operational); GeneralStateMachine.add_transition( - GeneralStates::Connecting, GeneralStates::Operational, - [&]() { return Comms::control_station_tcp->is_connected(); }); + GeneralStates::Connecting, GeneralStates::Operational, [&]() { + return Comms::control_station_tcp->is_connected() && + Comms::lcu_tcp->is_connected() && + Comms::bcu_tcp->is_connected(); + }); GeneralStateMachine.add_transition( - GeneralStates::Operational, GeneralStates::Fault, - [&]() { - return !Comms::control_station_tcp->is_connected(); }); + GeneralStates::Operational, GeneralStates::Fault, [&]() { + return !Comms::control_station_tcp->is_connected() || + !Comms::lcu_tcp->is_connected() || + !Comms::bcu_tcp->is_connected(); + }); GeneralStateMachine.add_transition( GeneralStates::Operational, GeneralStates::Fault, [&]() { @@ -62,7 +71,8 @@ class VCU_SM { GeneralStates::Operational, GeneralStates::Fault, [&]() { bool emergency_tape = static_cast(Comms::brakes->tape_emergency); - return !emergency_tape && Comms::brakes->tape_enable_status == PinState::ON; + return !emergency_tape && + Comms::brakes->tape_enable_status == PinState::ON; }); GeneralStateMachine.add_enter_action( @@ -80,41 +90,41 @@ class VCU_SM { }, GeneralStates::Fault); - // el sdc tambien manda a fault? + //----------- - /* OperationalStateMachine.add_transition( + OperationalStateMachine.add_transition( OperationalStates::Idle, OperationalStates::Energyzed, - [&]() { return ethernet->requested_close_contactors; }); + [&]() { return Comms::actuators->contactors_closed; }); OperationalStateMachine.add_transition( - OperationalStates::Idle, OperationalStates::EndOfRun, - [&]() { return ethernet->requested_end_of_run; }); + OperationalStates::Energyzed, OperationalStates::Idle, + [&]() { return !Comms::actuators->contactors_closed; }); OperationalStateMachine.add_transition( - OperationalStates::Energyzed, OperationalStates::Idle, - [&]() { return ethernet->requested_open_contactors; }); */ + OperationalStates::Demonstration, OperationalStates::Idle, + [&]() { return !Comms::actuators->contactors_closed; }); OperationalStateMachine.add_transition( - OperationalStates::Ready, OperationalStates::Energyzed, - [&]() { return Comms::brakes->Active_brakes; }); + OperationalStates::Ready, OperationalStates::Idle, + [&]() { return !Comms::actuators->contactors_closed; }); OperationalStateMachine.add_transition( OperationalStates::Energyzed, OperationalStates::Ready, - [&]() { return (!Comms::brakes->Active_brakes); }); + [&]() { return !Comms::brakes->Active_brakes; }); - /* OperationalStateMachine.add_enter_action( - [&]() { ethernet->requested_close_contactors = false; }, - OperationalStates::Energyzed); + OperationalStateMachine.add_transition( + OperationalStates::Ready, OperationalStates::Energyzed, + [&]() { return Comms::brakes->Active_brakes; }); - OperationalStateMachine.add_enter_action( - [&]() { ethernet->requested_end_of_run = false; }, - OperationalStates::EndOfRun); + //----------- - OperationalStateMachine.add_enter_action( - [&]() { ethernet->requested_open_contactors = false; }, - OperationalStates::Idle); */ + OperationalStateMachine.add_transition( + OperationalStates::Ready, OperationalStates::Demonstration, + [&]() { return Comms::order_demonstration_bitfield > 0; }); - // ethernet->initialize_state_orders(); + OperationalStateMachine.add_transition( + OperationalStates::Demonstration, OperationalStates::Ready, + [&]() { return Comms::order_demonstration_bitfield == 0; }); //-----------------CYCLYC ACTIONS----------------------- @@ -137,5 +147,18 @@ class VCU_SM { GeneralStateMachine.add_low_precision_cyclic_action( [&]() { Comms::reading_sensors = true; }, 100ms, GeneralStates::Fault); + + GeneralStateMachine.add_low_precision_cyclic_action( + [&]() { + /* while (!Comms::hvscu_tcp->is_connected()) { + Comms::hvscu_tcp->reconnect(); + } */ + while (!Comms::lcu_tcp->is_connected() || + !Comms::bcu_tcp->is_connected()) { + Comms::lcu_tcp->reconnect(); + Comms::bcu_tcp->reconnect(); + } + }, + 100ms, GeneralStates::Connecting); } }; \ No newline at end of file diff --git a/Core/Src/Runes/generated_metadata.cpp b/Core/Src/Runes/generated_metadata.cpp index cefe605..05fee6a 100644 --- a/Core/Src/Runes/generated_metadata.cpp +++ b/Core/Src/Runes/generated_metadata.cpp @@ -5,11 +5,11 @@ extern "C"{ const char DESCRIPTION[255] __attribute__((section(".metadata_pool")))= "****************" // placeholder for beggining - "20250705T184131" // DateTime using ISO-8601 format + "20250708T092902" // DateTime using ISO-8601 format " " // alignment "f8e647d0" // STLIB commit "--------" // ADJ commit - "690dd0e7" // Board commit + "86310c8b" // Board commit // the '=' is used for unparsing ; } \ No newline at end of file diff --git a/Core/Src/VCU/Comms.cpp b/Core/Src/VCU/Comms.cpp index 3cd30c3..4e2b5bb 100644 --- a/Core/Src/VCU/Comms.cpp +++ b/Core/Src/VCU/Comms.cpp @@ -2,6 +2,8 @@ #include "VCU/VCU.hpp" +//------------------------CALLBACKS------------------------ + void Comms::on_potencia_refri() { if (actuators->selected_pump == Actuators::Pump::PUMP_UNIDADES) { actuators->set_pump_1(actuators->selected_pump_duty); @@ -38,9 +40,28 @@ void Comms::on_Disable_tapes() { brakes->tape_enable_output.turn_off(); } -void Comms::on_brake() { brakes->brake(); } +void Comms::brake_callback() { brakes->brake(); } + +void Comms::unbrake_callback() { brakes->unbrake(); } + +void Comms::close_contactors_callback() { close_contactors_flag = true; } + +void Comms::open_contactors_callback() { open_contactors_flag = true; } + +void Comms::levitation_callback() { levitation_flag = true; } -void Comms::on_unbrake() { brakes->unbrake(); } +void Comms::stop_levitation_callback() { stop_levitation_flag = true; } + +void Comms::booster_callback() { booster_flag = true; } + +void Comms::stop_booster_callback() { stop_booster_flag = true; } + +void Comms::emergency_stop_callback() { + VCU::state_machine->GeneralStateMachine.force_change_state( + VCU_SM::GeneralStates::Fault); +} + +//------------------------------------------------------ void Comms::init() { leds = new Leds(); @@ -49,14 +70,24 @@ void Comms::init() { } void Comms::start() { - control_station_tcp = new ServerSocket(IPV4(VCU_IP), LOCAL_PORT); + control_station_tcp = new ServerSocket(IPV4(VCU_IP), REMOTE_PORT); + control_station_udp = new DatagramSocket(IPV4(VCU_IP), CONTROL_STATION_UDP_PORT, - IPV4(CONTROL_SATION_IP), CONTROL_STATION_UDP_PORT); + IPV4(CONTROL_STATION_IP), CONTROL_STATION_UDP_PORT); + + lcu_tcp = new Socket(IPV4(VCU_IP), LCU_PORT, IPV4(LCU_IP), REMOTE_PORT); + + /* hvscu_tcp = + new Socket(IPV4(VCU_IP), HVSCU_PORT, IPV4(HVSCU_IP), REMOTE_PORT); + hvscu_udp = new DatagramSocket(IPV4(VCU_IP), HVSCU_UDP_PORT, IPV4(HVSCU_IP), + HVSCU_UDP_PORT); */ + lcu_udp = new DatagramSocket(IPV4(VCU_IP), LCU_UDP_PORT, IPV4(LCU_IP), + LCU_UDP_PORT); - lcu_tcp = new Socket(IPV4(VCU_IP),LOCAL_PORT, IPV4(LCU_IP), LCU_PORT); + bcu_tcp = new Socket(IPV4(VCU_IP), BCU_PORT, IPV4(BCU_IP), JUANS_REMOTE_PORT); add_packets(); add_orders(); @@ -77,39 +108,72 @@ void Comms::add_packets() { pressure = new HeapPacket(static_cast(Packets_id::Pressure), &actuators->pressure_1, &actuators->pressure_2, &actuators->pressure_3, &actuators->pressure_4); - tapes = new HeapPacket( - static_cast(Packets_id::Tapes), &brakes->tape_enable_status,&brakes->tape_emergency); + tapes = + new HeapPacket(static_cast(Packets_id::Tapes), + &brakes->tape_enable_status, &brakes->tape_emergency); - sdc = new HeapPacket( - static_cast(Packets_id::Sdc), &actuators->Sdc); + sdc = + new HeapPacket(static_cast(Packets_id::Sdc), &actuators->Sdc); + + // Remote packets + + hvscu_state_packet = new HeapPacket( + static_cast(Packets_id::hvscu_state), &hvscu_state); + + lcu_state_packet = + new HeapPacket(static_cast(Packets_id::lcu_state), + &lcu_v_state, &lcu_h_state); } void Comms::add_orders() { Potencia_refri = new HeapOrder( - static_cast(Orders_id::Potencia_refri_id), &on_potencia_refri, + static_cast(Orders_id::Potencia_refri), &on_potencia_refri, &actuators->selected_pump_duty, &actuators->selected_pump); + Set_Regulator = new HeapOrder( - static_cast(Orders_id::Set_regulator_id), &on_Set_regulator, + static_cast(Orders_id::Set_Regulator), &on_Set_regulator, &actuators->selected_regulator_pressure, &actuators->selected_regulator); - Enable_tapes = new HeapOrder( - static_cast(Orders_id::Enable_tapes_id), &on_Enable_tapes); + + Enable_tapes = new HeapOrder(static_cast(Orders_id::Enable_tapes), + &on_Enable_tapes); + Disable_tapes = new HeapOrder( - static_cast(Orders_id::Disable_tapes_id), &on_Disable_tapes); + static_cast(Orders_id::Disable_tapes), &on_Disable_tapes); - add_state_orders(); -} + brake = + new HeapOrder(static_cast(Orders_id::Brake), &brake_callback); + + unbrake = new HeapOrder(static_cast(Orders_id::Unbrake), + &unbrake_callback); + + close_contactors = + new HeapOrder(static_cast(Orders_id::Close_contactors), + &close_contactors_callback); + + open_contactors = + new HeapOrder(static_cast(Orders_id::Open_contactors), + &open_contactors_callback); + + levitation = new HeapOrder(static_cast(Orders_id::Levitation), + &levitation_callback, &levitation_distance); + + stop_levitation = + new HeapOrder(static_cast(Orders_id::Stop_levitation), + &stop_levitation_callback); + + booster = new HeapOrder(static_cast(Orders_id::Booster), + &booster_callback); -void Comms::add_state_orders() { - StateOrder::set_socket(*control_station_tcp); - Brake = - new HeapStateOrder(static_cast(Orders_id::Brake), &on_brake, - VCU::state_machine->GeneralStateMachine, - VCU_SM::GeneralStates::Operational); - Unbrake = - new HeapStateOrder(static_cast(Orders_id::Unbrake), - &on_unbrake, VCU::state_machine->GeneralStateMachine, - VCU_SM::GeneralStates::Operational); + stop_booster = new HeapOrder(static_cast(Orders_id::Stop_booster), + &stop_booster_callback); + + forward_booster_order = + new HeapOrder(static_cast(Orders_id::Forward_booster)); + + emergency_stop = + new HeapOrder(static_cast(Orders_id::Emergency_stop), + &emergency_stop_callback); } void Comms::send_packets() { @@ -130,33 +194,195 @@ void Comms::read_sensors() { brakes->read_tape_emergency(); } -void Comms::update() { - read_sensors(); +void Comms::check_close_contactors_order() { + /* if (close_contactors_flag) { + if (*VCU::operational_state == VCU_SM::OperationalStates::Idle) { + if (!close_contactors_sent) { + hvscu_tcp->send_order(*close_contactors); + close_contactors_sent = true; + } else { + if (hvscu_state == + static_cast(Boards_states::HVSCU_Closed)) { + actuators->contactors_closed = true; + close_contactors_flag = false; + close_contactors_sent = false; + } else { + return; + } + } + } else { + WARNING("Cannot close contactors in this state"); + close_contactors_flag = false; + } + } */ + if (close_contactors_flag) { + if (*VCU::operational_state == VCU_SM::OperationalStates::Idle) { + actuators->contactors_closed = true; + close_contactors_flag = false; + } + } else { + WARNING("Cannot close contactors in this state"); + close_contactors_flag = false; + } +} + +void Comms::check_levitation_order() { + if (levitation_flag) { + if (*VCU::operational_state == VCU_SM::OperationalStates::Ready || + *VCU::operational_state == + VCU_SM::OperationalStates::Demonstration) { + static uint8_t timeout_id = -1; - for (auto& pending : id_to_pending) { - if (pending.second.control_station) { - Socket_to_board[id_to_orders[pending.first].Board]->send_order( - *id_to_orders[pending.first].order); - id_to_timeout[pending.first] = Time::set_timeout(5000, [&]() { - // InfoWarning::InfoWarningTrigger("Timeout for order to control - // station"); - if (id_to_timeout.contains(pending.first)) { - Time::cancel_timeout( - id_to_timeout[pending.first]); // No se si esto es - // valido - id_to_timeout.erase(pending.first); + if (!levitation_sent) { + lcu_tcp->send_order(*levitation); + levitation_sent = true; + } else { + if (lcu_v_state == + static_cast(LCU_states::LCU_Levitation)) { + Time::cancel_timeout(timeout_id); + timeout_id = -1; + order_demonstration_bitfield |= (1UL << levitation_bit); + levitation_flag = false; + levitation_sent = false; + } else { + if (timeout_id == -1) { + timeout_id = Time::set_timeout(100, [&]() { + VCU::state_machine->GeneralStateMachine + .force_change_state( + VCU_SM::GeneralStates::Fault); + }); + } } - }); - - } else if (pending.second.board) { - if (id_to_timeout.contains(pending.first)) { - Time::cancel_timeout(id_to_timeout[pending.first]); - id_to_timeout.erase(pending.first); - auto it = id_to_flag.find(pending.first); - if (it != id_to_flag.end()) { - *(it->second.first) = it->second.second; + } + } else { + WARNING("Cannot levitate in this state"); + levitation_flag = false; + } + } +} + +void Comms::check_stop_levitation_order() { + if (stop_levitation_flag) { + if (*VCU::operational_state == + VCU_SM::OperationalStates::Demonstration) { + static uint8_t timeout_id = -1; + + if (!stop_levitation_sent) { + lcu_tcp->send_order(*stop_levitation); + stop_levitation_sent = true; + } else { + if (lcu_v_state == + static_cast(LCU_states::LCU_Stop_Levitation)) { + Time::cancel_timeout(timeout_id); + timeout_id = -1; + order_demonstration_bitfield &= ~(1UL << levitation_bit); + stop_levitation_flag = false; + stop_levitation_sent = false; + } else { + if (timeout_id == -1) { + timeout_id = Time::set_timeout(100, [&]() { + VCU::state_machine->GeneralStateMachine + .force_change_state( + VCU_SM::GeneralStates::Fault); + }); + } } } + } else { + WARNING("Cannot stop levitating in this state"); + stop_levitation_flag = false; } } +} + +void Comms::check_booster_order() { + if (booster_flag) { + if (*VCU::operational_state == VCU_SM::OperationalStates::Ready || + *VCU::operational_state == + VCU_SM::OperationalStates::Demonstration) { + static uint8_t timeout_id = -1; + + if (!booster_sent) { + lcu_tcp->send_order(*booster); + booster_sent = true; + } else { + if (lcu_h_state == + static_cast(Booster_states::Booster_Enabled)) { + Time::cancel_timeout(timeout_id); + timeout_id = -1; + order_demonstration_bitfield |= (1UL << booster_bit); + bcu_tcp->send_order(*forward_booster_order); + booster_flag = false; + booster_sent = false; + } else { + if (timeout_id == -1) { + timeout_id = Time::set_timeout(100, [&]() { + VCU::state_machine->GeneralStateMachine + .force_change_state( + VCU_SM::GeneralStates::Fault); + }); + } + } + } + } else { + WARNING("Cannot boost in this state"); + booster_flag = false; + } + } +} + +void Comms::check_stop_booster_order() { + if (stop_booster_flag) { + if (*VCU::operational_state == + VCU_SM::OperationalStates::Demonstration) { + static uint8_t timeout_id = -1; + + if (!stop_booster_sent) { + lcu_tcp->send_order(*stop_booster); + stop_booster_sent = true; + } else { + if (lcu_h_state == + static_cast(Booster_states::Booster_Disabled)) { + Time::cancel_timeout(timeout_id); + timeout_id = -1; + order_demonstration_bitfield &= ~(1UL << booster_bit); + stop_booster_flag = false; + stop_booster_sent = false; + } else { + if (timeout_id == -1) { + timeout_id = Time::set_timeout(100, [&]() { + VCU::state_machine->GeneralStateMachine + .force_change_state( + VCU_SM::GeneralStates::Fault); + }); + } + } + } + } else { + WARNING("Cannot stop boosting in this state"); + stop_booster_flag = false; + } + } +} + +void Comms::check_orders() { + check_close_contactors_order(); + + if (open_contactors_flag) { + // hvscu_tcp->send_order(*open_contactors); + actuators->contactors_closed = false; + stop_levitation_flag = true; + + open_contactors_flag = false; + } + + check_levitation_order(); + check_stop_levitation_order(); + check_booster_order(); + check_stop_booster_order(); +} + +void Comms::update() { + check_orders(); + read_sensors(); } \ No newline at end of file diff --git a/Core/Src/VCU/VCU.cpp b/Core/Src/VCU/VCU.cpp index 1bfac7d..23163c3 100644 --- a/Core/Src/VCU/VCU.cpp +++ b/Core/Src/VCU/VCU.cpp @@ -14,9 +14,6 @@ void VCU::init(){ } void VCU::start(){ - //state_machine->actuators.init(); - //state_machine->brakes.init(); - Comms::actuators->init(); Comms::brakes->init(); @@ -32,13 +29,16 @@ void VCU::send_packets(){ void VCU::read_sensors(){ if(Comms::reading_sensors){ - Comms::update(); Comms::reading_sensors = false; } } void VCU::update(){ + state_machine->GeneralStateMachine.check_transitions(); + state_machine->OperationalStateMachine.check_transitions(); + + Comms::update(); read_sensors(); send_packets(); ProtectionManager::check_protections(); From d2416c36fe6b1f2e9fc4d64972b859100f69b0c3 Mon Sep 17 00:00:00 2001 From: jdmarmen Date: Tue, 8 Jul 2025 16:25:13 +0200 Subject: [PATCH 11/25] Order ID's refactor + small fix in emergency order --- Core/Inc/Code_generation/JSON_ADE | 2 +- Core/Inc/VCU/Comms.hpp | 79 +++++++++++++++++++-------- Core/Src/Runes/generated_metadata.cpp | 4 +- Core/Src/VCU/Comms.cpp | 45 ++++++++++++--- 4 files changed, 95 insertions(+), 35 deletions(-) diff --git a/Core/Inc/Code_generation/JSON_ADE b/Core/Inc/Code_generation/JSON_ADE index 124edb1..471ec21 160000 --- a/Core/Inc/Code_generation/JSON_ADE +++ b/Core/Inc/Code_generation/JSON_ADE @@ -1 +1 @@ -Subproject commit 124edb1996a162f5ca8542c233725324355ed82c +Subproject commit 471ec219ae9e132c43f4f06b9cf161432b35f433 diff --git a/Core/Inc/VCU/Comms.hpp b/Core/Inc/VCU/Comms.hpp index b59c750..0667e61 100644 --- a/Core/Inc/VCU/Comms.hpp +++ b/Core/Inc/VCU/Comms.hpp @@ -29,18 +29,52 @@ class Comms { // -----------------Enums----------------- enum class Orders_id : uint16_t { - Potencia_refri = 98, - Set_Regulator = 99, - Enable_tapes = 100, - Disable_tapes = 101, + Potencia_refri = 33, + Set_Regulator = 34, + Enable_tapes = 35, + Disable_tapes = 36, + + Levitation = 37, + Propulsion = 38, + Charging_lv = 39, + Charging_hv = 40, + Horizontal_levitation = 41, + Booster = 42, + Brake = 43, + Close_contactors = 44, + End_of_run = 45, + Stop_levitation = 46, + Stop_propulsion = 47, + Stop_charging_lv = 48, + Stop_charging_hv = 49, + Stop_horizontal_levitation = 50, + Stop_booster = 51, + Unbrake = 52, + Open_contactors = 53, + Forward_booster = 1788, + Emergency_stop = 55 + }; + + enum class Packets_id : uint8_t { + States = 249, + Flow = 250, + Reeds = 251, + Regulator = 252, + Pressure = 253, + Tapes = 254, + Sdc = 255, + + hvscu_state = 62, + lcu_state = 63 + }; + enum class External_ids : uint16_t{ Levitation = 102, Propulsion = 103, Charging_lv = 104, Charging_hv = 105, Horizontal_levitation = 106, Booster = 1790, - Brake = 108, Close_contactors = 900, End_of_run = 110, Stop_levitation = 111, @@ -49,23 +83,8 @@ class Comms { Stop_charging_hv = 114, Stop_horizontal_levitation = 115, Stop_booster = 1789, - Unbrake = 117, Open_contactors = 118, - Forward_booster = 1788, - Emergency_stop = 119 - }; - - enum class Packets_id : uint8_t { - States = 249, - Flow = 250, - Reeds = 251, - Regulator = 252, - Pressure = 253, - Tapes = 254, - Sdc = 255, - - hvscu_state = 62, - lcu_state = 63 + Forward_booster = 1788 }; enum class HVSCU_states : uint8_t { @@ -194,29 +213,41 @@ class Comms { static inline HeapOrder* Enable_tapes{}; static inline HeapOrder* Disable_tapes{}; - static inline HeapStateOrder* Unbrake{}; - static inline HeapStateOrder* Brake{}; - static inline HeapOrder* levitation{}; + static inline HeapOrder* remote_levitation{}; static inline HeapOrder* propulsion{}; + static inline HeapOrder* remote_propulsion{}; static inline HeapOrder* charging_lv{}; + static inline HeapOrder* remote_charging_lv{}; static inline HeapOrder* charging_hv{}; + static inline HeapOrder* remote_charging_hv{}; static inline HeapOrder* horizontal_levitation{}; + static inline HeapOrder* remote_horizontal_levitation{}; static inline HeapOrder* booster{}; + static inline HeapOrder* remote_booster{}; static inline HeapOrder* brake{}; static inline HeapOrder* close_contactors{}; + static inline HeapOrder* remote_close_contactors{}; static inline HeapOrder* end_of_run{}; + static inline HeapOrder* remote_end_of_run{}; static inline HeapOrder* stop_levitation{}; + static inline HeapOrder* remote_stop_levitation{}; static inline HeapOrder* stop_propulsion{}; + static inline HeapOrder* remote_stop_propulsion{}; static inline HeapOrder* stop_charging_lv{}; + static inline HeapOrder* remote_stop_charging_lv{}; static inline HeapOrder* stop_charging_hv{}; + static inline HeapOrder* remote_stop_charging_hv{}; static inline HeapOrder* stop_horizontal_levitation{}; + static inline HeapOrder* remote_stop_horizontal_levitation{}; static inline HeapOrder* stop_booster{}; + static inline HeapOrder* remote_stop_booster{}; static inline HeapOrder* unbrake{}; static inline HeapOrder* open_contactors{}; + static inline HeapOrder* remote_open_contactors{}; static inline HeapOrder* forward_booster_order{}; static inline HeapOrder* emergency_stop{}; diff --git a/Core/Src/Runes/generated_metadata.cpp b/Core/Src/Runes/generated_metadata.cpp index 05fee6a..ef9e262 100644 --- a/Core/Src/Runes/generated_metadata.cpp +++ b/Core/Src/Runes/generated_metadata.cpp @@ -5,11 +5,11 @@ extern "C"{ const char DESCRIPTION[255] __attribute__((section(".metadata_pool")))= "****************" // placeholder for beggining - "20250708T092902" // DateTime using ISO-8601 format + "20250708T161845" // DateTime using ISO-8601 format " " // alignment "f8e647d0" // STLIB commit "--------" // ADJ commit - "86310c8b" // Board commit + "992c46c1" // Board commit // the '=' is used for unparsing ; } \ No newline at end of file diff --git a/Core/Src/VCU/Comms.cpp b/Core/Src/VCU/Comms.cpp index 4e2b5bb..026fcff 100644 --- a/Core/Src/VCU/Comms.cpp +++ b/Core/Src/VCU/Comms.cpp @@ -57,8 +57,7 @@ void Comms::booster_callback() { booster_flag = true; } void Comms::stop_booster_callback() { stop_booster_flag = true; } void Comms::emergency_stop_callback() { - VCU::state_machine->GeneralStateMachine.force_change_state( - VCU_SM::GeneralStates::Fault); + ProtectionManager::fault_and_propagate(); } //------------------------------------------------------ @@ -87,7 +86,8 @@ void Comms::start() { lcu_udp = new DatagramSocket(IPV4(VCU_IP), LCU_UDP_PORT, IPV4(LCU_IP), LCU_UDP_PORT); - bcu_tcp = new Socket(IPV4(VCU_IP), BCU_PORT, IPV4(BCU_IP), JUANS_REMOTE_PORT); + bcu_tcp = + new Socket(IPV4(VCU_IP), BCU_PORT, IPV4(BCU_IP), JUANS_REMOTE_PORT); add_packets(); add_orders(); @@ -147,6 +147,33 @@ void Comms::add_orders() { unbrake = new HeapOrder(static_cast(Orders_id::Unbrake), &unbrake_callback); + //----------------------------------------------------------------------------- + + remote_close_contactors = + new HeapOrder(static_cast(External_ids::Close_contactors), + &close_contactors_callback); + + remote_open_contactors = + new HeapOrder(static_cast(External_ids::Open_contactors), + &open_contactors_callback); + + remote_levitation = + new HeapOrder(static_cast(External_ids::Levitation), + &levitation_callback, &levitation_distance); + + remote_stop_levitation = + new HeapOrder(static_cast(External_ids::Stop_levitation), + &stop_levitation_callback); + + remote_booster = new HeapOrder(static_cast(External_ids::Booster), + &booster_callback); + + remote_stop_booster = + new HeapOrder(static_cast(External_ids::Stop_booster), + &stop_booster_callback); + + //----------------------------------------------------------------------------- + close_contactors = new HeapOrder(static_cast(Orders_id::Close_contactors), &close_contactors_callback); @@ -234,7 +261,7 @@ void Comms::check_levitation_order() { static uint8_t timeout_id = -1; if (!levitation_sent) { - lcu_tcp->send_order(*levitation); + lcu_tcp->send_order(*remote_levitation); levitation_sent = true; } else { if (lcu_v_state == @@ -268,7 +295,7 @@ void Comms::check_stop_levitation_order() { static uint8_t timeout_id = -1; if (!stop_levitation_sent) { - lcu_tcp->send_order(*stop_levitation); + lcu_tcp->send_order(*remote_stop_levitation); stop_levitation_sent = true; } else { if (lcu_v_state == @@ -289,7 +316,9 @@ void Comms::check_stop_levitation_order() { } } } else { - WARNING("Cannot stop levitating in this state"); + //WARNING("Cannot stop levitating in this state"); + InfoWarning::InfoWarningTrigger( + "Cannot stop levitating in this state"); stop_levitation_flag = false; } } @@ -303,7 +332,7 @@ void Comms::check_booster_order() { static uint8_t timeout_id = -1; if (!booster_sent) { - lcu_tcp->send_order(*booster); + lcu_tcp->send_order(*remote_booster); booster_sent = true; } else { if (lcu_h_state == @@ -338,7 +367,7 @@ void Comms::check_stop_booster_order() { static uint8_t timeout_id = -1; if (!stop_booster_sent) { - lcu_tcp->send_order(*stop_booster); + lcu_tcp->send_order(*remote_stop_booster); stop_booster_sent = true; } else { if (lcu_h_state == From 143d17cf09b932c766d457112991d03436c8c58c Mon Sep 17 00:00:00 2001 From: jdmarmen Date: Thu, 10 Jul 2025 09:11:06 +0200 Subject: [PATCH 12/25] Working version also with pcu --- Core/Inc/Code_generation/JSON_ADE | 2 +- Core/Inc/VCU/{ => Comms}/Comms.hpp | 135 ++++- Core/Inc/VCU/Comms/Utility.hpp | 3 + Core/Inc/VCU/VCU.hpp | 2 +- Core/Inc/state_machine.hpp | 33 +- Core/Src/Runes/generated_metadata.cpp | 4 +- Core/Src/VCU/Comms.cpp | 417 -------------- Core/Src/VCU/Comms/Comms.cpp | 767 ++++++++++++++++++++++++++ Core/Src/VCU/Comms/Utility.cpp | 1 + Core/Src/main.cpp | 2 +- Tests/VCU.py | 4 +- 11 files changed, 917 insertions(+), 453 deletions(-) rename Core/Inc/VCU/{ => Comms}/Comms.hpp (71%) create mode 100644 Core/Inc/VCU/Comms/Utility.hpp delete mode 100644 Core/Src/VCU/Comms.cpp create mode 100644 Core/Src/VCU/Comms/Comms.cpp create mode 100644 Core/Src/VCU/Comms/Utility.cpp diff --git a/Core/Inc/Code_generation/JSON_ADE b/Core/Inc/Code_generation/JSON_ADE index 471ec21..4b76606 160000 --- a/Core/Inc/Code_generation/JSON_ADE +++ b/Core/Inc/Code_generation/JSON_ADE @@ -1 +1 @@ -Subproject commit 471ec219ae9e132c43f4f06b9cf161432b35f433 +Subproject commit 4b766061750fccdba1ccd918fbcf4bcca4c41285 diff --git a/Core/Inc/VCU/Comms.hpp b/Core/Inc/VCU/Comms/Comms.hpp similarity index 71% rename from Core/Inc/VCU/Comms.hpp rename to Core/Inc/VCU/Comms/Comms.hpp index 0667e61..1a6ded5 100644 --- a/Core/Inc/VCU/Comms.hpp +++ b/Core/Inc/VCU/Comms/Comms.hpp @@ -2,12 +2,11 @@ #include "ST-LIB.hpp" #include "VCU/Actuators.hpp" #include "VCU/Brakes.hpp" -#include "VCU/Comms.hpp" +#include "VCU/Comms/Comms.hpp" #include "VCU/Leds.hpp" class Comms { public: - static inline Leds* leds; static inline Actuators* actuators; static inline Brakes* brakes; @@ -24,6 +23,7 @@ class Comms { static constexpr uint8_t end_of_run_bit{8}; static inline uint8_t hvscu_state{}; + static inline uint8_t pcu_state{}; static inline uint8_t lcu_v_state{}; static inline uint8_t lcu_h_state{}; @@ -52,10 +52,17 @@ class Comms { Unbrake = 52, Open_contactors = 53, Forward_booster = 1788, - Emergency_stop = 55 + Emergency_stop = 55, + + Runs = 56, + SVPWM = 57, + Stop_motor = 58, + Current_control = 59, + Speed_control = 60, + Motor_brake = 61 }; - enum class Packets_id : uint8_t { + enum class Packets_id : uint16_t { States = 249, Flow = 250, Reeds = 251, @@ -64,11 +71,12 @@ class Comms { Tapes = 254, Sdc = 255, - hvscu_state = 62, - lcu_state = 63 + hvscu_state = 941, + lcu_state = 63, + pcu_state = 64 }; - enum class External_ids : uint16_t{ + enum class External_ids : uint16_t { Levitation = 102, Propulsion = 103, Charging_lv = 104, @@ -83,12 +91,22 @@ class Comms { Stop_charging_hv = 114, Stop_horizontal_levitation = 115, Stop_booster = 1789, - Open_contactors = 118, - Forward_booster = 1788 + Open_contactors = 901, + Forward_booster = 1788, + + Runs = 1000, + SVPWM = 1001, + Stop_motor = 1002, + Current_control = 1003, + Speed_control = 1004, + Motor_brake = 1005 }; - enum class HVSCU_states : uint8_t { - HVSCU_Closed = 2, + enum class HVSCU_states : uint8_t { HVSCU_Opened = 0, HVSCU_Closed = 2 }; + + enum class PCU_states : uint8_t { + PCU_Stop_Propulsion = 0, + PCU_Propulsion = 2 }; enum class LCU_states : uint8_t { @@ -128,22 +146,79 @@ class Comms { static inline bool unbrake_flag{}; static inline bool open_contactors_flag{}; + static inline bool runs_flag{}; + static inline bool svpwm_flag{}; + static inline bool stop_motor_flag{}; + static inline bool current_control_flag{}; + static inline bool speed_control_flag{}; + static inline bool motor_brake_flag{}; + // Orders sent to boards static inline bool close_contactors_sent{}; + static inline bool open_contactors_sent{}; + + static inline bool propulsion_sent{}; + static inline bool stop_propulsion_sent{}; + static inline bool levitation_sent{}; static inline bool stop_levitation_sent{}; + static inline bool booster_sent{}; static inline bool stop_booster_sent{}; + static inline bool runs_sent{}; + static inline bool svpwm_sent{}; + static inline bool stop_motor_sent{}; + static inline bool current_control_sent{}; + static inline bool speed_control_sent{}; + static inline bool motor_brake_sent{}; + // -----------------Order variables----------------- + // PCU + + enum class Direction : uint8_t { FORWARD = 0, BACKWARDS = 1 }; + + + //Order 1 Runs + static inline uint8_t run_id{}; + + + // Order 2 Start SVPWM // no movement + static inline Direction motor_direction_1; + static inline float max_voltage_1{}; + static inline float reference_voltage_1{}; + static inline float commutation_frequency_1{}; + static inline float modulation_frequency_1{}; + + // Order 3 Stop motor + //NO ARGS + + // Order 4 Send reference current + static inline float reference_current_2{}; + static inline Direction motor_direction_2; + static inline float max_voltage_2{}; + static inline float commutation_frequency_2{}; + static inline float modulation_frequency_2{}; + + // Order 5 Send reference speed + static inline Direction motor_direction_3{}; + static inline float reference_speed_3{}; + static inline float max_voltage_3{}; + static inline float commutation_frequency_3{}; + + // Order 6 Brake using motor + // NO ARGS + + + // LCU static inline float levitation_distance{}; // -----------------IP's/Ports----------------- static constexpr std::string CONTROL_STATION_IP = "192.168.0.9"; static constexpr std::string VCU_IP = "192.168.1.3"; - static constexpr std::string PCU_IP = "192.168.1.3"; + static constexpr std::string PCU_IP = "192.168.1.5"; static constexpr std::string HVSCU_IP = "192.168.1.7"; static constexpr std::string BMSL_IP = "192.168.1.3"; static constexpr std::string LCU_IP = "192.168.1.4"; @@ -206,6 +281,7 @@ class Comms { // Remote packets static inline HeapPacket* hvscu_state_packet{}; static inline HeapPacket* lcu_state_packet{}; + static inline HeapPacket* pcu_state_packet{}; // -----------------Orders----------------- static inline HeapOrder* Potencia_refri{}; @@ -252,6 +328,21 @@ class Comms { static inline HeapOrder* forward_booster_order{}; static inline HeapOrder* emergency_stop{}; + static inline HeapOrder* runs{}; + static inline HeapOrder* remote_runs{}; + static inline HeapOrder* svpwm{}; + static inline HeapOrder* remote_svpwm{}; + static inline HeapOrder* stop_motor{}; + static inline HeapOrder* remote_stop_motor{}; + static inline HeapOrder* current_control{}; + static inline HeapOrder* remote_current_control{}; + static inline HeapOrder* speed_control{}; + static inline HeapOrder* remote_speed_control{}; + static inline HeapOrder* motor_brake{}; + static inline HeapOrder* remote_motor_brake{}; + + + // -----------------Functions----------------- static void init(); static void start(); @@ -295,10 +386,30 @@ class Comms { static void emergency_stop_callback(); + static void runs_callback(); + static void svpwm_callback(); + static void stop_motor_callback(); + static void current_control_callback(); + static void speed_control_callback(); + static void motor_brake_callback(); + // Check orders static void check_close_contactors_order(); + static void check_open_contactors_order(); + + static void check_propulsion_order(); + static void check_stop_propulsion_order(); + static void check_levitation_order(); static void check_stop_levitation_order(); + static void check_booster_order(); static void check_stop_booster_order(); + + static void check_runs_order(); + static void check_svpwm_order(); + static void check_stop_motor_order(); + static void check_current_control_order(); + static void check_speed_control_order(); + static void check_motor_brake_order(); }; \ No newline at end of file diff --git a/Core/Inc/VCU/Comms/Utility.hpp b/Core/Inc/VCU/Comms/Utility.hpp new file mode 100644 index 0000000..edea65b --- /dev/null +++ b/Core/Inc/VCU/Comms/Utility.hpp @@ -0,0 +1,3 @@ +#pragma once +#include "Comms.hpp" + diff --git a/Core/Inc/VCU/VCU.hpp b/Core/Inc/VCU/VCU.hpp index 7b032c1..5b9b334 100644 --- a/Core/Inc/VCU/VCU.hpp +++ b/Core/Inc/VCU/VCU.hpp @@ -1,5 +1,5 @@ #pragma once -#include "Comms.hpp" +#include "Comms/Comms.hpp" #include "state_machine.hpp" class VCU { diff --git a/Core/Inc/state_machine.hpp b/Core/Inc/state_machine.hpp index fe82ef7..4384fa9 100644 --- a/Core/Inc/state_machine.hpp +++ b/Core/Inc/state_machine.hpp @@ -1,6 +1,6 @@ #pragma once #include "ST-LIB.hpp" -#include "VCU/Comms.hpp" +#include "VCU/Comms/Comms.hpp" using namespace std::chrono_literals; @@ -12,7 +12,7 @@ class VCU_SM { Fault, }; - enum OperationalStates { Idle, EndOfRun, Energyzed, Ready, Demonstration }; + enum OperationalStates { Idle, EndOfRun, Energized, Ready, Demonstration }; StateMachine GeneralStateMachine; StateMachine OperationalStateMachine; @@ -24,7 +24,7 @@ class VCU_SM { OperationalStateMachine = StateMachine(OperationalStates::Idle); OperationalStateMachine.add_state(OperationalStates::EndOfRun); - OperationalStateMachine.add_state(OperationalStates::Energyzed); + OperationalStateMachine.add_state(OperationalStates::Energized); OperationalStateMachine.add_state(OperationalStates::Ready); OperationalStateMachine.add_state(OperationalStates::Demonstration); @@ -34,15 +34,15 @@ class VCU_SM { GeneralStateMachine.add_transition( GeneralStates::Connecting, GeneralStates::Operational, [&]() { return Comms::control_station_tcp->is_connected() && - Comms::lcu_tcp->is_connected() && - Comms::bcu_tcp->is_connected(); + Comms::hvscu_tcp->is_connected() && + Comms::pcu_tcp->is_connected(); }); GeneralStateMachine.add_transition( GeneralStates::Operational, GeneralStates::Fault, [&]() { return !Comms::control_station_tcp->is_connected() || - !Comms::lcu_tcp->is_connected() || - !Comms::bcu_tcp->is_connected(); + !Comms::hvscu_tcp->is_connected() || + !Comms::pcu_tcp->is_connected(); }); GeneralStateMachine.add_transition( @@ -93,11 +93,11 @@ class VCU_SM { //----------- OperationalStateMachine.add_transition( - OperationalStates::Idle, OperationalStates::Energyzed, + OperationalStates::Idle, OperationalStates::Energized, [&]() { return Comms::actuators->contactors_closed; }); OperationalStateMachine.add_transition( - OperationalStates::Energyzed, OperationalStates::Idle, + OperationalStates::Energized, OperationalStates::Idle, [&]() { return !Comms::actuators->contactors_closed; }); OperationalStateMachine.add_transition( @@ -109,11 +109,11 @@ class VCU_SM { [&]() { return !Comms::actuators->contactors_closed; }); OperationalStateMachine.add_transition( - OperationalStates::Energyzed, OperationalStates::Ready, + OperationalStates::Energized, OperationalStates::Ready, [&]() { return !Comms::brakes->Active_brakes; }); OperationalStateMachine.add_transition( - OperationalStates::Ready, OperationalStates::Energyzed, + OperationalStates::Ready, OperationalStates::Energized, [&]() { return Comms::brakes->Active_brakes; }); //----------- @@ -150,13 +150,12 @@ class VCU_SM { GeneralStateMachine.add_low_precision_cyclic_action( [&]() { - /* while (!Comms::hvscu_tcp->is_connected()) { + if(!Comms::hvscu_tcp->is_connected()){ Comms::hvscu_tcp->reconnect(); - } */ - while (!Comms::lcu_tcp->is_connected() || - !Comms::bcu_tcp->is_connected()) { - Comms::lcu_tcp->reconnect(); - Comms::bcu_tcp->reconnect(); + } + + if(!Comms::pcu_tcp->is_connected()){ + Comms::pcu_tcp->reconnect(); } }, 100ms, GeneralStates::Connecting); diff --git a/Core/Src/Runes/generated_metadata.cpp b/Core/Src/Runes/generated_metadata.cpp index ef9e262..c136403 100644 --- a/Core/Src/Runes/generated_metadata.cpp +++ b/Core/Src/Runes/generated_metadata.cpp @@ -5,11 +5,11 @@ extern "C"{ const char DESCRIPTION[255] __attribute__((section(".metadata_pool")))= "****************" // placeholder for beggining - "20250708T161845" // DateTime using ISO-8601 format + "20250709T221517" // DateTime using ISO-8601 format " " // alignment "f8e647d0" // STLIB commit "--------" // ADJ commit - "992c46c1" // Board commit + "d2416c36" // Board commit // the '=' is used for unparsing ; } \ No newline at end of file diff --git a/Core/Src/VCU/Comms.cpp b/Core/Src/VCU/Comms.cpp deleted file mode 100644 index 026fcff..0000000 --- a/Core/Src/VCU/Comms.cpp +++ /dev/null @@ -1,417 +0,0 @@ -#include "VCU/Comms.hpp" - -#include "VCU/VCU.hpp" - -//------------------------CALLBACKS------------------------ - -void Comms::on_potencia_refri() { - if (actuators->selected_pump == Actuators::Pump::PUMP_UNIDADES) { - actuators->set_pump_1(actuators->selected_pump_duty); - } else if (actuators->selected_pump == Actuators::Pump::PUMP_PLACAS) { - actuators->set_pump_2(actuators->selected_pump_duty); - } -} - -void Comms::on_Set_regulator() { - if (actuators->selected_regulator_pressure > 6) { - actuators->selected_regulator_pressure = 6; - // InfoWarning::InfoWarningTrigger("Cannot exceed 6 bar on regulator - // pressure"); - } - if (actuators->selected_regulator == Actuators::Regulator::REGULATOR_1) { - actuators->regulator_1_pressure = - actuators->selected_regulator_pressure; - actuators->set_regulator_1(actuators->regulator_1_pressure); - } else if (actuators->selected_regulator == - Actuators::Regulator::REGULATOR_2) { - actuators->regulator_2_pressure = - actuators->selected_regulator_pressure; - actuators->set_regulator_2(actuators->regulator_2_pressure); - } -} - -void Comms::on_Enable_tapes() { - brakes->tape_enable_status = PinState::ON; - brakes->tape_enable_output.turn_on(); -} - -void Comms::on_Disable_tapes() { - brakes->tape_enable_status = PinState::OFF; - brakes->tape_enable_output.turn_off(); -} - -void Comms::brake_callback() { brakes->brake(); } - -void Comms::unbrake_callback() { brakes->unbrake(); } - -void Comms::close_contactors_callback() { close_contactors_flag = true; } - -void Comms::open_contactors_callback() { open_contactors_flag = true; } - -void Comms::levitation_callback() { levitation_flag = true; } - -void Comms::stop_levitation_callback() { stop_levitation_flag = true; } - -void Comms::booster_callback() { booster_flag = true; } - -void Comms::stop_booster_callback() { stop_booster_flag = true; } - -void Comms::emergency_stop_callback() { - ProtectionManager::fault_and_propagate(); -} - -//------------------------------------------------------ - -void Comms::init() { - leds = new Leds(); - actuators = new Actuators(); - brakes = new Brakes(); -} - -void Comms::start() { - control_station_tcp = new ServerSocket(IPV4(VCU_IP), REMOTE_PORT); - - control_station_udp = - new DatagramSocket(IPV4(VCU_IP), CONTROL_STATION_UDP_PORT, - IPV4(CONTROL_STATION_IP), CONTROL_STATION_UDP_PORT); - - lcu_tcp = new Socket(IPV4(VCU_IP), LCU_PORT, IPV4(LCU_IP), REMOTE_PORT); - - /* hvscu_tcp = - new Socket(IPV4(VCU_IP), HVSCU_PORT, IPV4(HVSCU_IP), REMOTE_PORT); - - hvscu_udp = new DatagramSocket(IPV4(VCU_IP), HVSCU_UDP_PORT, IPV4(HVSCU_IP), - HVSCU_UDP_PORT); */ - - lcu_udp = new DatagramSocket(IPV4(VCU_IP), LCU_UDP_PORT, IPV4(LCU_IP), - LCU_UDP_PORT); - - bcu_tcp = - new Socket(IPV4(VCU_IP), BCU_PORT, IPV4(BCU_IP), JUANS_REMOTE_PORT); - - add_packets(); - add_orders(); -} - -void Comms::add_packets() { - states = new HeapPacket(static_cast(Packets_id::States), - VCU::general_state, VCU::operational_state); - reeds = new HeapPacket(static_cast(Packets_id::Reeds), - &brakes->reed1, &brakes->reed2, &brakes->reed3, - &brakes->reed4, &brakes->reed5, &brakes->reed6, - &brakes->reed7, &brakes->reed8, &brakes->All_reeds); - flow = new HeapPacket(static_cast(Packets_id::Flow), - &actuators->flow1, &actuators->flow2); - regulator = new HeapPacket(static_cast(Packets_id::Regulator), - &actuators->regulator_1_pressure, - &actuators->regulator_2_pressure); - pressure = new HeapPacket(static_cast(Packets_id::Pressure), - &actuators->pressure_1, &actuators->pressure_2, - &actuators->pressure_3, &actuators->pressure_4); - tapes = - new HeapPacket(static_cast(Packets_id::Tapes), - &brakes->tape_enable_status, &brakes->tape_emergency); - - sdc = - new HeapPacket(static_cast(Packets_id::Sdc), &actuators->Sdc); - - // Remote packets - - hvscu_state_packet = new HeapPacket( - static_cast(Packets_id::hvscu_state), &hvscu_state); - - lcu_state_packet = - new HeapPacket(static_cast(Packets_id::lcu_state), - &lcu_v_state, &lcu_h_state); -} - -void Comms::add_orders() { - Potencia_refri = new HeapOrder( - static_cast(Orders_id::Potencia_refri), &on_potencia_refri, - &actuators->selected_pump_duty, &actuators->selected_pump); - - Set_Regulator = new HeapOrder( - static_cast(Orders_id::Set_Regulator), &on_Set_regulator, - &actuators->selected_regulator_pressure, - &actuators->selected_regulator); - - Enable_tapes = new HeapOrder(static_cast(Orders_id::Enable_tapes), - &on_Enable_tapes); - - Disable_tapes = new HeapOrder( - static_cast(Orders_id::Disable_tapes), &on_Disable_tapes); - - brake = - new HeapOrder(static_cast(Orders_id::Brake), &brake_callback); - - unbrake = new HeapOrder(static_cast(Orders_id::Unbrake), - &unbrake_callback); - - //----------------------------------------------------------------------------- - - remote_close_contactors = - new HeapOrder(static_cast(External_ids::Close_contactors), - &close_contactors_callback); - - remote_open_contactors = - new HeapOrder(static_cast(External_ids::Open_contactors), - &open_contactors_callback); - - remote_levitation = - new HeapOrder(static_cast(External_ids::Levitation), - &levitation_callback, &levitation_distance); - - remote_stop_levitation = - new HeapOrder(static_cast(External_ids::Stop_levitation), - &stop_levitation_callback); - - remote_booster = new HeapOrder(static_cast(External_ids::Booster), - &booster_callback); - - remote_stop_booster = - new HeapOrder(static_cast(External_ids::Stop_booster), - &stop_booster_callback); - - //----------------------------------------------------------------------------- - - close_contactors = - new HeapOrder(static_cast(Orders_id::Close_contactors), - &close_contactors_callback); - - open_contactors = - new HeapOrder(static_cast(Orders_id::Open_contactors), - &open_contactors_callback); - - levitation = new HeapOrder(static_cast(Orders_id::Levitation), - &levitation_callback, &levitation_distance); - - stop_levitation = - new HeapOrder(static_cast(Orders_id::Stop_levitation), - &stop_levitation_callback); - - booster = new HeapOrder(static_cast(Orders_id::Booster), - &booster_callback); - - stop_booster = new HeapOrder(static_cast(Orders_id::Stop_booster), - &stop_booster_callback); - - forward_booster_order = - new HeapOrder(static_cast(Orders_id::Forward_booster)); - - emergency_stop = - new HeapOrder(static_cast(Orders_id::Emergency_stop), - &emergency_stop_callback); -} - -void Comms::send_packets() { - control_station_udp->send_packet(*states); - control_station_udp->send_packet(*reeds); - control_station_udp->send_packet(*flow); - control_station_udp->send_packet(*regulator); - control_station_udp->send_packet(*pressure); - control_station_udp->send_packet(*tapes); -} - -void Comms::read_sensors() { - actuators->read_regulators(); - actuators->read_pressure(); - actuators->read_flow(); - actuators->read_sdc(); - brakes->read_reeds(); - brakes->read_tape_emergency(); -} - -void Comms::check_close_contactors_order() { - /* if (close_contactors_flag) { - if (*VCU::operational_state == VCU_SM::OperationalStates::Idle) { - if (!close_contactors_sent) { - hvscu_tcp->send_order(*close_contactors); - close_contactors_sent = true; - } else { - if (hvscu_state == - static_cast(Boards_states::HVSCU_Closed)) { - actuators->contactors_closed = true; - close_contactors_flag = false; - close_contactors_sent = false; - } else { - return; - } - } - } else { - WARNING("Cannot close contactors in this state"); - close_contactors_flag = false; - } - } */ - if (close_contactors_flag) { - if (*VCU::operational_state == VCU_SM::OperationalStates::Idle) { - actuators->contactors_closed = true; - close_contactors_flag = false; - } - } else { - WARNING("Cannot close contactors in this state"); - close_contactors_flag = false; - } -} - -void Comms::check_levitation_order() { - if (levitation_flag) { - if (*VCU::operational_state == VCU_SM::OperationalStates::Ready || - *VCU::operational_state == - VCU_SM::OperationalStates::Demonstration) { - static uint8_t timeout_id = -1; - - if (!levitation_sent) { - lcu_tcp->send_order(*remote_levitation); - levitation_sent = true; - } else { - if (lcu_v_state == - static_cast(LCU_states::LCU_Levitation)) { - Time::cancel_timeout(timeout_id); - timeout_id = -1; - order_demonstration_bitfield |= (1UL << levitation_bit); - levitation_flag = false; - levitation_sent = false; - } else { - if (timeout_id == -1) { - timeout_id = Time::set_timeout(100, [&]() { - VCU::state_machine->GeneralStateMachine - .force_change_state( - VCU_SM::GeneralStates::Fault); - }); - } - } - } - } else { - WARNING("Cannot levitate in this state"); - levitation_flag = false; - } - } -} - -void Comms::check_stop_levitation_order() { - if (stop_levitation_flag) { - if (*VCU::operational_state == - VCU_SM::OperationalStates::Demonstration) { - static uint8_t timeout_id = -1; - - if (!stop_levitation_sent) { - lcu_tcp->send_order(*remote_stop_levitation); - stop_levitation_sent = true; - } else { - if (lcu_v_state == - static_cast(LCU_states::LCU_Stop_Levitation)) { - Time::cancel_timeout(timeout_id); - timeout_id = -1; - order_demonstration_bitfield &= ~(1UL << levitation_bit); - stop_levitation_flag = false; - stop_levitation_sent = false; - } else { - if (timeout_id == -1) { - timeout_id = Time::set_timeout(100, [&]() { - VCU::state_machine->GeneralStateMachine - .force_change_state( - VCU_SM::GeneralStates::Fault); - }); - } - } - } - } else { - //WARNING("Cannot stop levitating in this state"); - InfoWarning::InfoWarningTrigger( - "Cannot stop levitating in this state"); - stop_levitation_flag = false; - } - } -} - -void Comms::check_booster_order() { - if (booster_flag) { - if (*VCU::operational_state == VCU_SM::OperationalStates::Ready || - *VCU::operational_state == - VCU_SM::OperationalStates::Demonstration) { - static uint8_t timeout_id = -1; - - if (!booster_sent) { - lcu_tcp->send_order(*remote_booster); - booster_sent = true; - } else { - if (lcu_h_state == - static_cast(Booster_states::Booster_Enabled)) { - Time::cancel_timeout(timeout_id); - timeout_id = -1; - order_demonstration_bitfield |= (1UL << booster_bit); - bcu_tcp->send_order(*forward_booster_order); - booster_flag = false; - booster_sent = false; - } else { - if (timeout_id == -1) { - timeout_id = Time::set_timeout(100, [&]() { - VCU::state_machine->GeneralStateMachine - .force_change_state( - VCU_SM::GeneralStates::Fault); - }); - } - } - } - } else { - WARNING("Cannot boost in this state"); - booster_flag = false; - } - } -} - -void Comms::check_stop_booster_order() { - if (stop_booster_flag) { - if (*VCU::operational_state == - VCU_SM::OperationalStates::Demonstration) { - static uint8_t timeout_id = -1; - - if (!stop_booster_sent) { - lcu_tcp->send_order(*remote_stop_booster); - stop_booster_sent = true; - } else { - if (lcu_h_state == - static_cast(Booster_states::Booster_Disabled)) { - Time::cancel_timeout(timeout_id); - timeout_id = -1; - order_demonstration_bitfield &= ~(1UL << booster_bit); - stop_booster_flag = false; - stop_booster_sent = false; - } else { - if (timeout_id == -1) { - timeout_id = Time::set_timeout(100, [&]() { - VCU::state_machine->GeneralStateMachine - .force_change_state( - VCU_SM::GeneralStates::Fault); - }); - } - } - } - } else { - WARNING("Cannot stop boosting in this state"); - stop_booster_flag = false; - } - } -} - -void Comms::check_orders() { - check_close_contactors_order(); - - if (open_contactors_flag) { - // hvscu_tcp->send_order(*open_contactors); - actuators->contactors_closed = false; - stop_levitation_flag = true; - - open_contactors_flag = false; - } - - check_levitation_order(); - check_stop_levitation_order(); - check_booster_order(); - check_stop_booster_order(); -} - -void Comms::update() { - check_orders(); - read_sensors(); -} \ No newline at end of file diff --git a/Core/Src/VCU/Comms/Comms.cpp b/Core/Src/VCU/Comms/Comms.cpp new file mode 100644 index 0000000..f99d497 --- /dev/null +++ b/Core/Src/VCU/Comms/Comms.cpp @@ -0,0 +1,767 @@ +#include "VCU/Comms/Comms.hpp" + +#include "VCU/VCU.hpp" + +//------------------------CALLBACKS------------------------ + +void Comms::on_potencia_refri() { + if (actuators->selected_pump == Actuators::Pump::PUMP_UNIDADES) { + actuators->set_pump_1(actuators->selected_pump_duty); + } else if (actuators->selected_pump == Actuators::Pump::PUMP_PLACAS) { + actuators->set_pump_2(actuators->selected_pump_duty); + } +} + +void Comms::on_Set_regulator() { + if (actuators->selected_regulator_pressure > 6) { + actuators->selected_regulator_pressure = 6; + // InfoWarning::InfoWarningTrigger("Cannot exceed 6 bar on regulator + // pressure"); + } + if (actuators->selected_regulator == Actuators::Regulator::REGULATOR_1) { + actuators->regulator_1_pressure = + actuators->selected_regulator_pressure; + actuators->set_regulator_1(actuators->regulator_1_pressure); + } else if (actuators->selected_regulator == + Actuators::Regulator::REGULATOR_2) { + actuators->regulator_2_pressure = + actuators->selected_regulator_pressure; + actuators->set_regulator_2(actuators->regulator_2_pressure); + } +} + +void Comms::on_Enable_tapes() { + brakes->tape_enable_status = PinState::ON; + brakes->tape_enable_output.turn_on(); +} + +void Comms::on_Disable_tapes() { + brakes->tape_enable_status = PinState::OFF; + brakes->tape_enable_output.turn_off(); +} + +void Comms::brake_callback() { brakes->brake(); } + +void Comms::unbrake_callback() { brakes->unbrake(); } + +void Comms::close_contactors_callback() { close_contactors_flag = true; } + +void Comms::open_contactors_callback() { open_contactors_flag = true; } + +void Comms::levitation_callback() { levitation_flag = true; } + +void Comms::stop_levitation_callback() { stop_levitation_flag = true; } + +void Comms::booster_callback() { booster_flag = true; } + +void Comms::stop_booster_callback() { stop_booster_flag = true; } + +void Comms::emergency_stop_callback() { + ProtectionManager::fault_and_propagate(); +} + +void Comms::runs_callback() { runs_flag = true; } +void Comms::svpwm_callback() { svpwm_flag = true; } +void Comms::stop_motor_callback() { stop_motor_flag = true; } +void Comms::current_control_callback() { current_control_flag = true; } +void Comms::speed_control_callback() { speed_control_flag = true; } +void Comms::motor_brake_callback() { motor_brake_flag = true; } + +//------------------------------------------------------ + +void Comms::init() { + leds = new Leds(); + actuators = new Actuators(); + brakes = new Brakes(); +} + +void Comms::start() { + control_station_tcp = new ServerSocket(IPV4(VCU_IP), REMOTE_PORT); + + control_station_udp = + new DatagramSocket(IPV4(VCU_IP), CONTROL_STATION_UDP_PORT, + IPV4(CONTROL_STATION_IP), CONTROL_STATION_UDP_PORT); + + lcu_tcp = new Socket(IPV4(VCU_IP), LCU_PORT, IPV4(LCU_IP), REMOTE_PORT); + + lcu_udp = new DatagramSocket(IPV4(VCU_IP), LCU_UDP_PORT, IPV4(LCU_IP), + LCU_UDP_PORT); + + hvscu_tcp = + new Socket(IPV4(VCU_IP), HVSCU_PORT, IPV4(HVSCU_IP), REMOTE_PORT); + + hvscu_udp = new DatagramSocket(IPV4(VCU_IP), HVSCU_UDP_PORT, IPV4(HVSCU_IP), + HVSCU_UDP_PORT); + + bcu_tcp = + new Socket(IPV4(VCU_IP), BCU_PORT, IPV4(BCU_IP), JUANS_REMOTE_PORT); + + pcu_tcp = new Socket(IPV4(VCU_IP), PCU_PORT, IPV4(PCU_IP), REMOTE_PORT); + + pcu_udp = new DatagramSocket(IPV4(VCU_IP), PCU_UDP_PORT, IPV4(PCU_IP), + PCU_UDP_PORT); + + add_packets(); + add_orders(); +} + +void Comms::add_packets() { + states = new HeapPacket(static_cast(Packets_id::States), + VCU::general_state, VCU::operational_state); + reeds = new HeapPacket(static_cast(Packets_id::Reeds), + &brakes->reed1, &brakes->reed2, &brakes->reed3, + &brakes->reed4, &brakes->reed5, &brakes->reed6, + &brakes->reed7, &brakes->reed8, &brakes->All_reeds); + flow = new HeapPacket(static_cast(Packets_id::Flow), + &actuators->flow1, &actuators->flow2); + regulator = new HeapPacket(static_cast(Packets_id::Regulator), + &actuators->regulator_1_pressure, + &actuators->regulator_2_pressure); + pressure = new HeapPacket(static_cast(Packets_id::Pressure), + &actuators->pressure_1, &actuators->pressure_2, + &actuators->pressure_3, &actuators->pressure_4); + tapes = + new HeapPacket(static_cast(Packets_id::Tapes), + &brakes->tape_enable_status, &brakes->tape_emergency); + + sdc = + new HeapPacket(static_cast(Packets_id::Sdc), &actuators->Sdc); + + // Remote packets + + hvscu_state_packet = new HeapPacket( + static_cast(Packets_id::hvscu_state), &hvscu_state); + + lcu_state_packet = + new HeapPacket(static_cast(Packets_id::lcu_state), + &lcu_v_state, &lcu_h_state); + + pcu_state_packet = new HeapPacket( + static_cast(Packets_id::pcu_state), &pcu_state); +} + +void Comms::add_orders() { + Potencia_refri = new HeapOrder( + static_cast(Orders_id::Potencia_refri), &on_potencia_refri, + &actuators->selected_pump_duty, &actuators->selected_pump); + + Set_Regulator = new HeapOrder( + static_cast(Orders_id::Set_Regulator), &on_Set_regulator, + &actuators->selected_regulator_pressure, + &actuators->selected_regulator); + + Enable_tapes = new HeapOrder(static_cast(Orders_id::Enable_tapes), + &on_Enable_tapes); + + Disable_tapes = new HeapOrder( + static_cast(Orders_id::Disable_tapes), &on_Disable_tapes); + + brake = + new HeapOrder(static_cast(Orders_id::Brake), &brake_callback); + + unbrake = new HeapOrder(static_cast(Orders_id::Unbrake), + &unbrake_callback); + + //----------------------------------------------------------------------------- + + remote_close_contactors = + new HeapOrder(static_cast(External_ids::Close_contactors), + &close_contactors_callback); + + remote_open_contactors = + new HeapOrder(static_cast(External_ids::Open_contactors), + &open_contactors_callback); + + remote_levitation = + new HeapOrder(static_cast(External_ids::Levitation), + &levitation_callback, &levitation_distance); + + remote_stop_levitation = + new HeapOrder(static_cast(External_ids::Stop_levitation), + &stop_levitation_callback); + + remote_booster = new HeapOrder(static_cast(External_ids::Booster), + &booster_callback); + + remote_stop_booster = + new HeapOrder(static_cast(External_ids::Stop_booster), + &stop_booster_callback); + + //----------------------------------------------------------------------------- + + close_contactors = + new HeapOrder(static_cast(Orders_id::Close_contactors), + &close_contactors_callback); + + open_contactors = + new HeapOrder(static_cast(Orders_id::Open_contactors), + &open_contactors_callback); + + /* propulsion = new HeapOrder(static_cast(Orders_id::Propulsion), + &propulsion_callback, &levitation_distance); + + stop_propulsion = + new HeapOrder(static_cast(Orders_id::Stop_propulsion), + &stop_propulsion_callback); */ + + runs = new HeapOrder(static_cast(Orders_id::Runs), &runs_callback, + &run_id); + + remote_runs = new HeapOrder(static_cast(External_ids::Runs), + &runs_callback, &run_id); + + svpwm = + new HeapOrder(static_cast(Orders_id::SVPWM), &svpwm_callback, + &modulation_frequency_1, &commutation_frequency_1, + &reference_voltage_1, &max_voltage_1, &motor_direction_1); + + remote_svpwm = new HeapOrder(static_cast(External_ids::SVPWM), + &svpwm_callback, &modulation_frequency_1, + &commutation_frequency_1, &reference_voltage_1, + &max_voltage_1, &motor_direction_1); + + stop_motor = new HeapOrder(static_cast(Orders_id::Stop_motor), + &stop_motor_callback); + remote_stop_motor = new HeapOrder( + static_cast(External_ids::Stop_motor), &stop_motor_callback); + + current_control = new HeapOrder( + static_cast(Orders_id::Current_control), + ¤t_control_callback, &modulation_frequency_2, &commutation_frequency_2, + &reference_current_2, &max_voltage_2, &motor_direction_2); + + remote_current_control = new HeapOrder( + static_cast(External_ids::Current_control), + ¤t_control_callback, &modulation_frequency_2, &commutation_frequency_2, + &reference_current_2, &max_voltage_2, &motor_direction_2); + + speed_control = new HeapOrder( + static_cast(Orders_id::Speed_control), + &speed_control_callback, &reference_speed_3, + &commutation_frequency_3, &max_voltage_3, &motor_direction_3); + + remote_speed_control = new HeapOrder( + static_cast(External_ids::Speed_control), + &speed_control_callback, &reference_speed_3, + &commutation_frequency_3, &max_voltage_3, &motor_direction_3); + + motor_brake = new HeapOrder(static_cast(Orders_id::Motor_brake), + &motor_brake_callback); + + remote_motor_brake = + new HeapOrder(static_cast(External_ids::Motor_brake), + &motor_brake_callback); + + levitation = new HeapOrder(static_cast(Orders_id::Levitation), + &levitation_callback, &levitation_distance); + + stop_levitation = + new HeapOrder(static_cast(Orders_id::Stop_levitation), + &stop_levitation_callback); + + booster = new HeapOrder(static_cast(Orders_id::Booster), + &booster_callback); + + stop_booster = new HeapOrder(static_cast(Orders_id::Stop_booster), + &stop_booster_callback); + + forward_booster_order = + new HeapOrder(static_cast(Orders_id::Forward_booster)); + + emergency_stop = + new HeapOrder(static_cast(Orders_id::Emergency_stop), + &emergency_stop_callback); +} + +void Comms::send_packets() { + control_station_udp->send_packet(*states); + control_station_udp->send_packet(*reeds); + control_station_udp->send_packet(*flow); + control_station_udp->send_packet(*regulator); + control_station_udp->send_packet(*pressure); + control_station_udp->send_packet(*tapes); +} + +void Comms::read_sensors() { + actuators->read_regulators(); + actuators->read_pressure(); + actuators->read_flow(); + actuators->read_sdc(); + brakes->read_reeds(); + brakes->read_tape_emergency(); +} + +void Comms::check_close_contactors_order() { + if (close_contactors_flag) { + if (*VCU::operational_state == VCU_SM::OperationalStates::Idle) { + static uint8_t timeout_id = -1; + if (!close_contactors_sent) { + hvscu_tcp->send_order(*remote_close_contactors); + close_contactors_sent = true; + } else { + if (hvscu_state == + static_cast(HVSCU_states::HVSCU_Closed)) { + Time::cancel_timeout(timeout_id); + timeout_id = -1; + actuators->contactors_closed = true; + close_contactors_flag = false; + close_contactors_sent = false; + } else { + if (timeout_id == -1) { + timeout_id = Time::set_timeout(6000, [&]() { + ProtectionManager::fault_and_propagate(); + }); + } + } + } + } else { + WARNING("Cannot close contactors in this state"); + close_contactors_flag = false; + } + } +} + +void Comms::check_open_contactors_order() { + if (open_contactors_flag) { + static uint8_t timeout_id = -1; + if (!open_contactors_sent) { + hvscu_tcp->send_order(*remote_open_contactors); + open_contactors_sent = true; + } else { + if (hvscu_state == + static_cast(HVSCU_states::HVSCU_Opened)) { + Time::cancel_timeout(timeout_id); + timeout_id = -1; + actuators->contactors_closed = false; + open_contactors_flag = false; + open_contactors_sent = false; + } else { + if (timeout_id == -1) { + timeout_id = Time::set_timeout(6000, [&]() { + ProtectionManager::fault_and_propagate(); + }); + } + } + } + } +} + +void Comms::check_propulsion_order() { + if (propulsion_flag) { + if (*VCU::operational_state == VCU_SM::OperationalStates::Ready || + *VCU::operational_state == + VCU_SM::OperationalStates::Demonstration) { + static uint8_t timeout_id = -1; + + if (!propulsion_sent) { + pcu_tcp->send_order(*remote_levitation); + propulsion_sent = true; + } else { + if (pcu_state == + static_cast(PCU_states::PCU_Propulsion)) { + Time::cancel_timeout(timeout_id); + timeout_id = -1; + order_demonstration_bitfield |= (1UL << propulsion_bit); + propulsion_flag = false; + propulsion_sent = false; + } else { + if (timeout_id == -1) { + timeout_id = Time::set_timeout(100, [&]() { + ProtectionManager::fault_and_propagate(); + }); + } + } + } + } else { + WARNING("Cannot propel in this state"); + levitation_flag = false; + } + } +} + +void Comms::check_stop_propulsion_order() { + if (stop_levitation_flag) { + if (*VCU::operational_state == + VCU_SM::OperationalStates::Demonstration) { + static uint8_t timeout_id = -1; + + if (!stop_levitation_sent) { + lcu_tcp->send_order(*remote_stop_levitation); + stop_levitation_sent = true; + } else { + if (lcu_v_state == + static_cast(LCU_states::LCU_Stop_Levitation)) { + Time::cancel_timeout(timeout_id); + timeout_id = -1; + order_demonstration_bitfield &= ~(1UL << levitation_bit); + stop_levitation_flag = false; + stop_levitation_sent = false; + } else { + if (timeout_id == -1) { + timeout_id = Time::set_timeout(100, [&]() { + ProtectionManager::fault_and_propagate(); + }); + } + } + } + } else { + WARNING("Cannot stop levitating in this state"); + stop_levitation_flag = false; + } + } +} + +//--------- PCU ---------------------------------------------------------- + +void Comms::check_runs_order() { + if (runs_flag) { + if (*VCU::operational_state == VCU_SM::OperationalStates::Ready || + *VCU::operational_state == + VCU_SM::OperationalStates::Demonstration) { + static uint8_t timeout_id = -1; + + if (!runs_sent) { + pcu_tcp->send_order(*remote_runs); + runs_sent = true; + } else { + if (pcu_state == + static_cast(PCU_states::PCU_Propulsion)) { + Time::cancel_timeout(timeout_id); + timeout_id = -1; + order_demonstration_bitfield |= (1UL << propulsion_bit); + runs_flag = false; + runs_sent = false; + } else { + if (timeout_id == -1) { + timeout_id = Time::set_timeout(100, [&]() { + ProtectionManager::fault_and_propagate(); + }); + } + } + } + } else { + WARNING("Cannot start a run in this state"); + runs_flag = false; + } + } +} + +void Comms::check_svpwm_order() { + if (svpwm_flag) { + if (*VCU::operational_state == VCU_SM::OperationalStates::Ready || + *VCU::operational_state == + VCU_SM::OperationalStates::Demonstration) { + static uint8_t timeout_id = -1; + + if (!svpwm_sent) { + pcu_tcp->send_order(*remote_svpwm); + svpwm_sent = true; + } else { + if (pcu_state == + static_cast(PCU_states::PCU_Propulsion)) { + Time::cancel_timeout(timeout_id); + timeout_id = -1; + order_demonstration_bitfield |= (1UL << propulsion_bit); + svpwm_flag = false; + svpwm_sent = false; + } else { + if (timeout_id == -1) { + timeout_id = Time::set_timeout(100, [&]() { + ProtectionManager::fault_and_propagate(); + }); + } + } + } + } else { + WARNING("Cannot send SVPWM in this state"); + svpwm_flag = false; + } + } +} + +void Comms::check_stop_motor_order() { + if (stop_motor_flag) { + if (*VCU::operational_state == + VCU_SM::OperationalStates::Demonstration) { + static uint8_t timeout_id = -1; + + if (!stop_motor_sent) { + pcu_tcp->send_order(*remote_stop_motor); + stop_motor_sent = true; + } else { + if (pcu_state == + static_cast(PCU_states::PCU_Stop_Propulsion)) { + Time::cancel_timeout(timeout_id); + timeout_id = -1; + order_demonstration_bitfield &= ~(1UL << propulsion_bit); + stop_motor_flag = false; + stop_motor_sent = false; + } else { + if (timeout_id == -1) { + timeout_id = Time::set_timeout(100, [&]() { + ProtectionManager::fault_and_propagate(); + }); + } + } + } + } else { + WARNING("Cannot stop the motor in this state"); + stop_motor_flag = false; + } + } +} + +void Comms::check_current_control_order() { + if (current_control_flag) { + if (*VCU::operational_state == VCU_SM::OperationalStates::Ready || + *VCU::operational_state == + VCU_SM::OperationalStates::Demonstration) { + static uint8_t timeout_id = -1; + + if (!current_control_sent) { + pcu_tcp->send_order(*remote_current_control); + current_control_sent = true; + } else { + if (pcu_state == + static_cast(PCU_states::PCU_Propulsion)) { + Time::cancel_timeout(timeout_id); + timeout_id = -1; + order_demonstration_bitfield |= (1UL << propulsion_bit); + current_control_flag = false; + current_control_sent = false; + } else { + if (timeout_id == -1) { + timeout_id = Time::set_timeout(100, [&]() { + ProtectionManager::fault_and_propagate(); + }); + } + } + } + } else { + WARNING("Cannot send current control in this state"); + current_control_flag = false; + } + } +} + +void Comms::check_speed_control_order() { + if (speed_control_flag) { + if (*VCU::operational_state == VCU_SM::OperationalStates::Ready || + *VCU::operational_state == + VCU_SM::OperationalStates::Demonstration) { + static uint8_t timeout_id = -1; + + if (!speed_control_sent) { + pcu_tcp->send_order(*remote_speed_control); + speed_control_sent = true; + } else { + if (pcu_state == + static_cast(PCU_states::PCU_Propulsion)) { + Time::cancel_timeout(timeout_id); + timeout_id = -1; + order_demonstration_bitfield |= (1UL << propulsion_bit); + speed_control_flag = false; + speed_control_sent = false; + } else { + if (timeout_id == -1) { + timeout_id = Time::set_timeout(100, [&]() { + ProtectionManager::fault_and_propagate(); + }); + } + } + } + } else { + WARNING("Cannot send speed control in this state"); + speed_control_flag = false; + } + } +} + +void Comms::check_motor_brake_order() { + if (motor_brake_flag) { + if (*VCU::operational_state == VCU_SM::OperationalStates::Ready || + *VCU::operational_state == + VCU_SM::OperationalStates::Demonstration) { + static uint8_t timeout_id = -1; + + if (!motor_brake_sent) { + pcu_tcp->send_order(*remote_motor_brake); + motor_brake_sent = true; + } else { + if (pcu_state == + static_cast(PCU_states::PCU_Propulsion)) { + Time::cancel_timeout(timeout_id); + timeout_id = -1; + order_demonstration_bitfield |= (1UL << propulsion_bit); + motor_brake_flag = false; + motor_brake_sent = false; + } else { + if (timeout_id == -1) { + timeout_id = Time::set_timeout(100, [&]() { + ProtectionManager::fault_and_propagate(); + }); + } + } + } + } else { + WARNING("Cannot brake with the motor in this state"); + motor_brake_flag = false; + } + } +} + +//--------- PCU ---------------------------------------------------------- + +void Comms::check_levitation_order() { + if (levitation_flag) { + if (*VCU::operational_state == VCU_SM::OperationalStates::Ready || + *VCU::operational_state == + VCU_SM::OperationalStates::Demonstration) { + static uint8_t timeout_id = -1; + + if (!levitation_sent) { + lcu_tcp->send_order(*remote_levitation); + levitation_sent = true; + } else { + if (lcu_v_state == + static_cast(LCU_states::LCU_Levitation)) { + Time::cancel_timeout(timeout_id); + timeout_id = -1; + order_demonstration_bitfield |= (1UL << levitation_bit); + levitation_flag = false; + levitation_sent = false; + } else { + if (timeout_id == -1) { + timeout_id = Time::set_timeout(100, [&]() { + ProtectionManager::fault_and_propagate(); + }); + } + } + } + } else { + WARNING("Cannot levitate in this state"); + levitation_flag = false; + } + } +} + +void Comms::check_stop_levitation_order() { + if (stop_levitation_flag) { + if (*VCU::operational_state == + VCU_SM::OperationalStates::Demonstration) { + static uint8_t timeout_id = -1; + + if (!stop_levitation_sent) { + lcu_tcp->send_order(*remote_stop_levitation); + stop_levitation_sent = true; + } else { + if (lcu_v_state == + static_cast(LCU_states::LCU_Stop_Levitation)) { + Time::cancel_timeout(timeout_id); + timeout_id = -1; + order_demonstration_bitfield &= ~(1UL << levitation_bit); + stop_levitation_flag = false; + stop_levitation_sent = false; + } else { + if (timeout_id == -1) { + timeout_id = Time::set_timeout(100, [&]() { + ProtectionManager::fault_and_propagate(); + }); + } + } + } + } else { + WARNING("Cannot stop levitating in this state"); + stop_levitation_flag = false; + } + } +} + +void Comms::check_booster_order() { + if (booster_flag) { + if (*VCU::operational_state == VCU_SM::OperationalStates::Ready || + *VCU::operational_state == + VCU_SM::OperationalStates::Demonstration) { + static uint8_t timeout_id = -1; + + if (!booster_sent) { + lcu_tcp->send_order(*remote_booster); + booster_sent = true; + } else { + if (lcu_h_state == + static_cast(Booster_states::Booster_Enabled)) { + Time::cancel_timeout(timeout_id); + timeout_id = -1; + order_demonstration_bitfield |= (1UL << booster_bit); + bcu_tcp->send_order(*forward_booster_order); + booster_flag = false; + booster_sent = false; + } else { + if (timeout_id == -1) { + timeout_id = Time::set_timeout(100, [&]() { + ProtectionManager::fault_and_propagate(); + }); + } + } + } + } else { + WARNING("Cannot boost in this state"); + booster_flag = false; + } + } +} + +void Comms::check_stop_booster_order() { + if (stop_booster_flag) { + if (*VCU::operational_state == + VCU_SM::OperationalStates::Demonstration) { + static uint8_t timeout_id = -1; + + if (!stop_booster_sent) { + lcu_tcp->send_order(*remote_stop_booster); + stop_booster_sent = true; + } else { + if (lcu_h_state == + static_cast(Booster_states::Booster_Disabled)) { + Time::cancel_timeout(timeout_id); + timeout_id = -1; + order_demonstration_bitfield &= ~(1UL << booster_bit); + stop_booster_flag = false; + stop_booster_sent = false; + } else { + if (timeout_id == -1) { + timeout_id = Time::set_timeout(100, [&]() { + ProtectionManager::fault_and_propagate(); + }); + } + } + } + } else { + WARNING("Cannot stop boosting in this state"); + stop_booster_flag = false; + } + } +} + +void Comms::check_orders() { + check_close_contactors_order(); + check_open_contactors_order(); + + check_levitation_order(); + check_stop_levitation_order(); + + check_booster_order(); + check_stop_booster_order(); + + check_runs_order(); + check_svpwm_order(); + check_stop_motor_order(); + check_current_control_order(); + check_speed_control_order(); + check_motor_brake_order(); +} + +void Comms::update() { + check_orders(); + read_sensors(); +} \ No newline at end of file diff --git a/Core/Src/VCU/Comms/Utility.cpp b/Core/Src/VCU/Comms/Utility.cpp new file mode 100644 index 0000000..e6ed9ef --- /dev/null +++ b/Core/Src/VCU/Comms/Utility.cpp @@ -0,0 +1 @@ +#include "VCU/Comms/Utility.hpp" \ No newline at end of file diff --git a/Core/Src/main.cpp b/Core/Src/main.cpp index 2c75f3f..efed533 100644 --- a/Core/Src/main.cpp +++ b/Core/Src/main.cpp @@ -10,7 +10,7 @@ int main(void) { VCU::init(); - STLIB::start("00:00:00:00:01:03","192.168.1.3"); + STLIB::start("00:00:00:00:01:03","192.168.1.3", "255.255.0.0"); VCU::start(); while (1) { diff --git a/Tests/VCU.py b/Tests/VCU.py index 5a988a5..7108d1a 100644 --- a/Tests/VCU.py +++ b/Tests/VCU.py @@ -35,7 +35,7 @@ ORDERS_WITH_ARGS = [START_PROPULSION, START_VERTICAL_LEVITATION] ORDERS_IDLE = [ START_CHARGING_G2V, STOP_CHARGING_G2V , OPEN_CONTACTORS , CLOSE_CONTACTORS, END_OF_RUN] -ORDERS_ENERGYZED = [BRAKE, UNBRAKE] +ORDERS_Energized = [BRAKE, UNBRAKE] ORDERS_READY = [START_VERTICAL_LEVITATION, STOP_VERTICAL_LEVITATION, START_HORIZONTAL_LEVITATION, STICK_DOWN, STOP_HORIZONTAL_LEVITATION, START_PROPULSION, STOP_PROPULSION , ENABLES_BOOSTER , DISABLES_BOOSTER] @@ -57,7 +57,7 @@ class General_SM(IntEnum): class Operational_SM(IntEnum): IDLE = 0 - ENERGYZED = 1 + Energized = 1 READY = 2 CHARGING_G2V = 3 ENDOFRUN = 4 From 4d8ba73da7e1836f85eb29b19be20545956f30ba Mon Sep 17 00:00:00 2001 From: jdmarmen Date: Sat, 12 Jul 2025 12:10:58 +0200 Subject: [PATCH 13/25] Fixes regarding bragink and cooling system + comms stuff --- Core/Inc/Code_generation/JSON_ADE | 2 +- Core/Inc/VCU/Actuators.hpp | 14 +++-- Core/Inc/VCU/Brakes.hpp | 8 --- Core/Inc/VCU/Comms/Comms.hpp | 4 ++ Core/Inc/state_machine.hpp | 29 +++++++--- Core/Src/Runes/generated_metadata.cpp | 4 +- Core/Src/VCU/Actuators.cpp | 53 ++++++++++++------ Core/Src/VCU/Brakes.cpp | 18 +----- Core/Src/VCU/Comms/Comms.cpp | 80 +++++++++++++++++++-------- Core/Src/VCU/VCU.cpp | 1 + 10 files changed, 132 insertions(+), 81 deletions(-) diff --git a/Core/Inc/Code_generation/JSON_ADE b/Core/Inc/Code_generation/JSON_ADE index 4b76606..4a995c3 160000 --- a/Core/Inc/Code_generation/JSON_ADE +++ b/Core/Inc/Code_generation/JSON_ADE @@ -1 +1 @@ -Subproject commit 4b766061750fccdba1ccd918fbcf4bcca4c41285 +Subproject commit 4a995c314959d4eb38e1b107441f32f40d0b6a46 diff --git a/Core/Inc/VCU/Actuators.hpp b/Core/Inc/VCU/Actuators.hpp index 6c5ab70..40836d8 100644 --- a/Core/Inc/VCU/Actuators.hpp +++ b/Core/Inc/VCU/Actuators.hpp @@ -9,8 +9,10 @@ class Actuators { DigitalSensor Regulator_in1; DigitalSensor Regulator_in2; - PWM Pump_c1; - PWM Pump_c2; + /* PWM Pump_c1; + PWM Pump_c2; */ + DigitalOutput Pump_c1; + DigitalOutput Pump_c2; DigitalSensor flow1_input; DigitalSensor flow2_input; @@ -44,10 +46,10 @@ class Actuators { PinState Sdc = PinState::ON; bool contactors_closed = false; - float pressure_1 = 0; - float pressure_2 = 0; - float pressure_3 = 0; - float pressure_4 = 0; + float pressure_high = 0; + float pressure_regulator = 0; + float pressure_brakes = 0; + float pressure_capsule = 0; Actuators(); diff --git a/Core/Inc/VCU/Brakes.hpp b/Core/Inc/VCU/Brakes.hpp index c342636..1bc3082 100644 --- a/Core/Inc/VCU/Brakes.hpp +++ b/Core/Inc/VCU/Brakes.hpp @@ -10,10 +10,6 @@ class Brakes { DigitalSensor reed2_input; DigitalSensor reed3_input; DigitalSensor reed4_input; - DigitalSensor reed5_input; - DigitalSensor reed6_input; - DigitalSensor reed7_input; - DigitalSensor reed8_input; // DigitalSensor Tape_input; @@ -27,10 +23,6 @@ class Brakes { PinState reed2 = PinState::OFF; PinState reed3 = PinState::OFF; PinState reed4 = PinState::OFF; - PinState reed5 = PinState::OFF; - PinState reed6 = PinState::OFF; - PinState reed7 = PinState::OFF; - PinState reed8 = PinState::OFF; bool All_reeds = false; diff --git a/Core/Inc/VCU/Comms/Comms.hpp b/Core/Inc/VCU/Comms/Comms.hpp index 1a6ded5..f0f9d20 100644 --- a/Core/Inc/VCU/Comms/Comms.hpp +++ b/Core/Inc/VCU/Comms/Comms.hpp @@ -53,6 +53,7 @@ class Comms { Open_contactors = 53, Forward_booster = 1788, Emergency_stop = 55, + Reset_vehicle = 62, Runs = 56, SVPWM = 57, @@ -397,6 +398,9 @@ class Comms { static void check_close_contactors_order(); static void check_open_contactors_order(); + static void check_brake_order(); + static void check_unbrake_order(); + static void check_propulsion_order(); static void check_stop_propulsion_order(); diff --git a/Core/Inc/state_machine.hpp b/Core/Inc/state_machine.hpp index 4384fa9..daa671d 100644 --- a/Core/Inc/state_machine.hpp +++ b/Core/Inc/state_machine.hpp @@ -35,14 +35,18 @@ class VCU_SM { GeneralStates::Connecting, GeneralStates::Operational, [&]() { return Comms::control_station_tcp->is_connected() && Comms::hvscu_tcp->is_connected() && - Comms::pcu_tcp->is_connected(); + Comms::pcu_tcp->is_connected() && + Comms::lcu_tcp->is_connected() && + Comms::bcu_tcp->is_connected(); }); GeneralStateMachine.add_transition( GeneralStates::Operational, GeneralStates::Fault, [&]() { return !Comms::control_station_tcp->is_connected() || !Comms::hvscu_tcp->is_connected() || - !Comms::pcu_tcp->is_connected(); + !Comms::pcu_tcp->is_connected() || + !Comms::lcu_tcp->is_connected() || + !Comms::bcu_tcp->is_connected(); }); GeneralStateMachine.add_transition( @@ -52,20 +56,20 @@ class VCU_SM { (!Comms::brakes->breaks_first_time)); }); - GeneralStateMachine.add_transition( + /* GeneralStateMachine.add_transition( GeneralStates::Connecting, GeneralStates::Fault, [&]() { return ((Comms::brakes->All_reeds && Comms::brakes->Active_brakes) && (!Comms::brakes->breaks_first_time)); - }); + }); */ GeneralStateMachine.add_transition( GeneralStates::Operational, GeneralStates::Fault, [&]() { return (!Comms::actuators->Sdc); }); - GeneralStateMachine.add_transition( + /* GeneralStateMachine.add_transition( GeneralStates::Connecting, GeneralStates::Fault, - [&]() { return (!Comms::actuators->Sdc); }); + [&]() { return (!Comms::actuators->Sdc); }); */ GeneralStateMachine.add_transition( GeneralStates::Operational, GeneralStates::Fault, [&]() { @@ -86,6 +90,7 @@ class VCU_SM { GeneralStateMachine.add_enter_action( [&]() { Comms::leds->leds_fault(); + Time::set_timeout(100, [&]() { Comms::brakes->brake(); }); }, GeneralStates::Fault); @@ -150,13 +155,21 @@ class VCU_SM { GeneralStateMachine.add_low_precision_cyclic_action( [&]() { - if(!Comms::hvscu_tcp->is_connected()){ + if (!Comms::hvscu_tcp->is_connected()) { Comms::hvscu_tcp->reconnect(); } - if(!Comms::pcu_tcp->is_connected()){ + if (!Comms::pcu_tcp->is_connected()) { Comms::pcu_tcp->reconnect(); } + + if (!Comms::lcu_tcp->is_connected()) { + Comms::lcu_tcp->reconnect(); + } + + if (!Comms::bcu_tcp->is_connected()) { + Comms::bcu_tcp->reconnect(); + } }, 100ms, GeneralStates::Connecting); } diff --git a/Core/Src/Runes/generated_metadata.cpp b/Core/Src/Runes/generated_metadata.cpp index c136403..e65e7c3 100644 --- a/Core/Src/Runes/generated_metadata.cpp +++ b/Core/Src/Runes/generated_metadata.cpp @@ -5,11 +5,11 @@ extern "C"{ const char DESCRIPTION[255] __attribute__((section(".metadata_pool")))= "****************" // placeholder for beggining - "20250709T221517" // DateTime using ISO-8601 format + "20250712T120745" // DateTime using ISO-8601 format " " // alignment "f8e647d0" // STLIB commit "--------" // ADJ commit - "d2416c36" // Board commit + "143d17cf" // Board commit // the '=' is used for unparsing ; } \ No newline at end of file diff --git a/Core/Src/VCU/Actuators.cpp b/Core/Src/VCU/Actuators.cpp index cb8db42..a4ab22a 100644 --- a/Core/Src/VCU/Actuators.cpp +++ b/Core/Src/VCU/Actuators.cpp @@ -9,31 +9,35 @@ Actuators::Actuators() { Regulator_in1 = DigitalSensor(REGULATOR_1_IN_PIN, ®ulator_1_input); Regulator_in2 = DigitalSensor(REGULATOR_2_IN_PIN, ®ulator_2_input); - Pump_c1 = PWM(PUMP_C1_PIN); - Pump_c2 = PWM(PUMP_C2_PIN); + /* Pump_c1 = PWM(PUMP_C1_PIN); + Pump_c2 = PWM(PUMP_C2_PIN); */ + + Pump_c1 = DigitalOutput(PUMP_C1_PIN); + Pump_c2 = DigitalOutput(PUMP_C2_PIN); flow1_input = DigitalSensor(FLOW1_PIN, &flow1); flow2_input = DigitalSensor(FLOW2_PIN, &flow2); SDC_sensor = DigitalSensor(SDC_PIN, &Sdc); PresionAlta = - LinearSensor(PRESION_ALTA_PIN, 0.00763, 0.318, &pressure_1); - PresionRegulador = - LinearSensor(PRESION_REGULADOR_PIN, 0.256, 0.375, &pressure_2); + LinearSensor(PRESION_ALTA_PIN, 0.00763, 0.318, &pressure_high); + PresionRegulador = LinearSensor(PRESION_REGULADOR_PIN, 0.256, 0.375, + &pressure_regulator); PresionFrenos = - LinearSensor(PRESION_FRENOS_PIN, 0.256, 0.375, &pressure_3); - PresionCapsula = LinearSensor( - PRESION_CAPSULA_PIN, 1, 0, &pressure_4); // Falta por caracterizar + LinearSensor(PRESION_FRENOS_PIN, 0.256, 0.375, &pressure_brakes); + PresionCapsula = + LinearSensor(PRESION_CAPSULA_PIN, 1, 0, + &pressure_capsule); // Falta por caracterizar } void Actuators::init() { - Pump_c1.set_frequency(10000); + /* Pump_c1.set_frequency(1); Pump_c1.set_duty_cycle(0); - Pump_c1.turn_on(); + Pump_c1.turn_on(); */ - Pump_c2.set_frequency(10000); + /* Pump_c2.set_frequency(1); Pump_c2.set_duty_cycle(0); - Pump_c2.turn_on(); + Pump_c2.turn_on(); */ Regulator_out1.set_frequency(30000); Regulator_out1.set_duty_cycle(0); @@ -45,9 +49,7 @@ void Actuators::init() { } void Actuators::set_regulator_1(float value) { - float regulator_value = static_cast( - value * (100.0 / 6)); // FAlta hacer la caracterizacion pero echale que - // al 100 deberia sacar 6 bares + float regulator_value = static_cast(value * (100.0 / 6)); Regulator_out1.set_duty_cycle(regulator_value); } @@ -56,8 +58,25 @@ void Actuators::set_regulator_2(float value) { Regulator_out2.set_duty_cycle(regulator_value); } -void Actuators::set_pump_1(uint32_t value) { Pump_c1.set_duty_cycle(value); } -void Actuators::set_pump_2(uint32_t value) { Pump_c2.set_duty_cycle(value); } +/* void Actuators::set_pump_1(uint32_t value) { Pump_c1.set_duty_cycle(value); } +void Actuators::set_pump_2(uint32_t value) { Pump_c2.set_duty_cycle(value); } */ + +void Actuators::set_pump_1(uint32_t value) { + if (value == 100) { + Pump_c1.turn_on(); + } else { + Pump_c1.turn_off(); + } + // Pump_c1.set_duty_cycle(value); +} +void Actuators::set_pump_2(uint32_t value) { + if (value == 100) { + Pump_c2.turn_on(); + } else { + Pump_c2.turn_off(); + // Pump_c2.set_duty_cycle(value); + } +} void Actuators::read_regulators() { Regulator_in1.read(); diff --git a/Core/Src/VCU/Brakes.cpp b/Core/Src/VCU/Brakes.cpp index 276335c..5f81624 100644 --- a/Core/Src/VCU/Brakes.cpp +++ b/Core/Src/VCU/Brakes.cpp @@ -8,10 +8,6 @@ Brakes::Brakes(){ reed2_input = DigitalSensor(REED2_PIN, &reed2); reed3_input = DigitalSensor(REED3_PIN, &reed3); reed4_input = DigitalSensor(REED4_PIN, &reed4); - reed5_input = DigitalSensor(REED5_PIN, &reed5); - reed6_input = DigitalSensor(REED6_PIN, &reed6); - reed7_input = DigitalSensor(REED7_PIN, &reed7); - reed8_input = DigitalSensor(REED8_PIN, &reed8); // Tape_input = DigitalSensor(TAPE_INPUT_PIN, &Tape_state); tape_enable_output = DigitalOutput(TAPE_E_PIN); @@ -20,8 +16,8 @@ Brakes::Brakes(){ } void Brakes::init(){ - Actuator_out.turn_on(); - tape_enable_output.turn_on();//Hardcodeado para desabilitar tapes creo, o al reves xd + Actuator_out.turn_off(); + tape_enable_output.turn_on(); } void Brakes::brake(){ @@ -45,19 +41,11 @@ void Brakes::read_reeds() { reed2_input.read(); reed3_input.read(); reed4_input.read(); - reed5_input.read(); - reed6_input.read(); - reed7_input.read(); - reed8_input.read(); All_reeds = reed1 && reed2 && reed3 && - reed4 && - reed5 && - reed6 && - reed7 && - reed8; + reed4; } void Brakes::read_tape_emergency() { diff --git a/Core/Src/VCU/Comms/Comms.cpp b/Core/Src/VCU/Comms/Comms.cpp index f99d497..568e803 100644 --- a/Core/Src/VCU/Comms/Comms.cpp +++ b/Core/Src/VCU/Comms/Comms.cpp @@ -40,9 +40,9 @@ void Comms::on_Disable_tapes() { brakes->tape_enable_output.turn_off(); } -void Comms::brake_callback() { brakes->brake(); } +void Comms::brake_callback() { brake_flag = true; } -void Comms::unbrake_callback() { brakes->unbrake(); } +void Comms::unbrake_callback() { unbrake_flag = true; } void Comms::close_contactors_callback() { close_contactors_flag = true; } @@ -110,16 +110,16 @@ void Comms::add_packets() { VCU::general_state, VCU::operational_state); reeds = new HeapPacket(static_cast(Packets_id::Reeds), &brakes->reed1, &brakes->reed2, &brakes->reed3, - &brakes->reed4, &brakes->reed5, &brakes->reed6, - &brakes->reed7, &brakes->reed8, &brakes->All_reeds); + &brakes->reed4, &brakes->All_reeds); flow = new HeapPacket(static_cast(Packets_id::Flow), &actuators->flow1, &actuators->flow2); regulator = new HeapPacket(static_cast(Packets_id::Regulator), &actuators->regulator_1_pressure, &actuators->regulator_2_pressure); - pressure = new HeapPacket(static_cast(Packets_id::Pressure), - &actuators->pressure_1, &actuators->pressure_2, - &actuators->pressure_3, &actuators->pressure_4); + pressure = new HeapPacket( + static_cast(Packets_id::Pressure), &actuators->pressure_high, + &actuators->pressure_regulator, &actuators->pressure_brakes, + &actuators->pressure_capsule); tapes = new HeapPacket(static_cast(Packets_id::Tapes), &brakes->tape_enable_status, &brakes->tape_emergency); @@ -225,25 +225,27 @@ void Comms::add_orders() { remote_stop_motor = new HeapOrder( static_cast(External_ids::Stop_motor), &stop_motor_callback); - current_control = new HeapOrder( - static_cast(Orders_id::Current_control), - ¤t_control_callback, &modulation_frequency_2, &commutation_frequency_2, - &reference_current_2, &max_voltage_2, &motor_direction_2); + current_control = + new HeapOrder(static_cast(Orders_id::Current_control), + ¤t_control_callback, &modulation_frequency_2, + &commutation_frequency_2, &reference_current_2, + &max_voltage_2, &motor_direction_2); - remote_current_control = new HeapOrder( - static_cast(External_ids::Current_control), - ¤t_control_callback, &modulation_frequency_2, &commutation_frequency_2, - &reference_current_2, &max_voltage_2, &motor_direction_2); + remote_current_control = + new HeapOrder(static_cast(External_ids::Current_control), + ¤t_control_callback, &modulation_frequency_2, + &commutation_frequency_2, &reference_current_2, + &max_voltage_2, &motor_direction_2); speed_control = new HeapOrder( static_cast(Orders_id::Speed_control), - &speed_control_callback, &reference_speed_3, - &commutation_frequency_3, &max_voltage_3, &motor_direction_3); + &speed_control_callback, &reference_speed_3, &commutation_frequency_3, + &max_voltage_3, &motor_direction_3); remote_speed_control = new HeapOrder( static_cast(External_ids::Speed_control), - &speed_control_callback, &reference_speed_3, - &commutation_frequency_3, &max_voltage_3, &motor_direction_3); + &speed_control_callback, &reference_speed_3, &commutation_frequency_3, + &max_voltage_3, &motor_direction_3); motor_brake = new HeapOrder(static_cast(Orders_id::Motor_brake), &motor_brake_callback); @@ -280,6 +282,7 @@ void Comms::send_packets() { control_station_udp->send_packet(*regulator); control_station_udp->send_packet(*pressure); control_station_udp->send_packet(*tapes); + control_station_udp->send_packet(*sdc); } void Comms::read_sensors() { @@ -334,10 +337,12 @@ void Comms::check_open_contactors_order() { timeout_id = -1; actuators->contactors_closed = false; open_contactors_flag = false; + brakes->brake(); // Ensure brakes are applied when contactors + // are open open_contactors_sent = false; } else { if (timeout_id == -1) { - timeout_id = Time::set_timeout(6000, [&]() { + timeout_id = Time::set_timeout(100, [&]() { ProtectionManager::fault_and_propagate(); }); } @@ -346,6 +351,33 @@ void Comms::check_open_contactors_order() { } } +void Comms::check_brake_order() { + if (brake_flag) { + if (*VCU::operational_state == VCU_SM::OperationalStates::Ready || + *VCU::operational_state == + VCU_SM::OperationalStates::Demonstration) { + brakes->brake(); + actuators->set_regulator_1(0); + brake_flag = false; + } else { + WARNING("Cannot brake in this state"); + brake_flag = false; + } + } +} + +void Comms::check_unbrake_order() { + if (unbrake_flag) { + if (*VCU::operational_state == VCU_SM::OperationalStates::Energized) { + brakes->unbrake(); + unbrake_flag = false; + } else { + WARNING("Cannot unbrake in this state"); + unbrake_flag = false; + } + } +} + void Comms::check_propulsion_order() { if (propulsion_flag) { if (*VCU::operational_state == VCU_SM::OperationalStates::Ready || @@ -747,6 +779,9 @@ void Comms::check_orders() { check_close_contactors_order(); check_open_contactors_order(); + check_brake_order(); + check_unbrake_order(); + check_levitation_order(); check_stop_levitation_order(); @@ -761,7 +796,4 @@ void Comms::check_orders() { check_motor_brake_order(); } -void Comms::update() { - check_orders(); - read_sensors(); -} \ No newline at end of file +void Comms::update() { check_orders(); } \ No newline at end of file diff --git a/Core/Src/VCU/VCU.cpp b/Core/Src/VCU/VCU.cpp index 23163c3..e49cd0f 100644 --- a/Core/Src/VCU/VCU.cpp +++ b/Core/Src/VCU/VCU.cpp @@ -29,6 +29,7 @@ void VCU::send_packets(){ void VCU::read_sensors(){ if(Comms::reading_sensors){ + Comms::read_sensors(); Comms::reading_sensors = false; } } From 257fc32d7fe6b122be79dbb593974e230aaa228f Mon Sep 17 00:00:00 2001 From: jdmarmen Date: Mon, 14 Jul 2025 19:51:32 +0200 Subject: [PATCH 14/25] Works no sense xd --- Core/Inc/VCU/Comms/Comms.hpp | 2 +- Core/Inc/state_machine.hpp | 35 +++++++++++++++++++++------ Core/Src/Runes/generated_metadata.cpp | 4 +-- Core/Src/VCU/Comms/Comms.cpp | 22 ++++++++--------- Core/Src/VCU/VCU.cpp | 2 +- Core/Src/main.cpp | 3 ++- 6 files changed, 45 insertions(+), 23 deletions(-) diff --git a/Core/Inc/VCU/Comms/Comms.hpp b/Core/Inc/VCU/Comms/Comms.hpp index f0f9d20..ae297af 100644 --- a/Core/Inc/VCU/Comms/Comms.hpp +++ b/Core/Inc/VCU/Comms/Comms.hpp @@ -221,7 +221,7 @@ class Comms { static constexpr std::string VCU_IP = "192.168.1.3"; static constexpr std::string PCU_IP = "192.168.1.5"; static constexpr std::string HVSCU_IP = "192.168.1.7"; - static constexpr std::string BMSL_IP = "192.168.1.3"; + static constexpr std::string BMSL_IP = "192.168.1.254"; static constexpr std::string LCU_IP = "192.168.1.4"; static constexpr std::string BCU_IP = "192.168.2.17"; static constexpr std::string BLCU_IP = "192.168.1.3"; diff --git a/Core/Inc/state_machine.hpp b/Core/Inc/state_machine.hpp index daa671d..9995b90 100644 --- a/Core/Inc/state_machine.hpp +++ b/Core/Inc/state_machine.hpp @@ -5,6 +5,8 @@ using namespace std::chrono_literals; class VCU_SM { + static inline bool bmsl_socket_created = false; + public: enum GeneralStates { Connecting, @@ -33,28 +35,32 @@ class VCU_SM { GeneralStateMachine.add_transition( GeneralStates::Connecting, GeneralStates::Operational, [&]() { - return Comms::control_station_tcp->is_connected() && + return Comms::control_station_tcp->is_connected() /* && Comms::hvscu_tcp->is_connected() && Comms::pcu_tcp->is_connected() && Comms::lcu_tcp->is_connected() && - Comms::bcu_tcp->is_connected(); + Comms::bcu_tcp->is_connected() */ + && Comms::bmsl_tcp->is_connected(); }); GeneralStateMachine.add_transition( GeneralStates::Operational, GeneralStates::Fault, [&]() { - return !Comms::control_station_tcp->is_connected() || + return !Comms::control_station_tcp->is_connected() /* || !Comms::hvscu_tcp->is_connected() || !Comms::pcu_tcp->is_connected() || !Comms::lcu_tcp->is_connected() || - !Comms::bcu_tcp->is_connected(); + !Comms::bcu_tcp->is_connected() */ + || !Comms::bmsl_tcp->is_connected(); }); - GeneralStateMachine.add_transition( + // CHECK THIS!!!!!!!!! + + /* GeneralStateMachine.add_transition( GeneralStates::Operational, GeneralStates::Fault, [&]() { return ((Comms::brakes->All_reeds && Comms::brakes->Active_brakes) && (!Comms::brakes->breaks_first_time)); - }); + }); */ /* GeneralStateMachine.add_transition( GeneralStates::Connecting, GeneralStates::Fault, [&]() { @@ -155,7 +161,7 @@ class VCU_SM { GeneralStateMachine.add_low_precision_cyclic_action( [&]() { - if (!Comms::hvscu_tcp->is_connected()) { + /* if (!Comms::hvscu_tcp->is_connected()) { Comms::hvscu_tcp->reconnect(); } @@ -169,6 +175,21 @@ class VCU_SM { if (!Comms::bcu_tcp->is_connected()) { Comms::bcu_tcp->reconnect(); + } */ + + if (Comms::control_station_tcp->is_connected()) { + if (!bmsl_socket_created) { + Comms::bmsl_tcp = + new Socket(IPV4("192.168.1.3"), 50503, + IPV4("192.168.1.254"), 50499); + bmsl_socket_created = true; + } + + if(bmsl_socket_created) + Comms::bmsl_tcp->reconnect(); + + } else { + return; } }, 100ms, GeneralStates::Connecting); diff --git a/Core/Src/Runes/generated_metadata.cpp b/Core/Src/Runes/generated_metadata.cpp index e65e7c3..1160fed 100644 --- a/Core/Src/Runes/generated_metadata.cpp +++ b/Core/Src/Runes/generated_metadata.cpp @@ -5,11 +5,11 @@ extern "C"{ const char DESCRIPTION[255] __attribute__((section(".metadata_pool")))= "****************" // placeholder for beggining - "20250712T120745" // DateTime using ISO-8601 format + "20250714T194856" // DateTime using ISO-8601 format " " // alignment "f8e647d0" // STLIB commit "--------" // ADJ commit - "143d17cf" // Board commit + "4d8ba73d" // Board commit // the '=' is used for unparsing ; } \ No newline at end of file diff --git a/Core/Src/VCU/Comms/Comms.cpp b/Core/Src/VCU/Comms/Comms.cpp index 568e803..9ddad11 100644 --- a/Core/Src/VCU/Comms/Comms.cpp +++ b/Core/Src/VCU/Comms/Comms.cpp @@ -82,7 +82,7 @@ void Comms::start() { new DatagramSocket(IPV4(VCU_IP), CONTROL_STATION_UDP_PORT, IPV4(CONTROL_STATION_IP), CONTROL_STATION_UDP_PORT); - lcu_tcp = new Socket(IPV4(VCU_IP), LCU_PORT, IPV4(LCU_IP), REMOTE_PORT); + /* lcu_tcp = new Socket(IPV4(VCU_IP), LCU_PORT, IPV4(LCU_IP), REMOTE_PORT); lcu_udp = new DatagramSocket(IPV4(VCU_IP), LCU_UDP_PORT, IPV4(LCU_IP), LCU_UDP_PORT); @@ -99,7 +99,7 @@ void Comms::start() { pcu_tcp = new Socket(IPV4(VCU_IP), PCU_PORT, IPV4(PCU_IP), REMOTE_PORT); pcu_udp = new DatagramSocket(IPV4(VCU_IP), PCU_UDP_PORT, IPV4(PCU_IP), - PCU_UDP_PORT); + PCU_UDP_PORT); */ add_packets(); add_orders(); @@ -296,7 +296,7 @@ void Comms::read_sensors() { void Comms::check_close_contactors_order() { if (close_contactors_flag) { - if (*VCU::operational_state == VCU_SM::OperationalStates::Idle) { + /* if (*VCU::operational_state == VCU_SM::OperationalStates::Idle) { static uint8_t timeout_id = -1; if (!close_contactors_sent) { hvscu_tcp->send_order(*remote_close_contactors); @@ -305,10 +305,10 @@ void Comms::check_close_contactors_order() { if (hvscu_state == static_cast(HVSCU_states::HVSCU_Closed)) { Time::cancel_timeout(timeout_id); - timeout_id = -1; + timeout_id = -1; */ actuators->contactors_closed = true; close_contactors_flag = false; - close_contactors_sent = false; + /* close_contactors_sent = false; } else { if (timeout_id == -1) { timeout_id = Time::set_timeout(6000, [&]() { @@ -316,17 +316,17 @@ void Comms::check_close_contactors_order() { }); } } - } + } */ } else { WARNING("Cannot close contactors in this state"); close_contactors_flag = false; } } -} +/* } */ void Comms::check_open_contactors_order() { if (open_contactors_flag) { - static uint8_t timeout_id = -1; + /* static uint8_t timeout_id = -1; if (!open_contactors_sent) { hvscu_tcp->send_order(*remote_open_contactors); open_contactors_sent = true; @@ -334,12 +334,12 @@ void Comms::check_open_contactors_order() { if (hvscu_state == static_cast(HVSCU_states::HVSCU_Opened)) { Time::cancel_timeout(timeout_id); - timeout_id = -1; + timeout_id = -1; */ actuators->contactors_closed = false; open_contactors_flag = false; brakes->brake(); // Ensure brakes are applied when contactors // are open - open_contactors_sent = false; + /* open_contactors_sent = false; } else { if (timeout_id == -1) { timeout_id = Time::set_timeout(100, [&]() { @@ -347,7 +347,7 @@ void Comms::check_open_contactors_order() { }); } } - } + } */ } } diff --git a/Core/Src/VCU/VCU.cpp b/Core/Src/VCU/VCU.cpp index e49cd0f..486dd7f 100644 --- a/Core/Src/VCU/VCU.cpp +++ b/Core/Src/VCU/VCU.cpp @@ -39,7 +39,7 @@ void VCU::update(){ state_machine->GeneralStateMachine.check_transitions(); state_machine->OperationalStateMachine.check_transitions(); - Comms::update(); + //Comms::update(); read_sensors(); send_packets(); ProtectionManager::check_protections(); diff --git a/Core/Src/main.cpp b/Core/Src/main.cpp index efed533..82168d6 100644 --- a/Core/Src/main.cpp +++ b/Core/Src/main.cpp @@ -10,8 +10,9 @@ int main(void) { VCU::init(); - STLIB::start("00:00:00:00:01:03","192.168.1.3", "255.255.0.0"); + STLIB::start("00:00:00:00:01:03","192.168.1.3", "255.255.255.0"); VCU::start(); + //HAL_Delay(200); while (1) { STLIB::update(); From a633ebc99dacd94b6e1b82d54f3c863baa26b085 Mon Sep 17 00:00:00 2001 From: jdmarmen Date: Wed, 16 Jul 2025 00:26:10 +0200 Subject: [PATCH 15/25] We are gonna pass electrical lfg --- Core/Inc/Code_generation/JSON_ADE | 2 +- Core/Inc/VCU/VCU.hpp | 1 + Core/Inc/state_machine.hpp | 80 ++++++++++++++++----------- Core/Src/Runes/generated_metadata.cpp | 4 +- Core/Src/VCU/Comms/Comms.cpp | 49 ++++++++-------- Core/Src/VCU/VCU.cpp | 28 +++++----- Core/Src/main.cpp | 6 +- 7 files changed, 95 insertions(+), 75 deletions(-) diff --git a/Core/Inc/Code_generation/JSON_ADE b/Core/Inc/Code_generation/JSON_ADE index 4a995c3..3527242 160000 --- a/Core/Inc/Code_generation/JSON_ADE +++ b/Core/Inc/Code_generation/JSON_ADE @@ -1 +1 @@ -Subproject commit 4a995c314959d4eb38e1b107441f32f40d0b6a46 +Subproject commit 352724222f8c34056d14750841a9e7a990c60091 diff --git a/Core/Inc/VCU/VCU.hpp b/Core/Inc/VCU/VCU.hpp index 5b9b334..c705bbc 100644 --- a/Core/Inc/VCU/VCU.hpp +++ b/Core/Inc/VCU/VCU.hpp @@ -11,6 +11,7 @@ class VCU { static inline uint8_t* general_state{}; static inline uint8_t* operational_state{}; + static void init(); static void start(); static void update(); diff --git a/Core/Inc/state_machine.hpp b/Core/Inc/state_machine.hpp index 9995b90..30644c9 100644 --- a/Core/Inc/state_machine.hpp +++ b/Core/Inc/state_machine.hpp @@ -8,6 +8,8 @@ class VCU_SM { static inline bool bmsl_socket_created = false; public: + static inline bool tetas = false; + enum GeneralStates { Connecting, Operational, @@ -35,22 +37,24 @@ class VCU_SM { GeneralStateMachine.add_transition( GeneralStates::Connecting, GeneralStates::Operational, [&]() { - return Comms::control_station_tcp->is_connected() /* && - Comms::hvscu_tcp->is_connected() && - Comms::pcu_tcp->is_connected() && - Comms::lcu_tcp->is_connected() && - Comms::bcu_tcp->is_connected() */ - && Comms::bmsl_tcp->is_connected(); + return Comms::control_station_tcp->is_connected() && + Comms::bmsl_tcp->is_connected() && + Comms::hvscu_tcp->is_connected() /* && + Comms::pcu_tcp->is_connected() && + Comms::lcu_tcp->is_connected() && + Comms::bcu_tcp->is_connected() */ + ; }); GeneralStateMachine.add_transition( GeneralStates::Operational, GeneralStates::Fault, [&]() { - return !Comms::control_station_tcp->is_connected() /* || + return !Comms::control_station_tcp->is_connected() || !Comms::hvscu_tcp->is_connected() || + !Comms::bmsl_tcp->is_connected() /* || !Comms::pcu_tcp->is_connected() || !Comms::lcu_tcp->is_connected() || !Comms::bcu_tcp->is_connected() */ - || !Comms::bmsl_tcp->is_connected(); + ; }); // CHECK THIS!!!!!!!!! @@ -101,6 +105,12 @@ class VCU_SM { }, GeneralStates::Fault); + OperationalStateMachine.add_enter_action( + [&]() { Comms::on_Enable_tapes(); }, OperationalStates::Ready); + + OperationalStateMachine.add_exit_action( + [&]() { Comms::on_Disable_tapes(); }, OperationalStates::Ready); + //----------- OperationalStateMachine.add_transition( @@ -159,37 +169,41 @@ class VCU_SM { [&]() { Comms::reading_sensors = true; }, 100ms, GeneralStates::Fault); - GeneralStateMachine.add_low_precision_cyclic_action( + /* GeneralStateMachine.add_low_precision_cyclic_action( [&]() { - /* if (!Comms::hvscu_tcp->is_connected()) { - Comms::hvscu_tcp->reconnect(); - } - - if (!Comms::pcu_tcp->is_connected()) { - Comms::pcu_tcp->reconnect(); - } - - if (!Comms::lcu_tcp->is_connected()) { - Comms::lcu_tcp->reconnect(); - } - - if (!Comms::bcu_tcp->is_connected()) { - Comms::bcu_tcp->reconnect(); - } */ + if (!tetas) { + Time::set_timeout(3000, [&]() { + if (!Comms::bmsl_tcp->is_connected()) { + Comms::bmsl_tcp->reconnect(); + } + + if (!Comms::hvscu_tcp->is_connected()) { + Comms::hvscu_tcp->reconnect(); + } + tetas = true; + }); + } else { + if (!Comms::bmsl_tcp->is_connected()) { + Comms::bmsl_tcp->reconnect(); + } - if (Comms::control_station_tcp->is_connected()) { - if (!bmsl_socket_created) { - Comms::bmsl_tcp = - new Socket(IPV4("192.168.1.3"), 50503, - IPV4("192.168.1.254"), 50499); - bmsl_socket_created = true; + if (!Comms::hvscu_tcp->is_connected()) { + Comms::hvscu_tcp->reconnect(); } + } + }, + 100ms, GeneralStates::Connecting); */ - if(bmsl_socket_created) + GeneralStateMachine.add_low_precision_cyclic_action( + [&]() { + if (tetas) { + if (!Comms::bmsl_tcp->is_connected()) { Comms::bmsl_tcp->reconnect(); + } - } else { - return; + if (!Comms::hvscu_tcp->is_connected()) { + Comms::hvscu_tcp->reconnect(); + } } }, 100ms, GeneralStates::Connecting); diff --git a/Core/Src/Runes/generated_metadata.cpp b/Core/Src/Runes/generated_metadata.cpp index 1160fed..f7b1250 100644 --- a/Core/Src/Runes/generated_metadata.cpp +++ b/Core/Src/Runes/generated_metadata.cpp @@ -5,11 +5,11 @@ extern "C"{ const char DESCRIPTION[255] __attribute__((section(".metadata_pool")))= "****************" // placeholder for beggining - "20250714T194856" // DateTime using ISO-8601 format + "20250716T002158" // DateTime using ISO-8601 format " " // alignment "f8e647d0" // STLIB commit "--------" // ADJ commit - "4d8ba73d" // Board commit + "257fc32d" // Board commit // the '=' is used for unparsing ; } \ No newline at end of file diff --git a/Core/Src/VCU/Comms/Comms.cpp b/Core/Src/VCU/Comms/Comms.cpp index 9ddad11..39a28c9 100644 --- a/Core/Src/VCU/Comms/Comms.cpp +++ b/Core/Src/VCU/Comms/Comms.cpp @@ -15,8 +15,6 @@ void Comms::on_potencia_refri() { void Comms::on_Set_regulator() { if (actuators->selected_regulator_pressure > 6) { actuators->selected_regulator_pressure = 6; - // InfoWarning::InfoWarningTrigger("Cannot exceed 6 bar on regulator - // pressure"); } if (actuators->selected_regulator == Actuators::Regulator::REGULATOR_1) { actuators->regulator_1_pressure = @@ -82,10 +80,7 @@ void Comms::start() { new DatagramSocket(IPV4(VCU_IP), CONTROL_STATION_UDP_PORT, IPV4(CONTROL_STATION_IP), CONTROL_STATION_UDP_PORT); - /* lcu_tcp = new Socket(IPV4(VCU_IP), LCU_PORT, IPV4(LCU_IP), REMOTE_PORT); - - lcu_udp = new DatagramSocket(IPV4(VCU_IP), LCU_UDP_PORT, IPV4(LCU_IP), - LCU_UDP_PORT); + bmsl_tcp = new Socket(IPV4(VCU_IP), BMSL_PORT, IPV4(BMSL_IP), REMOTE_PORT); hvscu_tcp = new Socket(IPV4(VCU_IP), HVSCU_PORT, IPV4(HVSCU_IP), REMOTE_PORT); @@ -93,6 +88,11 @@ void Comms::start() { hvscu_udp = new DatagramSocket(IPV4(VCU_IP), HVSCU_UDP_PORT, IPV4(HVSCU_IP), HVSCU_UDP_PORT); + /* lcu_tcp = new Socket(IPV4(VCU_IP), LCU_PORT, IPV4(LCU_IP), REMOTE_PORT); + + lcu_udp = new DatagramSocket(IPV4(VCU_IP), LCU_UDP_PORT, IPV4(LCU_IP), + LCU_UDP_PORT); + bcu_tcp = new Socket(IPV4(VCU_IP), BCU_PORT, IPV4(BCU_IP), JUANS_REMOTE_PORT); @@ -276,13 +276,15 @@ void Comms::add_orders() { } void Comms::send_packets() { - control_station_udp->send_packet(*states); - control_station_udp->send_packet(*reeds); - control_station_udp->send_packet(*flow); - control_station_udp->send_packet(*regulator); - control_station_udp->send_packet(*pressure); - control_station_udp->send_packet(*tapes); - control_station_udp->send_packet(*sdc); + Time::set_timeout(3000, [&]() { + control_station_udp->send_packet(*states); + control_station_udp->send_packet(*reeds); + control_station_udp->send_packet(*flow); + control_station_udp->send_packet(*regulator); + control_station_udp->send_packet(*pressure); + control_station_udp->send_packet(*tapes); + control_station_udp->send_packet(*sdc); + }); } void Comms::read_sensors() { @@ -296,7 +298,7 @@ void Comms::read_sensors() { void Comms::check_close_contactors_order() { if (close_contactors_flag) { - /* if (*VCU::operational_state == VCU_SM::OperationalStates::Idle) { + if (*VCU::operational_state == VCU_SM::OperationalStates::Idle) { static uint8_t timeout_id = -1; if (!close_contactors_sent) { hvscu_tcp->send_order(*remote_close_contactors); @@ -305,10 +307,10 @@ void Comms::check_close_contactors_order() { if (hvscu_state == static_cast(HVSCU_states::HVSCU_Closed)) { Time::cancel_timeout(timeout_id); - timeout_id = -1; */ + timeout_id = -1; actuators->contactors_closed = true; close_contactors_flag = false; - /* close_contactors_sent = false; + close_contactors_sent = false; } else { if (timeout_id == -1) { timeout_id = Time::set_timeout(6000, [&]() { @@ -316,17 +318,17 @@ void Comms::check_close_contactors_order() { }); } } - } */ + } } else { WARNING("Cannot close contactors in this state"); close_contactors_flag = false; } } -/* } */ +} void Comms::check_open_contactors_order() { if (open_contactors_flag) { - /* static uint8_t timeout_id = -1; + static uint8_t timeout_id = -1; if (!open_contactors_sent) { hvscu_tcp->send_order(*remote_open_contactors); open_contactors_sent = true; @@ -334,12 +336,10 @@ void Comms::check_open_contactors_order() { if (hvscu_state == static_cast(HVSCU_states::HVSCU_Opened)) { Time::cancel_timeout(timeout_id); - timeout_id = -1; */ + timeout_id = -1; actuators->contactors_closed = false; open_contactors_flag = false; - brakes->brake(); // Ensure brakes are applied when contactors - // are open - /* open_contactors_sent = false; + open_contactors_sent = false; } else { if (timeout_id == -1) { timeout_id = Time::set_timeout(100, [&]() { @@ -347,7 +347,7 @@ void Comms::check_open_contactors_order() { }); } } - } */ + } } } @@ -369,6 +369,7 @@ void Comms::check_brake_order() { void Comms::check_unbrake_order() { if (unbrake_flag) { if (*VCU::operational_state == VCU_SM::OperationalStates::Energized) { + // actuators->set_regulator_1(6); brakes->unbrake(); unbrake_flag = false; } else { diff --git a/Core/Src/VCU/VCU.cpp b/Core/Src/VCU/VCU.cpp index 486dd7f..ad7eaaf 100644 --- a/Core/Src/VCU/VCU.cpp +++ b/Core/Src/VCU/VCU.cpp @@ -1,45 +1,47 @@ #include "VCU/VCU.hpp" -void VCU::init(){ +void VCU::init() { state_machine = new VCU_SM(); general_state = &state_machine->GeneralStateMachine.current_state; operational_state = &state_machine->OperationalStateMachine.current_state; ProtectionManager::add_standard_protections(); ProtectionManager::initialize(); - ProtectionManager::link_state_machine(state_machine->GeneralStateMachine, VCU_SM::GeneralStates::Fault); - ProtectionManager::set_id(Boards::ID::VCU); + ProtectionManager::link_state_machine(state_machine->GeneralStateMachine, + VCU_SM::GeneralStates::Fault); + ProtectionManager::set_id(Boards::ID::VCU); Comms::init(); } -void VCU::start(){ +void VCU::start() { Comms::actuators->init(); Comms::brakes->init(); Comms::start(); } -void VCU::send_packets(){ - if(Comms::packet_sending){ +void VCU::send_packets() { + if (Comms::packet_sending) { Comms::send_packets(); Comms::packet_sending = false; } } -void VCU::read_sensors(){ - if(Comms::reading_sensors){ +void VCU::read_sensors() { + if (Comms::reading_sensors) { Comms::read_sensors(); Comms::reading_sensors = false; } } -void VCU::update(){ - - state_machine->GeneralStateMachine.check_transitions(); - state_machine->OperationalStateMachine.check_transitions(); +void VCU::update() { + if (state_machine->tetas) { + state_machine->GeneralStateMachine.check_transitions(); + state_machine->OperationalStateMachine.check_transitions(); + } - //Comms::update(); + Comms::update(); read_sensors(); send_packets(); ProtectionManager::check_protections(); diff --git a/Core/Src/main.cpp b/Core/Src/main.cpp index 82168d6..bbd02f8 100644 --- a/Core/Src/main.cpp +++ b/Core/Src/main.cpp @@ -11,8 +11,10 @@ int main(void) { VCU::init(); STLIB::start("00:00:00:00:01:03","192.168.1.3", "255.255.255.0"); - VCU::start(); - //HAL_Delay(200); + Time::set_timeout(2000, []() { + VCU::start(); + VCU::state_machine->tetas = true; + }); while (1) { STLIB::update(); From 8397cdfad38585cdd48ce30537c994550e894715 Mon Sep 17 00:00:00 2001 From: jdmarmen Date: Wed, 16 Jul 2025 12:56:32 +0200 Subject: [PATCH 16/25] added pcu --- Core/Inc/Code_generation/JSON_ADE | 2 +- Core/Inc/state_machine.hpp | 17 ++++++++++------- Core/Src/Runes/generated_metadata.cpp | 4 ++-- Core/Src/VCU/Comms/Comms.cpp | 4 ++-- 4 files changed, 15 insertions(+), 12 deletions(-) diff --git a/Core/Inc/Code_generation/JSON_ADE b/Core/Inc/Code_generation/JSON_ADE index 3527242..61a4e14 160000 --- a/Core/Inc/Code_generation/JSON_ADE +++ b/Core/Inc/Code_generation/JSON_ADE @@ -1 +1 @@ -Subproject commit 352724222f8c34056d14750841a9e7a990c60091 +Subproject commit 61a4e14b221392db7ee975cf6b30cc373b76b278 diff --git a/Core/Inc/state_machine.hpp b/Core/Inc/state_machine.hpp index 30644c9..56452ab 100644 --- a/Core/Inc/state_machine.hpp +++ b/Core/Inc/state_machine.hpp @@ -39,19 +39,18 @@ class VCU_SM { GeneralStates::Connecting, GeneralStates::Operational, [&]() { return Comms::control_station_tcp->is_connected() && Comms::bmsl_tcp->is_connected() && - Comms::hvscu_tcp->is_connected() /* && - Comms::pcu_tcp->is_connected() && - Comms::lcu_tcp->is_connected() && - Comms::bcu_tcp->is_connected() */ - ; + Comms::hvscu_tcp->is_connected() && + Comms::pcu_tcp->is_connected() /* && + Comms::lcu_tcp->is_connected() && + Comms::bcu_tcp->is_connected() */; }); GeneralStateMachine.add_transition( GeneralStates::Operational, GeneralStates::Fault, [&]() { return !Comms::control_station_tcp->is_connected() || !Comms::hvscu_tcp->is_connected() || - !Comms::bmsl_tcp->is_connected() /* || - !Comms::pcu_tcp->is_connected() || + !Comms::bmsl_tcp->is_connected() || + !Comms::pcu_tcp->is_connected() /* || !Comms::lcu_tcp->is_connected() || !Comms::bcu_tcp->is_connected() */ ; @@ -204,6 +203,10 @@ class VCU_SM { if (!Comms::hvscu_tcp->is_connected()) { Comms::hvscu_tcp->reconnect(); } + + if (!Comms::pcu_tcp->is_connected()) { + Comms::pcu_tcp->reconnect(); + } } }, 100ms, GeneralStates::Connecting); diff --git a/Core/Src/Runes/generated_metadata.cpp b/Core/Src/Runes/generated_metadata.cpp index f7b1250..780a540 100644 --- a/Core/Src/Runes/generated_metadata.cpp +++ b/Core/Src/Runes/generated_metadata.cpp @@ -5,11 +5,11 @@ extern "C"{ const char DESCRIPTION[255] __attribute__((section(".metadata_pool")))= "****************" // placeholder for beggining - "20250716T002158" // DateTime using ISO-8601 format + "20250716T093528" // DateTime using ISO-8601 format " " // alignment "f8e647d0" // STLIB commit "--------" // ADJ commit - "257fc32d" // Board commit + "a633ebc9" // Board commit // the '=' is used for unparsing ; } \ No newline at end of file diff --git a/Core/Src/VCU/Comms/Comms.cpp b/Core/Src/VCU/Comms/Comms.cpp index 39a28c9..971c8f2 100644 --- a/Core/Src/VCU/Comms/Comms.cpp +++ b/Core/Src/VCU/Comms/Comms.cpp @@ -95,11 +95,11 @@ void Comms::start() { bcu_tcp = new Socket(IPV4(VCU_IP), BCU_PORT, IPV4(BCU_IP), JUANS_REMOTE_PORT); - + */ pcu_tcp = new Socket(IPV4(VCU_IP), PCU_PORT, IPV4(PCU_IP), REMOTE_PORT); pcu_udp = new DatagramSocket(IPV4(VCU_IP), PCU_UDP_PORT, IPV4(PCU_IP), - PCU_UDP_PORT); */ + PCU_UDP_PORT); add_packets(); add_orders(); From 79b817581e2d4ebfdda031be28660aecb3f55373 Mon Sep 17 00:00:00 2001 From: jdmarmen Date: Thu, 17 Jul 2025 06:41:37 +0200 Subject: [PATCH 17/25] Added recovery state --- Core/Inc/state_machine.hpp | 66 ++++++++++++--------------- Core/Src/Runes/generated_metadata.cpp | 4 +- Core/Src/VCU/Brakes.cpp | 2 - Core/Src/VCU/Comms/Comms.cpp | 11 +++-- 4 files changed, 38 insertions(+), 45 deletions(-) diff --git a/Core/Inc/state_machine.hpp b/Core/Inc/state_machine.hpp index 56452ab..d612fde 100644 --- a/Core/Inc/state_machine.hpp +++ b/Core/Inc/state_machine.hpp @@ -16,7 +16,7 @@ class VCU_SM { Fault, }; - enum OperationalStates { Idle, EndOfRun, Energized, Ready, Demonstration }; + enum OperationalStates { Idle, EndOfRun, Energized, Ready, Demonstration, Recovery }; StateMachine GeneralStateMachine; StateMachine OperationalStateMachine; @@ -31,6 +31,7 @@ class VCU_SM { OperationalStateMachine.add_state(OperationalStates::Energized); OperationalStateMachine.add_state(OperationalStates::Ready); OperationalStateMachine.add_state(OperationalStates::Demonstration); + OperationalStateMachine.add_state(OperationalStates::Recovery); GeneralStateMachine.add_state_machine(OperationalStateMachine, GeneralStates::Operational); @@ -41,7 +42,7 @@ class VCU_SM { Comms::bmsl_tcp->is_connected() && Comms::hvscu_tcp->is_connected() && Comms::pcu_tcp->is_connected() /* && - Comms::lcu_tcp->is_connected() && + Comms::lcu_tcp->is_connected() */ /* && Comms::bcu_tcp->is_connected() */; }); @@ -51,26 +52,26 @@ class VCU_SM { !Comms::hvscu_tcp->is_connected() || !Comms::bmsl_tcp->is_connected() || !Comms::pcu_tcp->is_connected() /* || - !Comms::lcu_tcp->is_connected() || + !Comms::lcu_tcp->is_connected() */ /* || !Comms::bcu_tcp->is_connected() */ ; }); // CHECK THIS!!!!!!!!! - /* GeneralStateMachine.add_transition( + GeneralStateMachine.add_transition( GeneralStates::Operational, GeneralStates::Fault, [&]() { return ((Comms::brakes->All_reeds && - Comms::brakes->Active_brakes) && - (!Comms::brakes->breaks_first_time)); - }); */ + Comms::brakes->Active_brakes && + !Comms::brakes->breaks_first_time)); + }); - /* GeneralStateMachine.add_transition( + GeneralStateMachine.add_transition( GeneralStates::Connecting, GeneralStates::Fault, [&]() { return ((Comms::brakes->All_reeds && - Comms::brakes->Active_brakes) && - (!Comms::brakes->breaks_first_time)); - }); */ + Comms::brakes->Active_brakes && + !Comms::brakes->breaks_first_time)); + }); GeneralStateMachine.add_transition( GeneralStates::Operational, GeneralStates::Fault, @@ -110,12 +111,26 @@ class VCU_SM { OperationalStateMachine.add_exit_action( [&]() { Comms::on_Disable_tapes(); }, OperationalStates::Ready); + OperationalStateMachine.add_enter_action( + [&]() { Comms::on_Enable_tapes(); }, OperationalStates::Demonstration); + + OperationalStateMachine.add_exit_action( + [&]() { Comms::on_Disable_tapes(); }, OperationalStates::Demonstration); + + OperationalStateMachine.add_enter_action([](){ + Comms::on_Disable_tapes(); + }, OperationalStates::Recovery); + //----------- OperationalStateMachine.add_transition( OperationalStates::Idle, OperationalStates::Energized, [&]() { return Comms::actuators->contactors_closed; }); + OperationalStateMachine.add_transition(OperationalStates::Idle, OperationalStates::Recovery, [](){ + return Comms::brakes->tape_emergency == PinState::ON; + }); + OperationalStateMachine.add_transition( OperationalStates::Energized, OperationalStates::Idle, [&]() { return !Comms::actuators->contactors_closed; }); @@ -168,31 +183,6 @@ class VCU_SM { [&]() { Comms::reading_sensors = true; }, 100ms, GeneralStates::Fault); - /* GeneralStateMachine.add_low_precision_cyclic_action( - [&]() { - if (!tetas) { - Time::set_timeout(3000, [&]() { - if (!Comms::bmsl_tcp->is_connected()) { - Comms::bmsl_tcp->reconnect(); - } - - if (!Comms::hvscu_tcp->is_connected()) { - Comms::hvscu_tcp->reconnect(); - } - tetas = true; - }); - } else { - if (!Comms::bmsl_tcp->is_connected()) { - Comms::bmsl_tcp->reconnect(); - } - - if (!Comms::hvscu_tcp->is_connected()) { - Comms::hvscu_tcp->reconnect(); - } - } - }, - 100ms, GeneralStates::Connecting); */ - GeneralStateMachine.add_low_precision_cyclic_action( [&]() { if (tetas) { @@ -207,6 +197,10 @@ class VCU_SM { if (!Comms::pcu_tcp->is_connected()) { Comms::pcu_tcp->reconnect(); } + + /* if (!Comms::lcu_tcp->is_connected()) { + Comms::lcu_tcp->reconnect(); + } */ } }, 100ms, GeneralStates::Connecting); diff --git a/Core/Src/Runes/generated_metadata.cpp b/Core/Src/Runes/generated_metadata.cpp index 780a540..fb47950 100644 --- a/Core/Src/Runes/generated_metadata.cpp +++ b/Core/Src/Runes/generated_metadata.cpp @@ -5,11 +5,11 @@ extern "C"{ const char DESCRIPTION[255] __attribute__((section(".metadata_pool")))= "****************" // placeholder for beggining - "20250716T093528" // DateTime using ISO-8601 format + "20250717T063922" // DateTime using ISO-8601 format " " // alignment "f8e647d0" // STLIB commit "--------" // ADJ commit - "a633ebc9" // Board commit + "8397cdfa" // Board commit // the '=' is used for unparsing ; } \ No newline at end of file diff --git a/Core/Src/VCU/Brakes.cpp b/Core/Src/VCU/Brakes.cpp index 5f81624..289ef6c 100644 --- a/Core/Src/VCU/Brakes.cpp +++ b/Core/Src/VCU/Brakes.cpp @@ -9,7 +9,6 @@ Brakes::Brakes(){ reed3_input = DigitalSensor(REED3_PIN, &reed3); reed4_input = DigitalSensor(REED4_PIN, &reed4); - // Tape_input = DigitalSensor(TAPE_INPUT_PIN, &Tape_state); tape_enable_output = DigitalOutput(TAPE_E_PIN); tape_emergency_input = DigitalSensor(TAPE_PIN, &tape_emergency); @@ -25,7 +24,6 @@ void Brakes::brake(){ breaks_first_time = false; } - // TO DO: SET UP REGULATOR PRESSURE TO 6 bars Actuator_out.turn_off(); Active_brakes = true; diff --git a/Core/Src/VCU/Comms/Comms.cpp b/Core/Src/VCU/Comms/Comms.cpp index 971c8f2..395473b 100644 --- a/Core/Src/VCU/Comms/Comms.cpp +++ b/Core/Src/VCU/Comms/Comms.cpp @@ -91,11 +91,11 @@ void Comms::start() { /* lcu_tcp = new Socket(IPV4(VCU_IP), LCU_PORT, IPV4(LCU_IP), REMOTE_PORT); lcu_udp = new DatagramSocket(IPV4(VCU_IP), LCU_UDP_PORT, IPV4(LCU_IP), - LCU_UDP_PORT); + LCU_UDP_PORT); */ + + /* bcu_tcp = + new Socket(IPV4(VCU_IP), BCU_PORT, IPV4(BCU_IP), JUANS_REMOTE_PORT); */ - bcu_tcp = - new Socket(IPV4(VCU_IP), BCU_PORT, IPV4(BCU_IP), JUANS_REMOTE_PORT); - */ pcu_tcp = new Socket(IPV4(VCU_IP), PCU_PORT, IPV4(PCU_IP), REMOTE_PORT); pcu_udp = new DatagramSocket(IPV4(VCU_IP), PCU_UDP_PORT, IPV4(PCU_IP), @@ -368,7 +368,8 @@ void Comms::check_brake_order() { void Comms::check_unbrake_order() { if (unbrake_flag) { - if (*VCU::operational_state == VCU_SM::OperationalStates::Energized) { + if (*VCU::operational_state == VCU_SM::OperationalStates::Energized || + *VCU::operational_state == VCU_SM::OperationalStates::Recovery) { // actuators->set_regulator_1(6); brakes->unbrake(); unbrake_flag = false; From 4c4b7de226cb62aa217d16a230ae5bd5f7b5a14e Mon Sep 17 00:00:00 2001 From: jdmarmen Date: Fri, 18 Jul 2025 01:31:44 +0200 Subject: [PATCH 18/25] use LCU and PCU --- Core/Inc/VCU/Comms/Comms.hpp | 4 ++-- Core/Inc/state_machine.hpp | 14 ++++++-------- Core/Src/VCU/Comms/Comms.cpp | 8 +++++--- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/Core/Inc/VCU/Comms/Comms.hpp b/Core/Inc/VCU/Comms/Comms.hpp index ae297af..9d194e1 100644 --- a/Core/Inc/VCU/Comms/Comms.hpp +++ b/Core/Inc/VCU/Comms/Comms.hpp @@ -78,7 +78,7 @@ class Comms { }; enum class External_ids : uint16_t { - Levitation = 102, + Levitation = 9989, Propulsion = 103, Charging_lv = 104, Charging_hv = 105, @@ -86,7 +86,7 @@ class Comms { Booster = 1790, Close_contactors = 900, End_of_run = 110, - Stop_levitation = 111, + Stop_levitation = 9993, Stop_propulsion = 112, Stop_charging_lv = 113, Stop_charging_hv = 114, diff --git a/Core/Inc/state_machine.hpp b/Core/Inc/state_machine.hpp index d612fde..dcb13eb 100644 --- a/Core/Inc/state_machine.hpp +++ b/Core/Inc/state_machine.hpp @@ -41,8 +41,8 @@ class VCU_SM { return Comms::control_station_tcp->is_connected() && Comms::bmsl_tcp->is_connected() && Comms::hvscu_tcp->is_connected() && - Comms::pcu_tcp->is_connected() /* && - Comms::lcu_tcp->is_connected() */ /* && + Comms::pcu_tcp->is_connected() && + Comms::lcu_tcp->is_connected() /* && Comms::bcu_tcp->is_connected() */; }); @@ -51,8 +51,8 @@ class VCU_SM { return !Comms::control_station_tcp->is_connected() || !Comms::hvscu_tcp->is_connected() || !Comms::bmsl_tcp->is_connected() || - !Comms::pcu_tcp->is_connected() /* || - !Comms::lcu_tcp->is_connected() */ /* || + !Comms::pcu_tcp->is_connected() || + !Comms::lcu_tcp->is_connected() /* || !Comms::bcu_tcp->is_connected() */ ; }); @@ -62,14 +62,12 @@ class VCU_SM { GeneralStateMachine.add_transition( GeneralStates::Operational, GeneralStates::Fault, [&]() { return ((Comms::brakes->All_reeds && - Comms::brakes->Active_brakes && !Comms::brakes->breaks_first_time)); }); GeneralStateMachine.add_transition( GeneralStates::Connecting, GeneralStates::Fault, [&]() { return ((Comms::brakes->All_reeds && - Comms::brakes->Active_brakes && !Comms::brakes->breaks_first_time)); }); @@ -198,9 +196,9 @@ class VCU_SM { Comms::pcu_tcp->reconnect(); } - /* if (!Comms::lcu_tcp->is_connected()) { + if (!Comms::lcu_tcp->is_connected()) { Comms::lcu_tcp->reconnect(); - } */ + } } }, 100ms, GeneralStates::Connecting); diff --git a/Core/Src/VCU/Comms/Comms.cpp b/Core/Src/VCU/Comms/Comms.cpp index 395473b..61a2231 100644 --- a/Core/Src/VCU/Comms/Comms.cpp +++ b/Core/Src/VCU/Comms/Comms.cpp @@ -88,10 +88,10 @@ void Comms::start() { hvscu_udp = new DatagramSocket(IPV4(VCU_IP), HVSCU_UDP_PORT, IPV4(HVSCU_IP), HVSCU_UDP_PORT); - /* lcu_tcp = new Socket(IPV4(VCU_IP), LCU_PORT, IPV4(LCU_IP), REMOTE_PORT); + lcu_tcp = new Socket(IPV4(VCU_IP), LCU_PORT, IPV4(LCU_IP), REMOTE_PORT); lcu_udp = new DatagramSocket(IPV4(VCU_IP), LCU_UDP_PORT, IPV4(LCU_IP), - LCU_UDP_PORT); */ + LCU_UDP_PORT); /* bcu_tcp = new Socket(IPV4(VCU_IP), BCU_PORT, IPV4(BCU_IP), JUANS_REMOTE_PORT); */ @@ -370,7 +370,9 @@ void Comms::check_unbrake_order() { if (unbrake_flag) { if (*VCU::operational_state == VCU_SM::OperationalStates::Energized || *VCU::operational_state == VCU_SM::OperationalStates::Recovery) { - // actuators->set_regulator_1(6); + Time::set_timeout(2000, [&]() { + actuators->set_regulator_1(6); + }); brakes->unbrake(); unbrake_flag = false; } else { From 62ef3c123f2461ee35236fc6170ad7dd9a00ca8d Mon Sep 17 00:00:00 2001 From: jdmarmen Date: Fri, 18 Jul 2025 06:32:45 +0200 Subject: [PATCH 19/25] Finished recovery + pcu and order ids --- Core/Inc/Code_generation/JSON_ADE | 2 +- Core/Inc/VCU/Comms/Comms.hpp | 7 ++++++- Core/Inc/state_machine.hpp | 2 +- Core/Src/Runes/generated_metadata.cpp | 6 +++--- Core/Src/VCU/Comms/Comms.cpp | 11 ++++++----- deps/ST-LIB | 2 +- 6 files changed, 18 insertions(+), 12 deletions(-) diff --git a/Core/Inc/Code_generation/JSON_ADE b/Core/Inc/Code_generation/JSON_ADE index 61a4e14..5944165 160000 --- a/Core/Inc/Code_generation/JSON_ADE +++ b/Core/Inc/Code_generation/JSON_ADE @@ -1 +1 @@ -Subproject commit 61a4e14b221392db7ee975cf6b30cc373b76b278 +Subproject commit 5944165e8354e9db653e465e215c53307c08157c diff --git a/Core/Inc/VCU/Comms/Comms.hpp b/Core/Inc/VCU/Comms/Comms.hpp index 9d194e1..49daf5a 100644 --- a/Core/Inc/VCU/Comms/Comms.hpp +++ b/Core/Inc/VCU/Comms/Comms.hpp @@ -29,6 +29,8 @@ class Comms { // -----------------Enums----------------- enum class Orders_id : uint16_t { + Recovery = 32, + Potencia_refri = 33, Set_Regulator = 34, Enable_tapes = 35, @@ -136,6 +138,7 @@ class Comms { static inline bool brake_flag{}; static inline bool close_contactors_flag{}; static inline bool end_of_run_flag{}; + static inline bool recovery_flag{}; static inline bool stop_levitation_flag{}; static inline bool stop_propulsion_flag{}; @@ -342,7 +345,7 @@ class Comms { static inline HeapOrder* motor_brake{}; static inline HeapOrder* remote_motor_brake{}; - + static inline HeapOrder* recovery{}; // -----------------Functions----------------- static void init(); @@ -372,6 +375,8 @@ class Comms { static void booster_callback(); static void brake_callback(); + static void recovery_callback(); + static void close_contactors_callback(); static void end_of_run_callback(); diff --git a/Core/Inc/state_machine.hpp b/Core/Inc/state_machine.hpp index dcb13eb..2e5aa3d 100644 --- a/Core/Inc/state_machine.hpp +++ b/Core/Inc/state_machine.hpp @@ -126,7 +126,7 @@ class VCU_SM { [&]() { return Comms::actuators->contactors_closed; }); OperationalStateMachine.add_transition(OperationalStates::Idle, OperationalStates::Recovery, [](){ - return Comms::brakes->tape_emergency == PinState::ON; + return Comms::brakes->tape_emergency == PinState::ON || Comms::recovery_flag; }); OperationalStateMachine.add_transition( diff --git a/Core/Src/Runes/generated_metadata.cpp b/Core/Src/Runes/generated_metadata.cpp index fb47950..b015aef 100644 --- a/Core/Src/Runes/generated_metadata.cpp +++ b/Core/Src/Runes/generated_metadata.cpp @@ -5,11 +5,11 @@ extern "C"{ const char DESCRIPTION[255] __attribute__((section(".metadata_pool")))= "****************" // placeholder for beggining - "20250717T063922" // DateTime using ISO-8601 format + "20250718T062824" // DateTime using ISO-8601 format " " // alignment - "f8e647d0" // STLIB commit + "83d2dc9b" // STLIB commit "--------" // ADJ commit - "8397cdfa" // Board commit + "4c4b7de2" // Board commit // the '=' is used for unparsing ; } \ No newline at end of file diff --git a/Core/Src/VCU/Comms/Comms.cpp b/Core/Src/VCU/Comms/Comms.cpp index 61a2231..66ce581 100644 --- a/Core/Src/VCU/Comms/Comms.cpp +++ b/Core/Src/VCU/Comms/Comms.cpp @@ -39,7 +39,7 @@ void Comms::on_Disable_tapes() { } void Comms::brake_callback() { brake_flag = true; } - +void Comms::recovery_callback() { recovery_flag = true; } void Comms::unbrake_callback() { unbrake_flag = true; } void Comms::close_contactors_callback() { close_contactors_flag = true; } @@ -273,6 +273,9 @@ void Comms::add_orders() { emergency_stop = new HeapOrder(static_cast(Orders_id::Emergency_stop), &emergency_stop_callback); + + recovery = new HeapOrder(static_cast(Orders_id::Recovery), + &recovery_callback); } void Comms::send_packets() { @@ -370,10 +373,8 @@ void Comms::check_unbrake_order() { if (unbrake_flag) { if (*VCU::operational_state == VCU_SM::OperationalStates::Energized || *VCU::operational_state == VCU_SM::OperationalStates::Recovery) { - Time::set_timeout(2000, [&]() { - actuators->set_regulator_1(6); - }); - brakes->unbrake(); + actuators->set_regulator_1(6); + Time::set_timeout(2000, [&]() { brakes->unbrake(); }); unbrake_flag = false; } else { WARNING("Cannot unbrake in this state"); diff --git a/deps/ST-LIB b/deps/ST-LIB index f8e647d..83d2dc9 160000 --- a/deps/ST-LIB +++ b/deps/ST-LIB @@ -1 +1 @@ -Subproject commit f8e647d0c4f7d37dde4d6138719530086165ea76 +Subproject commit 83d2dc9bfa33e70b250038bc0d9ee1e32b0be9f1 From d4ca1fe8b14126122a978d651afe8e1de4d5c6df Mon Sep 17 00:00:00 2001 From: jdmarmen Date: Sat, 19 Jul 2025 01:35:29 +0200 Subject: [PATCH 20/25] Added recovery status packet --- Core/Inc/Code_generation/JSON_ADE | 2 +- Core/Inc/VCU/Comms/Comms.hpp | 8 +++- Core/Inc/state_machine.hpp | 65 +++++++++++++++++---------- Core/Src/Runes/generated_metadata.cpp | 4 +- Core/Src/VCU/Comms/Comms.cpp | 8 +++- 5 files changed, 56 insertions(+), 31 deletions(-) diff --git a/Core/Inc/Code_generation/JSON_ADE b/Core/Inc/Code_generation/JSON_ADE index 5944165..65551e4 160000 --- a/Core/Inc/Code_generation/JSON_ADE +++ b/Core/Inc/Code_generation/JSON_ADE @@ -1 +1 @@ -Subproject commit 5944165e8354e9db653e465e215c53307c08157c +Subproject commit 65551e43580d38c2da74b11075d166d7a8f1d007 diff --git a/Core/Inc/VCU/Comms/Comms.hpp b/Core/Inc/VCU/Comms/Comms.hpp index 49daf5a..55be376 100644 --- a/Core/Inc/VCU/Comms/Comms.hpp +++ b/Core/Inc/VCU/Comms/Comms.hpp @@ -27,6 +27,8 @@ class Comms { static inline uint8_t lcu_v_state{}; static inline uint8_t lcu_h_state{}; + static inline uint8_t recovery_status{0}; + // -----------------Enums----------------- enum class Orders_id : uint16_t { Recovery = 32, @@ -76,7 +78,8 @@ class Comms { hvscu_state = 941, lcu_state = 63, - pcu_state = 64 + pcu_state = 64, + vcu_state = 65, }; enum class External_ids : uint16_t { @@ -281,7 +284,8 @@ class Comms { static inline HeapPacket* pressure{}; static inline HeapPacket* tapes{}; static inline HeapPacket* sdc{}; - + static inline HeapPacket* vcu_state_packet{}; + // Remote packets static inline HeapPacket* hvscu_state_packet{}; static inline HeapPacket* lcu_state_packet{}; diff --git a/Core/Inc/state_machine.hpp b/Core/Inc/state_machine.hpp index 2e5aa3d..74f6b31 100644 --- a/Core/Inc/state_machine.hpp +++ b/Core/Inc/state_machine.hpp @@ -16,7 +16,14 @@ class VCU_SM { Fault, }; - enum OperationalStates { Idle, EndOfRun, Energized, Ready, Demonstration, Recovery }; + enum OperationalStates { + Idle, + EndOfRun, + Energized, + Ready, + Demonstration, + Recovery + }; StateMachine GeneralStateMachine; StateMachine OperationalStateMachine; @@ -36,23 +43,26 @@ class VCU_SM { GeneralStateMachine.add_state_machine(OperationalStateMachine, GeneralStates::Operational); - GeneralStateMachine.add_transition( - GeneralStates::Connecting, GeneralStates::Operational, [&]() { - return Comms::control_station_tcp->is_connected() && - Comms::bmsl_tcp->is_connected() && - Comms::hvscu_tcp->is_connected() && - Comms::pcu_tcp->is_connected() && - Comms::lcu_tcp->is_connected() /* && - Comms::bcu_tcp->is_connected() */; - }); + GeneralStateMachine + .add_transition(GeneralStates::Connecting, + GeneralStates::Operational, [&]() { + return Comms::control_station_tcp + ->is_connected() && + Comms::bmsl_tcp->is_connected() && + Comms::hvscu_tcp->is_connected() && + Comms::pcu_tcp->is_connected() /* && + Comms::lcu_tcp->is_connected() */ + /* && + Comms::bcu_tcp->is_connected() */; + }); GeneralStateMachine.add_transition( GeneralStates::Operational, GeneralStates::Fault, [&]() { return !Comms::control_station_tcp->is_connected() || !Comms::hvscu_tcp->is_connected() || !Comms::bmsl_tcp->is_connected() || - !Comms::pcu_tcp->is_connected() || - !Comms::lcu_tcp->is_connected() /* || + !Comms::pcu_tcp->is_connected() /* || + !Comms::lcu_tcp->is_connected() */ /* || !Comms::bcu_tcp->is_connected() */ ; }); @@ -62,13 +72,13 @@ class VCU_SM { GeneralStateMachine.add_transition( GeneralStates::Operational, GeneralStates::Fault, [&]() { return ((Comms::brakes->All_reeds && - !Comms::brakes->breaks_first_time)); + !Comms::brakes->breaks_first_time)); }); GeneralStateMachine.add_transition( GeneralStates::Connecting, GeneralStates::Fault, [&]() { return ((Comms::brakes->All_reeds && - !Comms::brakes->breaks_first_time)); + !Comms::brakes->breaks_first_time)); }); GeneralStateMachine.add_transition( @@ -110,14 +120,15 @@ class VCU_SM { [&]() { Comms::on_Disable_tapes(); }, OperationalStates::Ready); OperationalStateMachine.add_enter_action( - [&]() { Comms::on_Enable_tapes(); }, OperationalStates::Demonstration); + [&]() { Comms::on_Enable_tapes(); }, + OperationalStates::Demonstration); OperationalStateMachine.add_exit_action( - [&]() { Comms::on_Disable_tapes(); }, OperationalStates::Demonstration); + [&]() { Comms::on_Disable_tapes(); }, + OperationalStates::Demonstration); - OperationalStateMachine.add_enter_action([](){ - Comms::on_Disable_tapes(); - }, OperationalStates::Recovery); + OperationalStateMachine.add_enter_action( + []() { Comms::on_Disable_tapes(); }, OperationalStates::Recovery); //----------- @@ -125,9 +136,15 @@ class VCU_SM { OperationalStates::Idle, OperationalStates::Energized, [&]() { return Comms::actuators->contactors_closed; }); - OperationalStateMachine.add_transition(OperationalStates::Idle, OperationalStates::Recovery, [](){ - return Comms::brakes->tape_emergency == PinState::ON || Comms::recovery_flag; - }); + OperationalStateMachine.add_transition( + OperationalStates::Idle, OperationalStates::Recovery, []() { + return Comms::brakes->tape_emergency == PinState::OFF || + Comms::recovery_flag; + }); + + OperationalStateMachine.add_enter_action( + [&]() { Comms::recovery_status = 1; }, + OperationalStates::Recovery); OperationalStateMachine.add_transition( OperationalStates::Energized, OperationalStates::Idle, @@ -196,9 +213,9 @@ class VCU_SM { Comms::pcu_tcp->reconnect(); } - if (!Comms::lcu_tcp->is_connected()) { + /* if (!Comms::lcu_tcp->is_connected()) { Comms::lcu_tcp->reconnect(); - } + } */ } }, 100ms, GeneralStates::Connecting); diff --git a/Core/Src/Runes/generated_metadata.cpp b/Core/Src/Runes/generated_metadata.cpp index b015aef..249f6e4 100644 --- a/Core/Src/Runes/generated_metadata.cpp +++ b/Core/Src/Runes/generated_metadata.cpp @@ -5,11 +5,11 @@ extern "C"{ const char DESCRIPTION[255] __attribute__((section(".metadata_pool")))= "****************" // placeholder for beggining - "20250718T062824" // DateTime using ISO-8601 format + "20250718T202155" // DateTime using ISO-8601 format " " // alignment "83d2dc9b" // STLIB commit "--------" // ADJ commit - "4c4b7de2" // Board commit + "62ef3c12" // Board commit // the '=' is used for unparsing ; } \ No newline at end of file diff --git a/Core/Src/VCU/Comms/Comms.cpp b/Core/Src/VCU/Comms/Comms.cpp index 66ce581..0593867 100644 --- a/Core/Src/VCU/Comms/Comms.cpp +++ b/Core/Src/VCU/Comms/Comms.cpp @@ -88,10 +88,10 @@ void Comms::start() { hvscu_udp = new DatagramSocket(IPV4(VCU_IP), HVSCU_UDP_PORT, IPV4(HVSCU_IP), HVSCU_UDP_PORT); - lcu_tcp = new Socket(IPV4(VCU_IP), LCU_PORT, IPV4(LCU_IP), REMOTE_PORT); + /* lcu_tcp = new Socket(IPV4(VCU_IP), LCU_PORT, IPV4(LCU_IP), REMOTE_PORT); lcu_udp = new DatagramSocket(IPV4(VCU_IP), LCU_UDP_PORT, IPV4(LCU_IP), - LCU_UDP_PORT); + LCU_UDP_PORT); */ /* bcu_tcp = new Socket(IPV4(VCU_IP), BCU_PORT, IPV4(BCU_IP), JUANS_REMOTE_PORT); */ @@ -138,6 +138,9 @@ void Comms::add_packets() { pcu_state_packet = new HeapPacket( static_cast(Packets_id::pcu_state), &pcu_state); + + vcu_state_packet = new HeapPacket( + static_cast(Packets_id::vcu_state), &recovery_status); } void Comms::add_orders() { @@ -287,6 +290,7 @@ void Comms::send_packets() { control_station_udp->send_packet(*pressure); control_station_udp->send_packet(*tapes); control_station_udp->send_packet(*sdc); + pcu_udp->send_packet(*vcu_state_packet); }); } From 0901b287f082b7379cb6120a78152ac8ec3c370f Mon Sep 17 00:00:00 2001 From: jdmarmen Date: Sat, 19 Jul 2025 12:54:03 +0200 Subject: [PATCH 21/25] Added chill keepalives --- Core/Src/Runes/generated_metadata.cpp | 6 +++--- Core/Src/VCU/Comms/Comms.cpp | 2 +- deps/ST-LIB | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/Core/Src/Runes/generated_metadata.cpp b/Core/Src/Runes/generated_metadata.cpp index 249f6e4..32fc819 100644 --- a/Core/Src/Runes/generated_metadata.cpp +++ b/Core/Src/Runes/generated_metadata.cpp @@ -5,11 +5,11 @@ extern "C"{ const char DESCRIPTION[255] __attribute__((section(".metadata_pool")))= "****************" // placeholder for beggining - "20250718T202155" // DateTime using ISO-8601 format + "20250719T125331" // DateTime using ISO-8601 format " " // alignment - "83d2dc9b" // STLIB commit + "f73885a9" // STLIB commit "--------" // ADJ commit - "62ef3c12" // Board commit + "d4ca1fe8" // Board commit // the '=' is used for unparsing ; } \ No newline at end of file diff --git a/Core/Src/VCU/Comms/Comms.cpp b/Core/Src/VCU/Comms/Comms.cpp index 0593867..f4885d3 100644 --- a/Core/Src/VCU/Comms/Comms.cpp +++ b/Core/Src/VCU/Comms/Comms.cpp @@ -74,7 +74,7 @@ void Comms::init() { } void Comms::start() { - control_station_tcp = new ServerSocket(IPV4(VCU_IP), REMOTE_PORT); + control_station_tcp = new ServerSocket(IPV4(VCU_IP), REMOTE_PORT, 1000, 500, 10); control_station_udp = new DatagramSocket(IPV4(VCU_IP), CONTROL_STATION_UDP_PORT, diff --git a/deps/ST-LIB b/deps/ST-LIB index 83d2dc9..f73885a 160000 --- a/deps/ST-LIB +++ b/deps/ST-LIB @@ -1 +1 @@ -Subproject commit 83d2dc9bfa33e70b250038bc0d9ee1e32b0be9f1 +Subproject commit f73885a99171d3e75c6453efc6d9c9c958849538 From c3b39ef177c77c4a16c2fbac2916e362e68704cf Mon Sep 17 00:00:00 2001 From: jdmarmen Date: Sat, 19 Jul 2025 12:57:36 +0200 Subject: [PATCH 22/25] Added a bunch of keepalives --- Core/Src/Runes/generated_metadata.cpp | 4 ++-- Core/Src/VCU/Comms/Comms.cpp | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/Core/Src/Runes/generated_metadata.cpp b/Core/Src/Runes/generated_metadata.cpp index 32fc819..08fc44f 100644 --- a/Core/Src/Runes/generated_metadata.cpp +++ b/Core/Src/Runes/generated_metadata.cpp @@ -5,11 +5,11 @@ extern "C"{ const char DESCRIPTION[255] __attribute__((section(".metadata_pool")))= "****************" // placeholder for beggining - "20250719T125331" // DateTime using ISO-8601 format + "20250719T125721" // DateTime using ISO-8601 format " " // alignment "f73885a9" // STLIB commit "--------" // ADJ commit - "d4ca1fe8" // Board commit + "0901b287" // Board commit // the '=' is used for unparsing ; } \ No newline at end of file diff --git a/Core/Src/VCU/Comms/Comms.cpp b/Core/Src/VCU/Comms/Comms.cpp index f4885d3..6bf0453 100644 --- a/Core/Src/VCU/Comms/Comms.cpp +++ b/Core/Src/VCU/Comms/Comms.cpp @@ -80,10 +80,10 @@ void Comms::start() { new DatagramSocket(IPV4(VCU_IP), CONTROL_STATION_UDP_PORT, IPV4(CONTROL_STATION_IP), CONTROL_STATION_UDP_PORT); - bmsl_tcp = new Socket(IPV4(VCU_IP), BMSL_PORT, IPV4(BMSL_IP), REMOTE_PORT); + bmsl_tcp = new Socket(IPV4(VCU_IP), BMSL_PORT, IPV4(BMSL_IP), REMOTE_PORT, 1000, 500, 10); hvscu_tcp = - new Socket(IPV4(VCU_IP), HVSCU_PORT, IPV4(HVSCU_IP), REMOTE_PORT); + new Socket(IPV4(VCU_IP), HVSCU_PORT, IPV4(HVSCU_IP), REMOTE_PORT, 1000, 500, 10); hvscu_udp = new DatagramSocket(IPV4(VCU_IP), HVSCU_UDP_PORT, IPV4(HVSCU_IP), HVSCU_UDP_PORT); @@ -96,7 +96,7 @@ void Comms::start() { /* bcu_tcp = new Socket(IPV4(VCU_IP), BCU_PORT, IPV4(BCU_IP), JUANS_REMOTE_PORT); */ - pcu_tcp = new Socket(IPV4(VCU_IP), PCU_PORT, IPV4(PCU_IP), REMOTE_PORT); + pcu_tcp = new Socket(IPV4(VCU_IP), PCU_PORT, IPV4(PCU_IP), REMOTE_PORT, 1000, 500, 10); pcu_udp = new DatagramSocket(IPV4(VCU_IP), PCU_UDP_PORT, IPV4(PCU_IP), PCU_UDP_PORT); From b74ef8daaac59ca6e05935e8c45d9ff6483f9560 Mon Sep 17 00:00:00 2001 From: jdmarmen Date: Sat, 19 Jul 2025 13:54:15 +0200 Subject: [PATCH 23/25] Removed bmsl --- Core/Inc/state_machine.hpp | 8 ++++---- Core/Src/Runes/generated_metadata.cpp | 4 ++-- Core/Src/VCU/Comms/Comms.cpp | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/Core/Inc/state_machine.hpp b/Core/Inc/state_machine.hpp index 74f6b31..f8784cf 100644 --- a/Core/Inc/state_machine.hpp +++ b/Core/Inc/state_machine.hpp @@ -48,7 +48,7 @@ class VCU_SM { GeneralStates::Operational, [&]() { return Comms::control_station_tcp ->is_connected() && - Comms::bmsl_tcp->is_connected() && + /* Comms::bmsl_tcp->is_connected() && */ Comms::hvscu_tcp->is_connected() && Comms::pcu_tcp->is_connected() /* && Comms::lcu_tcp->is_connected() */ @@ -60,7 +60,7 @@ class VCU_SM { GeneralStates::Operational, GeneralStates::Fault, [&]() { return !Comms::control_station_tcp->is_connected() || !Comms::hvscu_tcp->is_connected() || - !Comms::bmsl_tcp->is_connected() || + /* !Comms::bmsl_tcp->is_connected() || */ !Comms::pcu_tcp->is_connected() /* || !Comms::lcu_tcp->is_connected() */ /* || !Comms::bcu_tcp->is_connected() */ @@ -201,9 +201,9 @@ class VCU_SM { GeneralStateMachine.add_low_precision_cyclic_action( [&]() { if (tetas) { - if (!Comms::bmsl_tcp->is_connected()) { + /* if (!Comms::bmsl_tcp->is_connected()) { Comms::bmsl_tcp->reconnect(); - } + } */ if (!Comms::hvscu_tcp->is_connected()) { Comms::hvscu_tcp->reconnect(); diff --git a/Core/Src/Runes/generated_metadata.cpp b/Core/Src/Runes/generated_metadata.cpp index 08fc44f..14c2aa5 100644 --- a/Core/Src/Runes/generated_metadata.cpp +++ b/Core/Src/Runes/generated_metadata.cpp @@ -5,11 +5,11 @@ extern "C"{ const char DESCRIPTION[255] __attribute__((section(".metadata_pool")))= "****************" // placeholder for beggining - "20250719T125721" // DateTime using ISO-8601 format + "20250719T135313" // DateTime using ISO-8601 format " " // alignment "f73885a9" // STLIB commit "--------" // ADJ commit - "0901b287" // Board commit + "c3b39ef1" // Board commit // the '=' is used for unparsing ; } \ No newline at end of file diff --git a/Core/Src/VCU/Comms/Comms.cpp b/Core/Src/VCU/Comms/Comms.cpp index 6bf0453..24d04b8 100644 --- a/Core/Src/VCU/Comms/Comms.cpp +++ b/Core/Src/VCU/Comms/Comms.cpp @@ -80,7 +80,7 @@ void Comms::start() { new DatagramSocket(IPV4(VCU_IP), CONTROL_STATION_UDP_PORT, IPV4(CONTROL_STATION_IP), CONTROL_STATION_UDP_PORT); - bmsl_tcp = new Socket(IPV4(VCU_IP), BMSL_PORT, IPV4(BMSL_IP), REMOTE_PORT, 1000, 500, 10); + /* bmsl_tcp = new Socket(IPV4(VCU_IP), BMSL_PORT, IPV4(BMSL_IP), REMOTE_PORT, 1000, 500, 10); */ hvscu_tcp = new Socket(IPV4(VCU_IP), HVSCU_PORT, IPV4(HVSCU_IP), REMOTE_PORT, 1000, 500, 10); From f961fb4b5c44ab63b633524f9c9e7fe4a8c91695 Mon Sep 17 00:00:00 2001 From: jdmarmen Date: Sat, 19 Jul 2025 14:46:16 +0200 Subject: [PATCH 24/25] XD --- Core/Inc/state_machine.hpp | 2 +- Core/Src/Runes/generated_metadata.cpp | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Core/Inc/state_machine.hpp b/Core/Inc/state_machine.hpp index f8784cf..3e63c72 100644 --- a/Core/Inc/state_machine.hpp +++ b/Core/Inc/state_machine.hpp @@ -128,7 +128,7 @@ class VCU_SM { OperationalStates::Demonstration); OperationalStateMachine.add_enter_action( - []() { Comms::on_Disable_tapes(); }, OperationalStates::Recovery); + []() { Comms::on_Enable_tapes(); }, OperationalStates::Recovery); //----------- diff --git a/Core/Src/Runes/generated_metadata.cpp b/Core/Src/Runes/generated_metadata.cpp index 14c2aa5..bb825d0 100644 --- a/Core/Src/Runes/generated_metadata.cpp +++ b/Core/Src/Runes/generated_metadata.cpp @@ -5,11 +5,11 @@ extern "C"{ const char DESCRIPTION[255] __attribute__((section(".metadata_pool")))= "****************" // placeholder for beggining - "20250719T135313" // DateTime using ISO-8601 format + "20250719T144559" // DateTime using ISO-8601 format " " // alignment "f73885a9" // STLIB commit "--------" // ADJ commit - "c3b39ef1" // Board commit + "b74ef8da" // Board commit // the '=' is used for unparsing ; } \ No newline at end of file From 8eee0acfe0765fcf0e8e1d2f78be113df4ad5fd3 Mon Sep 17 00:00:00 2001 From: jdmarmen Date: Tue, 22 Jul 2025 21:52:10 +0200 Subject: [PATCH 25/25] Final version --- Core/Inc/state_machine.hpp | 8 ++++---- Core/Src/Runes/generated_metadata.cpp | 4 ++-- Core/Src/VCU/Comms/Comms.cpp | 6 +++--- Core/Src/main.cpp | 2 +- 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/Core/Inc/state_machine.hpp b/Core/Inc/state_machine.hpp index 3e63c72..a2eee56 100644 --- a/Core/Inc/state_machine.hpp +++ b/Core/Inc/state_machine.hpp @@ -114,17 +114,17 @@ class VCU_SM { GeneralStates::Fault); OperationalStateMachine.add_enter_action( - [&]() { Comms::on_Enable_tapes(); }, OperationalStates::Ready); + [&]() { Comms::on_Disable_tapes(); }, OperationalStates::Ready); OperationalStateMachine.add_exit_action( - [&]() { Comms::on_Disable_tapes(); }, OperationalStates::Ready); + [&]() { Comms::on_Enable_tapes(); }, OperationalStates::Ready); OperationalStateMachine.add_enter_action( - [&]() { Comms::on_Enable_tapes(); }, + [&]() { Comms::on_Disable_tapes(); }, OperationalStates::Demonstration); OperationalStateMachine.add_exit_action( - [&]() { Comms::on_Disable_tapes(); }, + [&]() { Comms::on_Enable_tapes(); }, OperationalStates::Demonstration); OperationalStateMachine.add_enter_action( diff --git a/Core/Src/Runes/generated_metadata.cpp b/Core/Src/Runes/generated_metadata.cpp index bb825d0..273abe7 100644 --- a/Core/Src/Runes/generated_metadata.cpp +++ b/Core/Src/Runes/generated_metadata.cpp @@ -5,11 +5,11 @@ extern "C"{ const char DESCRIPTION[255] __attribute__((section(".metadata_pool")))= "****************" // placeholder for beggining - "20250719T144559" // DateTime using ISO-8601 format + "20250719T154310" // DateTime using ISO-8601 format " " // alignment "f73885a9" // STLIB commit "--------" // ADJ commit - "b74ef8da" // Board commit + "f961fb4b" // Board commit // the '=' is used for unparsing ; } \ No newline at end of file diff --git a/Core/Src/VCU/Comms/Comms.cpp b/Core/Src/VCU/Comms/Comms.cpp index 24d04b8..4fbae35 100644 --- a/Core/Src/VCU/Comms/Comms.cpp +++ b/Core/Src/VCU/Comms/Comms.cpp @@ -29,12 +29,12 @@ void Comms::on_Set_regulator() { } void Comms::on_Enable_tapes() { - brakes->tape_enable_status = PinState::ON; + brakes->tape_enable_status = PinState::OFF; brakes->tape_enable_output.turn_on(); } void Comms::on_Disable_tapes() { - brakes->tape_enable_status = PinState::OFF; + brakes->tape_enable_status = PinState::ON; brakes->tape_enable_output.turn_off(); } @@ -282,7 +282,7 @@ void Comms::add_orders() { } void Comms::send_packets() { - Time::set_timeout(3000, [&]() { + Time::set_timeout(5000, [&]() { control_station_udp->send_packet(*states); control_station_udp->send_packet(*reeds); control_station_udp->send_packet(*flow); diff --git a/Core/Src/main.cpp b/Core/Src/main.cpp index bbd02f8..7a0fefd 100644 --- a/Core/Src/main.cpp +++ b/Core/Src/main.cpp @@ -11,7 +11,7 @@ int main(void) { VCU::init(); STLIB::start("00:00:00:00:01:03","192.168.1.3", "255.255.255.0"); - Time::set_timeout(2000, []() { + Time::set_timeout(4000, []() { VCU::start(); VCU::state_machine->tetas = true; });