class DepthSensor:
    def __init__(self):
        self.sensor = ms5837.MS5837_02BA(
        )  # Default I2C bus is 1 (Raspberry Pi 3)
        self.client = Client(DEPTH_DRIVER_PORT)

        self.logger = DEFLOG.DEPTH_LOCAL_LOG
        if self.logger:
            self.logger = Logger(filename='depth_sensor',
                                 directory=DEFLOG.LOG_DIRECTORY)

    def run(self):
        # We must initialize the sensor before reading it
        if not self.sensor.init():
            error = "Sensor could not be initialized"
            self.log(error, 'error')
            print(error)
            exit(1)

        # We have to read values from sensor to update pressure and temperature
        if not self.sensor.read():
            error = "Sensor read failed!"
            self.log(error, 'error')
            print(error)
            exit(1)

        loop_condition = True
        while loop_condition:
            if self.sensor.read():
                self.client.send_data(self.sensor.depth())

                msg = str(self.sensor.depth())
                if self.logger:
                    self.logger.log(msg)
            else:
                loop_condition = False

    def __del__(self):
        self.logger.exit()

    def log(self, msg, logtype='info'):
        if self.logger:
            self.logger.log(msg, logtype=logtype)
Exemple #2
0
class Sonar:
    def __init__(self, USB_device_adress=USB.SONAR):
        self.logger = DEFLOG.DISTANCE_LOCAL_LOG
        if DEFLOG.DISTANCE_LOCAL_LOG:
            self.logger = Logger(filename='front_distance',
                                 directory=DEFLOG.LOG_DIRECTORY)

        self.sensor = Ping1D(USB_device_adress, 115200)
        self.log("connected to the device")
        self.client = Client(DISTANCE_DRIVER_PORT)
        self.log("connected to the server")

    def run(self):
        # We must initialize the sensor before reading it
        if not self.sensor.initialize():
            error = "Sensor could not be initialized"
            print(error)
            self.log(error, 'error')
            exit(1)

        loop_condition = True
        while loop_condition:
            data = self.sensor.get_distance()
            if data:
                self.client.send_data(data["distance"] / 10)
                msg = str(data["distance"])
                self.log(msg)
            else:
                loop_condition = False
            time.sleep(0.035)

    def log(self, msg, logtype='info'):
        if self.logger:
            self.logger.log(msg, logtype=logtype)

    def __del__(self):
        pass
        if self.logger:
            self.logger.exit()