Advertisement
microrobotics

UM7 Orientation Sensor

Apr 24th, 2023
1,054
1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. The UM7 Orientation Sensor is an Attitude and Heading Reference System (AHRS) that provides real-time 3D orientation data using a combination of accelerometers, gyroscopes, and magnetometers. To interface the UM7 with an Arduino, you can use the UART communication protocol.
  3.  
  4. First, download and install the UM7 Arduino library from the following link: https://github.com/romainreignier/UM7-Arduino
  5.  
  6. The following example demonstrates the complete functionality of the UM7 Orientation Sensor. It reads and displays the quaternion values, Euler angles (roll, pitch, yaw), and raw sensor data (accelerometer, gyroscope, magnetometer).
  7.  
  8. To use this code:
  9.  
  10. Connect the UM7 Orientation Sensor to your Arduino. Connect the sensor's GND pin to the Arduino's GND, the sensor's VCC pin to the Arduino's 3.3V or 5V (check the sensor's datasheet for the appropriate voltage), the sensor's TX pin to the Arduino's digital pin 10, and the sensor's RX pin to the Arduino's digital pin 11.
  11. Copy and paste the code above into a new Arduino sketch.
  12. Upload the code to your Arduino board.
  13. Open the Serial Monitor to view the quaternion values, Euler angles, and raw sensor data.
  14. The example code above reads the quaternion data, Euler angles, and raw sensor data (accelerometer, gyroscope, and magnetometer) from the UM7 sensor and prints them to the Serial Monitor. The um7.setBroadcastRate() function can be used to set the update rate of the sensor data. In this example, all data types are set to be broadcasted at 50 Hz. You can adjust the update rate as needed for your application.
  15.  
  16. This code provides a comprehensive overview of the UM7 Orientation Sensor's functionality. You can further customize the code to suit your specific requirements, such as data logging, sensor fusion, or integrating the sensor data into a control system.
  17. */
  18.  
  19. #include <SoftwareSerial.h>
  20. #include <UM7.h>
  21.  
  22. // Define the pins connected to the UM7
  23. const int rxPin = 10; // Arduino RX pin connected to UM7 TX pin
  24. const int txPin = 11; // Arduino TX pin connected to UM7 RX pin
  25.  
  26. // Create a SoftwareSerial instance for communication with the UM7
  27. SoftwareSerial um7Serial(rxPin, txPin);
  28.  
  29. // Create an instance of the UM7 class
  30. UM7 um7;
  31.  
  32. void setup() {
  33.   Serial.begin(115200);
  34.   um7Serial.begin(115200);
  35.  
  36.   // Initialize the UM7
  37.   while (!um7.begin(um7Serial)) {
  38.     Serial.println("Failed to connect to the UM7. Retrying in 3 seconds...");
  39.     delay(3000);
  40.   }
  41.   Serial.println("Connected to UM7!");
  42.  
  43.   // Enable quaternion data output
  44.   um7.setBroadcastRate(UM7_QUATERNIONS, 50); // Update rate in Hz (1-255)
  45.   um7.setBroadcastRate(UM7_EULER, 50);
  46.   um7.setBroadcastRate(UM7_RAW_ACCEL, 50);
  47.   um7.setBroadcastRate(UM7_RAW_GYRO, 50);
  48.   um7.setBroadcastRate(UM7_RAW_MAG, 50);
  49. }
  50.  
  51. void loop() {
  52.   // Read the UM7 data
  53.   if (um7.read()) {
  54.     float qw, qx, qy, qz;
  55.     float roll, pitch, yaw;
  56.     float ax, ay, az;
  57.     float gx, gy, gz;
  58.     float mx, my, mz;
  59.  
  60.     // Get the quaternion data
  61.     um7.getQuaternion(qw, qx, qy, qz);
  62.  
  63.     // Get the Euler angles
  64.     um7.getEuler(roll, pitch, yaw);
  65.  
  66.     // Get the raw accelerometer data
  67.     um7.getAccel(ax, ay, az);
  68.  
  69.     // Get the raw gyroscope data
  70.     um7.getGyro(gx, gy, gz);
  71.  
  72.     // Get the raw magnetometer data
  73.     um7.getMag(mx, my, mz);
  74.  
  75.     // Print the data to the serial monitor
  76.     Serial.print("Qw: ");
  77.     Serial.print(qw);
  78.     Serial.print("\tQx: ");
  79.     Serial.print(qx);
  80.     Serial.print("\tQy: ");
  81.     Serial.print(qy);
  82.     Serial.print("\tQz: ");
  83.     Serial.println(qz);
  84.  
  85.     Serial.print("Roll: ");
  86.     Serial.print(roll);
  87.     Serial.print("\tPitch: ");
  88.     Serial.print(pitch);
  89.     Serial.print("\tYaw: ");
  90.     Serial.println(yaw);
  91.  
  92.     Serial.print("Accel (g) - X: ");
  93.     Serial.print(ax);
  94.     Serial.print("\tY: ");
  95.     Serial.print(ay);
  96.     Serial.print("\tZ: ");
  97.     Serial.println(az);
  98.  
  99.     Serial.print("Gyro (dps) - X: ");
  100.     Serial.print(gx);
  101.     Serial.print("\tY: ");
  102.     Serial.print(gy);
  103.     Serial.print("\tZ: ");
  104.     Serial.println(gz);
  105.  
  106.     Serial.print("Mag (uT) - X: ");
  107.     Serial.print(mx);
  108.     Serial.print("\tY: ");
  109.     Serial.print(my);
  110.     Serial.print("\tZ: ");
  111.     Serial.println(mz);
  112.  
  113.     Serial.println();
  114.   }
  115.   delay(20); // Adjust the delay based on the broadcast rate
  116. }
  117.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement