what are the issues with this code "from buildhat import Motor from gpiozero import…
- User
what are the issues with this code "from buildhat import Motor from gpiozero import SmoothedInputDevice from gpiozero import DigitalInputDevice from gpiozero import HoldMixin import time import sys
Line Finder Import
class LineFinder(SmoothedInputDevice): """ Simple Line Finder Class
Args: pin(int): the digital pin to which the line finder is assigned pull_up(bool): whether the button is set to pull up or down. This impacts whether the button returns a 0 or 1 when pressed and must be set to false for the wiring setup of the base hat. sample_rate(float): The number of values to read from the device per second. Defaults to 100. """ def __init__(self, pin=None, *, pull_up=False, active_state=None, queue_len=1, sample_rate=5, threshold=0.5, partial=False, pin_factory=None): super().__init__( pin, pull_up=pull_up, active_state=active_state, threshold=threshold, queue_len=queue_len, sample_wait=1 / sample_rate, partial=partial, pin_factory=pin_factory) self._queue.start() @property def value(self): """ Returns a value representing the average of the queued values. This is 0 for black under the sensor, and 1 for white under the sensor. """ val = super().value print(f"Sensor Value: {'White (1)' if val == 1 else 'Black (0)'}") return val @property def line_detected(self): detected = not self.is_active print(f"Line Detected: {'Black' if detected else 'White'}") return detectedHall Sensor Import
class HallSensor(HoldMixin, DigitalInputDevice): """ Simple Hall Sensor Class
Args: pin(int): the digital pin to which the sensor is assigned pull_up(bool): whether the sensor is set to pull up or down. This impacts whether the sensor returns a 0 or 1 when near a magnet and must be set to false for the wiring setup of the base hat. bounce_time(float): The length of time (in seconds) after the button is pressed during which new inputs are ignored. This minimizes 'bouncy' signals, where the output of the button changes rapidly as it is being pressed. The base time for this is 1 second. """ def __init__(self, pin=None, *, pull_up=True, active_state=None, bounce_time=None, pin_factory=None): super().__init__( pin, pull_up=pull_up, active_state=active_state, bounce_time=bounce_time, pin_factory=pin_factory) @property def value(self): """ Returns 1 if the button is currently pressed, and 0 if it is not. """ return super().valueMain Function
def main(): lPin = 18 rPin = 26 halPin = 24
print("System booting") lMotor = Motor('A') backMotor = Motor('C') motor = Motor('D') rLineReader = LineFinder(rPin) lLineReader = LineFinder(lPin) hallSen = HallSensor(halPin) lineValueR = 1 lineValueL = 1 halValue = 0 halCounter = 0 # Change this value to determine which landing zone the robot should go over landZone = 1 readyToDeliver = 0 motor_activated = False # Initialize the motor activation flag try: while True: try: lineValueR = rLineReader.line_detected lineValueL = lLineReader.line_detected hallValue = hallSen.value print(f"Right Sensor: {lineValueR}, Left Sensor: {lineValueL}") print(f"Hall Sensor: {hallValue}") print(f"Magnet count: {halCounter}") lMotor.start(40) # Incrementing counter if magnet is detected if hallValue: halCounter += 1 # If proper magnet count is reached, turn robot to deliver if halCounter == landZone: readyToDeliver = 1 lMotor.stop() backMotor.run_for_rotations(0.2) time.sleep(0.3) lMotor.start(35) time.sleep(0.3) lMotor.stop() time.sleep(0.2) backMotor.run_for_rotations(-0.2) # Returns False over white, True over black if lineValueR and not lineValueL: lMotor.stop() backMotor.run_for_rotations(0.14) # was 0.125 time.sleep(0.3) lMotor.start(45) time.sleep(1) lMotor.stop() time.sleep(0.2) backMotor.run_for_rotations(-0.14) elif not lineValueR and lineValueL: lMotor.stop() backMotor.run_for_rotations(-0.14) time.sleep(0.3) lMotor.start(45) time.sleep(1) lMotor.stop() time.sleep(0.2) backMotor.run_for_rotations(0.14) # Code for magnets & moving the cargo motor if hallValue != 0: print("NOT DETECTED - Motor OFF") motor.stop() else: if not motor_activated: print("DETECTED - Activating Motor") motor_activated = True # Set the flag to prevent re-activation motor.run_for_rotations(0.18, speed=100) motor.stop() time.sleep(0.8) motor.run_for_rotations(-0.18, speed=100) else: print("DETECTED - Motor has already been activated. Ignoring further detections.") exit() except IOError: print("Error reading") backMotor.stop() lMotor.stop() break except KeyboardInterrupt: print("Ending") backMotor.stop() lMotor.stop() - Assistant