From 17cdb8ca93c091a6ff9e0769ba5d0f5d044ace19 Mon Sep 17 00:00:00 2001 From: Carsten Keller Date: Sun, 9 Jun 2024 17:57:49 +0200 Subject: [PATCH] =?UTF-8?q?PID-Controller=20f=C3=BCr=20Stromregelung?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Source/ElektronischeLast.cpp | 44 +++++++++++++++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/Source/ElektronischeLast.cpp b/Source/ElektronischeLast.cpp index ce3b2b8..74dea1a 100644 --- a/Source/ElektronischeLast.cpp +++ b/Source/ElektronischeLast.cpp @@ -7,29 +7,71 @@ #include #include +#include #include #include "STM32G071KBT6.hpp" #include "LED.hpp" #include "DAC.hpp" #include "ADC.hpp" #include "serial.hpp" +#include "PID.h" using namespace ElektronischeLast; +static std::uint32_t i_soll = 0U; +static PIDController pid = +{ + .Kp = 0.75f, + .Ki = 10.0f, + .Kd = 0.0f, + .limMin = 0.0f, + .limMax = 4095.0f, + .limMinInt = 0.0f, + .limMaxInt = 2048.0f, + .T = 0.001f, +}; +static uint32_t dac_value; +static struct +{ + uint32_t current_min; + uint32_t current_max; +} current_spitze; +static struct +{ + uint32_t voltage_min; + uint32_t voltage_max; +} voltage_spitze; + int main (void) { + __enable_irq(); serial_init(); LED led = LED(500U); iDAC dac = iDAC(); iADC adc = iADC(); + std::uint32_t last_tick = systick; printf("\r\nElektronische Last\r\n"); printf("- Initialisierung erfolgreich\r\n"); + PIDController_Init(&pid); + while(1) { led.blink(); - dac.write(iDAC::CHANNEL_1, adc.get_current()); + + if(last_tick != systick) + { + last_tick = systick; + uint32_t tmp = adc.get_current(); + if(tmp > current_spitze.current_max) current_spitze.current_max = tmp; + if(tmp < current_spitze.current_min) current_spitze.current_min = tmp; + tmp = adc.get_voltage(); + if(tmp > voltage_spitze.voltage_max) voltage_spitze.voltage_max = tmp; + if(tmp < voltage_spitze.voltage_min) voltage_spitze.voltage_min = tmp; + dac_value = (uint32_t)PIDController_Update(&pid, i_soll, adc.get_current()); + dac.write(iDAC::CHANNEL_1, dac_value); + } } }