Skip to content

Commit

Permalink
Add IMU module
Browse files Browse the repository at this point in the history
  • Loading branch information
maxpromer committed Sep 17, 2020
1 parent 12f72ba commit 8050527
Show file tree
Hide file tree
Showing 13 changed files with 67 additions and 4 deletions.
4 changes: 2 additions & 2 deletions ports/esp32/Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Select the board to build for: if not given on the command line,
# then default to GENERIC.
# BOARD ?= GENERIC
BOARD ?= KidBrightV15i
BOARD ?= KidBright32

# If the build directory is not given, make it reflect the board name.
BUILD ?= build-$(BOARD)
Expand Down Expand Up @@ -43,7 +43,7 @@ include $(TOP)/py/py.mk

GIT_SUBMODULES = lib/berkeley-db-1.xx

PORT ?= /dev/ttyS188
PORT ?= /dev/ttyS10
BAUD ?= 1152000
FLASH_MODE ?= dio
FLASH_FREQ ?= 40m
Expand Down
File renamed without changes.
63 changes: 63 additions & 0 deletions ports/esp32/boards/KidBright32/modules/imu.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# Dev by Sonthaya Nongnuch

from machine import Pin, I2C

MPU6050_ADDR = const(0x68)
LSM303AGR_ACC_ADDR = const(0x19)
LSM303AGR_MEG_ADDR = const(0x1E)

MPU6050 = const(0)
LSM303AGR = const(1)

acc = [ 0, 0, 0 ]
gyro = [ 0, 0, 0 ]
mag = [ 0, 0, 0 ]
temp = 0.0

# Check sensor on board
i2c1 = I2C(1, scl=Pin(5), sda=Pin(4), freq=100000)
devAddrs = i2c1.scan()
__device = -1
if MPU6050_ADDR in devAddrs:
__device = MPU6050
elif LSM303AGR_ACC_ADDR in devAddrs:
__device = LSM303AGR

if __device == MPU6050:
i2c1.writeto_mem(MPU6050_ADDR, 0x6B, b'\x00')
i2c1.writeto_mem(MPU6050_ADDR, 0x1C, b'\x00') # 2g
i2c1.writeto_mem(MPU6050_ADDR, 0x1B, b'\x00') # 250 */sec
elif __device == LSM303AGR:
i2c1.writeto_mem(LSM303AGR_ACC_ADDR, 0x2E, b'\x00') # FIFO_CTRL_REG_A
i2c1.writeto_mem(LSM303AGR_ACC_ADDR, 0x20, b'\x57') # CTRL_REG1_A
i2c1.writeto_mem(LSM303AGR_ACC_ADDR, 0x23, b'\x00') # Full-scale selection, 2g
i2c1.writeto_mem(LSM303AGR_MEG_ADDR, 0x60, b'\x8C') # CFG_REG_A_M
i2c1.writeto_mem(LSM303AGR_MEG_ADDR, 0x61, b'\x01') # CFG_REG_B_M


def b2i(x, y):
return (x << 8 | y) if not x & 0x80 else (-(((x ^ 255) << 8) | (y ^ 255) + 1))

def update():
global acc, gyro, mag, temp
if __device == MPU6050:
buff = i2c1.readfrom_mem(MPU6050_ADDR, 0x3B, 14)
for i in range(3):
acc[i] = round(b2i(buff[(i * 2)], buff[(i * 2) + 1]) / 16384.0 * 1000.0, 2)
temp = round(b2i(buff[6], buff[7]) / 340.00 + 36.53, 2)
for i in range(3):
gyro[i] = round(b2i(buff[(i * 2) + 8], buff[(i * 2) + 9]) / 131.0, 2)
elif __device == LSM303AGR:
buff = [ 0, 0, 0, 0, 0, 0 ]
for i in range(6):
value = i2c1.readfrom_mem(LSM303AGR_ACC_ADDR, 0x28 + i, 1)[0]
buff[i] = value
for i in range(3):
acc[i] = b2i(buff[(i * 2) + 1], buff[(i * 2) + 0])
acc[i] = round(((acc[i] >> 6) * 3900 + 500) / 1000, 2)
buff = [ 0, 0, 0, 0, 0, 0 ]
for i in range(6):
buff[i] = i2c1.readfrom_mem(LSM303AGR_MEG_ADDR, 0x68 + i, 1)[0]
for i in range(3):
mag[i] = b2i(buff[(i * 2) + 1], buff[(i * 2) + 0])
mag[i] = round(mag[i] * 1.5, 2)
2 changes: 2 additions & 0 deletions ports/esp32/boards/KidBright32/mpconfigboard.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#define MICROPY_HW_BOARD_NAME "KidBright32"
#define MICROPY_HW_MCU_NAME "ESP32"
2 changes: 0 additions & 2 deletions ports/esp32/boards/KidBrightV15i/mpconfigboard.h

This file was deleted.

0 comments on commit 8050527

Please sign in to comment.