We will use a sensor hat to attach I2C sensor modules.
We will install driver software and readout the sensors.
We will fuse sensor data to estimate altitude of the sensor.
We will publish the data to MQTT data broker (distribute on the web)
Before attching the sensor hat to the Pi make sure that none of the GPIO pins touch each other.
After attaching the sensor hat to the Pi, make sure that the resistance of the 3.3V pin to ground is more than 200 Ohm.
Do not attach the sensor board to the energized Pi without having it checked by course staff.
You need to measure resistance between 3.3V and GND and it should read more than 200 Ohms.
You need to measure resistance between 5V and GND and it should read more than 200 Ohms.
You need to measure resistance between 3.3V and 5 V and it should read more than 200 Ohms.
YOU CAN NOT MEASURE RESISTANCE WHEN YOUR CIRCUIT IS POWERED ON.
The MPU 6050 is a 6 axis pose sensor consisting of a 3-axis accelerometer and a 3-axis gyroscope. They are micro mechanical systems [MEMS] with oscillating parts for the gyroscope and a proof and spring system to measure acceleration.
From last minute engineers website:
How does accelerometer work:
A proof mass moves in opposite direction of the frame's acceleration. The distance to a fixed object is measured. We measure the distance with capacitators that consists of finger-like structure. If they are closer together the capacitance increases. We measure capacitance with an oscillator. When an oscillating potential is attached to a capacitor the output experiences a phase shift. This shift can be measured and is proportional to capacitance, which is proportional to the deflection of the proof mass which is proportional to the acceleration. In the actual implementation you can see two finger structures and the mass on the right side being larger than the left side. The difference between the left capacitor and right capacitor help calibrating the system. Accelerations relevant to human motion are much smaller than earth's gravity. However, gravity is always superimposed onto the signal we want to measure and determines noise terms.
How does gyroscope work:
When a bicycle wheel is spinning and you attempt changing the direction of the axle you experience a force perpendicular to the axle. You could measure the deflection of a spring this force creates. However, it's not trivial to create a rotating disk with MEMS (Micro-Electromechanical Systems). Instead of rotating, we can oscillate mechanical parts. We apply a field to a capacitor which pushes its plates apart creating linear motion. If the sensor rotates, the oscillating plates experience Coriolis forces as shown below for rotations around X, Y and Z axis. This force deflects the plates out of the plane of oscillation. For simplicity the deflection is shown without the oscillation. In the center a sensor measures deflection, either through capacitance or strain gauge. MEMS gyroscopes experience drift and are not sensitive enough to measures earth's rotation (2*pi/24/60/60 = 0.07 mrad/sec). To measure gyration more accurately, ring lasers are used and the color shift of the laser is measured after passing multiple times through a ring cavity. Such gyroscopes are used in planes but are about two orders of magnitude more expensive than MEMS.
The accelerometer and gyroscope axis in the MPU 6850 follow the convention below. Please note the "right hand rule" in respect to axis direction and rotation. For navigation there are different conventions one can choose such as X being forward, Y east and Z downwards. In that convention a positive pitch lifts the nose of a plane up (rotation around Y), a positive roll (rotation around X) moves the right wing down and a positive yaw (rotation around Z) moves the nose to the right from the pilot's perspective. Roll, pitch and yaw are also called Euler angles (Euler is the Swiss guy who gave us the e constant).
Computing the post or attitude from gyroscope and acceleration data is not trivial. It uses signal filters and feedback systems estimating the pose from multiple sensor inputs. One such filter is the Madgwick filter. The filter was first published in 2010 as a PhD dissertation. Dr. Madgwick maintains a python repository that is regularly updated imufusion. You should install it the same way you installed mpu6050 driver. The inner workings of the filter are shown below and are topic of graduate level courses.
There is vector math involved to compute pose (q in the diagram below). Often pose is expressed as roll, pitch and yaw of an object however the math in the filter uses quaternions which are 4 element vectors and advanced linear algebra. These are higher level math concepts and not covered here.
To initiate the pose estimation, references can be used such as earth's gravity and earth's magnetic field. Then the filter can estimate attitude on a continuing basis. Our sensor does not have a magnetometer which leaves the initial state of yaw (which is rotation around the gravity vector) undetermined. We can still measure relative changes.
Once we know the pose of our sensor, we can calculate gravity removed acceleration. When we integrate acceleration, we obtain velocity and when we integrate velocity we can calculate position. MEMS based sensors are not accurate enough to create a reasonable estimation of the position. However, they are good enough to keep a drone oriented and compute the orientation of your phone under various conditions. For short amount of time, they can be used to update position on a map when GPS data is not available (tunnel or jamming or under water). Often IMU data is fused together with data from vision systems and wheel rotation to accurately navigate a car (SLAM, Simultaneous Localization and Mapping).
Since we have 3 axes, we have a total of 3 measurements for the accelerometer as well as the gyroscope.
Several errors can occur:
The gravity sensor might measure values smaller or larger than 1g for earth's gravity. Similarly, the gyroscope might not measure rotation accurately.
When you flip the sensor, it should measure the same value but negative.
The sensors for x, y and z direction might not be perfectly perpendicular, and you can have cross axis sensitivity.
The gyroscope and the accelerometer might not align perfectly to each other when they were assembled.
The accelerometer can be easily calibrated because we can measure earth's gravity. We gently rotate the sensor and record data for all possible directions. The data can be fit to an ellipsoid. The center of the ellipsoid is the offset error. The axis length of the ellipsoid is the scaling factor. Cross axis sensitivity can also be extracted from a data fit.
The gyroscope calibration is more difficult, and we would need a turntable with known rotation speed. However, we can easily calibrate offset as when the sensor is not moving the gyroscope output is supposed to be zero.
Calibration of a magnetometer can be achieved my measuring the earth's magnetic field similar to gravity. In Tucson the magnetic field is pointing towards ground in northern direction but with a couple degrees offset to true north not matching magnetic north. The magnetic field is changing over geological time frames and not homogeneous around the earth. Magnetometers usually have large offset and require calibration to be useful. The influence of electric motors or magnetic metals in proximity to the sensor cannot be calibrated away as they change in urban and technical environments.
In addition, all the calibration values might be subject to temperature changes.
The sensors in your phone are trying to self-calibrate by collecting data when it for example detects that its stationary. But occasionally it might ask you to flip and turn the phone.
For simplicity, in this lab we will skip calibration.
Follow instructions on crimping. (below)
Connectorized one sensor by crimping 4 wires of 12-inch length using the colors show below.
Obtain and install driver software for Circuit Python MPU 6050
Start developing software so you can read acceleration and gyration.
Complete the sensor, connect it to the sensor hat and make sure there is proper electrical connection and there is no short between 3.3V and ground.
Create program that outputs the raw sensor readings. Take screen shot and submit
Continue lab with:
Fuse the readings into accurate pose detection. Calculate the gravity free acceleration.
Create graphical display.
Complete graphical display and publish data on the lab MQTT server.
One prebuilt Sensor Hat
One bare sensor breakout board,
6 crimpable pins
One 4 pin Dupont JST HX female connector
Wire red for 3.3V
Wire black for GND
Wire white for I2C clock (SCL)
Wire blue for I2C data (SDA)
Bare Sensor
Cut 4 wires of appropriate color (red, black, white, blue) for your sensor and 12 inches long.
Strip approximately 2mm of insulation on one side.
Crimp the wire onto the metal pin as demonstrated here: https://youtu.be/l0rAEPJoWeE.
First crimp the middle of the connector to the bare wire,
do not push the wire past the middle section (see image below, the green wire).
use 1.3mm crimp.
insert the pin into the crimper, it will fit snug.
push the big flaps towards the crimper so that the end part of the pin that slides onto the pin in the connector is not getting damaged.
insert wire into the crimper so that insulation is on the big flaps and the bare wire in the crimper.
push crimper hard so that wire will not fall out when you are done.
Second crimp the big flaps onto the insulation of the wire. Use 1.9mm crimp.
do not push hard, otherwise you will damage the insulation.
Make sure the pins are fully attached to the wire by gently pulling on them.
Insert all pins with the wire into the Dupont JST connector. According to the following order: GND, 3.3V, SDA, SCL (as per diagram below). The sensors will need to be made so that they are interchangeable with any sensor-hat.
Cut all 4 wires to the same length
Strip the insulation on the bare side
Solder the wire to the appropriate pad on the bare sensor breakout board. Most people want to solder the wire by sticking it through the hole. But it's much easier to fill the whole with solder first and then to attach the wire parallel to the PCB on top of the hole.
Prebuilt Hat
Make sure the prebuilt sensor hat has a 40pin GPIO connector attached. It will need to have the long pins, so you can stack multiple boards on top of each other.
If the prebuilt board does not have the GPIO connector attached, place it through the holes. The long pins will need to come out at the same side as the Dupont JST HX connectors and the black plastic will need to be on the solder side of the sensor hat PCB.
The following legs will need to be soldered onto the sensor board: 3.3V (pin 1), SDA (pin 3), SCL (pin 5), GND (pin 9), GND (pin 39) and 5V (pin 2). Do not solder the other pins. Best is to solder one pin, then check if the connector is flush with the board. Then solder pin on the opposite side of the connector. Solder only at the pad not along the whole pin (otherwise you can no longer stack other hats on top of yours).
Make sure the installed sensor hat does not short on the CPU heatsink. If it is close to the cooling fins, you need to attach electrical tape to the bottom of the sensor hat.
if you want to see if your sensor is recognized:
sudo i2cdetect -y 1
Should show you hardware clock at 0x68, the sensors at 0x??.
Install the driver software.
cd ~/pythonBME210
source env/bin/activate
pip3 install adafruit_circuitpython_mpu6050
In the example program, you open the device with
import time
import board
import adafruit_mpu6050
i2c = board.I2C()
mpu = adafruit_mpu6050.MPU6050(i2c, address = 0xaddress_from_i2cdetect...)
mpu.accelration gives accelartion in m/s^2
mpu.gyro gives gyration in rad/sec
mpu.temperature gives temperature in deg C
Demo your sensor to TA/ULA.
Your demo code showing acceleration and gyration readings. In the link below turn in a screenshot of the code output in the terminal.
Fusion software is available at https://pypi.org/project/imufusion/. The maintainer of the software is x-io.co.uk which was founded by Dr. Madgwick.
You will need to add import imufusion to the import section of your code.
You initialize the fusion objecti with ahrs = imufusion.Ahrs() AHRS stands for attitude heading reference system.
And you update the algorithm each time you obtain new readings with: ahrs.update_no_magnetometer(gyroscope, accelerometer, dt) where dt is the time difference from last update to current time.
You can obtain roll, pitch and yaw with euler = imufusion.quaternion_to_euler(ahrs.quaternion)
To create a nice program displaying the sensor pose I asked chatgpt:
I am using MPU 6050 in python with Adafruit circuit python driver and I want to use imufusion python package to compute the pose of my IMU. I would also like to visualize the pose using pyqt. Can you create example code that reads the accelerometer and gyroscope, fuses the data and then uses quaternion to Euler conversion from imufusion to produce roll, pitch and yaw and display a rectangular block in a 3d display? The long axis of the rectangle should be X and the short axis Y and Z should be perpendicular to the rectangle. Since I do not have magnetometer it would be nice to have a button on the qt display to reset yaw direction. The imufusion code is located here: https://github.com/xioTechnologies/Fusion and the MPU6050 driver is here: https://github.com/adafruit/Adafruit_CircuitPython_MPU6050.
Then I asked it to update the code to also display acceleration vector:
Please overlay in the display window a vector representing gravity removed acceleration. The vector should start in the middle of the rectangular object and grow with magnitude and orient with direction.
This likely will produce a functioning program that 5 years ago required several months of my attention. If the fusion is not working properly, you should verify that gyroscope and accelerometer axis are indeed oriented as shown above and match the configuration in the program. Also you might want to specify in the program what orientation of coordiante system you want, for example North, East and Down for X, Y and Z direction (North, Eat, Up would not match the right hand rule where x is your thumb, y is your pointer and z is your middle finger.]
Continue lab with MQTT data broker lab.