Ejemplo n.º 1
0
    def step_full(self, steps, delay=0.0001, verbose=False):
        """Stepper motor motion in full steps
        (four steps per step commanded)

        Parameters
        ----------
        steps : int, number of motor steps where
            positive numbers are clockwise
            negative numbers are counter clockwise
        delay : float, seconds to delay between step commands,
            default is 0.0001 seconds which smooths operation on the pi
        verbose : bool, print debug statements
        """

        self.steps = steps

        if steps > 0:
            step_codes = self.step_codes_cw
            self.direction = "step_cw"
        if steps < 0:
            step_codes = self.step_codes_ccw
            self.direction = "step_ccw"
        if steps == 0:
            self.stop()
            return

        self.set_mode(mode_type="full_step")  # set speed to 255, i.e. full current

        for _ in range(abs(steps)):
            for sc in step_codes:
                if verbose: print("{0:04b}".format(sc))
                self.bus.write_n_bytes([0xaa, sc, 0x01])
                time.sleep(delay)

        self.stop()  # set speed to 0, i.e. current off
Ejemplo n.º 2
0
    def publish(self, description='NA', n=1, delay=None):
        """Output relay status data in JSON.

        Parameters
        ----------
        description : str, description of data sample collected
        n : int, number of samples to record in this burst
        delay : float, seconds to delay between samples if n > 1

        Returns
        -------
        str, formatted in JSON with keys:
            description: str, description of sample under test
            temperature : float, temperature in degrees Celcius
        """
        data_list = []
        for m in range(n):
            data_list.append(
                self.json_writer.publish([description, m,
                                          self.get_temp()]))
            if n == 1:
                return data_list[0]
            if delay is not None:
                time.sleep(delay)
        return data_list
Ejemplo n.º 3
0
    def publish(self, description='NA', n=1, delay=None):
        """Get RTC time and temperature in JSON, plus metadata at intervals
        set by self.metadata_interval

        Parameters
        ----------
        description : char, description of data sample collected, default='NA'
        n : int, number of samples to record in this burst
        delay : float, seconds to delay between samples if n > 1

        Returns
        -------
        str, formatted in JSON with keys:
            description : str
            n : sample number in this burst
            std_time : str, time formatted in YY-MM-DD hh:mm:ss
            temp_C : float, temperature of RTC in degrees C
        """
        data_list = []
        for m in range(n):
            std_time = '{:02d}-{:02d}-{:02d} {:02d}:{:02d}:{:02d}'.format(
                *self.get_time())
            temp_C = self.get_temp()
            data_list.append(
                self.json_writer.publish([description, 0, std_time, temp_C]))
            if n == 1:
                return data_list[0]
            if delay is not None:
                time.sleep(delay)
        return data_list
Ejemplo n.º 4
0
    def write(self, description='NA', n=1, delay=0):
        """Format output and save to file, formatted as either .csv or .json.

        Parameters
        ----------
        description : str, description of data sample collected
        n : int, number of samples to record in this burst
        delay : float, seconds to delay between samples if n > 1

        Returns
        -------
        None, writes to disk the following data:
            description : str, description of sample
            sample_n : int, sample number in this burst
            voltage, float, Volts measured at the shunt resistor
            current : float, Amps of current accross the shunt resistor
        """
        wr = {"csv": self.csv_writer,
              "json": self.json_writer}[self.writer_output]
        for m in range(n):
            measure = self.measure()
            if ((isinstance(measure, float)) or 
                (isinstance(measure, int)) or 
                (isinstance(measure, str))):
                    measure = [measure]
            wr.write([description, m] + measure)
            time.sleep(max(self.long_delay, delay))
Ejemplo n.º 5
0
    def get(self, description='no_description', n=1, delay=0):
        """Get formatted output, assumes subclass has method 'measure'

        Parameters
        ----------
        description : char, description of data sample collected
        n : int, number of samples to record in this burst
        delay : float, seconds to delay between samples if n > 1

        Returns
        -------
        data : list, data that will be saved to disk with self.write containing:
            description : str
            c : float, sensor measurement
        """

        data_list = []
        for m in range(n):
            measure = self.measure()
            if isinstance(measure, float):
                measure = [measure]
            data = [description, m] + measure
            data_list.append(data)
            if n == 1:
                return data_list[0]
            time.sleep(max(self.long_delay, delay))
        return data_list
Ejemplo n.º 6
0
    def publish(self, description='NA', n=1, delay=0):
        """Output relay status data in JSON.

        Parameters
        ----------
        description : str, description of data sample collected
        n : int, number of samples to record in this burst
        delay : float, seconds to delay between samples if n > 1

        Returns
        -------
        str, formatted in JSON with keys:
            description: str, description of sample under test
            measurement : float, measurement made
        """
        data_list = []
        for m in range(n):
            measure = self.measure()
            if isinstance(measure, float):
                measure = [measure]
            data_list.append(self.json_writer.publish([description, m] + measure))
            if n == 1:
                return data_list[0]
            time.sleep(max(self.long_delay, delay))
        return data_list
