Untitled
unknown
plain_text
10 months ago
4.6 kB
5
Indexable
import board
import busio
import adafruit_ads1x15.ads1115 as ADS
from adafruit_ads1x15.analog_in import AnalogIn
from adafruit_tca9548a import TCA9548A
from gpiozero import DistanceSensor
import time
from datetime import datetime
from rich.console import Console
from rich.panel import Panel
from rich.layout import Layout
from rich.live import Live
from rich.table import Table
class DualSensorSystem:
def __init__(self, ultrasonic_trigger=17, ultrasonic_echo=27, distance_threshold=0.2):
# Initialize the console for rich formatting
self.console = Console()
# Initialize Laser Beam Sensor
self.init_laser_sensor()
# Initialize Ultrasonic Sensor
self.ultrasonic = DistanceSensor(
echo=ultrasonic_echo,
trigger=ultrasonic_trigger
)
self.distance_threshold = distance_threshold
# Detection states
self.laser_detected = False
self.ultrasonic_detected = False
def init_laser_sensor(self):
i2c_bus = busio.I2C(board.SCL, board.SDA)
self.multiplexer = TCA9548A(i2c_bus, address=0x70)
self.channel = self.multiplexer[7]
self.ads = ADS.ADS1115(self.channel)
self.ads.gain = 2/3
self.ldr = AnalogIn(self.ads, ADS.P0)
self.baseline = None
self.threshold = 0.7
def calibrate_laser(self, samples=10, delay=0.1):
"""Calibrate the laser sensor"""
self.console.print("[yellow]Calibrating laser sensor - Please ensure laser is properly aligned...[/]")
readings = []
for i in range(samples):
readings.append(self.read_light_level())
time.sleep(delay)
self.baseline = sum(readings) / len(readings)
self.console.print(f"[green]Calibration complete. Baseline light level: {self.baseline:.1f}%[/]")
def read_light_level(self):
"""Get light level as percentage"""
voltage = self.ldr.voltage
return (voltage / 3.3) * 100
def is_beam_broken(self):
"""Check if laser beam is broken"""
if self.baseline is None:
return False
current_level = self.read_light_level()
return current_level < (self.baseline * self.threshold)
def get_distance(self):
"""Get distance in cm from ultrasonic sensor"""
return self.ultrasonic.distance * 100
def create_status_table(self):
"""Create rich table with current status"""
table = Table(show_header=False, border_style="bold")
# Current time
table.add_row(
"[cyan]Time[/]",
datetime.now().strftime("%H:%M:%S")
)
# Laser sensor status
light_level = self.read_light_level()
laser_status = "[red]BROKEN[/]" if self.is_beam_broken() else "[green]CLEAR[/]"
table.add_row(
"[cyan]Laser Beam[/]",
f"{laser_status} ({light_level:.1f}%)"
)
# Ultrasonic sensor status
distance = self.get_distance()
ultrasonic_status = "[red]DETECTED[/]" if distance < (self.distance_threshold * 100) else "[green]CLEAR[/]"
table.add_row(
"[cyan]Ultrasonic[/]",
f"{ultrasonic_status} ({distance:.1f} cm)"
)
# Combined detection status
combined = self.is_beam_broken() or distance < (self.distance_threshold * 100)
table.add_row(
"[cyan]Overall Status[/]",
"[red]OBJECT DETECTED[/]" if combined else "[green]NO OBJECT DETECTED[/]"
)
return table
def main():
try:
# Initialize the system
system = DualSensorSystem()
# Calibrate the laser sensor
system.calibrate_laser()
# Clear the screen
system.console.clear()
with Live(
Panel(system.create_status_table(), title="[bold blue]Dual Sensor Detection System[/]"),
refresh_per_second=4
) as live:
while True:
live.update(
Panel(system.create_status_table(), title="[bold blue]Dual Sensor Detection System[/]")
)
time.sleep(0.25)
except KeyboardInterrupt:
system.console.print("\n[yellow]Shutting down sensors...[/]")
except Exception as e:
system.console.print(f"\n[red]Error: {e}[/]")
if __name__ == "__main__":
main()Editor is loading...
Leave a Comment