Skip to content

Feature/state machines #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
100 changes: 75 additions & 25 deletions Core/Inc/VCU.hpp
Original file line number Diff line number Diff line change
@@ -1,21 +1,6 @@
#pragma once

#include <VCU_Communications/VCU_TCP/VCU_TCP.hpp>
#include <VCU_Communications/VCU_UDP/VCU_UDP.hpp>
#include "VCU_Pinout/Pinout.hpp"
#include "VCU_Mode/VCU_Mode.hpp"
#include "VCU_Data/VCU_Data.hpp"
#include "VCU_Sensors/VCU_EnviromentalSensors.hpp"
#include "VCU_Sensors/VCU_RegulatorSensor.hpp"
#include "VCU_Sensors/VCU_Reed.hpp"
#include "VCU_Actuators/VCU_LedsActuator.hpp"
#include "VCU_Actuators/VCU_RegulatorActuator.hpp"
#include "VCU_Actuators/VCU_ValveActuator.hpp"
#include "VCU_Brakes/VCU_Brakes.hpp"
#include "VCU_Utilities/VCU_Types.hpp"
#include "VCU_Communications/VCU_TCP/IncomingOrders.hpp"
#include "VCU_Communications/VCU_UDP/Packets.hpp"

#include "VCU_Utilities/VCU_Includes.hpp"