Ejemplo n.º 7
0
    def publish(self, description='NA', n=1, nmea_sentences=None, delay=None):
        """Output relay status data in JSON.

        Parameters
        ----------
        description : str, description of data sample collected
        n : int, number of samples to record in this burst
        nmea_sentence : list of str, NMEA sentence codes to return i.e. 'GSV'
        delay : float, seconds to delay between samples if n > 1

        Returns
        -------
        str, formatted in JSON with keys:
            description : str
            n : sample number in this burst
            nmea_sentence : str, NMEA sentence
        """
        if nmea_sentences is None:
            nmea_sentences = self.metadata.supported_nmea_sentences

        data_list = []
        for m in range(n):
            nmea_data = self.get(nmea_sentences=nmea_sentences)
            for p in range(len(nmea_data)):
                data = self.json_writer.publish([description, m, nmea_data[p]])
                data_list.append(data)
            if delay is not None:
                time.sleep(delay)
        return data_list
Ejemplo n.º 8
0
    def write(self, description='NA', n=1, nmea_sentences=None, delay=None):
        """Format output and save to file, formatted as either .csv or .json.

        Parameters
        ----------
        description : str, description of data sample collected
        n : int, number of samples to record in this burst
        nmea_sentence : list of str, NMEA sentence codes to return i.e. 'GSV'
        delay : float, seconds to delay between samples if n > 1

        Returns
        -------
        None, writes to disk the following data:
            description : str
            n : sample number in this burst
            nmea_sentence : str, NMEA sentence
        """ 
        if nmea_sentences is None:
            nmea_sentences = self.metadata.supported_nmea_sentences
        
        wr = {"csv": self.csv_writer,
              "json": self.json_writer}[self.writer_output]
        for m in range(n):
            nmea_data = self.get(nmea_sentences=nmea_sentences)
            for p in range(len(nmea_data)):
                wr.write([description, m, '"' + nmea_data[p] + '"'])
            if delay is not None:
                time.sleep(delay)
Ejemplo n.º 9
0
    def publish(self, description='NA', n=1, delay=None):
        """Get ADC data in JSON.

        Parameters
        ----------
        description : str, description of data sample collected
        n : int, number of samples to record in this burst
        delay : float, seconds to delay between samples if n > 1

        Returns
        -------
        str, formatted in JSON with keys:
            description : str
            n : sample number in this burst
            mux : XXX, multiplexer pin pair the voltage reading was taken with
            v : float, voltage measurement
        """
        data_list = []
        for m in range(n):
            data_list.append(
                self.json_writer.publish([
                    description, m, self.bin_mux[self.mux_value],
                    self.voltage()
                ]))
            if n == 1:
                return data_list[0]
            if delay is not None:
                time.sleep(delay)
        return data_list
Ejemplo n.º 10
0
    def write(self, description='NA', n=1, delay=None):
        """Get ADC output and save to file, formatted as either .csv or .json.

        Parameters
        ----------
        description : str, description of data sample collected
        n : int, number of samples to record in this burst
        delay : float, seconds to delay between samples if n > 1

        Returns
        -------
        None, writes to disk the following data:
            description : str
            n : sample number in this burst
            mux : XXX, multiplexer pin pair the voltage reading was taken with
            v : float, voltage measurement
        """
        wr = {
            "csv": self.csv_writer,
            "json": self.json_writer
        }[self.writer_output]
        for m in range(n):
            wr.write(
                [description, m, self.bin_mux[self.mux_value],
                 self.voltage()])
            if delay is not None:
                time.sleep(delay)
Ejemplo n.º 11
0
    def get(self, description='no_description', n=1, delay=None):
        """Get ADC data.

        Parameters
        ----------
        description : char, description of data sample collected
        n : int, number of samples to record in this burst
        delay : float, seconds to delay between samples if n > 1

        Returns
        -------
        data : list, data that will be saved to disk with self.write containing:
            description : str
            n : sample number in this burst
            mux : XXX, multiplexer pin pair the voltage reading was taken with
            v : float, voltage measurement
        """
        data_list = []
        for m in range(1, n + 1):
            data_list.append(
                [description, m, self.bin_mux[self.mux_value],
                 self.voltage()])
            if n == 1:
                return data_list[0]
            if delay is not None:
                time.sleep(delay)
        return data_list
