Skip to content

Commit 9fa9657

Browse files
add state orders
1 parent 7a1dcdc commit 9fa9657

16 files changed

+220
-105
lines changed

Core/Inc/OBCCU/Actions.hpp

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#pragma once
2+
3+
#include "BMS-LIB.hpp"
4+
#include "OBCCU/Conditions.hpp"
5+
#include "OBCCU/Leds.hpp"
6+
#include "OBCCU/Contactors.hpp"
7+
#include "OBCCU/IMD.hpp"
8+
9+
namespace OBCCU {
10+
namespace Actions {
11+
void start_charging() {
12+
Conditions::want_to_charge = true;
13+
};
14+
15+
void stop_charging() {
16+
Conditions::want_to_charge = false;
17+
};
18+
19+
void open_contactors() {
20+
Contactors::high.turn_off();
21+
Contactors::low.turn_off();
22+
Conditions::contactors_closed = false;
23+
24+
Leds::can.turn_off();
25+
};
26+
27+
// void close_contactors() {
28+
// Contactors::low.turn_on();
29+
// Contactors::high.turn_on();
30+
// Conditions::contactors_closed = true;
31+
32+
// Leds::can.turn_on();
33+
// };
34+
35+
// TSD
36+
void close_contactors() {
37+
Contactors::low.turn_on();
38+
39+
Time::set_timeout(1500, []() {
40+
Contactors::high.turn_on();
41+
Time::set_timeout(500, [&]() {
42+
Contactors::low.turn_off();
43+
Conditions::contactors_closed = true;
44+
Leds::can.turn_on();
45+
});
46+
});
47+
};
48+
49+
void turn_on_IMD() {
50+
OBCCU::imd.turn_on();
51+
Leds::full_charge.turn_on();
52+
};
53+
54+
void turn_off_IMD() {
55+
OBCCU::imd.turn_off();
56+
Leds::full_charge.turn_off();
57+
};
58+
59+
void reset() {
60+
NVIC_SystemReset();
61+
}
62+
}
63+
}

Core/Inc/OBCCU/Conditions.hpp

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#pragma once
2+
13
#include "BMS-LIB.hpp"
24

