示例#1
0
class main:
    def __init__(self):
        """
        Sets up program variables, resets files, does not tell arduino to start
        """
        GPIO.setmode(GPIO.BCM)
        self.ei = ErrorIndicator(False)
        self.ih = ImageHandler(False, self.ei)
        self.processor = ImageProcessor(self.ih, self.ei)
        self.arduino = Arduino(self.ei)
        GPIO.setup(24, GPIO.IN, pull_up_down=GPIO.PUD_UP)
        GPIO.setup(25, GPIO.IN)
        GPIO.setwarnings(False)

        if os.path.exists("out/"):
            shutil.rmtree("out")
        os.makedirs("out")
        if os.path.exists("log.txt"):
            os.remove("log.txt")

        logging.basicConfig(filename='log.txt',
                            level=logging.DEBUG,
                            format='%(asctime)s %(message)s',
                            datefmt='%m/%d/%Y %I:%M:%S %p')

    def check_for_gps_fix(self):
        """
        Checks for GPS fix, flashes error if not
        :return: True if GPS has a fix
        """
        if not main.arduino.gps_has_fix():
            logging.info("Waiting for GPS fix..")
            self.ei.message([0, 0, 0, 1])
            time.sleep(0.5)
            self.ei.message([0, 0, 0, 0])
            time.sleep(0.5)
            return False
        return True

    def check_arduino(self, initial):
        """
        Checks to make sure arduino sensors are working, no check for GPS fix
        :param initial: First time being called, checks if comms with arduino work
        :return: If no errors are present
        """
        success = True
        if initial:
            if not main.arduino.init():
                self.ei.message([1, 0, 0, 0])
                return False
        if main.arduino.get_dof_error():
            self.ei.message([1, 1, 1, 0])
            success = False
        if main.arduino.get_gps_error():
            self.ei.message([0, 1, 1, 0])
            success = False
        if success:
            self.ei.reset()
        return success

    def check_pi(self):  # TODO handle exception issue
        """
        Checks if the camera on Pi is working
        :return: False if camera is too dark
        """
        if self.ih.camera_error():
            self.ei.message([1, 1, 0, 0])
            return False
        self.ei.reset()
        return True

    def run(self, count):
        """
        Method run while lander is in decent
        :param count: Number of loops called (Used for logger)
        """
        logging.info("*********************" + str(count) +
                     "*********************")
        current_altitude = self.arduino.get_altitude() * 3.28084  # ft
        logging.info("Current Altitude: " + str(current_altitude))

        current_distance = self.arduino.get_distance() * 3.28084  # ft
        logging.info("Current Distance: " + str(current_distance))

        total_tarp_area = Tc.get_total_tarp_area(current_altitude,
                                                 current_distance)  # pixels^3

        logging.info("Creating Background Mask")
        self.processor.create_background_mask()
        logging.info("Filtering by size")
        self.processor.filter_by_size(total_tarp_area)
        logging.info("Getting Tarp Mask")
        self.processor.get_tarps()
        logging.info("Saving Tarps")
        self.processor.save_tarps(count)

    @staticmethod
    def told_to_start():  # Wire GPIO 24 to button to ground
        """
        Determines if switch is on, telling us to start the arduino and enter standby
        :return: True if switch is closed
        """
        if not GPIO.input(24):
            return True
        return False

    @staticmethod
    def told_to_end():  # Wire GPIO 24 to button to ground
        """
        Determines if switch is off, telling us to ensure loops are ended and to save log
        :return: True if switch is open
        """
        if GPIO.input(24):
            return True
        return False

    def starting_notification(self):
        self.ei.message([1, 1, 1, 1])
        self.ei.turn_buzzer_on()
        time.sleep(5)
        self.ei.turn_buzzer_off()
        self.ei.reset()

    def second_notification(self):
        for i in range(0, 10):
            self.ei.message([1, 1, 1, 1])
            self.ei.turn_buzzer_on()
            time.sleep(0.5)
            self.ei.turn_buzzer_off()
            self.ei.reset()

    def third_notification(self):
        self.ei.message([1, 1, 1, 1])
        self.ei.turn_buzzer_on()
        time.sleep(5)
        self.ei.turn_buzzer_off()
        self.ei.reset()

    @staticmethod
    def is_in_rocket():  # Connect photoresistor to GPIO 25
        """
        Checks if lander is in rocket by using a photoresistor
        :return: True if in rocket
        """
        if GPIO.input(25):
            return True
        else:
            return False

    def is_at_low_altitude(self):
        """
        Checks if lander is under 10m
        :return: True if under 10m in air
        """
        if self.arduino.get_altitude() < 10:
            false_positive = False
            for i in range(0, 10):
                if main.arduino.get_altitude(
                ) > 10:  # So a goofy misread doesn't screw us
                    false_positive = True
            if not false_positive:
                return True

    def init(self):
        """
        Initializes arduino, turning all sensors on and placing in standby
        :return: True if arduino and pi have no errors (No check for GPS)
        """
        return self.check_arduino(True) and self.check_pi()