Ejemplo n.º 1
0
    def __init__(self, i2c=None):
        super().__init__('rtf_lis3mdl')

        if i2c is None:
            self.i2c = busio.I2C(board.SCL, board.SDA)
        else:
            self.i2c = i2c

        self.lis = adafruit_lis3mdl.LIS3MDL(
            self.i2c)  # 155 Hz, 4 gauss, continuous
        self.lis.data_rate = adafruit_lis3mdl.Rate.RATE_155_HZ

        self.timer_mag = self.create_timer(1 / 100, self.callback)
        self.pub_mag = self.create_publisher(MagneticField, 'magnetic', 10)

        self.mags_bias = self.declare_parameter('mags_bias', [0., 0., 0.])

        frame_id = self.declare_parameter('frame_id', "base_imu_link").value

        self.mag_msg = MagneticField()
        self.mag_msg.header.frame_id = frame_id

        mc = 0.01
        self.mag_msg.magnetic_field_covariance = [
            mc, 0.0, 0.0, 0.0, mc, 0.0, 0.0, 0.0, mc
        ]

        self.calibrate = False
    def __init__(self):
        # Define I2C:
        self._i2c = board.I2C()

        # Define touch:
        # Initially, self._touches stores the pin used for a particular touch. When that touch is
        # used for the first time, the pin is replaced with the corresponding TouchIn object.
        # This saves a little RAM over using a separate read-only pin tuple.
        # For example, after `clue.touch_2`, self._touches is equivalent to:
        # [board.D0, board.D1, touchio.TouchIn(board.D2)]
        self._touches = [board.D0, board.D1, board.D2]
        self._touch_threshold_adjustment = 0

        # Define buttons:
        self._a = digitalio.DigitalInOut(board.BUTTON_A)
        self._a.switch_to_input(pull=digitalio.Pull.UP)
        self._b = digitalio.DigitalInOut(board.BUTTON_B)
        self._b.switch_to_input(pull=digitalio.Pull.UP)
        self._gamepad = gamepad.GamePad(self._a, self._b)

        # Define LEDs:
        self._white_leds = digitalio.DigitalInOut(board.WHITE_LEDS)
        self._white_leds.switch_to_output()
        self._pixel = neopixel.NeoPixel(board.NEOPIXEL, 1)
        self._red_led = digitalio.DigitalInOut(board.L)
        self._red_led.switch_to_output()

        # Define audio:
        self._mic = audiobusio.PDMIn(
            board.MICROPHONE_CLOCK,
            board.MICROPHONE_DATA,
            sample_rate=16000,
            bit_depth=16,
        )
        self._sample = None
        self._samples = None
        self._sine_wave = None
        self._sine_wave_sample = None

        # Define sensors:
        # Accelerometer/gyroscope:
        self._accelerometer = adafruit_lsm6ds.LSM6DS33(self._i2c)

        # Magnetometer:
        self._magnetometer = adafruit_lis3mdl.LIS3MDL(self._i2c)

        # DGesture/proximity/color/light sensor:
        self._sensor = adafruit_apds9960.apds9960.APDS9960(self._i2c)

        # Humidity sensor:
        self._humidity = adafruit_sht31d.SHT31D(self._i2c)

        # Barometric pressure sensor:
        self._pressure = adafruit_bmp280.Adafruit_BMP280_I2C(self._i2c)

        # Create displayio object for passing.
        self.display = board.DISPLAY
Ejemplo n.º 3
0
 def detect_motion_lis3mdl(self):
     try:
         import board
         import busio
         import adafruit_lis3mdl
         self.sensor_lis3mdl = adafruit_lis3mdl.LIS3MDL(
             busio.I2C(board.SCL, board.SDA))
         return True
     except:
         return False
