def test(): mpu9150 = MPU9150() testfunc(mpu9150) print() utime.sleep_ms(250) print("Repeating") testfunc(mpu9150)
def test(): mpu9150 = MPU9150('X') testfunc(mpu9150) print() pyb.delay(250) print("Repeating") testfunc(mpu9150)
# fusiontest.py Simple test program for sensor fusion on Pyboard # Author Peter Hinch # Released under the MIT License (MIT) # Copyright (c) 2017 Peter Hinch # V0.8 14th May 2017 Option for external switch for cal test. Make platform independent. # V0.7 25th June 2015 Adapted for new MPU9x50 interface from machine import Pin import utime as time from mpu9150 import MPU9150 from fusion import Fusion imu = MPU9150('X') fuse = Fusion() # Code for external switch switch = Pin('Y7', Pin.IN, pull=Pin.PULL_UP) # Switch to ground on Y7 def sw(): return not switch.value() # Code for Pyboard switch #sw = pyb.Switch() # Choose test to run Calibrate = True Timing = True
# E 6 2 Y2 # D7 14 3 Y3 # D6 13 4 Y4 # D5 12 5 Y5 # D4 11 6 Y6 from machine import Pin import uasyncio as asyncio import gc from mpu9150 import MPU9150 from fusion_async import Fusion # Using async version from alcd import LCD, PINLIST # Library supporting Hitachi LCD module switch = Pin('Y7', Pin.IN, pull=Pin.PULL_UP) # Switch to ground on Y7 imu = MPU9150('X') # Attached to 'X' bus, 1 device, disable interruots lcd = LCD(PINLIST, cols=24) # Should work with 16 column LCD # User coro returns data and determines update rate. # For 9DOF sensors returns three 3-tuples (x, y, z) for accel, gyro and mag # For 6DOF sensors two 3-tuples (x, y, z) for accel and gyro async def read_coro(): imu.mag_trigger() await asyncio.sleep_ms(20) # Plenty of time for mag to be ready return imu.accel.xyz, imu.gyro.xyz, imu.mag_nonblocking.xyz fuse = Fusion(read_coro)
import time from mpu9150 import MPU9150 import micropython micropython.alloc_emergency_exception_buf(100) def cb(timer): # Callback: populate array members imu.get_gyro_irq() imu.get_accel_irq() imu.get_mag_irq() tim = Timer.Alarm(cb, 400, periodic=True) imu = MPU9150() tim.callback(cb) print("You should see slightly different values on each pair of readings.") print( " Accelerometer Gyro Magnetometer" ) for count in range(10): time.sleep_ms(400) scale = 3.33198 # Correction factors involve floating point mag = list( map(lambda x, y: x * y / scale, imu.mag.ixyz, imu.mag_correction)) print("Interrupt:", [x / 16384 for x in imu.accel.ixyz], [x / 131 for x in imu.gyro.ixyz], mag) time.sleep_ms(100) print("Normal: ", imu.accel.xyz, imu.gyro.xyz, imu.mag.xyz)
from mpu9150 import MPU9150 import time import cPickle as pickle import numpy as np import signal import sys accel_data = [] gyro_data = [] mag_data = [] time_data = [] mpu = MPU9150() def sigint_handler(signal, frame): print 'SIGINT caught' print 'Saving data and exiting...' d = { 'time': np.array(time_data), 'accel_data': np.array(accel_data), 'gyro_data': np.array(gyro_data), 'mag_data': np.array(mag_data) } filename = 'mpu_data_{:s}.p'.format(time.strftime('%Y-%m-%d_%H-%M-%S')) with open(filename, 'wb') as f: pickle.dump(d, f) sys.exit(0)
from machine import I2C from as_drivers.hd44780.alcd import LCD, PINLIST # Library supporting Hitachi LCD module from mpu9150 import MPU9150 async def lcd_task(mylcd, imu): print('Running...') k = imu.mag_correction mylcd[1] = "x {:5.3f} y {:5.3f} z {:5.3f}".format(k[0],k[1],k[2]) while True: try: while not imu.mag_ready: await asyncio.sleep(0) except OSError: mylcd[0] = "Mag read failure" continue xyz = imu.mag.xyz mylcd[0] = "x {:5.1f} y {:5.1f} z {:5.1f}".format(xyz[0], xyz[1], xyz[2]) await asyncio.sleep_ms(500) lcd0 = LCD(PINLIST, cols = 24) imu = MPU9150(I2C(1)) try: asyncio.run(lcd_task(lcd0, imu)) except KeyboardInterrupt: print('Interrupted') finally: asyncio.new_event_loop()
from usched import Sched, wait, Poller from lcdthread import LCD, PINLIST # Library supporting Hitachi LCD module from mpu9150 import MPU9150 def pollfunc(mpu): return 1 if mpu.mag_ready else None def lcd_thread(mylcd, mpu9150): k = mpu9150.mag_correction mylcd[1] = "x {:5.3f} y {:5.3f} z {:5.3f}".format(k[0], k[1], k[2]) while True: reason = (yield Poller(pollfunc, (mpu9150, ))) # Scheduler returns when pollfunc if reason[ 1] == 1: # returns something other than None. 1 indicates ready. xyz = mpu9150.mag.xyz mylcd[0] = "x {:5.1f} y {:5.1f} z {:5.1f}".format( xyz[0], xyz[1], xyz[2]) elif reason[1] == 2: mylcd[0] = "Mag read failure" yield from wait(0.5) objSched = Sched() lcd0 = LCD(PINLIST, objSched, cols=24) mpu9150 = MPU9150('X') objSched.add_thread(lcd_thread(lcd0, mpu9150)) objSched.run()