Пример #1
0
def enable_data_logging(sensor, odr=17):

    LOGGER.info('enable_data_logging start')

    sensor.set_power_on()

    #
    # parameter validation
    #

    if odr >= 17:
        odrkey = 'AVG_16_50MS'
    elif odr == 8:
        odrkey = 'AVG_32_100MS'
    else:  # odr == 4
        odrkey = 'AVG_64_200MS'

    sensor.set_odr(e.BM1383AGLV_MODE_CONTROL_REG_AVE_NUM[odrkey])

    #
    # interrupts settings
    #

    if evkit_config.get('generic', 'drdy_operation') == 'ADAPTER_GPIO1_INT':
        sensor.enable_drdy_pin()
        sensor.reset_drdy_pin()

    #
    # Turn on measurement
    #

    sensor.start_continuous_measurement()

    LOGGER.info('enable_data_logging done')
Пример #2
0
def enable_data_logging(sensor, odr=20, avg="4TIMES"):

    LOGGER.info('enable_data_logging start')

    #
    # parameter validation
    #

    assert avg in e.BM1422GMV_AVER_AVG, \
        'Invalid value for avg. Valid values are %s' % \
        e.BM1422GMV_AVER_AVG.keys()

    valid_odrs = e.BM1422GMV_CNTL1_ODR.keys()

    assert convert_to_enumkey(odr) in valid_odrs, \
        'Invalid odr value "{}". Valid values are {}'.format(odr, valid_odrs)

    sensor.set_power_on()

    sensor.set_odr(e.BM1422GMV_CNTL1_ODR[convert_to_enumkey(odr)])

    sensor.set_averaging(e.BM1422GMV_AVER_AVG[avg])

    #
    # interrupts settings
    #

    if evkit_config.get('generic', 'drdy_operation') == 'ADAPTER_GPIO1_INT':
        sensor.enable_drdy_pin()
        if POLARITY_DICT[sensor.resource[CFG_POLARITY]] == ACTIVE_HIGH:
            sensor.set_drdy_high_active()
            #print("active high")
        else:
            sensor.set_drdy_low_active()
            #print("active low")
    elif evkit_config.get('generic', 'drdy_operation') == 'DRDY_REG_POLL':
        sensor.enable_drdy_pin()

    #
    # Turn on measurement
    #

    sensor.start_continuous_measurement()

    LOGGER.info('enable_data_logging done')
Пример #3
0
    def __init__(self):

        # TODO 2 find a way to check this which works on both py2 and py3
        #assert self.read_data.__func__ == SensorDriver.read_data.__func__,\
        #'read_data() must not be overriden. Implement sensor spesific data reading to __read_data().'

        self.connected = False
        self.connection_manager = None
        self.resource = None
        self.supported_connectivity = None
        self.name = 'undefined'
        self.channel_header = 'ax!ay!az'
        self._registers = {}
        self._dump_range = [0, 2]
        self.WHOAMI = None
        self.poll_delay_s = evkit_config.getint('generic',
                                                'drdy_poll_interval') / 1000.
        self.sensor_type = SENSOR_TYPE_DIGITAL_3D
        self.axis_mapper = None
        self.selected_connectivity = None

        self._drdy_LUT = {
            'INTERVAL_READ': self._poll_delay,
            'DRDY_REG_POLL': self._poll_drdy_register,
            'ADAPTER_GPIO1_INT': self._poll_gpio_line1,
            'ADAPTER_GPIO2_INT': self._poll_gpio_line2
        }
        try:
            drdy_operation = evkit_config.get('generic', 'drdy_operation')
            LOGGER.debug('Using "{}" for data ready.'.format(drdy_operation))
            self.drdy_function = self._drdy_LUT[evkit_config.get(
                'generic', 'drdy_operation')]
        except KeyError:
            raise EvaluationKitException(
                'Missing or invalid "drdy_operation" definition in settings.cfg'
            )
