This lesson shows you some of the simple math, that will allow you to approximate tilt angle from a three axis accelerometer. In this entire series of lessons, we are using the BNO055 9-axis sensor connected to an Arduino Nano. It is possible to approximate tilt along the x-axis and tilt along the y-axis. These could roughly be considered to be like the Euler Angles of Pitch and Roll. It should be noted that the approximations are reasonable for tilt angles less than 45 degrees. Also note, that in this simple demonstration, real acceleration will be interpreted as tilt, hence the system will incorrectly interpret motion as tilt. Because of this, this approach should not be used on things like drones or other moving systems. It is just for simple demonstration purposes.
The code developed in the video above is included below for your convenience.
The code below is for demo purposes only, and should not be used in any real applications. It just demonstrates how to work with this sensor in benchtop presentations.
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 | #include <Wire.h> #include <Adafruit_Sensor.h> #include <Adafruit_BNO055.h> #include <utility/imumaths.h> #include <math.h> float theta; float phi; #define BNO055_SAMPLERATE_DELAY_MS (100) Adafruit_BNO055 myIMU = Adafruit_BNO055(); void setup() { // put your setup code here, to run once: Serial.begin(115200); myIMU.begin(); delay(1000); int8_t temp=myIMU.getTemp(); myIMU.setExtCrystalUse(true); } void loop() { // put your main code here, to run repeatedly: uint8_t system, gyro, accel, mg = 0; myIMU.getCalibration(&system, &gyro, &accel, &mg); imu::Vector<3> acc =myIMU.getVector(Adafruit_BNO055::VECTOR_ACCELEROMETER); theta=-atan2(acc.x()/9.8,acc.z()/9.8)/2/3.141592654*360; phi=-atan2(acc.y()/9.8,acc.z()/9.8)/2/3.141592654*360; Serial.print(acc.x()/9.8); Serial.print(","); Serial.print(acc.y()/9.8); Serial.print(","); Serial.print(acc.z()/9.8); Serial.print(","); Serial.print(accel); Serial.print(","); Serial.print(gyro); Serial.print(","); Serial.print(mg); Serial.print(","); Serial.print(system); Serial.print(","); Serial.print(theta); Serial.print(","); Serial.println(phi); delay(BNO055_SAMPLERATE_DELAY_MS); } |