Example #1
0
def test_online():
    import imports  # pylint: disable=unused-import
    from kx_lib.kx_board import ConnectionManager
    from kx_lib.kx_util import evkit_config
    from kmx62 import kmx62_data_logger
    from kmx62.kmx62_driver import KMX62Driver

    class CallBack(object):
        def __init__(self):
            self.prediction = Algorithm_prediction()

        def callback(self, data):
            ch, ax, ay, az, mx, my, mz, temp = data
            self.prediction.feed(ax)

            #print("\nThe vacuum cleaner is on the {}".format(self.prediction.result))

    ODR = 50
    callback_object = CallBack()

    connection_manager = ConnectionManager(
        board_config_json='rokix_board_rokix_sensor_node_i2c.json')
    sensor_kmx62 = KMX62Driver()
    try:
        connection_manager.add_sensor(sensor_kmx62)
        kmx62_data_logger.enable_data_logging(sensor_kmx62, odr=ODR)
        stream = kmx62_data_logger.KMX62DataStream([sensor_kmx62])
        stream.read_data_stream(console=False,
                                callback=callback_object.callback)

    finally:
        sensor_kmx62.set_power_off()
        connection_manager.disconnect()
Example #2
0
def app_main(odr=17):

    args = get_datalogger_args()
    if args.odr:
        odr = args.odr

    # For continuous more there 3 possible odr values: 17Hz, 8Hz and 4Hz
    allowed_odrs = (17, 8, 4)

    assert odr in allowed_odrs, \
        "Invalid odr, valid values {}".format(allowed_odrs)

    sensor = bm1383aglv_driver()
    connection_manager = ConnectionManager(odr=odr)

    connection_manager.add_sensor(sensor)

    enable_data_logging(sensor, odr=odr)

    if args.stream_mode:
        read_with_stream(sensor, args.loop)

    elif args.timer_stream_mode:
        read_with_stream(sensor, args.loop, timer=1. / odr)

    else:
        read_with_polling(sensor, args.loop)

    sensor.set_power_off()
    connection_manager.disconnect()
def print_version_info():
    cm = ConnectionManager()
    print('Protocol version   ' + cm.kx_adapter.fw_protocol_version)
    print('Board ID           %d' % cm.kx_adapter.board_id)
    print('Board UID          ' +
          ':'.join(['%02X' % t for t in cm.kx_adapter.get_dev_id()]))
    print('Firmware version   ' +
          ''.join(['%02x' % t for t in cm.kx_adapter.get_firmware_id()]))

    try:
        # Bootloader version only on nRF based boards
        print('Bootloader version ' +
              ''.join(['%02x' % t for t in cm.kx_adapter.get_bootloader_id()]))
    except ProtocolException:
        pass

    cm.disconnect()
Example #4
0
def test_online():
    import imports  # pylint: disable=unused-import
    from kx_lib.kx_board import ConnectionManager
    from kx_lib.kx_util import evkit_config
    from kmx62 import kmx62_data_logger
    from kmx62.kmx62_driver import KMX62Driver
    ODR = 50

    connection_manager = ConnectionManager(board_config_json='rokix_board_rokix_sensor_node_i2c.json')
    sensor_kmx62 = KMX62Driver()
    try:
        connection_manager.add_sensor(sensor_kmx62)
        kmx62_data_logger.enable_data_logging(sensor_kmx62, odr=ODR)
        stream = kmx62_data_logger.KMX62DataStream([sensor_kmx62])
        stream.read_data_stream(console=False, callback=callback)

    finally:
        sensor_kmx62.set_power_off()
        connection_manager.disconnect()
Example #5
0
def app_main(odr=25):

    args = get_datalogger_args()
    if args.odr:
        odr = args.odr
    sensor = KMX62Driver()
    connection_manager = ConnectionManager()
    connection_manager.add_sensor(sensor)
    enable_data_logging(sensor, odr=odr)

    if args.stream_mode:
        read_with_stream(sensor, args.loop)

    elif args.timer_stream_mode:
        raise EvaluationKitException('Timer polling not yet implemented')

    else:
        read_with_polling(sensor, args.loop)

    sensor.set_power_off()
    connection_manager.disconnect()
Example #6
0
def test_online():
    import imports  # pylint: disable=unused-import
    from kx_lib.kx_board import ConnectionManager
    from kx_lib.kx_util import evkit_config
    from kmx62 import kmx62_data_logger
    from kmx62.kmx62_driver import KMX62Driver

    class CallBack(object):
        def __init__(self):
            self.lateral_g = Algorithm_g_force(['ax', 'ay', 'az'])
            self.longitudinal_g = Algorithm_g_force(['ax', 'ay', 'az'])

        def callback(self, data):
            ch, ax, ay, az, mx, my, mz, temp = data
            self.lateral_g.feed((ay, ax, az))
            self.longitudinal_g.feed((ax, ay, az))
            print('%.02f\t%0.2f' %
                  (self.lateral_g.g_force, self.longitudinal_g.g_force))

    callback_object = CallBack()

    connection_manager = ConnectionManager(
        board_config_json='rokix_board_rokix_sensor_node_i2c.json')
    sensor_kmx62 = KMX62Driver()
    try:
        connection_manager.add_sensor(sensor_kmx62)
        kmx62_data_logger.enable_data_logging(sensor_kmx62,
                                              odr=global_settings.sensor.odr,
                                              max_range_acc='%dG' %
                                              global_settings.sensor.g_range)

        stream = kmx62_data_logger.KMX62DataStream([sensor_kmx62])
        stream.read_data_stream(console=False,
                                callback=callback_object.callback)

    finally:
        sensor_kmx62.set_power_off()
        connection_manager.disconnect()
Example #7
0
def app_main(odr=25):
    sp = ShapePlot()
    v1 = acc0
    p2 = [8, 0, 0]
    v2 = acc1
    p1 = [-8, 0, 0]
    points = [p1, p2]
    # for i in range(40):
    # 	print(v1)
    # 	# v1[0] = v1[0] + 500
    # 	# v1[2] = v1[2] - 500
    # 	vecs = [v1,v2]
    # 	points = [p1,p2]
    # 	sp.plot(vecs,points)
    # 	time.sleep(.1)

    args = get_datalogger_args()

    if args.odr:
        odr = args.odr

    sensor0 = KX022Driver()
    sensor1 = KX022Driver()

    connection_manager0 = ConnectionManager(odr=odr, req_port=0)
    connection_manager1 = ConnectionManager(odr=odr, req_port=1)

    connection_manager0.add_sensor(sensor0)
    connection_manager1.add_sensor(sensor1)

    enable_data_logging(sensor0, odr=odr)
    enable_data_logging(sensor1, odr=odr)

    while True:

        if args.stream_mode:
            read_with_stream(sensor, args.loop)

            print 'here11'
        elif args.timer_stream_mode:
            read_with_stream(sensor, loop=args.loop, timer=1. / odr)

            print 'here12'
        else:
            # read_with_polling(sensor, args.loop)
            read_with_polling(sensor0, 1, 0)
            read_with_polling(sensor1, 1, 1)
            print 'here13'

        v1 = acc0
        v2 = acc1
        sp.plot([v1, v2], points)
        # time.sleep(0.01)
    print 'here'

    sensor0.set_power_off()
    sensor1.set_power_off()

    connection_manager0.disconnect()
    connection_manager1.disconnect()

    print 'here1'
class DataloggerBase(object):

    def __init__(self, sensors=None, kwargs_parser=get_datalogger_config):
        """
            sensors : list of sensor objects to be initialized. (Not instances!)
            odr : total ODR of all sensor (ODR is needed here to determine board power saving mode)
            args : global parameters
        """

        # override .cfg parameters in application if needed
        self.override_config_parameters()

        # override .cfg paramters with command line arguments
        if kwargs_parser:
            kwargs_parser()

        self.sensors = []
        self.sensor = None

        # ODR is needed here to determine board power saving mode
        self.connection_manager = ConnectionManager(
            odr=evkit_config.odr)
        self.stream = None

        # add sensors if sensor list given
        if sensors:
            for sensor in sensors:
                self.add_sensor(sensor())

            # reset the sensor so no any old settings are in place
            for sensor in self.sensors:
                sensor.por()

    def override_config_parameters(self):
        # .cfg parameters can be overriden here
        pass

    def add_sensor(self, sensor):
        self.sensors.append(sensor)
        self.connection_manager.add_sensor(sensor)

    def enable_data_logging(self, **kwargs):
        raise NotImplementedError()

    def make_reader_arguments(self, reader_arguments={}):

        if evkit_config.loop == 0:
            reader_arguments['loop'] = None
        else:
            reader_arguments['loop'] = evkit_config.loop

        if evkit_config.max_timeout_count == 0:
            reader_arguments['max_timeout_count'] = None
        else:
            reader_arguments['max_timeout_count'] = evkit_config.max_timeout_count

        return reader_arguments

    def read_with_polling(
            self,
            loop,
            hdr,
            console=True,
            callback=None,
            max_timeout_count=0):

        sensor = self.sensors[0]

        count = 0
        dl = SensorDataLogger(console=console)
        dl.add_channel(hdr)
        dl.start()
        try:
            while (loop is None) or (count < loop):
                count += 1
                sensor.drdy_function(timeout=max_timeout_count)
                data = sensor.read_data()
                data = [10] + data
                dl.feed_values(data)

                if callback is not None:
                    # callback function returns False if need to stop reading
                    if callback(data) is False:
                        break

        except KeyboardInterrupt:
            pass

        finally:
            dl.stop()

    def read_with_stream(self, stream_class, pin_index=None, reader_arguments={}):
        self.stream = stream_class(
            sensors=self.sensors,
            pin_index=pin_index,  # None = fetches pin_index from settings.cfg
            timer=None if evkit_config.drdy_function_mode != TIMER_POLL else evkit_config.drdy_timer_interval
        )
        self.stream.read_data_stream(**reader_arguments)

    def run(self, stream_class, pin_index=None, reader_arguments={}):
        "Read sensor data until stopped or loop count reached"

        if self.sensor:
            self.sensors = [self.sensor]
            LOGGER.error('Old way to use this class')
        try:
            reader_arguments = self.make_reader_arguments(reader_arguments)
            if evkit_config.stream_mode:
                self.read_with_stream(
                    stream_class,
                    pin_index,
                    reader_arguments=reader_arguments
                )
            else:
                self.read_with_polling(
                    hdr=stream_class.hdr,
                    **reader_arguments
                )
        finally:
            self.power_off()
            self.connection_manager.disconnect()

    def power_off(self):
        for sensor in self.sensors:
            sensor.set_power_off()