2024-12-29 21:58:19 +01:00
|
|
|
from adafruit_hcsr04 import HCSR04 as hcsr04 # Ultrasound sensor
|
|
|
|
|
import board # General board pin mapper
|
|
|
|
|
from adafruit_servokit import ServoKit # Servo libraries for PWM driver board
|
2024-12-28 21:06:10 +01:00
|
|
|
import adafruit_pcf8591.pcf8591 as PCF # AD/DA converter board for potentiometer
|
|
|
|
|
from adafruit_pcf8591.analog_in import AnalogIn # Analogue in pin library
|
|
|
|
|
from adafruit_pcf8591.analog_out import AnalogOut # Analogue out pin library
|
|
|
|
|
import statistics as st # Mean and median calculations
|
|
|
|
|
import csv # CSV handling
|
2025-01-04 17:50:27 +01:00
|
|
|
from time import sleep # Sleep/pause
|
2025-01-06 20:54:51 +01:00
|
|
|
import pandas as pd
|
2025-01-10 19:27:37 +01:00
|
|
|
from datetime import datetime
|
2025-01-12 17:36:19 +01:00
|
|
|
import busio
|
|
|
|
|
import adafruit_vl6180x
|
2025-01-13 22:04:04 +01:00
|
|
|
import math
|
2025-01-12 17:36:19 +01:00
|
|
|
|
|
|
|
|
# laser sensor controls.
|
2025-01-13 22:04:04 +01:00
|
|
|
# i2c = busio.I2C(board.SCL, board.SDA)
|
|
|
|
|
# laser = adafruit_vl6180x.VL6180X(i2c)
|
2024-12-30 18:58:40 +01:00
|
|
|
|
2024-12-25 16:52:07 +01:00
|
|
|
# Variables to control sensor
|
2024-12-28 21:06:10 +01:00
|
|
|
TRIGGER_PIN = board.D4 # GPIO pin xx
|
|
|
|
|
ECHO_PIN = board.D17 # GPIO pin xx
|
2025-01-05 22:02:02 +01:00
|
|
|
PIN_TIMEOUT: float = 0.1 # Timeout for echo wait -- don't change
|
|
|
|
|
RUN_TIMEOUT: float = 0.0 # Sleep time in function
|
2025-01-08 15:25:41 +01:00
|
|
|
MIN_DISTANCE: int = 6 # Minimum sensor distance to be considered valid (1 on bar)
|
2025-01-10 19:27:37 +01:00
|
|
|
MAX_DISTANCE: int = 40 # Maximum sensor distance to be considered valid (35 on bar)
|
2024-12-28 21:06:10 +01:00
|
|
|
|
|
|
|
|
# Variables to control servo
|
|
|
|
|
KIT = ServoKit(channels=16) # Define the type of board (8, 16)
|
2025-01-04 17:50:27 +01:00
|
|
|
MIN_PULSE: int = 400 # Defines angle 80, for current PID setup
|
|
|
|
|
MAX_PULSE: int = 2500 # Defines angle 100, for current PID setup
|
2025-01-10 19:27:37 +01:00
|
|
|
OFFSET: int = -1
|
2024-12-28 21:06:10 +01:00
|
|
|
KIT.servo[0].set_pulse_width_range(MIN_PULSE, MAX_PULSE)
|
2024-12-25 16:52:07 +01:00
|
|
|
|
|
|
|
|
# Variables to control logging.
|
2025-01-12 17:36:19 +01:00
|
|
|
LOG: bool = True # Log data to files
|
2024-12-28 21:06:10 +01:00
|
|
|
SCREEN: bool = True # Log data to screen
|
2025-01-10 19:27:37 +01:00
|
|
|
DEBUG: bool = False # More data to display
|
2025-01-13 22:04:04 +01:00
|
|
|
TWIN_MODE: bool = True # Run in live or twin mode
|
2024-12-28 21:06:10 +01:00
|
|
|
|
2025-01-04 17:50:27 +01:00
|
|
|
# Control the number of samples for single distance measurement (average from burst)
|
2025-01-10 19:27:37 +01:00
|
|
|
MAX_SAMPLES: int = 1
|
2025-01-04 17:50:27 +01:00
|
|
|
|
|
|
|
|
# Control the potentiometer
|
|
|
|
|
# Description:
|
|
|
|
|
# POT_MIN = min_scaled: 0.012890821698329136 (0.01V)
|
|
|
|
|
# POT_MAX = max_scaled: 3.28715953307393000 (3.29V)
|
|
|
|
|
# POT_RNG = range_scaled: 3.274268711375600864 (3.28V) -> POT_MAX - POT_MIN
|
|
|
|
|
# POT_ARM = usable_arm_range: 35cm
|
|
|
|
|
# POT_PCM = 35 / 3.274268711375600864 = 10.689409784359341315326937965383 -> POT_ARM / POT_RNG
|
|
|
|
|
PCF_VAL: int = 65535
|
|
|
|
|
POT_MIN: float = 0.012890821698329136
|
|
|
|
|
POT_MAX: float = 3.287159533073930000
|
|
|
|
|
POT_RNG: float = 3.274268711375600864
|
|
|
|
|
POT_ARM: int = 35
|
|
|
|
|
POT_PCM: float = 10.689409784359341315326937965383
|
|
|
|
|
POT_INT: float = 0.1
|
|
|
|
|
|
|
|
|
|
# Pin control potentiometer board
|
|
|
|
|
i2c = board.I2C()
|
|
|
|
|
pcf = PCF.PCF8591(i2c)
|
|
|
|
|
pcf_in_0 = AnalogIn(pcf, PCF.A0)
|
|
|
|
|
pcf_out = AnalogOut(pcf, PCF.OUT)
|
|
|
|
|
pcf_out.value = PCF_VAL
|
2024-12-29 21:58:19 +01:00
|
|
|
|
2024-12-28 21:06:10 +01:00
|
|
|
# Variables to control PID values (PID formula tweaks)
|
2025-01-10 19:27:37 +01:00
|
|
|
p_value: float = 0.5
|
|
|
|
|
i_value: float = 0.01
|
2024-12-29 14:30:37 +01:00
|
|
|
d_value: float = 0.0
|
2024-12-25 16:52:07 +01:00
|
|
|
|
2024-12-28 21:06:10 +01:00
|
|
|
# Initial variables, used in pid_calculations()
|
2024-12-29 14:30:37 +01:00
|
|
|
i_result: float = 0.0
|
|
|
|
|
previous_time: float = 0.0
|
|
|
|
|
previous_error: float = 0.0
|
2024-12-25 16:52:07 +01:00
|
|
|
|
2025-01-06 20:54:51 +01:00
|
|
|
# Error sum array
|
2025-01-10 19:27:37 +01:00
|
|
|
error_sum_max: int = 10
|
|
|
|
|
error_sum_array: list = [0]*error_sum_max
|
2025-01-06 20:54:51 +01:00
|
|
|
error_sum_counter: int = 0
|
2025-01-10 19:27:37 +01:00
|
|
|
|
2025-01-06 20:54:51 +01:00
|
|
|
|
|
|
|
|
# Digital twin
|
|
|
|
|
previous_speed:float = 0.0
|
2025-01-13 22:04:04 +01:00
|
|
|
previous_position: float = 0.0
|
|
|
|
|
previous_angle: int = 90
|
2025-01-06 20:54:51 +01:00
|
|
|
|
2025-01-10 19:27:37 +01:00
|
|
|
#maximum angle the servo can move away from steady position. With 10 the range is between 80 and 100, with steady at 90
|
2025-01-13 22:04:04 +01:00
|
|
|
max_angle = 10
|
2025-01-10 19:27:37 +01:00
|
|
|
|
|
|
|
|
# servo slower
|
|
|
|
|
current_angle:int = 90
|
|
|
|
|
|
2025-01-13 22:04:04 +01:00
|
|
|
watch_variable: int = 0
|
|
|
|
|
|
|
|
|
|
# base time of the system
|
|
|
|
|
base_time: float = float(datetime.strftime(datetime.now(), '%Y%m%d%H%M%S.%f'))
|
|
|
|
|
|
2024-12-28 21:06:10 +01:00
|
|
|
# Write data to any of the logfiles
|
2025-01-05 22:02:02 +01:00
|
|
|
def log_data(data_file: str, data_line: str, remark: str|None):
|
2024-12-29 14:30:37 +01:00
|
|
|
log_stamp: str = datetime.strftime(datetime.now(), '%Y%m%d%H%M%S.%f')[:-3]
|
2024-12-28 21:06:10 +01:00
|
|
|
|
2025-01-05 22:02:02 +01:00
|
|
|
with open("pid-balancer_" + "time_file.txt", "r") as time_file:
|
|
|
|
|
file_stamp: str = time_file.readline()
|
|
|
|
|
|
2024-12-29 14:30:37 +01:00
|
|
|
with open("pid-balancer_" + data_file + "_data_" + file_stamp + ".csv", "a") as data_file:
|
2024-12-28 21:06:10 +01:00
|
|
|
data_writer = csv.writer(data_file)
|
|
|
|
|
data_writer.writerow([log_stamp,data_line, remark])
|
2024-12-25 16:52:07 +01:00
|
|
|
|
2025-01-04 17:50:27 +01:00
|
|
|
def read_distance_sensor():
|
2025-01-10 19:27:37 +01:00
|
|
|
start_time = float(datetime.strftime(datetime.now(), '%Y%m%d%H%M%S.%f'))
|
|
|
|
|
|
2025-01-08 15:25:41 +01:00
|
|
|
# Init array, used in read_distance_sensor()
|
|
|
|
|
sample_array: list = []
|
2024-12-25 16:52:07 +01:00
|
|
|
|
2025-01-05 22:02:02 +01:00
|
|
|
# Do a burst (MAX_SAMPLES) of measurements, filter out the obvious wrong ones (too short or to long a distance)
|
2024-12-29 14:30:37 +01:00
|
|
|
# Return the mean timestamp and median distance.
|
2025-01-05 22:02:02 +01:00
|
|
|
with hcsr04(trigger_pin=TRIGGER_PIN, echo_pin=ECHO_PIN, timeout=PIN_TIMEOUT) as sonar:
|
2024-12-28 21:06:10 +01:00
|
|
|
samples: int = 0
|
2024-12-29 14:30:37 +01:00
|
|
|
max_samples: int = MAX_SAMPLES
|
2025-01-04 17:50:27 +01:00
|
|
|
timestamp_last: float = 0.0
|
2024-12-28 21:06:10 +01:00
|
|
|
timestamp_first: float = 0.0
|
2025-01-04 17:50:27 +01:00
|
|
|
|
2024-12-27 16:10:25 +01:00
|
|
|
while samples != max_samples:
|
2025-01-05 22:02:02 +01:00
|
|
|
sleep(RUN_TIMEOUT)
|
2024-12-25 16:52:07 +01:00
|
|
|
try:
|
2025-01-12 17:36:19 +01:00
|
|
|
distance: float = sonar.distance # reading distance from the sonic sensor
|
|
|
|
|
# distance: float = laser.range * 10 # reading distance from the laser sensor
|
|
|
|
|
|
2024-12-25 16:52:07 +01:00
|
|
|
if MIN_DISTANCE < distance < MAX_DISTANCE:
|
|
|
|
|
|
2025-01-05 22:02:02 +01:00
|
|
|
log_data(data_file="sensor", data_line=str(distance), remark="") if LOG else None
|
2025-01-08 15:25:41 +01:00
|
|
|
# print("Distance_in_range: ", distance) if SCREEN else None
|
2025-01-10 19:27:37 +01:00
|
|
|
if max_samples == 1:
|
|
|
|
|
median_distance = distance
|
|
|
|
|
mean_timestamp = float(datetime.strftime(datetime.now(),'%Y%m%d%H%M%S.%f')[:-3])
|
|
|
|
|
else:
|
|
|
|
|
sample_array.append(distance)
|
|
|
|
|
if samples == 0: timestamp_first = float(datetime.strftime(datetime.now(),
|
|
|
|
|
'%Y%m%d%H%M%S.%f')[:-3])
|
|
|
|
|
if samples == max_samples - 1:
|
|
|
|
|
timestamp_last = float(datetime.strftime(datetime.now(),
|
|
|
|
|
'%Y%m%d%H%M%S.%f')[:-3])
|
|
|
|
|
|
|
|
|
|
timestamp_first_float: float = float(timestamp_first)
|
|
|
|
|
timestamp_last_float: float = float(timestamp_last)
|
|
|
|
|
median_distance: float = st.median(sample_array)
|
|
|
|
|
mean_timestamp: float = st.mean([timestamp_first_float, timestamp_last_float])
|
|
|
|
|
print("Distance_median: ", median_distance) if SCREEN else None
|
|
|
|
|
print("Timestamp_mean: ", mean_timestamp) if DEBUG else None
|
|
|
|
|
print("Distance_in_range: ", distance) if SCREEN else None
|
|
|
|
|
|
|
|
|
|
data_line = str(sample_array) + ',' + str(median_distance)
|
|
|
|
|
log_data(data_file="sensor_array", data_line= data_line,
|
|
|
|
|
remark="") if LOG else None
|
|
|
|
|
print("Distance_in_range_rounded: ", round(distance, 4)) if SCREEN else None
|
|
|
|
|
|
|
|
|
|
samples: int = samples + 1
|
2024-12-27 16:10:25 +01:00
|
|
|
|
2024-12-25 16:52:07 +01:00
|
|
|
else:
|
2025-01-10 19:27:37 +01:00
|
|
|
log_data(data_file="sensor", data_line=str(distance), remark="Distance_out_of_range") if LOG else None
|
2025-01-05 22:02:02 +01:00
|
|
|
print("Distance_out_of_range: ", round(distance, 4)) if SCREEN else None
|
2024-12-25 16:52:07 +01:00
|
|
|
|
|
|
|
|
except RuntimeError:
|
2025-01-05 22:02:02 +01:00
|
|
|
log_data(data_file="sensor", data_line="999.999", remark="Timeout") if LOG and DEBUG else None
|
|
|
|
|
print("Distance_timed_out") if SCREEN else None
|
2024-12-25 16:52:07 +01:00
|
|
|
|
2025-01-10 19:27:37 +01:00
|
|
|
end_time = float(datetime.strftime(datetime.now(), '%Y%m%d%H%M%S.%f'))
|
|
|
|
|
data_line = str(start_time - end_time)
|
|
|
|
|
log_data(data_file="function", data_line=data_line, remark="read_distance_sensor") if LOG else None
|
|
|
|
|
|
2024-12-27 16:10:25 +01:00
|
|
|
return median_distance, mean_timestamp
|
|
|
|
|
|
2025-01-04 17:50:27 +01:00
|
|
|
def read_setpoint():
|
2025-01-10 19:27:37 +01:00
|
|
|
start_time = float(datetime.strftime(datetime.now(), '%Y%m%d%H%M%S.%f'))
|
2024-12-28 21:06:10 +01:00
|
|
|
|
|
|
|
|
while True:
|
2024-12-30 18:58:40 +01:00
|
|
|
raw_value: int = pcf_in_0.value
|
2025-01-04 17:50:27 +01:00
|
|
|
scaled_value: float = (raw_value / PCF_VAL) * pcf_in_0.reference_voltage
|
|
|
|
|
|
|
|
|
|
log_line = str(scaled_value) + "," + str(raw_value) + "," + str("angle")
|
2025-01-05 22:02:02 +01:00
|
|
|
log_data(data_file="potmeter", data_line=log_line, remark="") if LOG else None
|
|
|
|
|
|
|
|
|
|
cm_rounded: int = int(round(scaled_value * POT_PCM, 0))
|
2024-12-28 21:06:10 +01:00
|
|
|
|
2025-01-10 19:27:37 +01:00
|
|
|
if DEBUG:
|
2025-01-05 22:02:02 +01:00
|
|
|
print('Scaled_rounded = ' , round(scaled_value, 4), ' CM_rounded= ', cm_rounded)
|
|
|
|
|
print('Scaled_raw= ' , scaled_value, ' CM_raw= ', int(scaled_value * POT_PCM))
|
|
|
|
|
|
2025-01-10 19:27:37 +01:00
|
|
|
print('setpoing in cm: ', cm_rounded) if SCREEN else None
|
|
|
|
|
|
2025-01-04 17:50:27 +01:00
|
|
|
sleep(POT_INT)
|
2025-01-10 19:27:37 +01:00
|
|
|
|
|
|
|
|
end_time = float(datetime.strftime(datetime.now(), '%Y%m%d%H%M%S.%f'))
|
|
|
|
|
data_line = str(start_time - end_time)
|
|
|
|
|
log_data(data_file="function", data_line=data_line, remark="read_setpoint") if LOG else None
|
|
|
|
|
|
2025-01-05 22:02:02 +01:00
|
|
|
return cm_rounded
|
|
|
|
|
|
2025-01-13 22:04:04 +01:00
|
|
|
def digital_twin():
|
|
|
|
|
# a: acceleration
|
|
|
|
|
# g: gravity (9.81 m/s^2)
|
|
|
|
|
# theta: angle of the inclined plane
|
|
|
|
|
# u: coefficient of the friction between the cart and the inclined plane.
|
|
|
|
|
acceleration: float = 0.0
|
|
|
|
|
global previous_position, previous_speed, base_time, watch_variable
|
|
|
|
|
gravity: float = 9.81
|
|
|
|
|
friction: float = 0.1
|
|
|
|
|
delta_t: float = 0.1
|
2025-01-05 22:02:02 +01:00
|
|
|
|
2025-01-13 22:04:04 +01:00
|
|
|
angle = (previous_angle - 90)
|
|
|
|
|
acceleration = gravity * math.sin(math.radians(angle))
|
|
|
|
|
friction_force = friction * gravity * math.cos(math.radians(angle)) * delta_t
|
2025-01-10 19:27:37 +01:00
|
|
|
|
2025-01-13 22:04:04 +01:00
|
|
|
friction_force = abs(friction_force)
|
2025-01-10 19:27:37 +01:00
|
|
|
|
2025-01-13 22:04:04 +01:00
|
|
|
work_speed = previous_speed + acceleration * delta_t
|
|
|
|
|
watch_variable = watch_variable + 1
|
2024-12-25 16:52:07 +01:00
|
|
|
|
2025-01-13 22:04:04 +01:00
|
|
|
if watch_variable >= 150:
|
|
|
|
|
print("breakpoint")
|
2024-12-30 18:58:40 +01:00
|
|
|
|
2025-01-13 22:04:04 +01:00
|
|
|
print("watch_variable", watch_variable)
|
|
|
|
|
if friction_force < work_speed:
|
|
|
|
|
if work_speed > 0:
|
|
|
|
|
work_speed = work_speed - friction_force
|
|
|
|
|
elif work_speed < 0:
|
|
|
|
|
work_speed = work_speed + friction_force
|
|
|
|
|
else:
|
|
|
|
|
work_speed = work_speed
|
2025-01-05 22:02:02 +01:00
|
|
|
|
2025-01-13 22:04:04 +01:00
|
|
|
current_speed = work_speed
|
|
|
|
|
|
|
|
|
|
current_position = previous_position + (current_speed * delta_t)
|
2024-12-25 16:52:07 +01:00
|
|
|
|
2025-01-13 22:04:04 +01:00
|
|
|
|
|
|
|
|
print("angle", angle)
|
|
|
|
|
print("friction", friction)
|
|
|
|
|
print("acceleration", acceleration)
|
|
|
|
|
print("current speed", current_speed)
|
|
|
|
|
print("current position", current_position)
|
|
|
|
|
print("")
|
|
|
|
|
print("----------------")
|
|
|
|
|
print("")
|
|
|
|
|
|
|
|
|
|
base_time = base_time + delta_t
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
previous_speed = current_speed
|
|
|
|
|
previous_position = current_position
|
|
|
|
|
|
|
|
|
|
return current_position, base_time
|
2025-01-10 19:27:37 +01:00
|
|
|
|
|
|
|
|
def pid_calculations():
|
|
|
|
|
start_time = float(datetime.strftime(datetime.now(), '%Y%m%d%H%M%S.%f'))
|
2024-12-30 18:58:40 +01:00
|
|
|
|
2025-01-04 17:50:27 +01:00
|
|
|
global i_result, previous_time, previous_error # Can not be annotated with :float, because variables are global.
|
2025-01-06 20:54:51 +01:00
|
|
|
global error_sum_counter, error_sum_array # counter for error_sum_array and error_sum_array itself
|
2025-01-13 22:04:04 +01:00
|
|
|
global previous_angle
|
2025-01-06 20:54:51 +01:00
|
|
|
offset_value: int = 0
|
|
|
|
|
if TWIN_MODE:
|
|
|
|
|
measurement, measurement_time = digital_twin()
|
|
|
|
|
else:
|
|
|
|
|
measurement, measurement_time = read_distance_sensor()
|
|
|
|
|
|
2025-01-10 19:27:37 +01:00
|
|
|
setpoint = read_setpoint()
|
2025-01-04 17:50:27 +01:00
|
|
|
error = setpoint - measurement
|
2024-12-27 16:10:25 +01:00
|
|
|
|
|
|
|
|
if previous_time is None:
|
2024-12-29 14:30:37 +01:00
|
|
|
previous_error = 0.0
|
2025-01-06 20:54:51 +01:00
|
|
|
previous_time = measurement_time
|
2024-12-29 14:30:37 +01:00
|
|
|
i_result = 0.0
|
2024-12-27 16:10:25 +01:00
|
|
|
|
2025-01-06 20:54:51 +01:00
|
|
|
error_sum_array[error_sum_counter] = (error * (measurement_time - previous_time))
|
|
|
|
|
p_result = p_value * error
|
|
|
|
|
i_result = i_value * sum(error_sum_array)
|
|
|
|
|
d_result = d_value * ((error - previous_error) / (measurement_time - previous_time))
|
|
|
|
|
pid_result = offset_value + p_result + i_result + d_result
|
2024-12-29 14:30:37 +01:00
|
|
|
previous_error = error
|
|
|
|
|
previous_time = measurement_time
|
2024-12-27 16:10:25 +01:00
|
|
|
|
2025-01-10 19:27:37 +01:00
|
|
|
|
|
|
|
|
#function to set the 2 max angles. Or set the angle to a specific number = pid_result * max movement + correction
|
|
|
|
|
if pid_result >= max_angle: # if PID result is greater than 1, set to 1. 1 = max upward angle
|
|
|
|
|
output_angle = (90 + max_angle)
|
|
|
|
|
elif pid_result <= -max_angle: # if PID result is greater than 1, set to 1. 1 = max downward angle
|
|
|
|
|
output_angle = (90-max_angle)
|
|
|
|
|
elif -max_angle < pid_result < max_angle:
|
|
|
|
|
output_angle = pid_result + 90
|
|
|
|
|
else:
|
|
|
|
|
output_angle = 90
|
|
|
|
|
|
2024-12-30 18:58:40 +01:00
|
|
|
log_line = str(p_result) + "," + str(i_result) + "," + str(d_result) + "," + str(pid_result)
|
2025-01-05 22:02:02 +01:00
|
|
|
log_data(data_file="pid", data_line=log_line, remark="") if LOG else None
|
|
|
|
|
|
2025-01-10 19:27:37 +01:00
|
|
|
if DEBUG:
|
2025-01-05 22:02:02 +01:00
|
|
|
print("P_result: ", p_result)
|
|
|
|
|
print("D_result: ", d_result)
|
|
|
|
|
print("I_result: ", i_result)
|
2025-01-06 20:54:51 +01:00
|
|
|
print("PID_result: ", pid_result)
|
|
|
|
|
|
2025-01-10 19:27:37 +01:00
|
|
|
if error_sum_counter <= error_sum_max-2:
|
2025-01-06 20:54:51 +01:00
|
|
|
error_sum_counter = error_sum_counter + 1
|
|
|
|
|
else:
|
|
|
|
|
error_sum_counter = 0
|
2025-01-05 22:02:02 +01:00
|
|
|
|
2025-01-10 19:27:37 +01:00
|
|
|
print("error sum counter", error_sum_counter) if DEBUG else None
|
|
|
|
|
|
|
|
|
|
end_time = float(datetime.strftime(datetime.now(), '%Y%m%d%H%M%S.%f'))
|
|
|
|
|
data_line = str(start_time - end_time)
|
|
|
|
|
log_data(data_file="function", data_line=data_line, remark="pid_calculations") if LOG else None
|
|
|
|
|
|
|
|
|
|
output_angle = round(output_angle)
|
2025-01-13 22:04:04 +01:00
|
|
|
previous_angle = output_angle
|
2025-01-10 19:27:37 +01:00
|
|
|
|
|
|
|
|
return output_angle
|
2024-12-25 16:52:07 +01:00
|
|
|
|
2025-01-04 17:50:27 +01:00
|
|
|
def control_server_angle(angle):
|
2025-01-10 19:27:37 +01:00
|
|
|
start_time = float(datetime.strftime(datetime.now(), '%Y%m%d%H%M%S.%f'))
|
|
|
|
|
|
|
|
|
|
servo_angle = angle + OFFSET
|
|
|
|
|
|
|
|
|
|
KIT.servo[0].angle = servo_angle # Set angle
|
2025-01-04 17:50:27 +01:00
|
|
|
log_line = str(angle)
|
2025-01-05 22:02:02 +01:00
|
|
|
log_data(data_file="servo", data_line=log_line, remark="") if LOG else None
|
2025-01-10 19:27:37 +01:00
|
|
|
print("angle: ", servo_angle) if SCREEN else None
|
|
|
|
|
|
|
|
|
|
end_time = float(datetime.strftime(datetime.now(), '%Y%m%d%H%M%S.%f'))
|
|
|
|
|
data_line = str(start_time - end_time)
|
|
|
|
|
log_data(data_file="function", data_line=data_line, remark="control_server_angle") if LOG else None
|
2025-01-06 20:54:51 +01:00
|
|
|
|
2025-01-10 19:27:37 +01:00
|
|
|
def servo_slower():
|
|
|
|
|
start_time = float(datetime.strftime(datetime.now(), '%Y%m%d%H%M%S.%f'))
|
|
|
|
|
|
|
|
|
|
global current_angle
|
|
|
|
|
pid_angle = pid_calculations()
|
|
|
|
|
if (pid_angle - current_angle) > 5:
|
|
|
|
|
servo_angle = current_angle + 5
|
|
|
|
|
elif (pid_angle - current_angle) < -5:
|
|
|
|
|
servo_angle = current_angle - 5
|
|
|
|
|
else:
|
|
|
|
|
servo_angle = pid_angle
|
|
|
|
|
|
|
|
|
|
current_angle = servo_angle
|
|
|
|
|
|
|
|
|
|
end_time = float(datetime.strftime(datetime.now(), '%Y%m%d%H%M%S.%f'))
|
|
|
|
|
data_line = str(start_time - end_time)
|
|
|
|
|
log_data(data_file="function", data_line=data_line, remark="servo_slower") if LOG else None
|
|
|
|
|
|
|
|
|
|
return servo_angle
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
KIT.servo[0].angle = 90
|
|
|
|
|
while True:
|
2025-01-13 22:04:04 +01:00
|
|
|
# digital_twin()
|
|
|
|
|
control_server_angle(pid_calculations())
|
2025-01-10 19:27:37 +01:00
|
|
|
except RuntimeError:
|
|
|
|
|
print("bbbb")
|