namespace VCU{
template<VCU_MODE> class VCU_CLASS;
Expand All @@ -25,23 +10,29 @@ namespace VCU{
static VCU_CLASS* vcu;

Data<VCU_MODE::BRAKE_VALIDATION> data;
Brakes<VCU_MODE::BRAKE_VALIDATION> brakes;
Actuators<VCU_MODE::BRAKE_VALIDATION> actuators;
TCP<VCU_MODE::BRAKE_VALIDATION> tcp_handler;
UDP<VCU_MODE::BRAKE_VALIDATION> udp_handler;
IncomingOrders<VCU_MODE::BRAKE_VALIDATION> incoming_orders;
Packets<VCU_MODE::BRAKE_VALIDATION> packets;
GeneralStateMachine<VCU_MODE::BRAKE_VALIDATION> general_state_machine;

VCU_CLASS():data(), brakes(data), tcp_handler(), udp_handler(), incoming_orders(data), packets(data){}
VCU_CLASS():
data(), actuators(data), tcp_handler(), udp_handler(), incoming_orders(data), packets(data),
general_state_machine(data, actuators, tcp_handler)
{}

void init(){
STLIB::start();
brakes.init();
actuators.brakes.init();
udp_handler.init();
tcp_handler.init();
general_state_machine.init();
data.add_protections();
}

static void read_brakes_sensors(){
vcu->brakes.read();
vcu->actuators.brakes.read();
}

static void send_to_backend(){
Expand All @@ -50,28 +41,87 @@ namespace VCU{
vcu->udp_handler.BACKEND_CONNECTION.send(vcu->packets.bottle_temperature_packet);
vcu->udp_handler.BACKEND_CONNECTION.send(vcu->packets.reed_packet);
}

static void update_state_machine(){
vcu->general_state_machine.general_state_machine.check_transitions();
}
};

template<> class VCU_CLASS<VCU_MODE::VEHICLE>{
public:
static VCU_CLASS* vcu;

Data<VCU_MODE::VEHICLE> data;
Actuators<VCU_MODE::VEHICLE> actuators;
EnvironmentalSensors environmental_sensors;
TCP<VCU_MODE::VEHICLE> tcp_handler;
UDP<VCU_MODE::VEHICLE> udp_handler;
IncomingOrders<VCU_MODE::VEHICLE> incoming_orders;
OutgoingOrders<VCU_MODE::VEHICLE> outgoing_orders;
Packets<VCU_MODE::VEHICLE> packets;
EncoderSensor encoder;
GeneralStateMachine<VCU_MODE::VEHICLE> state_machine_handler;

VCU_CLASS():data(), actuators(data), environmental_sensors(data), tcp_handler(), udp_handler(), incoming_orders(data), packets(data),
encoder(Pinout::TAPE1, Pinout::TAPE2, &data.tapes_position, &data.tapes_direction, &data.tapes_speed, &data.tapes_acceleration)
,state_machine_handler(data, actuators, tcp_handler, outgoing_orders, encoder)
{}

void init(){
STLIB::start();
actuators.brakes.init();
tcp_handler.init();
udp_handler.init();
}

static void read_brakes_sensors(){
vcu->actuators.brakes.read();
}

static void read_environmental_sensors(){
vcu->environmental_sensors.read();
}

static void send_to_backend(){
vcu->udp_handler.send_to_backend(vcu->packets.regulator_packet);
vcu->udp_handler.send_to_backend(vcu->packets.pressure_packets);
vcu->udp_handler.send_to_backend(vcu->packets.bottle_temperature_packet);
vcu->udp_handler.send_to_backend(vcu->packets.environmental_packet);
vcu->udp_handler.send_to_backend(vcu->packets.states_packet);
}

static void update_state_machine(){
vcu->state_machine_handler.general_state_machine.check_transitions();
}
};

void set_regulator_pressure(){
VCU_CLASS<BRAKE_VALIDATION>::vcu->brakes.set_regulator_pressure(VCU_CLASS<BRAKE_VALIDATION>::vcu->incoming_orders.new_pressure);
float n_pressure = VCU_CLASS<BRAKE_VALIDATION>::vcu->incoming_orders.new_pressure;
if( n_pressure < 0 || n_pressure > 10 ){
ProtectionManager::warn("The new value for the regulator is out of range!");
return;
}

VCU_CLASS<BRAKE_VALIDATION>::vcu->actuators.brakes.set_regulator_pressure(n_pressure);
}

void brake(){
VCU_CLASS<BRAKE_VALIDATION>::vcu->brakes.brake();
VCU_CLASS<BRAKE_VALIDATION>::vcu->actuators.brakes.brake();
}

void unbrake(){
VCU_CLASS<BRAKE_VALIDATION>::vcu->brakes.not_brake();
VCU_CLASS<BRAKE_VALIDATION>::vcu->actuators.brakes.not_brake();
}

void disable_emergency_tape(){
VCU_CLASS<BRAKE_VALIDATION>::vcu->brakes.disable_emergency_brakes();
VCU_CLASS<BRAKE_VALIDATION>::vcu->actuators.brakes.disable_emergency_brakes();
}

void enable_emergency_tape(){
VCU_CLASS<BRAKE_VALIDATION>::vcu->brakes.enable_emergency_brakes();
VCU_CLASS<BRAKE_VALIDATION>::vcu->actuators.brakes.enable_emergency_brakes();
}

}

VCU::VCU_CLASS<VCU::VCU_MODE::BRAKE_VALIDATION>* VCU::VCU_CLASS<VCU::VCU_MODE::BRAKE_VALIDATION>::vcu = nullptr;
VCU::VCU_CLASS<VCU::VCU_MODE::VEHICLE>* VCU::VCU_CLASS<VCU::VCU_MODE::VEHICLE>::vcu = nullptr;
41 changes: 41 additions & 0 deletions Core/Inc/VCU_Actuators/VCU_Actuators.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* VCU_Actuators.hpp
*
* Created on: Jun 14, 2023
* Author: stefancostea & Pablo
*/

#pragma once

#include "VCU_Brakes/VCU_Brakes.hpp"
#include "VCU_LedsActuator.hpp"

namespace VCU{
template<VCU_MODE MODE> class Actuators;

template<>
class Actuators<BRAKE_VALIDATION>{
public:
Brakes<BRAKE_VALIDATION> brakes;
DigitalOutput led_sleep, led_flash, led_fault, led_operational, led_can;
Actuators(Data<BRAKE_VALIDATION>& data) :
brakes(data),
led_sleep(Pinout::SLEEP_LED), led_flash(Pinout::FLASH_LED), led_fault(Pinout::FAULT_LED),
led_operational(Pinout::OPERATIONAL_LED), led_can(Pinout::CAN_LED)
{}
};

template<>
class Actuators<VEHICLE>{
public:
Brakes<VEHICLE> brakes;
LEDSActuator vehicle_leds;
DigitalOutput led_sleep, led_flash, led_fault, led_operational, led_can;
Actuators(Data<VEHICLE>& data) :
brakes(data),
vehicle_leds(Pinout::LEDR, Pinout::LEDG, Pinout::LEDB),
led_sleep(Pinout::SLEEP_LED), led_flash(Pinout::FLASH_LED), led_fault(Pinout::FAULT_LED),
led_operational(Pinout::OPERATIONAL_LED), led_can(Pinout::CAN_LED)
{}
};
}
36 changes: 34 additions & 2 deletions Core/Inc/VCU_Actuators/VCU_LedsActuator.hpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,22 @@
/*
* VCU_LedsActuator.hpp
*
* Created on: Jun 1, 2023
* Author: Pablo & stefancostea
*/

#pragma once

#include "ST-LIB.hpp"

namespace VCU{
class RGBColor{
public:
uint8_t red, green, blue;
RGBColor(uint8_t red, uint8_t green, uint8_t blue) : red(red), green(green), blue(blue){}
static RGBColor RED, BLUE, GREEN, MAGENTA, PURPLE, ORANGE, TURQUOISE, PISTACCIO_GREEN, WHITE;
};

class LEDSActuator{
private:
PWM red;
Expand All @@ -11,6 +25,10 @@ namespace VCU{

public:
LEDSActuator(Pin& red_pin, Pin& green_pin, Pin& blue_pin): red(red_pin), green(green_pin), blue(blue_pin){

}

void leds_init(){
red.set_frequency(1000);
green.set_frequency(1000);
blue.set_frequency(1000);
Expand All @@ -27,7 +45,7 @@ namespace VCU{
void set_color(uint8_t red, uint8_t green, uint8_t blue){
if (red > 255 || green > 255 || blue > 255)
{
ErrorHandler("Invalid color");
ErrorHandler("Invalid color RGB code: %d , %d , %d", red, green, blue);
return;
}

Expand All @@ -36,11 +54,25 @@ namespace VCU{
this->blue.set_duty_cycle(blue / 255.0f * 100.0f);
}

void set_color(RGBColor& color){
set_color(color.red,color.green, color.blue);
}

void turn_off(){
this->red.set_duty_cycle(0.0f);
this->green.set_duty_cycle(0.0f);
this->blue.set_duty_cycle(0.0f);
}
};

}
RGBColor RGBColor::RED(255, 0,0);
RGBColor RGBColor::BLUE(0,0,255);
RGBColor RGBColor::GREEN(0,255,0);
RGBColor RGBColor::MAGENTA(255,0,255);
RGBColor RGBColor::PURPLE(160,32,240);
RGBColor RGBColor::ORANGE(255,165,0);
RGBColor RGBColor::TURQUOISE(93,193,185);
RGBColor RGBColor::PISTACCIO_GREEN(147,197,114);
RGBColor RGBColor::WHITE(255,255,255);

}
8 changes: 8 additions & 0 deletions Core/Inc/VCU_Actuators/VCU_RegulatorActuator.hpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
/*
* VCU_RegulatorActuator.hpp
*
* Created on: Jun 1, 2023
* Author: Pablo
*/


#pragma once

#include "ST-LIB.hpp"
Expand Down
8 changes: 8 additions & 0 deletions Core/Inc/VCU_Actuators/VCU_ValveActuator.hpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
/*
* VCU_ValveActuator.hpp
*
* Created on: Jun 1, 2023
* Author: Pablo
*/


#pragma once

#include "ST-LIB.hpp"
Expand Down
42 changes: 24 additions & 18 deletions Core/Inc/VCU_Brakes/VCU_Brakes.hpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
/*
* VCU_Brakes.hpp
*
* Created on: Jun 1, 2023
* Author: Pablo
*/

#pragma once

#include "ST-LIB.hpp"
Expand Down Expand Up @@ -56,9 +63,7 @@ namespace VCU{
high_pressure_sensor(Pinout::HIGH_PRESSURE, high_pressure_sensor_slope, high_pressure_sensor_offset, &data.high_pressure1),
low_pressure_sensor1(Pinout::LOW_PRESSURE1, low_pressure_sensors_slope, low_pressure_sensors_offset, &data.low_pressure1),
low_pressure_sensor2(Pinout::LOW_PRESSURE2, low_pressure_sensors_slope, low_pressure_sensors_offset, &data.low_pressure2)
{

}
{}


void read(){
Expand Down Expand Up @@ -90,8 +95,6 @@ namespace VCU{
emergency_tape_enable.turn_off();
}



void check_reeds(){
reed.read();
}
Expand All @@ -116,12 +119,11 @@ namespace VCU{
class Brakes<VCU::VCU_MODE::VEHICLE>{
constexpr static uint16_t ntc_lookup_table_size = 256;

constexpr static float high_pressure_sensor_slope = 0.006681691;
constexpr static float high_pressure_sensor_offset = -43.75;
constexpr static float high_pressure_sensor_slope = 113.46153*1.20822977;
constexpr static float high_pressure_sensor_offset = (-516.25/11.8)*1.20822977;

constexpr static float low_pressure_sensors_slope = 0.000190905;
constexpr static float low_pressure_sensors_offset = -1.25;


constexpr static float operating_pressure = 8.0f;

Expand Down Expand Up @@ -157,7 +159,13 @@ namespace VCU{
regulator_actuator(Pinout::REGULATOR_OUT, data.regulator_reference_pressure),
regulator_sensor(Pinout::REGULATOR_IN, data.regulator_real_pressure),

emergency_tape(Pinout::EMERGENCY_TAPE, [&](){emergency_tape.read();}, data.emergency_tape),
emergency_tape(Pinout::EMERGENCY_TAPE, [&](){
emergency_tape.read();
//INOF: emergency tape enable a nivel bajo
if (data.emergeny_tape_enable == PinState::OFF) {
data.emergency_braking = true;
}
}, data.emergency_tape),
emergency_tape_enable(Pinout::EMERGENCY_TAPE_ENABLE),

reed1(Pinout::REED1, &data.reed1),
Expand Down Expand Up @@ -189,18 +197,17 @@ namespace VCU{
void brake(){
valve_actuator.close();


Time::set_timeout(1, [&](){
check_reeds();
});
// Time::set_timeout(1, [&](){
// check_reeds();
// });
}

void not_brake(){
valve_actuator.open();

Time::set_timeout(1, [&](){
check_reeds();
});
data.emergency_braking = false;
// Time::set_timeout(1, [&](){
// check_reeds();
// });
}

void disable_emergency_brakes(){
Expand Down Expand Up @@ -228,6 +235,5 @@ namespace VCU{
regulator_actuator.set_pressure(operating_pressure);
read();
}

};
}
15 changes: 15 additions & 0 deletions Core/Inc/VCU_Communications/VCU_Communications.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#pragma once

namespace VCU{
const IPV4 VCU_IP = {"192.168.1.3"};
const IPV4 LCU_MASTER_IP = {"192.168.1.4"};
const IPV4 LCU_SLAVE_IP = {"192.168.1.5"};
const IPV4 PCU_IP = {"192.168.1.6"};
const IPV4 BLCU_IP = {"192.168.1.7"};
const IPV4 BMSL_IP = {"192.168.1.8"};
const IPV4 OBCCU_IP = {"192.168.1.9"};
const IPV4 BACKEND_IP = {"192.168.0.9"};
constexpr uint16_t SERVER_PORT = 50500;
constexpr uint16_t CLIENT_PORT = 50501;
constexpr uint16_t UDP_PORT = 50400;
}
Loading