Пример #4
0
def enable_data_logging(
    sensor,
    odr=25,
    max_range='2G',
    lp_mode=False,
    low_pass_filter='ODR_9',
    power_off_on=True,  # set to False if this function is part of other configuration
    int_number=None):
    LOGGER.info('enable_data_logging start')

    #
    # parameter validation
    #

    if sensor.name == 'KX122':
        valid_odrs = e122.KX122_ODCNTL_OSA.keys()
    else:
        valid_odrs = e.KX022_ODCNTL_OSA.keys()

    assert convert_to_enumkey(
        odr
    ) in valid_odrs, 'Invalid odr value "{}". Valid values are {}'.format(
        odr, valid_odrs)

    assert max_range in e.KX022_CNTL1_GSEL.keys(
    ), 'Invalid max_range value "{}". Valid values are {}'.format(
        max_range, e.KX022_CNTL1_GSEL.keys())

    assert (
        lp_mode in list(e.KX022_LP_CNTL_AVC.keys()) + [False]
    ), 'Invalid lp_mode value "{}". Valid values are: False or {}'.format(
        lp_mode, e.KX022_LP_CNTL_AVC.keys())

    assert low_pass_filter in list(e.KX022_ODCNTL_LPRO.keys()) + \
        ['BYPASS'], 'Invalid filter value "{}". Valid values are: BYPASS or {}'.format(
            filter, e.KX022_ODCNTL_LPRO.keys())

    # Set sensor to stand-by to enable setup change
    if power_off_on:
        sensor.set_power_off()

    #
    # Configure sensor
    #

    # odr setting for data logging
    if sensor.WHOAMI in sensor._WAIS122:
        sensor.set_odr(e122.KX122_ODCNTL_OSA[convert_to_enumkey(odr)])
    else:
        sensor.set_odr(e.KX022_ODCNTL_OSA[convert_to_enumkey(odr)])

    # select g-range
    sensor.set_range(e.KX022_CNTL1_GSEL[max_range])

    # resolution / power mode selection

    if lp_mode is not False:
        # enable low current mode
        sensor.reset_bit(r.KX022_CNTL1, b.KX022_CNTL1_RES)
        # define averaging value
        sensor.set_average(e.KX022_LP_CNTL_AVC[lp_mode])
    else:
        # full resolution
        sensor.set_bit(r.KX022_CNTL1, b.KX022_CNTL1_RES)

    # set bandwitdh
    if low_pass_filter != 'BYPASS':
        sensor.set_BW(e.KX022_ODCNTL_LPRO[low_pass_filter], 0, CH_ACC)
        sensor.enable_iir()
    else:
        sensor.disable_iir()
    #
    # interrupt pin routings and settings
    #

    _intpin = 0
    if int_number is None:
        if evkit_config.get('generic',
                            'drdy_operation') == 'ADAPTER_GPIO1_INT':
            _intpin = 1

        elif evkit_config.get('generic',
                              'drdy_operation') == 'ADAPTER_GPIO2_INT':
            _intpin = 2

        elif evkit_config.get('generic', 'drdy_operation') == 'DRDY_REG_POLL':
            # interrupt must be enabled to get register updates
            sensor.enable_drdy(intpin=1)

        else:
            pass  # INTERVAL_READ no need to do anything

    else:
        _intpin = int_number

    if _intpin > 0:
        LOGGER.debug('Configuring interrupt pin {}'.format(_intpin))
        if _intpin == 1:
            # TODO 2 set_bit_pattern
            sensor.reset_bit(r.KX022_INC1,
                             b.KX022_INC1_IEL1)  # latched interrupt
            sensor.set_bit(r.KX022_INC1, b.KX022_INC1_IEN1)  # enable interrupt
        else:
            # TODO 2 set_bit_pattern
            sensor.reset_bit(r.KX022_INC5,
                             b.KX022_INC5_IEL2)  # latched interrupt
            sensor.set_bit(r.KX022_INC5, b.KX022_INC5_IEN2)  # enable interrupt

        polarity = POLARITY_DICT[sensor.resource[CFG_POLARITY]]
        LOGGER.debug('Configuring interrupt polarity {}'.format(
            sensor.resource[CFG_POLARITY]))
        sensor.set_interrupt_polarity(intpin=_intpin, polarity=polarity)

        # use acc data ready
        sensor.enable_drdy(intpin=_intpin)

    #
    # Turn on operating mode (disables setup)
    #

    if power_off_on:
        sensor.set_power_on()

    # sensor.register_dump()#;sys.exit()

    LOGGER.info('enable_data_logging done')
