From d0999b83072e1a99eee9277ba082b10af4c732d4 Mon Sep 17 00:00:00 2001 From: Siwat Sirichai Date: Mon, 14 Nov 2022 08:15:28 +0700 Subject: [PATCH] change ultrasonic sensor to analog type --- kuukar/kuukar_config.py | 9 +++------ kuukar/kuukar_sensors.py | 19 +++++++------------ 2 files changed, 10 insertions(+), 18 deletions(-) diff --git a/kuukar/kuukar_config.py b/kuukar/kuukar_config.py index 64783da..18ddfc5 100644 --- a/kuukar/kuukar_config.py +++ b/kuukar/kuukar_config.py @@ -2,12 +2,9 @@ SERIAL_MCU = "COM4" SERIAL_DRV = "COM20" # HC Sonar Pins -SONAR_1_TRIG_PIN = 0 -SONAR_1_ECHO_PIN = 1 -SONAR_2_TRIG_PIN = 2 -SONAR_2_ECHO_PIN = 3 -SONAR_3_TRIG_PIN = 4 -SONAR_3_ECHO_PIN = 5 +SONAR_1_ADC_PIN = 0 +SONAR_2_ADC_PIN = 1 +SONAR_3_ADC_PIN = 2 # Motor Pins MOTOR_FL_F = 7 diff --git a/kuukar/kuukar_sensors.py b/kuukar/kuukar_sensors.py index 03fff7a..e3b27a7 100644 --- a/kuukar/kuukar_sensors.py +++ b/kuukar/kuukar_sensors.py @@ -1,14 +1,12 @@ from telemetrix_rpi_pico import telemetrix_rpi_pico -from kuukar.kuukar_config import DHT22_PIN, SONAR_1_ECHO_PIN, SONAR_1_TRIG_PIN,\ - SONAR_2_ECHO_PIN, SONAR_2_TRIG_PIN, SONAR_3_ECHO_PIN, SONAR_3_TRIG_PIN,\ - LIGHT_ANALOG_PIN +from kuukar.kuukar_config import DHT22_PIN, SONAR_1_ADC_PIN, SONAR_2_ADC_PIN, SONAR_3_ADC_PIN, LIGHT_ANALOG_PIN class sensors: - __sonar_trig_pins = [SONAR_1_TRIG_PIN, - SONAR_2_TRIG_PIN, SONAR_3_TRIG_PIN] + __sonar_adc_pins = [SONAR_1_ADC_PIN, + SONAR_2_ADC_PIN, SONAR_3_ADC_PIN] __sonar_distances = [0, 0, 0] __temperature = 0 __humidity = 0 @@ -16,12 +14,9 @@ class sensors: def __init__(self, mcu: telemetrix_rpi_pico.TelemetrixRpiPico) -> None: self.mcu = mcu - self.mcu.set_pin_mode_sonar( - SONAR_1_TRIG_PIN, SONAR_1_ECHO_PIN, self.__sonar_callback) - self.mcu.set_pin_mode_sonar( - SONAR_2_TRIG_PIN, SONAR_2_ECHO_PIN, self.__sonar_callback) - self.mcu.set_pin_mode_sonar( - SONAR_3_TRIG_PIN, SONAR_3_ECHO_PIN, self.__sonar_callback) + self.mcu.set_pin_mode_analog_input(SONAR_1_ADC_PIN, 10, self.__sonar_callback) + self.mcu.set_pin_mode_analog_input(SONAR_2_ADC_PIN, 10, self.__sonar_callback) + self.mcu.set_pin_mode_analog_input(SONAR_3_ADC_PIN, 10, self.__sonar_callback) self.mcu.set_pin_mode_dht(DHT22_PIN, self.__dht22_callback) self.mcu.set_pin_mode_analog_input(LIGHT_ANALOG_PIN, 0, self.__light_sensor_callback) @@ -31,7 +26,7 @@ class sensors: def __sonar_callback(self, data): pin = data[1] distance = data[2] - sonar_id = self.__sonar_trig_pins.index(pin) + sonar_id = self.__sonar_adc_pins.index(pin) self.__sonar_distances[sonar_id] = distance def get_brightness_pct(self) -> float: