The repository comtains everything for IMU data reading via micropython firmware for ESP32
Create python environment with all dependencies and activate it:
python -m venv /venv
source /venv/bin/activate
pip install -r requirements.txt
All the code is stored in esp32_part Remember that the program entry point is main.py
If you are putting MicroPython on your board for the first time then you should first erase the entire flash using:
Check the port connected to the device:
ld /dev/ttyUSB*
esptool.py --chip esp32 --port /dev/ttyUSBn erase_flash
Then upload the needed micropython firmware instruction all available firmware is here.
esptool.py --chip esp32 --port /dev/ttyUSB0 --baud 460800 write_flash -z 0x1000 FIRMWARE.bin
Load the program via the instruction:
ampy --port /dev/ttyUSBn put esp32_part/main.py
ampy --port /dev/ttyUSBn put esp32_part/micropython_mpu9250
Install picocom
sudo apt-get install picocom
Check the port connected to the device:
ld /dev/ttyUSB*
Use the port name when start reading data via picocom. Use default baudrate with -b flag
sudo picocom /dev/ttyUSBn -b 115200
sudo python pc_part/IMU_serial_reading.py
All the code is stored in esp32_part Remember that the program entry point is main.py
To calibrate sensor, use the calibration function for gyroscope and magnetometer.
sensor = MPU9250(i2c)
sensor.ak8963.calibrate()
sensor.mpu6500.calibrate()
main.py example with sensor calibration:
import utime
from machine import I2C, Pin
from micropython_mpu9250.mpu9250 import MPU9250
import json
i2c = I2C(scl=Pin(22), sda=Pin(21))
sensor = MPU9250(i2c)
print("MPU9250 id: " + hex(sensor.whoami))
sensor.ak8963.calibrate()
sensor.mpu6500.calibrate()
print("Calibration complete")
while True:
print("Gyro offset", sensor.mpu6500._gyro_offset)
print("Mag offset", sensor.ak8963._offset)
print("Mag scale", sensor.ak8963._scale)
utime.sleep_ms(1000)
Store the results and use them later after sensor initialization:
sensor = MPU9250(i2c)
sensor.mpu6500._gyro_offset = ...
sensor.ak8963._offset = ...
sensor.ak8963._scale = ...
# start data reading
example for data reading:
import utime
from machine import I2C, Pin
from micropython_mpu9250.mpu9250 import MPU9250
import json
i2c = I2C(scl=Pin(22), sda=Pin(21))
sensor = MPU9250(i2c)
print("MPU9250 id: " + hex(sensor.whoami))
while True:
data = {"acc": sensor.acceleration, "gyr": sensor.gyro, "mag": sensor.magnetic, "tem": sensor.temperature}
print(json.dumps(data))
utime.sleep_ms(1000)
==To start the code on ESP, open the port via picocom and press Ctrl+d==
The simplest example contains in simple_serial_reading_to_csv.py. We collect data from the /dev/ttyUSB0 port and then writes it to .csv file for further exploration.
It is almost impossible to check if the sensor works correctly without visualization, therefore lets start with graphs.
The base for graph plotting script: simple_visualize_graph.py.
The data buffer of the serial port use queue to store data, therefore (since the Matplotlib is slow) the visualization will have a growing delay. So, in several seconds it will be hard to map the graph with real inertia of IMU.
So, now we will use Redis.!.
After installation you are ready to start redis service:
sudo systemctl start redis
Updated version of graph plotting script: visualize_graphs_vs_redis.py.
So, to start reading the data from IMU and store it to Redis, run the script: Terminal 1:
sudo python pc_part/read_serial_vs_redis.py
Open the new terminal and run the visualization script: Terminal 2:
sudo python pc_part/visualize_graphs_vs_redis.py
Also, we can speed up calculations, because of splitting up it to several processes.
Terminal 4:
sudo python pc_part/3d_visualization_vs_redis.py
The visualization works if it runs via Debugger. I know, I know, vpython sucks. But easy <3