Пример #5
0
    def __init__(self, board_config_json=None, odr=None, req_port=None):
        LOGGER.debug('>init')

        if board_config_json is None:
            board_config_json = evkit_config.get('configuration', 'board')

        LOGGER.info('Opening board configuration %s' %
                    format(board_config_json))

        bus2_name = evkit_config.get('bus2_settings', 'bus2')

        assert bus2_name in [
            BUS2_USB, BUS2_BLE, BUS2_SOCKET, BUS2_USB_SERIAL, BUS2_USB_AARDVARK
        ]

        self.found_sensors = {}
        self._pin_mode_cache = {
        }  # store gpio pin mode _GPIO_STATE_INPUT / _GPIO_STATE_OUTPUT
        self.bus2_configuration = None

        self.__board_config_json = board_config_json

        LOGGER.debug('Loading %s' % format(board_config_json))

        with open(board_config_json, 'r') as infile:
            self.__board_config = json.load(infile)

        # verify board config version
        if self.__board_config[
                'structure_version'] not in SUPPORTED_BOARD_CONFIGURATION_VERSIONS:
            raise EvaluationKitException(
                'Board config version is %s. Supported versions are %s' %
                (self.__board_config['structure_version'],
                 SUPPORTED_BOARD_CONFIGURATION_VERSIONS))

        # verify that asked connection was found from board configuration
        connections_list = self.__board_config['configuration']['bus2'][
            'connections']
        number_of_usb_bus2 = sum([
            BUS2_USB in bus2_connection['connection']
            for bus2_connection in connections_list
        ])

        if number_of_usb_bus2 > 1 and bus2_name == BUS2_USB:
            raise EvaluationKitException(
                'Multiple USB bus2 connections found from board configuration %s. Please select which one to use.' % \
                board_config_json)

        # find asked bus2 configuration
        for bus2_connection in connections_list:
            if bus2_connection['connection'] == bus2_name:
                self.bus2_configuration = bus2_connection
                break

            # check that BUS2_USB is supported in board config
            elif bus2_name == BUS2_USB and bus2_connection[
                    'connection'].startswith(BUS2_USB):
                LOGGER.info('%s bus2 selected' %
                            format(bus2_connection['connection']))
                self.bus2_configuration = bus2_connection
                break

        if self.bus2_configuration is None:
            raise EvaluationKitException(
                'Selected bus2 "{}" not found from board configuration {}'.
                format(bus2_name, board_config_json))

        # open the connection
        if self.bus2_configuration['connection'] in [BUS2_USB_SERIAL]:
            bus2connection = KxComPort(
                bus2_configuration=self.bus2_configuration)
            serial_port = evkit_config.get('bus2_settings', 'serial_port')

            if serial_port == 'auto':
                # auto discover the com port
                found_ports = bus2connection.get_com_port()
            else:
                # com port defined in rokix_settings.cfg
                found_ports = [serial_port]

            if req_port != None:
                found_ports = [found_ports[req_port]]

            print found_ports
            print len(found_ports)
            for com_port in found_ports:
                print com_port
                LOGGER.debug("Finding board from %s" % com_port)
                bus2connection.initialize(com_port)
                self.kx_adapter = KxAdapterEvk(bus2=bus2connection)

                if self.kx_adapter.board_id != self.__board_config[
                        CFG_CONFIGURATION]['board_id']:
                    bus2connection.close(
                    )  # this was not right port. close it.
                    LOGGER.debug(
                        'Board id %s received. Expected id is %s.' %
                        (self.kx_adapter.board_id,
                         self.__board_config[CFG_CONFIGURATION]['board_id']))
                else:
                    print 'vader'
                    continue

            print 'here'
            if self.kx_adapter.board_id != self.__board_config[
                    CFG_CONFIGURATION]['board_id']:
                raise EvaluationKitException(
                    'Expected evaluation board not found.')

        elif self.bus2_configuration['connection'] == BUS2_USB_AARDVARK:
            self.kx_adapter = KxAdapterAardvark(
                bus1config=self.__board_config['configuration']['bus1']
                ['targets'][0])

        elif self.bus2_configuration['connection'] == BUS2_BLE:
            bus2connection = KxWinBLE(
                bus2_configuration=self.bus2_configuration)
            # use mac address with BLE
            bus2connection.initialize(
                mac_address=evkit_config.get('bus2_settings', 'ble_mac'))

            self.kx_adapter = KxAdapterEvk(bus2=bus2connection)

        else:
            raise EvaluationKitException('No rule found to configure bus 2.')

        # verify that board FW is supported
        if self.kx_adapter.fw_protocol_version not in SUPPORTED_FIRMWARE_PROTOCOL_VERSIONS:
            raise EvaluationKitException(
                "Board reported protocol version %s. Supported versions are %s"
                % (self.kx_adapter.fw_protocol_version,
                   SUPPORTED_FIRMWARE_PROTOCOL_VERSIONS))

        # verify that board fw is compatible with board config
        if self.kx_adapter.fw_protocol_version not in self.__board_config[
                'protocol_version']:
            raise EvaluationKitException(
                "Board reported protocol version %s. Board config expects %s" %
                (self.kx_adapter.fw_protocol_version,
                 self.__board_config['protocol_version']))

        # Apply board init if found from board configuration
        if 'board_init' in self.__board_config[CFG_CONFIGURATION]:
            if self.__board_config['configuration']['board_init']['reg_write']:
                LOGGER.debug(
                    'board_init found from board configuration. Initializing the board'
                )

                for message_content in self.__board_config['configuration'][
                        'board_init']['reg_write']:
                    sensor_name, sensor_register, register_value = message_content
                    for target_blob in self.__board_config['configuration'][
                            'bus1']['targets']:
                        if sensor_name in target_blob['parts']:
                            sensor_resource = target_blob['parts'][sensor_name]

                            LOGGER.debug('%d %d %d %d' %
                                         (target_blob[CFG_TARGET],
                                          sensor_resource[CFG_SAD],
                                          sensor_register, register_value))

                            self.kx_adapter.adapter_write_sensor_register_i2c(
                                target_blob[CFG_TARGET],
                                sensor_resource[CFG_SAD], sensor_register,
                                register_value)

        else:
            LOGGER.debug('board_init not found from board configuration')

        # configure power mode
        if odr:
            self.set_cpu_power_mode(odr)

        LOGGER.debug('<init')