Ejemplo n.º 12
0
    def get(self, description='NA', n=1, delay=None):
        """Get formatted output.

        Parameters
        ----------
        description : str, description of data sample collected
        n : int, number of samples to record in this burst
        delay : float, seconds to delay between samples if n > 1
        
        Returns
        -------
        data : list, data that will be saved to disk with self.write containing:
            description: str, description of sample under test
            sample_n : int, sample number in this burst
            voltage, float, Volts measured at the shunt resistor
            current : float, Amps of current accross the shunt resistor
        """
        data_list = []
        for m in range(1, n + 1):
            data_list.append([
                description, m,
                self.get_bus_voltage(),
                self.get_current_simple()
            ])
            if n == 1:
                return data_list[0]
            if delay is not None:
                time.sleep(delay)
        return data_list
Ejemplo n.º 13
0
    def publish(self, description='NA', n=1, delay=None):
        """Output relay status data in JSON.

        Parameters
        ----------
        description : str, description of data sample collected
        n : int, number of samples to record in this burst
        delay : float, seconds to delay between samples if n > 1

        Returns
        -------
        str, formatted in JSON with keys:
            description: str, description of sample under test
            sample_n : int, sample number in this burst
            voltage, float, Volts measured at the shunt resistor
            current : float, Amps of current accross the shunt resistor
        """
        data_list = []
        for m in range(n):
            data_list.append(
                self.json_writer.publish([
                    description, m,
                    self.get_bus_voltage(),
                    self.get_current_simple()
                ]))
            if n == 1:
                return data_list[0]
            if delay is not None:
                time.sleep(delay)
        return data_list
Ejemplo n.º 14
0
    def write(self, description='NA', n=1, delay=None):
        """Get RTC time and temperature and save to file,
        formatted as either .csv or .json.

        Parameters
        ----------
        description : str, description of data sample collected
        n : int, number of samples to record in this burst
        delay : float, seconds to delay between samples if n > 1

        Returns
        -------
        None, writes to disk the following data:
            description : str
            n : sample number in this burst
            std_time : str, time formatted in YY-MM-DD hh:mm:ss
            temp_C : float, temperature of RTC in degrees C
        """
        wr = {
            "csv": self.csv_writer,
            "json": self.json_writer
        }[self.writer_output]
        for m in range(n):
            std_time = '{:02d}-{:02d}-{:02d} {:02d}:{:02d}:{:02d}'.format(
                *self.get_time())
            temp_C = self.get_temp()
            wr.write([description, m, std_time, temp_C])
            if delay is not None:
                time.sleep(delay)
Ejemplo n.º 15
0
    def write(self, description='NA', n=1, delay=None):
        """Format output and save to file, formatted as either
        .csv or .json.

        Parameters
        ----------
        description : str, description of data sample collected
        n : int, number of samples to record in this burst
        delay : float, seconds to delay between samples if n > 1
        
        Returns
        -------
        None, writes to disk the following data:
            description : str, description of sample
            sample_n : int, sample number in this burst
            temperature : float, temperature in degrees Celcius
        """
        wr = {
            "csv": self.csv_writer,
            "json": self.json_writer
        }[self.writer_output]
        for m in range(n):
            wr.write([description, m, self.get_temp()])
            if delay is not None:
                time.sleep(delay)
Ejemplo n.º 16
0
 def perform_self_test(self):
     """Perform a self test as an end-of-line test to check sensor
     functionality and the power supply to the sensor. See Ch 3.9.3"""
     self.bus.write_n_bytes([0x36, 0x39])
     time.sleep(11)
     d = self.bus.read_n_bytes(3)
     d = CRC_check(d)
     return d[0] << 8 | d[1]
Ejemplo n.º 17
0
 def product_type(self):
     """Read product type. See ch 6.3.9"""
     self.bus.write_n_bytes([0XD0, 0X02])
     time.sleep(0.1)
     d = self.bus.read_n_bytes(12)
     d = CRC_check(d)
     d = [chr(x) for x in d if ascii_check(x)]
     return ''.join(d)
Ejemplo n.º 18
0
 def serial(self):
     """Read device serial number. See ch 6.3.9"""
     self.bus.write_n_bytes([0xD0, 0x33])
     time.sleep(0.1)
     d = self.bus.read_n_bytes(48)
     d = CRC_check(d)
     d = [chr(x) for x in d if ascii_check(x)]
     return ''.join(d)
Ejemplo n.º 19
0
    def single_shot(self):
        """Write 0x1 to bit 15 of the configuration register to initialize
        a single shot conversion.  The configuration register must be read at
        least once to get the current configuration, otherwise the chip default is used.
        Chip clears bit on completion of ADC conversion.
        """

        self.os()
        time.sleep(self.delay)
        self.get_conversion()
Ejemplo n.º 20
0
def shared_timestamp():
    t = tp_ex.get_time()
    bme.json_writer.set_time(t)
    mcp.json_writer.set_time(t)
    x = bme.publish()
    y = mcp.publish()
    print(x)
    print('-' * 40)
    print(y)
    print('=' * 40)
    time.sleep(5)
