Skip to content

Commit

Permalink
minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
jdmarmen committed Jan 29, 2025
1 parent ff441c8 commit 5f602de
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 51 deletions.
2 changes: 1 addition & 1 deletion Core/Inc/Communication/Communication.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class Communication{
static bool received_disable_buffer;
static bool received_pwm_order;
static bool received_stop_pwm_order;
static uint32_t frequency_received;
static float frequency_received;
static float duty_cycle_received;
static PWM_ACTIVE pwm_received;
void send_UDP_packets();
Expand Down
2 changes: 1 addition & 1 deletion Core/Inc/Three_Phased_PWM/Three_Phased_PWM.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include "ST-LIB.hpp"
#include "Data/Data.hpp"
static constexpr uint32_t initial_frequency = 10000;
static constexpr std::chrono::nanoseconds dead_time_ns = static_cast<std::chrono::nanoseconds>(300);
static constexpr std::chrono::nanoseconds dead_time_ns(300);
class Three_Phased_PWM{
private:
DualPWM U_Dual;
Expand Down
4 changes: 2 additions & 2 deletions Core/Src/Communication/Communication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ bool Communication::received_disable_buffer = false;
bool Communication::received_pwm_order = false;
bool Communication::received_stop_pwm_order = false;
float Communication::duty_cycle_received = 0.0;
uint32_t Communication::frequency_received = 0;
float Communication::frequency_received = 0;
PWM_ACTIVE Communication::pwm_received = PWM_ACTIVE::NONE;

void received_enable_buffer_callback(){
Expand All @@ -21,7 +21,7 @@ void stop_pwm_order_callback(){
Communication::received_stop_pwm_order = true;
}
Communication::Communication(Data_struct *data): Data(data){
ControlStationSocket = new ServerSocket(Communication_Data::PCU_IP,Communication_Data::TCP_CLIENT);
ControlStationSocket = new ServerSocket(Communication_Data::PCU_IP,Communication_Data::TCP_SERVER);
datagramSocket = new DatagramSocket(Communication_Data::PCU_IP,Communication_Data::UDP_PORT_PCU,Communication_Data::Backend,Communication_Data::UDP_PORT);
Enable_Buffer_Order = new HeapOrder(Communication_Data::ENABLE_BUFFER_ORDER,&received_enable_buffer_callback);
Disable_Buffer_Order = new HeapOrder(Communication_Data::DISABLE_BUFFER_ORDER,&received_disable_buffer_callback);
Expand Down
4 changes: 2 additions & 2 deletions Core/Src/Runes/Runes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,8 @@ PWMmap TimerPeripheral::available_pwm = {
DualPWMmap TimerPeripheral::available_dual_pwms = {
{{PB8, PB6}, {timer16, {TIM_CHANNEL_1, NORMAL}}},
{{PB9, PB7}, {timer17, {TIM_CHANNEL_1, PHASED}}},
{{PE11, PE10}, {timer1, {TIM_CHANNEL_2, PHASED}}},
{{PE13, PE12}, {timer1, {TIM_CHANNEL_3, PHASED}}},
{{PE11, PE10}, {timer1, {TIM_CHANNEL_2, NORMAL}}},
{{PE13, PE12}, {timer1, {TIM_CHANNEL_3, NORMAL}}},
{{PE5, PE4}, {timer15, {TIM_CHANNEL_1, NORMAL}}},
{{PE9, PE8}, {timer1, {TIM_CHANNEL_1, NORMAL}}},
};
Expand Down
12 changes: 7 additions & 5 deletions Core/Src/StateMachinePCU/StateMachinePCU.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ three_phased_pwm(three_phased)
void StateMachinePCU::start(Communication *comms){
communication = comms;
three_phased_pwm->start();

stateMachine->add_low_precision_cyclic_action([this](){
communication->send_UDP_packets();
},ms(100));
}

void StateMachinePCU::add_states(){
Expand Down Expand Up @@ -65,19 +67,19 @@ void StateMachinePCU::update(){
three_phased_pwm->turn_off_active_pwm();
return;
case PWM_ACTIVE::U:
three_phased_pwm->set_frequency_u(Communication::frequency_received);
three_phased_pwm->set_frequency_u(static_cast<uint32_t>(Communication::frequency_received));
three_phased_pwm->set_duty_u(Communication::duty_cycle_received);
three_phased_pwm->turn_on_u();
return;
case PWM_ACTIVE::V:
three_phased_pwm->set_frequency_v(Communication::frequency_received);
three_phased_pwm->set_frequency_v(static_cast<uint32_t>(Communication::frequency_received));
three_phased_pwm->set_duty_v(Communication::duty_cycle_received);
three_phased_pwm->turn_on_v();
return;
case PWM_ACTIVE::W:
three_phased_pwm->set_frequency_w(Communication::frequency_received);
three_phased_pwm->set_frequency_w(static_cast<uint32_t>(Communication::frequency_received));
three_phased_pwm->set_duty_w(Communication::duty_cycle_received);
three_phased_pwm->turn_on_w();
three_phased_pwm->turn_on_w();
return;
}
}
Expand Down
69 changes: 30 additions & 39 deletions Core/Src/Three_Phased_PWM/Three_Phased_PWM.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "../../Inc/Three_Phased_PWM/Three_Phased_PWM.hpp"
using namespace std::chrono_literals;

Three_Phased_PWM::Three_Phased_PWM(Pin& u,Pin& u_negated,Pin& v,Pin& v_negated,Pin& w,Pin& w_negated,Pin& enable,Pin& reset,Data_struct *data):
U_Dual(u,u_negated),
Expand All @@ -9,21 +10,31 @@ Reset(reset),
data(data)
{}
void Three_Phased_PWM::start(){
U_Dual.set_dead_time(dead_time_ns);
V_Dual.set_dead_time(dead_time_ns);
W_Dual.set_dead_time(dead_time_ns);
U_Dual.set_frequency(initial_frequency);
V_Dual.set_frequency(initial_frequency);
W_Dual.set_frequency(initial_frequency);
U_Dual.set_dead_time(1us);
V_Dual.set_dead_time(1us);
W_Dual.set_dead_time(1us);
U_Dual.set_duty_cycle(0.0);
V_Dual.set_duty_cycle(0.0);
W_Dual.set_duty_cycle(0.0);
U_Dual.turn_on();
V_Dual.turn_on();
W_Dual.turn_on();


Enable.turn_on(); //works Active low
Reset.turn_on(); // has to be active to work
data->pwm_active = PWM_ACTIVE::NONE;
data->actual_duty = 0;
data->actual_frequency = 0.0;
}
void Three_Phased_PWM::stop_all(){
turn_off_u();
turn_off_v();
turn_off_w();
U_Dual.set_duty_cycle(0.0);
V_Dual.set_duty_cycle(0.0);
W_Dual.set_duty_cycle(0.0);
disable();
reset();
}
void Three_Phased_PWM::disable(){
Enable.turn_on();
Expand All @@ -36,33 +47,40 @@ void Three_Phased_PWM::enable(){
}
void Three_Phased_PWM::set_frequency_u(uint32_t frequency){
U_Dual.set_frequency(frequency);
data->actual_frequency = get_frequency_u();
}
void Three_Phased_PWM::set_frequency_v(uint32_t frequency){
V_Dual.set_frequency(frequency);
data->actual_frequency = get_frequency_v();
}
void Three_Phased_PWM::set_frequency_w(uint32_t frequency){
W_Dual.set_frequency(frequency);
data->actual_frequency = get_frequency_w();
}
void Three_Phased_PWM::set_duty_u(float duty_cycle){
if(duty_cycle < 0.0) duty_cycle = 0.0;
else if(duty_cycle > 100.0) duty_cycle = 100.0;

U_Dual.set_duty_cycle(duty_cycle);
data->actual_duty = get_duty_u();
}
void Three_Phased_PWM::set_duty_v(float duty_cycle){
if(duty_cycle < 0.0) duty_cycle = 0.0;
else if(duty_cycle > 100.0) duty_cycle = 100.0;

V_Dual.set_duty_cycle(duty_cycle);
data->actual_duty = get_duty_v();
}
void Three_Phased_PWM::set_duty_w(float duty_cycle){
if(duty_cycle < 0.0) duty_cycle = 0.0;
else if(duty_cycle > 100.0) duty_cycle = 100.0;

W_Dual.set_duty_cycle(duty_cycle);
data->actual_duty = get_duty_w();
}
float Three_Phased_PWM::Three_Phased_PWM::get_duty_u(){
return U_Dual.get_duty_cycle();
return U_Dual.get_duty_cycle();

}
float Three_Phased_PWM::get_duty_v(){
return V_Dual.get_duty_cycle();
Expand All @@ -79,69 +97,42 @@ uint32_t Three_Phased_PWM::get_frequency_v(){
uint32_t Three_Phased_PWM::get_frequency_w(){
return W_Dual.get_frequency();
}

/**/
void Three_Phased_PWM::turn_on_u(){
if(data->pwm_active == PWM_ACTIVE::U) return;

turn_off_active_pwm();
U_Dual.turn_on();
data->actual_duty = get_duty_u();
data->actual_frequency = get_frequency_u();
data->pwm_active = PWM_ACTIVE::U;
}
void Three_Phased_PWM::turn_on_w(){
if(data->pwm_active == PWM_ACTIVE::W) return;

turn_off_active_pwm();
W_Dual.turn_on();
data->actual_duty = get_duty_w();
data->actual_frequency = get_frequency_w();
data->pwm_active = PWM_ACTIVE::W;
}
void Three_Phased_PWM::turn_on_v(){
if(data->pwm_active == PWM_ACTIVE::V) return;

turn_off_active_pwm();
V_Dual.turn_on();
data->actual_duty = get_duty_v();
data->actual_frequency = get_frequency_v();
data->pwm_active = PWM_ACTIVE::V;
}
void Three_Phased_PWM::turn_off_u(){
U_Dual.turn_off();
U_Dual.set_duty_cycle(0.0);
data->actual_duty = 0.0;
data->actual_frequency = 0;
data->pwm_active = PWM_ACTIVE::NONE;
}
void Three_Phased_PWM::turn_off_v(){
V_Dual.turn_off();
V_Dual.set_duty_cycle(0.0);
data->actual_duty = 0.0;
data->actual_frequency = 0;
data->pwm_active = PWM_ACTIVE::NONE;
}
void Three_Phased_PWM::turn_off_w(){
W_Dual.turn_off();
W_Dual.set_duty_cycle(0.0);
data->actual_duty = 0.0;
data->actual_frequency = 0;
data->pwm_active = PWM_ACTIVE::NONE;
}
void Three_Phased_PWM::turn_off_active_pwm(){
switch (data->pwm_active)
{
case PWM_ACTIVE::NONE:
return;
case PWM_ACTIVE::U:
U_Dual.turn_off();
break;
case PWM_ACTIVE::V:
V_Dual.turn_off();
break;
case PWM_ACTIVE::W:
W_Dual.turn_off();
break;
}
data->pwm_active = PWM_ACTIVE::NONE;
}
void Three_Phased_PWM::reset(){
Reset.turn_off();
}
2 changes: 1 addition & 1 deletion Core/Src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ int main(void) {
Data_struct Data;
Three_Phased_PWM three_phased_pwm(Pinout::U_PWM,Pinout::U_PWM_NEGATED,Pinout::V_PWM,Pinout::V_PWM_NEGATED,Pinout::W_PWM,Pinout::W_PWM_NEGATED,Pinout::ENABLE_BUFFER,Pinout::Reset,&Data);
StateMachinePCU stateMachinePCU(&Data,&three_phased_pwm);
STLIB::start();
STLIB::start("192.168.0.5");
Communication comms(&Data);
stateMachinePCU.start(&comms);

Expand Down

0 comments on commit 5f602de

Please sign in to comment.