Пример #6
0
def enable_data_logging(sensor,
                        odr=25,
                        max_range_acc='2G',
                        lp_mode=False,
                        power_off_on=True,
                        int_number=None):

    LOGGER.info('enable_data_logging start')

    #
    # parameter validation
    #

    assert convert_to_enumkey(odr) in e.KMX62_ODCNTL_OSA.keys(
    ), 'Invalid odr_OSA value "{}". Support values are {}'.format(
        odr, e.KMX62_ODCNTL_OSA.keys())

    assert max_range_acc in e.KMX62_CNTL2_GSEL, 'Invalid range value "{}". Support values are {}'.format(
        max_range_acc, e.KMX62_CNTL2_GSEL.keys())

    assert (
        lp_mode in list(e.KMX62_CNTL2_RES.keys()) + [False]
    ), 'Invalid lp_mode value "{}". Support values are {} and False'.format(
        lp_mode, e.KMX62_CNTL2_RES.keys())

    # Set sensor to stand-by to enable setup change
    if power_off_on:
        sensor.set_power_off()

    #
    # Configure sensor
    #
    # Select acc ODRs
    sensor.set_odr(e.KMX62_ODCNTL_OSA[convert_to_enumkey(odr)], CH_ACC)
    # Select mag ODRs
    sensor.set_odr(e.KMX62_ODCNTL_OSM[convert_to_enumkey(odr)], CH_MAG)

    # select g-range (for acc)
    sensor.set_range(e.KMX62_CNTL2_GSEL[max_range_acc])

    # resolution / power mode selection

    if lp_mode not in ['MAX1', 'MAX2', False]:
        # Low power mode
        sensor.set_average(e.KMX62_CNTL2_RES[lp_mode], None, CH_ACC)
    else:
        # Full power mode
        sensor.set_average(b.KMX62_CNTL2_RES_MAX2, None, CH_ACC)

    #
    # interrupt pin routings and settings
    #

    if int_number is None:
        # KMX62: interrupt source selection activates also physical interrupt pin
        if evkit_config.get('generic',
                            'drdy_operation') == 'ADAPTER_GPIO1_INT':
            sensor.enable_drdy(1, CH_ACC)  # acc data ready to int1
            # sensor.enable_drdy(1, CH_MAG)                       # mag data ready to int1
        elif evkit_config.get('generic',
                              'drdy_operation') == 'ADAPTER_GPIO2_INT':
            sensor.enable_drdy(2, CH_ACC)  # acc data ready to int2
            # sensor.enable_drdy(2, CH_MAG)                       # mag data ready to int2
        elif evkit_config.get('generic', 'drdy_operation') == 'DRDY_REG_POLL':
            sensor.enable_drdy(1, CH_ACC)  # acc data ready to int1
            # sensor.enable_drdy(1, CH_MAG)                       # mag data ready to int1
    else:
        sensor.enable_drdy(int_number, CH_ACC)

    polarity = POLARITY_DICT[sensor.resource[CFG_POLARITY]]
    LOGGER.debug('Configuring interrput polarity %s' %
                 format(sensor.resource[CFG_POLARITY]))
    if polarity == ACTIVE_HIGH:
        IEA1 = b.KMX62_INC3_IEA1_HIGH
        IEA2 = b.KMX62_INC3_IEA2_HIGH
    elif polarity == ACTIVE_LOW:
        IEA1 = b.KMX62_INC3_IEA1_LOW
        IEA2 = b.KMX62_INC3_IEA2_LOW
    else:
        raise EvaluationKitException('Unsupported interrupt polarity %s' %
                                     sensor.resource[CFG_POLARITY])

    # interrupt signal parameters
    sensor.write_register(
        r.KMX62_INC3,
        b.KMX62_INC3_IED1_PUSHPULL | IEA1 | b.KMX62_INC3_IEL1_LATCHED
        | b.KMX62_INC3_IED2_PUSHPULL | IEA2 | b.KMX62_INC3_IEL2_LATCHED)
    #
    # Turn on operating mode (disables setup)
    #

    if power_off_on:
        # sensor.set_power_on(CH_MAG )                       # mag ON
        # sensor.set_power_on(CH_ACC | CH_MAG )              # acc + mag ON
        sensor.set_power_on(CH_ACC | CH_MAG | CH_TEMP)

    # sensor.register_dump()#;sys.exit()

    LOGGER.info('enable_data_logging done')