35
namespace OBCCU {

Core/Inc/OBCCU/Contactors.hpp

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#pragma once
2+
13
#include "BMS-LIB.hpp"
24

35
namespace OBCCU {

Core/Inc/OBCCU/IMD.hpp

+11
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ namespace OBCCU {
1414
double duty_cycle;
1515
PinState OK;
1616
bool drift;
17+
bool is_on;
1718

1819

1920
IMD() = default;
@@ -36,6 +37,16 @@ namespace OBCCU {
3637

3738
ok_sensor.read();
3839
}
40+
41+
void turn_on() {
42+
power.turn_on();
43+
is_on = true;
44+
}
45+
46+
void turn_off() {
47+
power.turn_off();
48+
is_on = false;
49+
}
3950
};
4051

4152
IMD imd;

Core/Inc/OBCCU/Leds.hpp

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#pragma once
2+
13
#include "BMS-LIB.hpp"
24

35
namespace OBCCU {

Core/Inc/OBCCU/Measurements.hpp

+1
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@ namespace OBCCU {
1010
double transformer_temperature;
1111
double rectifier_temperature;
1212
double average_current;
13+
float total_voltage;
1314
};
1415
};

Core/Inc/OBCCU/OBCCU.hpp

+22-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
#pragma once
2+
13
#include "BMS-LIB.hpp"
2-
#include "ST-LIB.hpp"
34
#include "OBCCU/Orders.hpp"
45
#include "OBCCU/IMD.hpp"
56
#include "OBCCU/Sensors.hpp"
@@ -8,7 +9,6 @@
89
#include "OBCCU/Protections.hpp"
910

1011
namespace OBCCU {
11-
IncomingOrders incoming_orders;
1212
UDP udp;
1313
TCP tcp;
1414
Packets packets;
@@ -18,14 +18,15 @@ namespace OBCCU {
1818
void update();
1919

2020
void inscribe() {
21+
imd = IMD(PE2, PF0, PF4);
2122
bms = BMSH(SPI::spi3);
2223
high_voltage_charger = HalfBridge(PE9, PE8, PE13, PE12, PD4);
2324

24-
imd = IMD(PE2, PF0, PF4);
25-
2625
StateMachines::general = StateMachine(States::General::CONNECTING);
2726
StateMachines::operational = StateMachine(States::Operational::IDLE);
2827
StateMachines::charging = StateMachine(States::Charging::PRECHARGE);
28+
StateMachines::contactors_sm = StateMachine(States::Contactors::OPEN);
29+
StateMachines::imd_sm = StateMachine(States::IMD::OFF);
2930

3031
Sensors::inscribe();
3132

@@ -45,13 +46,13 @@ namespace OBCCU {
4546
STLIB::start("192.168.1.9");
4647
udp.init();
4748
tcp.init();
49+
50+
// StateOrder::set_socket(tcp.backend);
51+
// StateOrder::set_socket(udp.backend);
4852

4953
bms.initialize();
5054
StateMachines::start();
5155

52-
ProtectionManager::set_id(Boards::ID::OBCCU);
53-
ProtectionManager::link_state_machine(StateMachines::general, States::General::FAULT);
54-
5556
int i = 0;
5657
for (LTC6811& adc : bms.external_adcs) {
5758
for (Battery& battery: adc.batteries) {
@@ -60,10 +61,21 @@ namespace OBCCU {
6061
}
6162
}
6263

63-
Time::set_timeout(1000, [&](){
64+
Time::set_timeout(5000, [&](){
6465
Conditions::ready = true;
66+
67+
StateMachines::general.refresh_state_orders();
68+
StateMachines::contactors_sm.refresh_state_orders();
69+
StateMachines::imd_sm.refresh_state_orders();
6570
});
71+
72+
Protections::inscribe();
73+
ProtectionManager::set_id(Boards::ID::OBCCU);
74+
ProtectionManager::link_state_machine(StateMachines::general, States::General::FAULT);
75+
6676
StateMachines::general.check_transitions();
77+
StateMachines::contactors_sm.check_transitions();
78+
StateMachines::imd_sm.check_transitions();
6779
}
6880

6981
void send_to_backend() {
@@ -79,6 +91,8 @@ namespace OBCCU {
7991
void update() {
8092
STLIB::update();
8193
StateMachines::general.check_transitions();
94+
StateMachines::imd_sm.check_transitions();
95+
StateMachines::contactors_sm.check_transitions();
8296

8397
if (Conditions::ready) {
8498
ProtectionManager::check_protections();

Core/Inc/OBCCU/Orders.hpp

+15-67
Original file line numberDiff line numberDiff line change
@@ -5,79 +5,27 @@
55
#include "OBCCU/Leds.hpp"
66
#include "OBCCU/Contactors.hpp"
77
#include "OBCCU/IMD.hpp"
8+
#include "OBCCU/Actions.hpp"
9+
#include "OBCCU/StateMachine.hpp"
810

911
namespace OBCCU {
10-
namespace Orders {
11-
void start_charging() {
12-
Conditions::want_to_charge = true;
13-
};
14-
15-
void stop_charging() {
16-
Conditions::want_to_charge = false;
17-
};
18-
19-
void open_contactors() {
20-
Contactors::high.turn_off();
21-
Contactors::low.turn_off();
22-
Conditions::contactors_closed = false;
23-
24-
Leds::can.turn_off();
25-
};
26-
27-
// void close_contactors() {
28-
// Contactors::low.turn_on();
29-
// Contactors::high.turn_on();
30-
// Conditions::contactors_closed = true;
31-
32-
// Leds::can.turn_on();
33-
// };
34-
35-
// TSD
36-
void close_contactors() {
37-
Contactors::low.turn_on();
38-
39-
Time::set_timeout(1500, []() {
40-
Contactors::high.turn_on();
41-
Time::set_timeout(500, [&]() {
42-
Contactors::low.turn_off();
43-
Conditions::contactors_closed = true;
44-
Leds::can.turn_on();
45-
});
46-
});
47-
};
48-
49-
void turn_on_IMD() {
50-
imd.power.turn_on();
51-
Leds::full_charge.turn_on();
52-
};
53-
54-
void turn_off_IMD() {
55-
imd.power.turn_off();
56-
Leds::full_charge.turn_off();
57-
};
58-
59-
void reset() {
60-
NVIC_SystemReset();
61-
}
62-
};
63-
6412
class IncomingOrders {
6513
public:
66-
HeapOrder start_charging_order;
67-
HeapOrder stop_charging_order;
68-
HeapOrder open_contactors_order;
69-
HeapOrder close_contactors_order;
70-
HeapOrder turn_on_IMD;
71-
HeapOrder turn_off_IMD;
14+
HeapStateOrder start_charging_order;
15+
HeapStateOrder stop_charging_order;
16+
HeapStateOrder open_contactors_order;
17+
HeapStateOrder close_contactors_order;
18+
HeapStateOrder turn_on_IMD;
19+
HeapStateOrder turn_off_IMD;
7220
HeapOrder reset_board;
7321

74-
IncomingOrders() : start_charging_order(900, Orders::start_charging),
75-
stop_charging_order(901, Orders::stop_charging),
76-
open_contactors_order(902, Orders::open_contactors),
77-
close_contactors_order(903, Orders::close_contactors),
78-
turn_on_IMD(904, Orders::turn_on_IMD),
79-
turn_off_IMD(905, Orders::turn_off_IMD),
80-
reset_board(906, Orders::reset) {}
22+
IncomingOrders() : start_charging_order(900, Actions::start_charging, StateMachines::operational, States::Operational::IDLE),
23+
stop_charging_order(901, Actions::stop_charging, StateMachines::operational, States::Operational::CHARGING),
24+
open_contactors_order(902, Actions::open_contactors, StateMachines::contactors_sm, States::Contactors::CLOSED),
25+
close_contactors_order(903, Actions::close_contactors, StateMachines::contactors_sm, States::Contactors::OPEN),
26+
turn_on_IMD(904, Actions::turn_on_IMD, StateMachines::imd_sm, States::IMD::OFF),
27+
turn_off_IMD(905, Actions::turn_off_IMD, StateMachines::imd_sm, States::IMD::ON),
28+
reset_board(906, Actions::reset) {}
8129
};
8230

8331
class TCP {

Core/Inc/OBCCU/Packets.hpp

+5-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
#include "BMS-LIB.hpp"
44
#include "OBCCU/Measurements.hpp"
5+
#include "OBCCU/IMD.hpp"
56

67
namespace OBCCU {
7-
float total_voltage;
88

99
struct battery_data {
1010
float* data[13];
@@ -46,6 +46,7 @@ namespace OBCCU {
4646
HeapPacket total_voltage_packet;
4747
array<HeapPacket*, 10> battery_packets;
4848
HeapPacket IMD_packet;
49+
HeapPacket conditions_packet;
4950

5051
Packets() :
5152
battery1_packet(910, &batteries_data[0].data),
@@ -59,8 +60,9 @@ namespace OBCCU {
5960
battery9_packet(918, &batteries_data[8].data),
6061
battery10_packet(919, &batteries_data[9].data),
6162
charging_current_packet(920, &Measurements::charging_current),
62-
total_voltage_packet(921, &OBCCU::total_voltage),
63-
IMD_packet(922, &OBCCU::imd.duty_cycle, &OBCCU::imd.frequency, &OBCCU::imd.OK, &OBCCU::imd.drift)
63+
total_voltage_packet(921, &Measurements::total_voltage),
64+
IMD_packet(922, &OBCCU::imd.duty_cycle, &OBCCU::imd.frequency, &OBCCU::imd.OK, &OBCCU::imd.drift),
65+
conditions_packet(923, &Conditions::ready, &Conditions::want_to_charge, &Conditions::fault, &Conditions::contactors_closed, &Conditions::first_read)
6466
{
6567
battery_packets = {&battery1_packet, &battery2_packet, &battery3_packet, &battery4_packet, &battery5_packet, &battery6_packet, &battery7_packet, &battery8_packet, &battery9_packet, &battery10_packet};
6668
}

Core/Inc/OBCCU/Sensors.hpp

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#pragma once
2+
13
#include "BMS-LIB.hpp"
24
#include "OBCCU/Measurements.hpp"
35

0 commit comments

Comments
 (0)