Ejemplo n.º 4
0
    def __init__(self):
        # Define I2C:
        self._i2c = cutebot._i2c

        # Define buttons:
        self._a = digitalio.DigitalInOut(board.BUTTON_A)
        self._a.switch_to_input(pull=digitalio.Pull.UP)
        self._b = digitalio.DigitalInOut(board.BUTTON_B)
        self._b.switch_to_input(pull=digitalio.Pull.UP)
        self._gamepad = gamepad.GamePad(self._a, self._b)

        # Define LEDs:
        self._white_leds = digitalio.DigitalInOut(board.WHITE_LEDS)
        self._white_leds.switch_to_output()
        self._pixel = neopixel.NeoPixel(board.NEOPIXEL, 1)
        self._red_led = digitalio.DigitalInOut(board.L)
        self._red_led.switch_to_output()

        # Define audio:
        self._mic = audiobusio.PDMIn(
            board.MICROPHONE_CLOCK,
            board.MICROPHONE_DATA,
            sample_rate=16000,
            bit_depth=16,
        )
        self._sample = None
        self._samples = None
        self._sine_wave = None
        self._sine_wave_sample = None

        # Define sensors:
        # Accelerometer/gyroscope:
        self._accelerometer = adafruit_lsm6ds.lsm6ds33.LSM6DS33(self._i2c)

        # Magnetometer:
        self._magnetometer = adafruit_lis3mdl.LIS3MDL(self._i2c)

        # DGesture/proximity/color/light sensor:
        self._sensor = adafruit_apds9960.apds9960.APDS9960(self._i2c)

        # Humidity sensor:
        self._humidity = adafruit_sht31d.SHT31D(self._i2c)

        # Barometric pressure sensor:
        self._pressure = adafruit_bmp280.Adafruit_BMP280_I2C(self._i2c)

        # Create displayio object for passing.
        self.display = board.DISPLAY
def magnetometer():
    i2c = board.I2C()  # uses board.SCL and board.SDA
    sensor = adafruit_lis3mdl.LIS3MDL(i2c)

    rospy.loginfo("Initializing mag publisher")
    mag_pub = rospy.Publisher('/mag', MagneticField, queue_size=5)
    rospy.loginfo("Publishing MagneticField at: " + mag_pub.resolved_name)
    rospy.init_node('mag_node')

    rate = rospy.Rate(10) # 50hz
    while not rospy.is_shutdown():
        mag_x, mag_y, mag_z = sensor.magnetic
        mag_x, mag_y, mag_z = calibrate(mag_x, mag_y, mag_z)
        rospy.loginfo('X:{0:10.2f}, Y:{1:10.2f}, Z:{2:10.2f} uT'.format(mag_x, mag_y, mag_z))
        rospy.loginfo("")

        mag = MagneticField()
        mag.header.stamp = rospy.Time.now()
        mag.header.frame_id = 'LIS3MDL Triple-axis Magnetometer'
        mag.magnetic_field_covariance = 0
        mag.magnetic_field = create_vector3(mag_x, mag_y, mag_z)
        mag_pub.publish(mag)
        rate.sleep()
Ejemplo n.º 6
0
"""This uses the Feather Sense as a Bluetooth LE sensor node."""

import time
import adafruit_ble_broadcastnet
import board
import adafruit_lsm6ds   # accelerometer
import adafruit_sht31d   # humidity sensor
import adafruit_bmp280   # barometric sensor
import adafruit_lis3mdl  # magnetic sensor

i2c = board.I2C()

sense_accel = adafruit_lsm6ds.LSM6DS33(i2c)
sense_humid = adafruit_sht31d.SHT31D(i2c)
sense_barometric = adafruit_bmp280.Adafruit_BMP280_I2C(i2c)
sense_magnet = adafruit_lis3mdl.LIS3MDL(i2c)

print("This is BroadcastNet Feather Sense sensor:", adafruit_ble_broadcastnet.device_address)

while True:
    measurement = adafruit_ble_broadcastnet.AdafruitSensorMeasurement()

    measurement.temperature = sense_barometric.temperature
    measurement.pressure = sense_barometric.pressure
    measurement.relative_humidity = sense_humid.relative_humidity
    measurement.acceleration = sense_accel.acceleration
    measurement.magnetic = sense_magnet.magnetic

    # print(measurement)
    adafruit_ble_broadcastnet.broadcast(measurement)
    time.sleep(60)
# import adafruit_apds9960.apds9960
import adafruit_lis3mdl
import adafruit_lsm6ds
import adafruit_ble_broadcastnet

print("This is BroadcastNet sensor:", adafruit_ble_broadcastnet.device_address)

i2c = board.I2C()

# Define sensors:
# Accelerometer/gyroscope:
lsm6ds = adafruit_lsm6ds.LSM6DS33(i2c)

# Magnetometer:
lis3mdl = adafruit_lis3mdl.LIS3MDL(i2c)