def enable_wakeup(
    sensor,
    direction_mask=b.KXTJ3_INT_CTRL_REG2_XNWU |  # x- direction mask
    b.KXTJ3_INT_CTRL_REG2_XPWU |  # x+ direction mask
    b.KXTJ3_INT_CTRL_REG2_YNWU |  # y- direction mask
    b.KXTJ3_INT_CTRL_REG2_YPWU |  # y+ direction mask
    b.KXTJ3_INT_CTRL_REG2_ZNWU |  # z- direction mask
    b.KXTJ3_INT_CTRL_REG2_ZPWU,  # z+ direction mask
    cfg=Parameter_set_1,
    power_off_on=True  # set to False if this function is part of other configuration
):

    LOGGER.info('Wakeup event init start')

    #
    # parameter validation
    #

    assert convert_to_enumkey(cfg.odr) in e.KXTJ3_DATA_CTRL_REG_OSA.keys(),\
    'Invalid odr value "{}". Valid values are {}'.format(
    cfg.odr,e.KXTJ3_DATA_CTRL_REG_OSA.keys())

    assert cfg.LOW_POWER_MODE in [True, False],\
    'Invalid cfg.LOW_POWER_MODE value "{}". Valid values are True and False'.format(
    cfg.LOW_POWER_MODE)

    # Set sensor to stand-by to enable setup change
    if power_off_on:
        sensor.set_power_off()
    #
    # Configure sensor
    #

    # NOTE for wake up detection the g-range is fixed to +-8g

    # resolution / power mode selection
    if cfg.LOW_POWER_MODE is True:
        # low current
        sensor.reset_bit(r.KXTJ3_CTRL_REG1, b.KXTJ3_CTRL_REG1_RES)
    else:
        # full resolution
        sensor.set_bit(r.KXTJ3_CTRL_REG1, b.KXTJ3_CTRL_REG1_RES)

    # enable wakeup detection engine
    sensor.set_bit(r.KXTJ3_CTRL_REG1, b.KXTJ3_CTRL_REG1_WUFE)
    # stream odr (if stream odr is biggest odr, it makes effect to current consumption)
    sensor.set_odr(e.KXTJ3_DATA_CTRL_REG_OSA[convert_to_enumkey(cfg.odr)])
    # Set wuf detection odr
    sensor.set_bit_pattern(r.KXTJ3_CTRL_REG2,
                           e.KXTJ3_CTRL_REG2_OWUF[convert_to_enumkey(cfg.odr)],
                           m.KXTJ3_CTRL_REG2_OWUF_MASK)

    #
    # Init wuf detection engine
    #

    # WUF direction definition
    sensor.write_register(r.KXTJ3_INT_CTRL_REG2, direction_mask)
    # WUF timer
    sensor.write_register(r.KXTJ3_WAKEUP_THRESHOLD_H, cfg.ATH_VALUE >> 4)
    sensor.write_register(r.KXTJ3_WAKEUP_THRESHOLD_L, cfg.ATH_VALUE & 0xf << 4)
    # WUF threshold
    sensor.write_register(r.KXTJ3_WAKEUP_COUNTER, cfg.WUFC_VALUE)

    #
    # interrupt pin routings and settings
    #

    # enable interrrupt pin
    sensor.set_bit(r.KXTJ3_INT_CTRL_REG1, b.KXTJ3_INT_CTRL_REG1_IEN)

    # latched interrupt
    sensor.reset_bit(r.KXTJ3_INT_CTRL_REG1, b.KXTJ3_INT_CTRL_REG1_IEL)
    if evkit_config.get('generic', 'int1_active_high') == 'TRUE':
        sensor.set_bit(r.KXTJ3_INT_CTRL_REG1,
                       b.KXTJ3_INT_CTRL_REG1_IEA)  # active high
    else:
        sensor.reset_bit(r.KXTJ3_INT_CTRL_REG1,
                         b.KXTJ3_INT_CTRL_REG1_IEA)  # active low

    #
    # Turn on operating mode (disables setup)
    #

    if power_off_on:
        sensor.set_power_on()

    # sensor.register_dump()#;sys.exit()

    LOGGER.info('Wakeup event initialized.')