refactor
This commit is contained in:
parent
f175b29eda
commit
2d671257e6
2
.idea/misc.xml
generated
2
.idea/misc.xml
generated
@ -3,5 +3,5 @@
|
|||||||
<component name="Black">
|
<component name="Black">
|
||||||
<option name="sdkName" value="Python 3.11 (pid-balancer)" />
|
<option name="sdkName" value="Python 3.11 (pid-balancer)" />
|
||||||
</component>
|
</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>
|
</project>
|
||||||
@ -14,7 +14,6 @@ import csv # CSV handling
|
|||||||
from datetime import datetime # Date and time formatting
|
from datetime import datetime # Date and time formatting
|
||||||
import time # Time formatting
|
import time # Time formatting
|
||||||
|
|
||||||
from serial.tools.list_ports_osx import kCFNumberSInt8Type
|
|
||||||
|
|
||||||
######################################## Variables (start) ##################################
|
######################################## Variables (start) ##################################
|
||||||
# Variables to control sensor
|
# Variables to control sensor
|
||||||
@ -36,20 +35,20 @@ SCREEN: bool = True # Log data to screen
|
|||||||
DEBUG: bool = False # More data to display
|
DEBUG: bool = False # More data to display
|
||||||
|
|
||||||
# Variables to assist PID calculations
|
# Variables to assist PID calculations
|
||||||
current_time: float = 0
|
current_time = 0
|
||||||
integral: float = 0
|
integral = 0
|
||||||
time_prev: float = -1e-6
|
time_prev = -1e-6
|
||||||
error_prev: float = 0
|
error_prev = 0
|
||||||
|
|
||||||
# Variables to control PID values (PID formula tweaks)
|
# Variables to control PID values (PID formula tweaks)
|
||||||
p_value: float = 2
|
p_value = 2
|
||||||
i_value: float = 0
|
i_value = 0
|
||||||
d_value: float = 0
|
d_value = 0
|
||||||
|
|
||||||
# Initial variables, used in pid_calculations()
|
# Initial variables, used in pid_calculations()
|
||||||
i_result: float = 0
|
i_result = 0
|
||||||
previous_time: float
|
previous_time = 0
|
||||||
previous_error: float
|
previous_error = 0
|
||||||
|
|
||||||
# Init array, used in read_distance_sensor()
|
# Init array, used in read_distance_sensor()
|
||||||
sample_array: list = []
|
sample_array: list = []
|
||||||
@ -153,18 +152,18 @@ def pid_calculations(setpoint):
|
|||||||
error_sum: float = 0.0
|
error_sum: float = 0.0
|
||||||
|
|
||||||
if previous_time is None:
|
if previous_time is None:
|
||||||
previous_error: float = 0.0
|
previous_error = 0.0
|
||||||
previous_time: float = current_time
|
previous_time = current_time
|
||||||
i_result: float = 0.0
|
i_result = 0.0
|
||||||
error_sum: float = error * 0.008 # sensor sampling number approximation.
|
error_sum = error * 0.008 # sensor sampling number approximation.
|
||||||
|
|
||||||
error_sum: float = error_sum + (error * (current_time - previous_time))
|
error_sum = error_sum + (error * (current_time - previous_time))
|
||||||
p_result: float = p_value * error
|
p_result = p_value * error
|
||||||
i_result: float = i_value * error_sum
|
i_result = i_value * error_sum
|
||||||
d_result: float = d_value * ((error - previous_error) / (current_time - previous_time))
|
d_result = d_value * ((error - previous_error) / (current_time - previous_time))
|
||||||
pid_result: float = offset_value + p_result + i_result + d_result
|
pid_result = offset_value + p_result + i_result + d_result
|
||||||
previous_error: float = error
|
previous_error = error
|
||||||
previous_time: float = current_time
|
previous_time = current_time
|
||||||
|
|
||||||
return pid_result
|
return pid_result
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user