Ejemplo n.º 21
0
 def get_serial_number(self):
     """Read the serial number to identify the chip and 
     verify presense of the sensor. See Ch 3.9.2"""
     self.bus.write_n_bytes([0x36, 0x82])
     time.sleep(0.01)
     d = self.bus.read_n_bytes(9)
     d = CRC_check(d)
     da = []
     for n in range(0, len(d), 2):
         da.append(d[n] << 8 | d[n + 1])
     return da[0] << 32 | da[1] << 16 | da[2]
Ejemplo n.º 22
0
 def get_all_channels(self):
     """Get all channel states, as a single 8 bit value. Each bit 
     represents one channel where 0 = On, 1 = Off.
     
     Returns
     -------
     state : int, 8 bits
     """
     self.reg_olat = self.bus.read_register_8bit(reg_addr=0x0A)
     time.sleep(0.01)
     return self.reg_olat
Ejemplo n.º 23
0
 def status(self):
     """Get status device status. See ch 4.4"""
     self.bus.write_n_bytes([0xD2, 0x06])
     time.sleep(0.1)
     d = self.bus.read_n_bytes(6)
     d = CRC_check(d)
     d = (d[0] << 8) + d[1]
     self.metadata.speed_warning = d >> 21 & 1
     self.metadata.laser_error = d >> 5 & 1
     self.metadata.fan_error = d >> 4 & 1
     return (self.metadata.speed_warning, self.metadata.laser_error,
             self.metadata.fan_error)
Ejemplo n.º 24
0
 def set_ambient_pressure(self, pressure):
     """Set ambient pressure to enable continuous pressure
     compensation. See Ch 3.6.5
     
     Parameters
     ----------
     pressure : int, pressure in Pascals (Pa)
     """
     pressure = pressure / 100
     crc = CRC_calc([0x00, pressure])
     d = [0x00, pressure, crc]
     self.bus.write_n_bytes([0xE0, 0x00] + d)
     time.sleep(0.002)
Ejemplo n.º 25
0
 def get_sensor_altitude(self):
     """Get the altitude set in memory, this is not an 
     active measurement. See Ch 3.6.4
     
     Returns
     -------
     int, altitude in meters above sea level
     """
     self.bus.write_n_bytes([0x23, 0x22])
     time.sleep(0.002)
     d = self.bus.read_n_bytes(3)
     d = CRC_check(d)
     return d[0] << 8 | d[1]
Ejemplo n.º 26
0
 def set_temperature_offset(self, tC):
     """Set the temperature offest, which only affects
     RH and T output. See Ch 3.6.1
     
     Parameters
     tC : float, temperature offset in degrees Celsius
     """
     tC = (tC * 2**16) / 175
     d0 = tC >> 8
     d1 = tC & 0xff
     d2 = CRC_check([d0, d1])
     self.bus.write_n_bytes([0x24, 0x1D, d0, d1, d2])
     time.sleep(0.01)
Ejemplo n.º 27
0
    def mode(self, x):
        """Set operating mode to either single or continuous.

        Parameters
        ----------
        x: str, either 'single' or 'continuous'
        """

        self.config_value = ((self.config_value & BIT_MODE)
                             | (self.str_mode[x] << 8))
        self.set_config()
        time.sleep(self.delay)
        self.mode_value = (self.config_value >> 8) & 0b1
Ejemplo n.º 28
0
 def get_temperature_offset(self):
     """Get the temperature offset, which only affects 
     RH and T output. See Ch 3.6.2
     
     Returns
     -------
     float, temperature offset in degrees Celsius
     """
     self.bus.write_n_bytes([0x23, 0x18])
     time.sleep(0.01)
     d = self.bus.read_n_bytes(3)
     d = CRC_check(d)
     d = d[0] << 8 | d[1]
     return (175 * d) / 2**16
Ejemplo n.º 29
0
    def data_ready(self):
        """Read Data-Ready Flag. See ch 6.3.3

        Returns
        -------
        bool, True if data is ready, otherwise False
        """
        self.bus.write_n_bytes([0x02, 0x02])
        time.sleep(0.1)
        d = self.bus.read_n_bytes(3)
        d = CRC_check(d)
        if d[1] == 0x00:
            return False
        elif d[1] == 0x01:
            return True
Ejemplo n.º 30
0
    def pga(self, x):
        """Set programmable gain amplifier range.

        Parameters
        ----------
        x : str, +/- voltage range value.  Supported values:
            '6.144', '4.096', '2.048', '1.024', '0.512', '0.256'
        """

        self.config_value = ((self.config_value & BIT_PGA)
                             | (self.str_pga[x] << 9))
        self.set_config()
        time.sleep(self.delay)  # needs at least 7 ms to complete
        self.pga_value = (self.config_value >> 9) & 0b111
        self.pga_float = self.bin_pga[self.pga_value]