From f3a02c8db376597ccad6bbca690897e6ab9b5d4b Mon Sep 17 00:00:00 2001 From: rudi Date: Sun, 12 Jan 2025 17:36:19 +0100 Subject: [PATCH] added all the code to use the new laser sensor. Its like 5 lines of code. --- Examples/Adafruit_Servokit_servo_driver.py | 8 ++------ control_functions.py | 18 +++++++++++------- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/Examples/Adafruit_Servokit_servo_driver.py b/Examples/Adafruit_Servokit_servo_driver.py index e6e6e3e..b4aa024 100644 --- a/Examples/Adafruit_Servokit_servo_driver.py +++ b/Examples/Adafruit_Servokit_servo_driver.py @@ -20,12 +20,8 @@ kit.servo[0].set_pulse_width_range(MIN_PULSE, MAX_PULSE) # property angle: float | None # The servo angle in degrees. Must be in the range 0 to actuation_range. # Is None when servo is disabled. -while True: - kit.servo[0].angle = 110 - sleep(1) - kit.servo[0].angle = 90 - sleep(0.1) - print("test") + +kit.servo[0].angle = 90 diff --git a/control_functions.py b/control_functions.py index faa60c3..c45c40e 100644 --- a/control_functions.py +++ b/control_functions.py @@ -1,5 +1,3 @@ -from csv import excel - 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 @@ -8,10 +6,15 @@ 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 -from datetime import datetime # Date and time formatting from time import sleep # Sleep/pause import pandas as pd from datetime import datetime +import busio +import adafruit_vl6180x + +# laser sensor controls. +i2c = busio.I2C(board.SCL, board.SDA) +laser = adafruit_vl6180x.VL6180X(i2c) # Variables to control sensor TRIGGER_PIN = board.D4 # GPIO pin xx @@ -29,10 +32,10 @@ OFFSET: int = -1 KIT.servo[0].set_pulse_width_range(MIN_PULSE, MAX_PULSE) # Variables to control logging. -LOG: bool = True # Log data to files +LOG: bool = True # Log data to files SCREEN: bool = True # Log data to screen DEBUG: bool = False # More data to display -TWIN_MODE: bool = False +TWIN_MODE: bool = False # Run in live or twin mode # Control the number of samples for single distance measurement (average from burst) MAX_SAMPLES: int = 1 @@ -114,7 +117,9 @@ def read_distance_sensor(): while samples != max_samples: sleep(RUN_TIMEOUT) try: - distance: float = sonar.distance + distance: float = sonar.distance # reading distance from the sonic sensor + # distance: float = laser.range * 10 # reading distance from the laser sensor + if MIN_DISTANCE < distance < MAX_DISTANCE: log_data(data_file="sensor", data_line=str(distance), remark="") if LOG else None @@ -334,7 +339,6 @@ def servo_slower(): return servo_angle - try: with open("pid-balancer_" + "time_file.txt", "w") as time_file: time_file.write(datetime.strftime(datetime.now(), '%Y%m%d%H%M%S.%f')[:-3])