# DGesture/proximity/color/light sensor:
# TODO: How do we get the light level?
# apds9960 = adafruit_apds9960.apds9960.APDS9960(i2c)
# apds9960.enable_color = True

# Humidity sensor:
sht31d = adafruit_sht31d.SHT31D(i2c)

# Barometric pressure sensor:
bmp280 = adafruit_bmp280.Adafruit_BMP280_I2C(i2c)

while True:
    measurement = adafruit_ble_broadcastnet.AdafruitSensorMeasurement()
    measurement.temperature = (sht31d.temperature, bmp280.temperature)
""" Display magnetometer data once per second """

import time
import board
import busio
import adafruit_lis3mdl

i2c = busio.I2C(board.SCL, board.SDA)
sensor = adafruit_lis3mdl.LIS3MDL(i2c)

while True:
    mag_x, mag_y, mag_z = sensor.magnetic

    print("X:{0:10.2f}, Y:{1:10.2f}, Z:{2:10.2f} uT".format(
        mag_x, mag_y, mag_z))
    print("")
    time.sleep(1.0)
Ejemplo n.º 9
0
import adafruit_lsm6ds.lsm6ds33 # motion
import adafruit_lis3mdl # magnetometer
import adafruit_apds9960.apds9960 # light

    

core_service = CoreService()
last_update = 0

ble = BLERadio()
i2c = board.I2C()

# dummy_sensor = DummySensor()
battery = analogio.AnalogIn(board.VOLTAGE_MONITOR)
motion = adafruit_lsm6ds.lsm6ds33.LSM6DS33(i2c)
magnetometer = adafruit_lis3mdl.LIS3MDL(i2c)
light = adafruit_apds9960.apds9960.APDS9960(i2c)
light.enable_color = True
microphone = audiobusio.PDMIn(board.MICROPHONE_CLOCK, board.MICROPHONE_DATA,
                              sample_rate=16000, bit_depth=16)

def normalized_rms(values):
    minbuf = int(sum(values) / len(values))
    return int(math.sqrt(sum(float(sample - minbuf) *
                             (sample - minbuf) for sample in values) / len(values)))

adv = PhysBrykServerAdvertisement()
adv.complete_name = "PhysBrykAlpha"

while True:
    # Advertise when not connected.
Ejemplo n.º 10
0
rad2deg = 180 / pi
thumb = False
bag = BagIt(Pickle)
hz = 20
# rate = Rate(hz)

i2c = busio.I2C(board.SCL, board.SDA, 100000)

# 'RANGE_1000_DPS', 'RANGE_125_DPS', 'RANGE_2000_DPS', 'RANGE_250_DPS', 'RANGE_4000_DPS', 'RANGE_500_DPS'
imu = LSM6DS33(i2c)
imu.accelerometer_range = AccelRange.RANGE_2G  # pylint: disable=no-member
imu.gyro_range = GyroRange.RANGE_1000_DPS

lps = adafruit_lps2x.LPS22(i2c)
lis = adafruit_lis3mdl.LIS3MDL(i2c)  # 155 Hz, 4 gauss, continuous
lis.data_rate = adafruit_lis3mdl.Rate.RATE_560_HZ

start = time.monotonic()
last = start
cnt = 0
m, p, t = 0, 0, 0

try:
    while True:
        # ts = time.time()
        # ts = time.monotonic()
        # dt = ts - last
        # mhz = int(1/dt)
        # last = ts
Ejemplo n.º 11
0
import board
import adafruit_lsm6ds
import adafruit_lis3mdl
import adafruit_apds9960.apds9960
import adafruit_sht31d
import adafruit_bmp280
import adafruit_rockblock

# RockBlock setup
uart = board.UART()
uart.baudrate = 19200
rb = adafruit_rockblock.RockBlock(uart)

# all the sensors
accelo = adafruit_lsm6ds.LSM6DS33(board.I2C())
magno = adafruit_lis3mdl.LIS3MDL(board.I2C())
prox = adafruit_apds9960.apds9960.APDS9960(board.I2C())
sht = adafruit_sht31d.SHT31D(board.I2C())
bmp = adafruit_bmp280.Adafruit_BMP280_I2C(board.I2C())

