Skip to content

Commit

Permalink
Added CurrentSensor
Browse files Browse the repository at this point in the history
  • Loading branch information
oganigl committed Feb 7, 2025
1 parent 464bc83 commit 32b086b
Show file tree
Hide file tree
Showing 11 changed files with 112 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Core/Inc/Communication/Communication.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#pragma once
#include "ST-LIB.hpp"
#include "Data/Data.hpp"
class Communication{
private:
Expand All @@ -18,6 +17,7 @@ class Communication{
HeapOrder *Stop_space_vector;
HeapPacket *Pwm_packet;
HeapPacket *batteries_Packet;
HeapPacket *Current_sensor_Packet;
public:
Communication(Data_struct *data);
static bool received_enable_buffer;
Expand Down
29 changes: 29 additions & 0 deletions Core/Inc/Data/Data.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,27 @@ namespace Pinout{
static constexpr Pin& Batt_Voltage_B = PF12;
static constexpr Pin& LED_COMMUTION = PG6;
static constexpr Pin& LED_FAULT = PG7;
//current sensors
static constexpr Pin& CURRENT_SENSOR_U_A = PA0;
static constexpr Pin& CURRENT_SENSOR_U_B = PA6;
static constexpr Pin& CURRENT_SENSOR_V_A = PA4;
static constexpr Pin& CURRENT_SENSOR_V_B = PB0;
static constexpr Pin& CURRENT_SENSOR_W_A =PA5;
static constexpr Pin& CURRENT_SENSOR_W_B = PB1;
};
namespace Current_Sensors{
static constexpr float slope_u_a = 1;
static constexpr float offset_u_a = 0;
static constexpr float slope_v_a = 1;
static constexpr float offset_v_a = 0;
static constexpr float slope_w_a = 1;
static constexpr float offset_w_a = 0;
static constexpr float slope_u_b = 1;
static constexpr float offset_u_b = 0;
static constexpr float slope_v_b = 1;
static constexpr float offset_v_b = 0;
static constexpr float slope_w_b = 1;
static constexpr float offset_w_b = 0;
};

namespace Communication_Data{
Expand All @@ -33,6 +54,7 @@ namespace Communication_Data{
static constexpr uint16_t STOP_SPACE_VECTOR_ORDER = 508;
static constexpr uint16_t PWM_PACKET = 550;
static constexpr uint16_t BATTERIES_PACKET = 551;
static constexpr uint16_t CURRENT_SENSOR_PACKET = 552;
};
enum class PWM_ACTIVE: uint8_t{
NONE = 0,
Expand All @@ -56,4 +78,11 @@ struct Data_struct{
//batteries
float actual_voltage_batteries{};
Battery_Connector connector_Batteries = Battery_Connector::A;
//current sensor
float actual_current_sensor_u_a{};
float actual_current_sensor_u_b{};
float actual_current_sensor_v_a{};
float actual_current_sensor_v_b{};
float actual_current_sensor_w_a{};
float actual_current_sensor_w_b{};
};
16 changes: 16 additions & 0 deletions Core/Inc/Sensors/CurrentSensors.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#pragma once
#include "Data/Data.hpp"
class CurrentSensors{
private:
Data_struct *data;
LinearSensor<float> sensor_u_a{Pinout::CURRENT_SENSOR_U_A,Current_Sensors::slope_u_a,Current_Sensors::offset_u_a,&data->actual_current_sensor_u_a};
LinearSensor<float> sensor_u_b{Pinout::CURRENT_SENSOR_U_B,Current_Sensors::slope_u_b,Current_Sensors::offset_u_b,&data->actual_current_sensor_u_b};
LinearSensor<float> sensor_v_a{Pinout::CURRENT_SENSOR_V_A,Current_Sensors::slope_v_a,Current_Sensors::offset_v_a,&data->actual_current_sensor_v_a};
LinearSensor<float> sensor_v_b{Pinout::CURRENT_SENSOR_V_B,Current_Sensors::slope_v_b,Current_Sensors::offset_v_b,&data->actual_current_sensor_v_b};
LinearSensor<float> sensor_w_a{Pinout::CURRENT_SENSOR_W_A,Current_Sensors::slope_w_a,Current_Sensors::offset_w_a,&data->actual_current_sensor_w_a};
LinearSensor<float> sensor_w_b{Pinout::CURRENT_SENSOR_W_B,Current_Sensors::slope_w_b,Current_Sensors::offset_w_b,&data->actual_current_sensor_w_b};
void zeroing();
public:
CurrentSensors(Data_struct *data);
void read();
};
12 changes: 12 additions & 0 deletions Core/Inc/Sensors/Sensors.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#pragma once
#include "Data/Data.hpp"
#include "Sensors/CurrentSensors.hpp"
class Sensors{
private:
Data_struct *data;
CurrentSensors currentSensors;
public:
Sensors(Data_struct *data);
void read();

};
4 changes: 3 additions & 1 deletion Core/Inc/StateMachinePCU/StateMachinePCU.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "ST-LIB.hpp"
#include "Communication/Communication.hpp"
#include "Three_Phased_PWM/Three_Phased_PWM.hpp"
#include "Sensors/Sensors.hpp"
#include "Control/SpaceVector.hpp"

enum State_PCU{
Expand All @@ -20,11 +21,12 @@ class StateMachinePCU{
StateMachine *operationalStateMachine;
Data_struct *Data;
Three_Phased_PWM *three_phased_pwm;
Sensors *sensors;
Communication *communication;
SpaceVector *spaceVectorControl;
public:
static bool space_vector_on;
StateMachinePCU(Data_struct *data, Three_Phased_PWM *three_phased,SpaceVector *spVec);
StateMachinePCU(Data_struct *data, Three_Phased_PWM *three_phased,Sensors *sensors,SpaceVector *spVec);
void start(Communication *comms);
void add_states();
void add_transitions();
Expand Down
2 changes: 2 additions & 0 deletions Core/Src/Communication/Communication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,12 @@ Communication::Communication(Data_struct *data): Data(data){
Stop_space_vector = new HeapOrder(Communication_Data::STOP_SPACE_VECTOR_ORDER,&received_stop_space_vector_callback);
Pwm_packet = new HeapPacket(Communication_Data::PWM_PACKET,&Data->pwm_active,&Data->actual_frequency,&Data->actual_duty,&Data->buffer_enable);
batteries_Packet = new HeapPacket(Communication_Data::BATTERIES_PACKET,&Data->actual_voltage_batteries,&Data->connector_Batteries);
Current_sensor_Packet = new HeapPacket(Communication_Data::CURRENT_SENSOR_PACKET,&Data->actual_current_sensor_u_a,&Data->actual_current_sensor_v_a,&Data->actual_current_sensor_w_a,&Data->actual_current_sensor_u_b,&Data->actual_current_sensor_v_b,&Data->actual_current_sensor_w_b);
}
void Communication::send_UDP_packets(){
datagramSocket->send_packet(*Pwm_packet);
datagramSocket->send_packet(*batteries_Packet);
datagramSocket->send_packet(*Current_sensor_Packet);
}

bool Communication::is_connected(){
Expand Down
37 changes: 37 additions & 0 deletions Core/Src/Sensors/CurrentSensors.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include "Sensors/CurrentSensors.hpp"
#define TIMES_TO_CREATE_ZERO 10000
CurrentSensors::CurrentSensors(Data_struct *data):data(data){
zeroing();
}
void CurrentSensors::zeroing(){
float new_offset_u_a = 0;
float new_offset_u_b = 0;
float new_offset_v_a = 0;
float new_offset_v_b = 0;
float new_offset_w_a = 0;
float new_offset_w_b = 0;
for(int i = 1; i < TIMES_TO_CREATE_ZERO; i++){
read();
new_offset_u_a = (new_offset_u_a * (i - 1) + data->actual_current_sensor_u_a)/i;
new_offset_u_b = (new_offset_u_b * (i - 1) + data->actual_current_sensor_u_b)/i;
new_offset_v_a = (new_offset_v_a * (i - 1) + data->actual_current_sensor_v_a)/i;
new_offset_v_b = (new_offset_v_b * (i - 1) + data->actual_current_sensor_v_b)/i;
new_offset_w_a = (new_offset_w_a * (i - 1) + data->actual_current_sensor_w_a)/i;
new_offset_w_b = (new_offset_w_b * (i - 1) + data->actual_current_sensor_w_b)/i;
}
//once we have the average of 10000 thousand values
sensor_u_a.set_offset(new_offset_u_a);
sensor_u_b.set_offset(new_offset_u_b);
sensor_v_a.set_offset(new_offset_v_a);
sensor_v_b.set_offset(new_offset_v_b);
sensor_w_a.set_offset(new_offset_w_a);
sensor_w_b.set_offset(new_offset_w_b);
}
void CurrentSensors::read(){
sensor_u_a.read();
sensor_u_b.read();
sensor_v_a.read();
sensor_v_b.read();
sensor_w_a.read();
sensor_w_b.read();
}
7 changes: 7 additions & 0 deletions Core/Src/Sensors/Sensors.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include "Sensors/Sensors.hpp"

Sensors::Sensors(Data_struct *data):data(data),currentSensors(data){}

void Sensors::read(){
currentSensors.read();
}
3 changes: 2 additions & 1 deletion Core/Src/StateMachinePCU/StateMachinePCU.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@


bool StateMachinePCU::space_vector_on = false;
StateMachinePCU::StateMachinePCU(Data_struct *data, Three_Phased_PWM *three_phased,SpaceVector *spVec):
StateMachinePCU::StateMachinePCU(Data_struct *data, Three_Phased_PWM *three_phased,Sensors *sensors,SpaceVector *spVec):
Data(data),
three_phased_pwm(three_phased),
sensors(sensors),
spaceVectorControl(spVec)
{
stateMachine = new StateMachine(State_PCU::Connecting);
Expand Down
3 changes: 2 additions & 1 deletion Core/Src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ int main(void) {
SharedMemory::start();
#endif
Data_struct Data;
Sensors sensors(&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,Pinout::Batt_Voltage_A,Pinout::Batt_Voltage_B,Pinout::LED_COMMUTION,&Data);
SpaceVector spaceVec(&three_phased_pwm);
StateMachinePCU stateMachinePCU(&Data,&three_phased_pwm,&spaceVec);
StateMachinePCU stateMachinePCU(&Data,&three_phased_pwm,&sensors,&spaceVec);
STLIB::start("192.168.0.5");
Communication comms(&Data);
stateMachinePCU.start(&comms);
Expand Down

0 comments on commit 32b086b

Please sign in to comment.