-
Notifications
You must be signed in to change notification settings - Fork 24
/
spi_mpu9250.py
141 lines (107 loc) · 4 KB
/
spi_mpu9250.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# noinspection GrazieInspection
"""
Copyright (c) 2021 Alan Yorinks All rights reserved.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
Version 3 as published by the Free Software Foundation; either
or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE
along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
"""
"""
This example initializes an MPU9250 and then reads the accelerometer
and gyro values and prints them to the screen.
The processing of the data returned from the MPU9250 is done within
the callback functions.
"""
import sys
import time
from telemetrix import telemetrix
# Instantiate the TelemetrixRpiPico class accepting all default parameters.
board = telemetrix.Telemetrix()
# Convenience values for the pins.
# Note that the CS value is within a list
# These are the standard pins for many Arduino AVR boards.
# Change to match your particular board.
# if using an Uno, CS = 10, if Mkr WiFi 1010, CS = 9.
# change for your board.
CS = [10]
CS_PIN = 10
NUM_BYTES_TO_READ = 6
"""
CALLBACKS
These functions process the data returned from the MPU9250
"""
def the_device_callback(report):
"""
Verify the device ID
:param report: [SPI_REPORT, read register, Number of bytes, device_id]
"""
if report[3] == 0x71:
print('MPU9250 Device ID confirmed.')
else:
print(f'Unexpected device ID: {report[3]}')
# noinspection GrazieInspection
def accel_callback(report):
"""
Print the AX, AY and AZ values.
:param report: [SPI_REPORT, Register, Number of bytes, AX-msb, AX-lsb
AY-msb, AY-lsb, AX-msb, AX-lsb]
"""
print(f"AX = {int.from_bytes(report[3:5], byteorder='big', signed=True)} "
f"AY = {int.from_bytes(report[5:7], byteorder='big', signed=True)} "
f"AZ = {int.from_bytes(report[7:9], byteorder='big', signed=True)} ")
def gyro_callback(report):
# noinspection GrazieInspection
"""
Print the GX, GY, and GZ values.
:param report: [SPI_REPORT, Register, Number of bytes, GX-msb, GX-lsb
GY-msb, GY-lsb, GX-msb, GX-lsb]
"""
print(f"GX = {int.from_bytes(report[3:5], byteorder='big', signed=True)} "
f"GY = {int.from_bytes(report[5:7], byteorder='big', signed=True)} "
f"GZ = {int.from_bytes(report[7:9], byteorder='big', signed=True)} ")
# This is a utility function to read SPI data
def read_data_from_device(register, number_of_bytes, callback):
# noinspection GrazieInspection
"""
This function reads the number of bytes using the register value.
Data is returned via the specified callback.
:param register: register value
:param number_of_bytes: number of bytes to read
:param callback: callback function
"""
# the read bit is OR'ed in on the device sketch
data = register
# activate chip select
board.spi_cs_control(CS_PIN, 0)
board.spi_read_blocking(data, number_of_bytes, call_back=callback)
# deactivate chip select
board.spi_cs_control(CS_PIN, 1)
time.sleep(.1)
# initialize the device
board.set_pin_mode_spi(CS)
# reset the device
board.spi_cs_control(CS_PIN, 0)
board.spi_write_blocking([0x6B, 0])
board.spi_cs_control(CS_PIN, 1)
time.sleep(.1)
# get the device ID
read_data_from_device(0x75, 1, the_device_callback)
while True:
try:
time.sleep(1)
# get the acceleration values
read_data_from_device(0x3b, 6, accel_callback)
time.sleep(.1)
# get the gyro values
read_data_from_device(0x43, 6, gyro_callback)
time.sleep(.1)
except KeyboardInterrupt:
board.shutdown()
sys.exit(0)