# build data
# can decode on other end with struct.unpack("<6fB5f", data)
data = struct.pack("3f", *accelo.acceleration)
data += struct.pack("3f", *magno.magnetic)
data += struct.pack("B", prox.proximity())
data += struct.pack("2f", sht.relative_humidity, sht.temperature)
data += struct.pack("3f", bmp.pressure, bmp.altitude, bmp.temperature)

# send data
rb.data_out = data
print("Talking to satellite...")
Ejemplo n.º 12
0
def main():
    DEBUG = True
    BOARD = True  #flag indicating whether attached to a board.

    try:
        import board
    except NotImplementedError:
        # no board attached so mock sensors, services etc
        import mock as mk
        print('No valid board. Using mock sensors and services')
        BOARD = False

    # sensors
    import adafruit_lsm6ds.lsm6ds33  # motion
    import adafruit_lis3mdl  # magnetometer
    import adafruit_apds9960.apds9960  # EMR

    import time

    dummy_sensor = DummySensor()

    if BOARD:  # valid board present use real sensors
        import analogio

        battery = analogio.AnalogIn(board.VOLTAGE_MONITOR)
        motion = adafruit_lsm6ds.lsm6ds33.LSM6DS33(board.I2C())
        magnet = adafruit_lis3mdl.LIS3MDL(board.I2C())
        emr = adafruit_apds9960.apds9960.APDS9960(board.I2C())
        # emr.enable_proximity = True
        emr.enable_color = True

        # Create and initialize the available services.
        ble = BLERadio()
        battery_svc = BatteryService()
        motion_svc = MotionService()
        magnet_svc = MagnetService()
        emr_svc = EMRService()
        dummy_svc = DummyService()
        adv = PhysBrykServerAdvertisement()

    else:  #use mock sensors and services

        # Accelerometer and gyro
        motion = mk.Sensor()
        magnet = mk.Sensor()
        emr = mk.Sensor()
        battery = mk.Sensor()

        ble = mk.Service()
        battery_svc = mk.Service()
        motion_svc = mk.Service()
        magnet_svc = mk.Service()
        emr_svc = mk.Service()
        dummy_svc = mk.Service()
        adv = mk.Service()

    ble.name = "PhysBryk_Alpha"

    last_update = 0

    while True:
        # Advertise when not connected.
        ble.start_advertising(adv)
        if DEBUG: print('Connecting...')
        while not ble.connected:
            pass
        ble.stop_advertising()
        if DEBUG:
            print('Connected!')

        while ble.connected:
            now_msecs = time.monotonic_ns() // 1000000  # pylint: disable=no-member

            if now_msecs - last_update >= MEASUREMENT_PERIOD:
                battery_svc.voltage = battery_svc.get_voltage(battery)
                motion_svc.acceleration = motion.acceleration  # m/s/s
                motion_svc.gyro = motion.gyro  # rad/s
                magnet_svc.magnetic = magnet.magnetic  # microT

                emr_svc.intensity = emr_svc.get_lux(emr.color_data)
                emr_svc.spectrum = emr.color_data
                emr_svc.proximity = emr.proximity
                dummy_svc.value = 42
                dummy_sensor.update()
                last_update = now_msecs

                if DEBUG:
                    print(f'motion acceleration: {motion_svc.acceleration}')
                    print(f'motion gyro: {motion_svc.gyro}')
                    print(f'magnet magnet: {magnet_svc.magnetic}')
                    print(f'emr intensity: {emr_svc.intensity}')
                    print(f'emr spectrum: {emr_svc.spectrum}')
                    print(f'emr proximity: {emr_svc.proximity}')
                    print(f'battery: {battery_svc.voltage}')
                    print(f'dummy: {dummy_svc.value}')
                if not BOARD:
                    for s in mk.sensors:
                        s.update()
import busio
import adafruit_blinka
from adafruit_bus_device.i2c_device import I2CDevice
import adafruit_lis3mdl as lis3mdl
import time
from math import atan2, degrees

i2c = busio.I2C()
sensor = lis3mdl.LIS3MDL(i2c, address=30)  # lis address = 30 decimal


def vector_2_degrees(x, y):
    angle = degrees(atan2(y, x))
    if angle < 0:
        angle += 360
    return angle


def get_heading(_sensor):
    magnet_x, magnet_y, _ = _sensor.magnetic
    return vector_2_degrees(magnet_x, magnet_y)


while True:
    print("heading: {:.2f} degrees".format(get_heading(sensor)))
    time.sleep(0.2)