PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MadgwickIMU.ino 1.1KB

4 年之前
12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. // Basic Inertial Monitoring Unit (IMU) using Madgwick filter.
  2. //
  3. // To view this data, use the Arduino Serial Monitor to watch the
  4. // scrolling angles, or run the OrientationVisualiser example in Processing.
  5. #include <NXPMotionSense.h>
  6. #include <MadgwickAHRS.h>
  7. #include <Wire.h>
  8. #include <EEPROM.h>
  9. NXPMotionSense imu;
  10. Madgwick filter;
  11. void setup() {
  12. Serial.begin(9600);
  13. imu.begin();
  14. filter.begin(100);
  15. }
  16. void loop() {
  17. float ax, ay, az;
  18. float gx, gy, gz;
  19. float mx, my, mz;
  20. float roll, pitch, heading;
  21. if (imu.available()) {
  22. // Read the motion sensors
  23. imu.readMotionSensor(ax, ay, az, gx, gy, gz, mx, my, mz);
  24. // Update the Madgwick filter
  25. filter.updateIMU(gx, gy, gz, ax, ay, az);
  26. //filter.update(gx, gy, gz, ax, ay, az, mx, my, mz);
  27. // print the heading, pitch and roll
  28. roll = filter.getRoll();
  29. pitch = filter.getPitch();
  30. heading = filter.getYaw();
  31. Serial.print("Orientation: ");
  32. Serial.print(heading);
  33. Serial.print(" ");
  34. Serial.print(pitch);
  35. Serial.print(" ");
  36. Serial.println(roll);
  37. }
  38. }