This commit is contained in:
Rudi klein 2024-12-29 14:46:53 +01:00
parent f175b29eda
commit 2d671257e6
2 changed files with 22 additions and 23 deletions

2
.idea/misc.xml generated
View File

@ -3,5 +3,5 @@
<component name="Black">
<option name="sdkName" value="Python 3.11 (pid-balancer)" />
</component>
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.11 (pid-balancer)" project-jdk-type="Python SDK" />
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.11 (pid-balancer) (2)" project-jdk-type="Python SDK" />
</project>

View File

@ -14,7 +14,6 @@ import csv # CSV handling
from datetime import datetime # Date and time formatting
import time # Time formatting
from serial.tools.list_ports_osx import kCFNumberSInt8Type
######################################## Variables (start) ##################################
# Variables to control sensor
@ -36,20 +35,20 @@ SCREEN: bool = True # Log data to screen
DEBUG: bool = False # More data to display
# Variables to assist PID calculations
current_time: float = 0
integral: float = 0
time_prev: float = -1e-6
error_prev: float = 0
current_time = 0
integral = 0
time_prev = -1e-6
error_prev = 0
# Variables to control PID values (PID formula tweaks)
p_value: float = 2
i_value: float = 0
d_value: float = 0
p_value = 2
i_value = 0
d_value = 0
# Initial variables, used in pid_calculations()
i_result: float = 0
previous_time: float
previous_error: float
i_result = 0
previous_time = 0
previous_error = 0
# Init array, used in read_distance_sensor()
sample_array: list = []
@ -153,18 +152,18 @@ def pid_calculations(setpoint):
error_sum: float = 0.0
if previous_time is None:
previous_error: float = 0.0
previous_time: float = current_time
i_result: float = 0.0
error_sum: float = error * 0.008 # sensor sampling number approximation.
previous_error = 0.0
previous_time = current_time
i_result = 0.0
error_sum = error * 0.008 # sensor sampling number approximation.
error_sum: float = error_sum + (error * (current_time - previous_time))
p_result: float = p_value * error
i_result: float = i_value * error_sum
d_result: float = d_value * ((error - previous_error) / (current_time - previous_time))
pid_result: float = offset_value + p_result + i_result + d_result
previous_error: float = error
previous_time: float = current_time
error_sum = error_sum + (error * (current_time - previous_time))
p_result = p_value * error
i_result = i_value * error_sum
d_result = d_value * ((error - previous_error) / (current_time - previous_time))
pid_result = offset_value + p_result + i_result + d_result
previous_error = error
previous_time = current_time
return pid_result