change ultrasonic sensor to analog type

This commit is contained in:
Siwat Sirichai 2022-11-14 08:15:28 +07:00
parent d6029b8cb8
commit d0999b8307
2 changed files with 10 additions and 18 deletions

View File

@ -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

View File

@ -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: