i2c

Classes:

I2C_9DOF([accel, gyro, mag, gyro_hpf, ...])

A Sparkfun 9DOF combined accelerometer, magnetometer, and gyroscope.

MLX90640([fps, integrate_frames, interpolate])

A MLX90640 Temperature sensor.

class I2C_9DOF(accel: bool = True, gyro: bool = True, mag: bool = True, gyro_hpf: float = 0.2, accel_range=16, kalman_mode: str = 'both', invert_gyro=False, *args, **kwargs)[source]

Bases: autopilot.hardware.Hardware

A Sparkfun 9DOF combined accelerometer, magnetometer, and gyroscope.

Sensor Datasheet: https://cdn.sparkfun.com/assets/learn_tutorials/3/7/3/LSM9DS1_Datasheet.pdf

Hardware Datasheet: https://github.com/sparkfun/9DOF_Sensor_Stick

Documentation on calculating position values: https://arxiv.org/pdf/1704.06053.pdf

This device uses I2C, so must be connected accordingly:

  • VCC: 3.3V (pin 2)

  • Ground: (any ground pin

  • SDA: I2C.1 SDA (pin 3)

  • SCL: I2C.1 SCL (pin 5)

This class uses code from the Adafruit Circuitfun library, modified to use pigpio

Parameters
  • accel (bool) – Whether the accelerometer should be made active (default: True)

  • gyro (bool) – Whether the gyroscope should be made active (default: True) – accel must be true if gyro is true

  • mag (bool) – Whether the magnetomete should be made active (default: True)

  • gyro_hpf (int, float) – Highpass filter cutoff for onboard gyroscope filter. One of GYRO_HPF_CUTOFF (default: 4), or False to disable

  • kalman_mode (‘both’, ‘accel’, None) – Whether to use a kalman filter that integrates accelerometer and gyro readings (‘both’, default), a kalman filter with just the accelerometer values (‘accel’), or just return the raw calculated orientation values from rotation

  • invert_gyro (list, tuple) – if not False (default), a list/tuple of the numerical axis index to invert on the gyroscope. eg. passing (1, 2) will invert the y and z axes.

Attributes:

ACCELRANGE_2G

ACCELRANGE_16G

ACCELRANGE_4G

ACCELRANGE_8G

MAGGAIN_4GAUSS

MAGGAIN_8GAUSS

MAGGAIN_12GAUSS

MAGGAIN_16GAUSS

GYROSCALE_245DPS

GYROSCALE_500DPS

GYROSCALE_2000DPS

GYRO_HPF_CUTOFF

Highpass-filter cutoff frequencies (keys, in Hz) mapped to binary flag.

accel_range

The accelerometer range.

mag_gain

The magnetometer gain.

gyro_scale

The gyroscope scale.

gyro_filter

Set the high-pass filter for the gyroscope.

gyro_polarity

acceleration

The calibrated x, y, z acceleration in m/s^2

magnetic

The magnetometer X, Y, Z axis values as a 3-tuple of gauss values.

gyro

The gyroscope X, Y, Z axis values as a 3-tuple of degrees/second values.

rotation

Return roll (rotation around x axis) and pitch (rotation around y axis) computed from the accelerometer

temperature

Returns: float: Temperature in Degrees C

Methods:

calibrate([what, samples, sample_dur])

Calibrate sensor readings to correct for bias and scale errors

ACCELRANGE_2G = 0
ACCELRANGE_16G = 8
ACCELRANGE_4G = 16
ACCELRANGE_8G = 24
MAGGAIN_4GAUSS = 0
MAGGAIN_8GAUSS = 32
MAGGAIN_12GAUSS = 64
MAGGAIN_16GAUSS = 96
GYROSCALE_245DPS = 0
GYROSCALE_500DPS = 8
GYROSCALE_2000DPS = 24
GYRO_HPF_CUTOFF = {0.1: 9, 0.2: 8, 0.5: 7, 1: 6, 2: 5, 4: 4, 8: 3, 15: 2, 30: 1, 57: 0}

Highpass-filter cutoff frequencies (keys, in Hz) mapped to binary flag.

Note

the frequency of a given binary flag is dependent on the output frequency (952Hz by default, changing frequency is not currently exposed in this object). See Table 52 of the sensor datasheet for more.

property accel_range

The accelerometer range. Must be one of: - I2C_9DOF.ACCELRANGE_2G - I2C_9DOF.ACCELRANGE_4G - I2C_9DOF.ACCELRANGE_8G - I2C_9DOF.ACCELRANGE_16G

property mag_gain

The magnetometer gain. Must be a value of: - I2C_9DOF.MAGGAIN_4GAUSS - I2C_9DOF.MAGGAIN_8GAUSS - I2C_9DOF.MAGGAIN_12GAUSS - I2C_9DOF.MAGGAIN_16GAUSS

property gyro_scale

The gyroscope scale. Must be a value of: - I2C_9DOF.GYROSCALE_245DPS - I2C_9DOF.GYROSCALE_500DPS - I2C_9DOF.GYROSCALE_2000DPS

property gyro_filter: Union[int, float, bool]

Set the high-pass filter for the gyroscope.

Note

the frequency of a given binary flag is dependent on the output frequency (952Hz by default, changing frequency is not currently exposed in this object). See Table 52 of the sensor datasheet for more.

Parameters

gyro_filter (int, float, False) – Filter frequency (in GYRO_HPF_CUTOFF) or False to disable

Returns

current HPF cutoff or False if disabled

Return type

float, bool

property gyro_polarity
property acceleration

The calibrated x, y, z acceleration in m/s^2

Returns

x, y, z acceleration

Return type

accel (tuple)

property magnetic

The magnetometer X, Y, Z axis values as a 3-tuple of gauss values.

Returns

x, y, z gauss values

Return type

(tuple)

property gyro

The gyroscope X, Y, Z axis values as a 3-tuple of degrees/second values.

property rotation

Return roll (rotation around x axis) and pitch (rotation around y axis) computed from the accelerometer

Uses transform.geometry.IMU_Orientation to fuse accelerometer and gyroscope with Kalman filter

Returns

np.ndarray - [roll, pitch]

property temperature

Returns: float: Temperature in Degrees C

calibrate(what: str = 'accelerometer', samples: int = 10000, sample_dur: Optional[float] = None) dict[source]

Calibrate sensor readings to correct for bias and scale errors

Note

Currently only calibrating the accelerometer is implemented.

The accelerometer is calibrated by rotating the sensor slowly in all three rotational dimensions in such a way that minimizes linear acceleration (not due to gravity). A perfect sensor would output a sphere of points centered at 0

Parameters
  • what (str) – which sensor is to be calibrated (currentlty only “accelerometer” implemented)

  • samples (int) – number of samples that should be used to compute the calibration

  • sample_dur (float) – number of seconds to sample for, overrides samples if not None (default)

Returns

calibration dictionary (also saved to disk using Hardware.calibration )

Return type

dict

logger: logging.Logger
class MLX90640(fps=64, integrate_frames=64, interpolate=3, **kwargs)[source]

Bases: autopilot.hardware.cameras.Camera

A MLX90640 Temperature sensor.

Parameters
  • fps (int) – Acquisition framerate, must be one of MLX90640.ALLOWED_FPS

  • integrate_frames (int) – Number of frames to average over

  • interpolate (int) – Interpolation multiplier – 3 “increases the resolution” 3x

  • **kwargs – passed to Camera

Variables
  • shape (tuple) – :attr:`~MLX90640.SHAPE_SENSOR

  • integrate_frames (int) – Number of frames to average over

  • interpolate (int) – Interpolation multiplier – 3 “increases the resolution” 3x

  • _grab_event (threading.Event) – capture thread sets every time it gets a frame, _grab waits every time, keeps us from returning same frame twice

This device uses I2C, so must be connected accordingly:

  • VCC: 3.3V (pin 2)

  • Ground: (any ground pin

  • SDA: I2C.1 SDA (pin 3)

  • SCL: I2C.1 SCL (pin 5)

Uses a modified version of the MLX90640 Library that is capable of outputting 64fps. You must install the library separately, see the setup_mlx90640.sh script.

Capture works a bit differently from other Cameras – the capture_init() method spawns a _threaded_capture() thread, which continually puts frames in the _frames array which serves as a ring buffer. The _grab() method then awaits the _grab_event to be set by the capture thread, and when it is set returns the mean across frames of the ring buffer.

Note

The setup script modifies the systemwide i2c baudrate to 1MHz, which may interfere with other I2C devices. It can be returned to 400kHz (default) by editing /config/boot.txt to read dtparam=i2c_arm_baudrate=400000

Attributes:

type

what are we anyway?

ALLOWED_FPS

FPS must be one of these

SHAPE_SENSOR

(H, W) Output shape of this sensor is always the same.

fps

integrate_frames

interpolate

Methods:

init_cam()

Set the camera object to use our MLX90640.fps

capture_init()

Spawn a _threaded_capture() thread

interpolate_frame(frame)

Interpolate frame according to interpolate using scipy.interpolate.griddata()

release()

Stops the capture thread, cleans up the camera, and calls the superclass release method.

type = 'MLX90640'

what are we anyway?

Type

(str)

ALLOWED_FPS = (1, 2, 4, 8, 16, 32, 64)

FPS must be one of these

SHAPE_SENSOR = (32, 24)

(H, W) Output shape of this sensor is always the same. May differ from MLX90640.shape if interpolate >1

logger: logging.Logger
property fps
property integrate_frames
property interpolate
init_cam()[source]

Set the camera object to use our MLX90640.fps

capture_init()[source]

Spawn a _threaded_capture() thread

interpolate_frame(frame)[source]

Interpolate frame according to interpolate using scipy.interpolate.griddata()

Parameters

frame (numpy.ndarray) – Frame to interpolate

Returns

Interpolated Frame

Return type

(numpy.ndarray)

release()[source]

Stops the capture thread, cleans up the camera, and calls the superclass release method.