The Alpha Geek – Geeking Out

Gyroscope

Project #25 – Movement – Quaternion – Mk06

——

#DonLucElectronics #DonLuc #SparkFunRedBoard #Movement #MPU9150 #9DOF #Quaternion #Magnetometer #Accelerometer #Gyroscope #Arduino #Project #Fritzing #Programming #Electronics #Microcontrollers #Consultant

——

Quaternion

——

Quaternion

——

Quaternion

——

Quaternion

In mathematics, the quaternion number system extends the complex numbers. Quaternions were first described by the Irish mathematician William Rowan Hamilton in 1843 and applied to mechanics in three-dimensional space. Hamilton defined a quaternion as the quotient of two directed lines in a three-dimensional space, as the quotient of two vectors. Multiplication of quaternions is noncommutative.

Quaternions are used in pure mathematics, but also have practical uses in applied mathematics, particularly for calculations involving three-dimensional rotations, such as in three-dimensional computer graphics, computer vision, and crystallographic texture analysis. They can be used alongside other methods of rotation, such as Euler angles and rotation matrices, or as an alternative to them, depending on the application.

SparkFun 9 Degrees of Freedom Breakout – MPU-9150

The SparkFun 9DOF MPU-9150 is the world’s first 9-axis MotionTracking MEMS device designed for the low power, low cost, and high performance requirements of consumer electronics equipment including smartphones, tablets and wearable sensors. And guess what? You get to play with it.

This breakout board makes it easy to prototype with the InvenSense MPU-9150 by breaking out all the pins you need to standard 0.1″ spaced headers. The board also provides I2C pullup resistors and a solder jumper to switch the I2C address of the device.

The MPU-9150 is a System in Package (SiP) that combines two chips: the MPU-6050, which contains a 3-axis gyroscope, 3-axis accelerometer, and an onboard Digital Motion Processor™ (DMP™) capable of processing complex MotionFusion algorithms; and the AK8975, a 3-axis digital compass. The part’s integrated 6-axis MotionFusion algorithms access all internal sensors to gather a full set of sensor data.

DL2211Mk02

1 x SparkFun RedBoard Qwiic
1 x SparkFun 9 Degrees of Freedom Breakout – MPU-9150
1 x SparkFun Cerberus USB Cable

SparkFun RedBoard Qwiic

SDA – Analog A4
SCL – Analog A5
VIN – +3.3V
GND – GND

——

DL2211Mk02p.ino

/* ***** Don Luc Electronics © *****
Software Version Information
Project #25 - Movement - Quaternion - Mk06
25-06
DL2211Mk02p.ino
1 x SparkFun RedBoard Qwiic
1 1 x SparkFun 9 Degrees of Freedom Breakout - MPU-9150
1 x SparkFun Cerberus USB Cable
*/

// Include the Library Code
// Two Wire Interface (TWI/I2C)
#include <Wire.h>
// I2CDev I2C utilities
#include "I2Cdev.h"
// MPU9150Lib 9-axis fusion
#include "MPU9150Lib.h"
// CalLib magnetometer and accelerometer calibration
#include "CalLib.h"
// Motion Driver InvenSense Embedded SDK v5.1
#include <dmpKey.h>
#include <dmpmap.h>
#include <inv_mpu.h>
#include <inv_mpu_dmp_motion_driver.h>
// EEPROM Magnetometer and Accelerometer data is stored
#include <EEPROM.h>

// the MPU object
MPU9150Lib MPU;
//  MPU_UPDATE_RATE defines the rate (in Hz)
// at which the MPU updates the sensor data and DMP output
#define MPU_UPDATE_RATE  (20)
//  MAG_UPDATE_RATE defines the rate (in Hz) at which the
// MPU updates the magnetometer data
//  MAG_UPDATE_RATE should be less than or equal to the MPU_UPDATE_RATE
#define MAG_UPDATE_RATE  (10)
//  MPU_MAG_MIX defines the influence that the magnetometer has on the yaw output.
//  The magnetometer itself is quite noisy so some mixing with the gyro yaw can help
//  significantly. Some example values are defined below:
// Just use gyro yaw
#define  MPU_MAG_MIX_GYRO_ONLY          0
// Just use magnetometer and no gyro yaw
#define  MPU_MAG_MIX_MAG_ONLY           1
// A good mix value 
#define  MPU_MAG_MIX_GYRO_AND_MAG       10
// mainly gyros with a bit of mag correction
#define  MPU_MAG_MIX_GYRO_AND_SOME_MAG  50
//  MPU_LPF_RATE is the low pas filter rate and can be between 5 and 188Hz
#define MPU_LPF_RATE   5

// This is our earth frame gravity vector - quaternions and vectors
MPUQuaternion gravity;                                     

//  SERIAL_PORT_SPEED defines the speed to use for the debug serial port
#define  SERIAL_PORT_SPEED  115200

// Software Version Information
String sver = "25-06";

void loop() {

  // MPU
  isMPU();
  
}

getMPU.ino

// MPU
// Setup MPU
void isSetupMPU() {

  // MPU
  MPU.init(MPU_UPDATE_RATE, MPU_MAG_MIX_GYRO_AND_MAG, MAG_UPDATE_RATE, MPU_LPF_RATE);     // start the MPU

  // Set up the initial gravity vector for quaternion rotation
  // Max value down the z axis
  gravity[QUAT_W] = 0;
  gravity[QUAT_X] = 0;
  gravity[QUAT_Y] = 0;
  gravity[QUAT_Z] = SENSOR_RANGE;
  
}
// MPU
void isMPU() {

  // Quaternion
  // This is our body frame gravity vector
  MPUQuaternion rotatedGravity;
  // This is the conjugate of the fused quaternion
  MPUQuaternion fusedConjugate;
  // Used in the rotation
  MPUQuaternion qTemp;
  // The accelerations
  MPUVector3 result;

  // Get the latest data
  if (MPU.read()) {
    
    // Need this for the rotation
    MPUQuaternionConjugate(MPU.m_fusedQuaternion, fusedConjugate);
    
    // Rotate the gravity vector into the body frame
    MPUQuaternionMultiply(gravity, MPU.m_fusedQuaternion, qTemp);
    MPUQuaternionMultiply(fusedConjugate, qTemp, rotatedGravity);
    
    // Now subtract rotated gravity from the body accels to get real accelerations.
    // Note that signs are reversed to get +ve acceleration results
    // in the conventional axes.
    result[VEC3_X] = -(MPU.m_calAccel[VEC3_X] - rotatedGravity[QUAT_X]);
    result[VEC3_Y] = -(MPU.m_calAccel[VEC3_Y] - rotatedGravity[QUAT_Y]);
    result[VEC3_Z] = -(MPU.m_calAccel[VEC3_Z] - rotatedGravity[QUAT_Z]);
    
    // print the residual accelerations
    MPU.printVector(result);
    Serial.println();
    
  }

}

setup.ino

// Setup
void setup() {

  // Serial
  Serial.begin(SERIAL_PORT_SPEED);
  Serial.println("Accel9150 starting");
  
  // Give display time to power on
  delay(100);
  
  // Set up I2C bus
  Wire.begin();

  // Setup MPU
  isSetupMPU();

}

——

People can contact us: https://www.donluc.com/?page_id=1927

Technology Experience

  • Single-Board Microcontrollers (PIC, Arduino, Raspberry Pi,Espressif, etc…)
  • IoT
  • Wireless (Radio Frequency, Bluetooth, WiFi, Etc…)
  • Robotics
  • Camera and Video Capture Receiver Stationary, Wheel/Tank and Underwater Vehicle
  • Unmanned Vehicles Terrestrial and Marine
  • Machine Learning
  • RTOS
  • Research & Development (R & D)

Instructor and E-Mentor

  • IoT
  • PIC Microcontrollers
  • Arduino
  • Raspberry Pi
  • Espressif
  • Robotics

Follow Us

Luc Paquin – Curriculum Vitae – 2023
https://www.donluc.com/luc/

Web: https://www.donluc.com/
Facebook: https://www.facebook.com/neosteam.labs.9/
YouTube: https://www.youtube.com/channel/UC5eRjrGn1CqkkGfZy0jxEdA
Twitter: https://twitter.com/labs_steam
Pinterest: https://www.pinterest.com/NeoSteamLabs/
Instagram: https://www.instagram.com/neosteamlabs/

Don Luc

Project #25 – Movement – 9-DOF – Mk04

——

#DonLucElectronics #DonLuc #SparkFunRedBoard #Movement #9DOF #Accelerometer #Magnetometer #Gyroscope #Arduino #Project #Fritzing #Programming #Electronics #Microcontrollers #Consultant

——

9-DOF

——

9-DOF

——

9-DOF

——

Roll, Pitch, and Yaw

How is Controlling an Airplane or Robotic Different than Controlling a Car or Boat?

Stability and control are much more complex for an airplane, which can move freely in three dimensions, than for cars or boats, which only move in two. A change in any one of the three types of motion affects the other two.

Imagine three lines running through an airplane and intersecting at right angles at the airplane’s center of gravity.

  • Rotation around the front-to-back axis is called Roll.
  • Rotation around the side-to-side axis is called Pitch.
  • Rotation around the vertical axis is called Yaw.

SparkFun 9 Degrees of Freedom – Sensor Stick

The SparkFun 9DOF Sensor Stick is a very small sensor board with 9 degrees of freedom. It includes the ADXL345 accelerometer, the HMC5883L magnetometer, and the ITG-3200 MEMS gyro. The “Stick” has a simple I2C interface and a mounting hole for attaching it to your project. Also, the board is a mere allowing it to be easily mounted in just about any application.

DL2210Mk08

1 x SparkFun RedBoard Qwiic
1 x SparkFun Micro OLED (Qwiic)
1 x Qwiic Cable – 100mm
1 x SparkFun 9 Degrees of Freedom – Sensor Stick
1 x SparkFun Cerberus USB Cable

SparkFun RedBoard Qwiic

SDA – Analog A4
SCL – Analog A5
VIN – +3.3V
GND – GND

——

DL2210Mk08p.ino

/* ***** Don Luc Electronics © *****
Software Version Information
Project #25 - Movement - 9-DOF - Mk04
25-04
DL2210Mk06p.ino
1 x SparkFun RedBoard Qwiic
1 x SparkFun Micro OLED (Qwiic)
1 x Qwiic Cable - 100mm
1 x SparkFun 9 Degrees of Freedom - Sensor Stick
1 x SparkFun Cerberus USB Cable
*/

// Include the Library Code
// Two Wire Interface (TWI/I2C)
#include <Wire.h>
// SparkFun Micro OLED
#include <SFE_MicroOLED.h>
// Includes and variables for IMU integration
// Accelerometer
#include <ADXL345.h>
// Magnetometer
#include <HMC58X3.h>
// MEMS Gyroscope
#include <ITG3200.h>
// Debug
#include "DebugUtils.h"
// FreeIMU
#include <CommunicationUtils.h>
#include <FreeIMU.h>

// Set the FreeIMU object
FreeIMU my3IMU = FreeIMU();

// Yaw Pitch Roll
float ypr[3];
float Yaw = 0;
float Pitch = 0;
float Roll = 0;

// SparkFun Micro OLED
#define PIN_RESET 9
#define DC_JUMPER 1
// I2C declaration
MicroOLED oled(PIN_RESET, DC_JUMPER);

// Software Version Information
String sver = "25-04";

void loop() {

  // isFreeIMU
  isFreeIMU();
  
  // Micro OLED
  isMicroOLED();

  // One delay in between reads
  delay(1000);
  
}

getFreeIMU.ino

// FreeIMU
// isFreeIMU
void isFreeIMU(){

  // FreeIMU
  // Yaw Pitch Roll
  my3IMU.getYawPitchRoll(ypr);
  // Yaw
  Yaw = ypr[0];
  // Pitch
  Pitch = ypr[1];
  // Roll
  Roll = ypr[2];

}

getMicroOLED.ino

// SparkFun Micro OLED
// Setup Micro OLED
void isSetupMicroOLED() {

  // Initialize the OLED
  oled.begin();
  // Clear the display's internal memory
  oled.clear(ALL);
  // Display what's in the buffer (splashscreen)
  oled.display();

  // Delay 1000 ms
  delay(1000);

  // Clear the buffer.
  oled.clear(PAGE);
  
}
// Micro OLED
void isMicroOLED() {

  // Text Display FreeIMU
  // Clear the display
  oled.clear(PAGE);
  // Set cursor to top-left
  oled.setCursor(0, 0);
  // Set font to type 0
  oled.setFontType(0);
  // FreeIMU
  oled.print("FreeIMU");
  oled.setCursor(0, 12);
  // Yaw
  oled.print("Y: ");
  oled.print(Yaw);
  oled.setCursor(0, 25);
  // Pitch
  oled.print("P: ");
  oled.print(Pitch);
  oled.setCursor(0, 39);
  // Roll
  oled.print("R: ");
  oled.print(Roll);
  oled.display();

}

setup.ino

// Setup
void setup() {

  // Give display time to power on
  delay(100);
  
  // Set up I2C bus
  Wire.begin();

  // Setup Micro OLED
  isSetupMicroOLED();

  // Pause
  delay(5);
  // Initialize IMU
  my3IMU.init();
  // Pause
  delay(5);

}

——

People can contact us: https://www.donluc.com/?page_id=1927

Technology Experience

  • Single-Board Microcontrollers (PIC, Arduino, Raspberry Pi,Espressif, etc…)
  • IoT
  • Wireless (Radio Frequency, Bluetooth, WiFi, Etc…)
  • Robotics
  • Camera and Video Capture Receiver Stationary, Wheel/Tank and Underwater Vehicle
  • Unmanned Vehicles Terrestrial and Marine
  • Machine Learning
  • RTOS
  • Research & Development (R & D)

Instructor and E-Mentor

  • IoT
  • PIC Microcontrollers
  • Arduino
  • Raspberry Pi
  • Espressif
  • Robotics

Follow Us

Luc Paquin – Curriculum Vitae – 2022
https://www.donluc.com/luc/

Web: https://www.donluc.com/
Facebook: https://www.facebook.com/neosteam.labs.9/
YouTube: https://www.youtube.com/channel/UC5eRjrGn1CqkkGfZy0jxEdA
Twitter: https://twitter.com/labs_steam
Pinterest: https://www.pinterest.com/NeoSteamLabs/
Instagram: https://www.instagram.com/neosteamlabs/

Don Luc

Project #25 – Movement – Gyroscope L3G4200D – Mk03

——

#DonLucElectronics #DonLuc #SparkFunRedBoard #Movement #Gyroscope #Arduino #Project #Fritzing #Programming #Electronics #Microcontrollers #Consultant

——

Gyroscope

——

Gyroscope

——

Gyroscope

——

Gyroscope

A gyroscope is a device used for measuring or maintaining orientation and angular velocity.It is a spinning wheel or disc in which the axis of rotation is free to assume any orientation by itself. When rotating, the orientation of this axis is unaffected by tilting or rotation of the mounting, according to the conservation of angular momentum.

Gyroscopes based on other operating principles also exist, such as the microchip-packaged MEMS gyroscopes found in electronic devices, solid-state ring lasers, fibre optic gyroscopes, and the extremely sensitive quantum gyroscope. MEMS gyroscopes are popular in some consumer electronics, such as smartphones.

SparkFun Tri-Axis Gyro Breakout – L3G4200D

This is a breakout board for the L3G4200D low-power three-axis angular rate sensor. The L3G4200D is a MEMS motion sensor and has a full scale of ±250 or ±500 or ±2000 dps and is capable of measuring rates with a user selectable bandwidth. These work great in gaming and virtual reality input devices, GPS navigation systems and robotics.

DL2210Mk07

1 x SparkFun RedBoard Qwiic
1 x SparkFun Micro OLED (Qwiic)
1 x Qwiic Cable – 100mm
1 x SparkFun Tri-Axis Gyro Breakout – L3G4200D
1 x SparkFun Cerberus USB Cable

SparkFun RedBoard Qwiic

SDA – Analog A4
SCL – Analog A5
VIN – +3.3V
GND – GND

DL2210Mk07p.ino

/* ***** Don Luc Electronics © *****
Software Version Information
Project #25 - Movement - Gyroscope L3G4200D - Mk03
25-02
DL2210Mk06p.ino
1 x SparkFun RedBoard Qwiic
1 x SparkFun Micro OLED (Qwiic)
1 x Qwiic Cable - 100mm
1 x SparkFun Tri-Axis Gyro Breakout - L3G4200D
1 x SparkFun Cerberus USB Cable
*/

// Include the Library Code
// Two Wire Interface (TWI/I2C)
#include <Wire.h>
// SparkFun Micro OLED
#include <SFE_MicroOLED.h>
// Gyroscope
#include <L3G4200D.h>

// Gyroscope
L3G4200D gyroscope;

// Timers
unsigned long timer = 0;
float timeStep = 0.01;

// Pitch, Roll and Yaw values
float pitch = 0;
float roll = 0;
float yaw = 0;

// SparkFun Micro OLED
#define PIN_RESET 9
#define DC_JUMPER 1
// I2C declaration
MicroOLED oled(PIN_RESET, DC_JUMPER);

// Software Version Information
String sver = "25-03";

void loop() {

  // Gyroscope
  isGyroscope(),
  
  // Micro OLED
  isMicroOLED();

  // Wait to full timeStep period
  delay((timeStep*1000) - (millis() - timer));
  
}

getGyroscope.ino

// L3G4200D Triple Axis Gyroscope
// Setup Gyroscope
void isSetupGyroscope() {

  // Setup Gyroscope
  // Set scale 2000 dps and 400HZ Output data rate (cut-off 50)
  while(!gyroscope.begin(L3G4200D_SCALE_2000DPS, L3G4200D_DATARATE_400HZ_50))
  {
    // Could not find a valid L3G4200D sensor, check wiring!
    delay(500);
    
  }

  // Calibrate gyroscope. The calibration must be at rest.
  // If you don't want calibrate, comment this line.
  gyroscope.calibrate(100);
  
}
// L3G4200D Gyroscope
void isGyroscope(){

  // Timer
  timer = millis();

  // Read normalized values
  Vector norm = gyroscope.readNormalize();

  // Calculate Pitch, Roll and Yaw
  pitch = pitch + norm.YAxis * timeStep;
  roll = roll + norm.XAxis * timeStep;
  yaw = yaw + norm.ZAxis * timeStep;

}

getMicroOLED.ino

// SparkFun Micro OLED
// Setup Micro OLED
void isSetupMicroOLED() {

  // Initialize the OLED
  oled.begin();
  // Clear the display's internal memory
  oled.clear(ALL);
  // Display what's in the buffer (splashscreen)
  oled.display();

  // Delay 1000 ms
  delay(1000);

  // Clear the buffer.
  oled.clear(PAGE);
  
}
// Micro OLED
void isMicroOLED() {

  // Text Display Gyroscope
  // Clear the display
  oled.clear(PAGE);
  // Set cursor to top-left
  oled.setCursor(0, 0);
  // Set font to type 0
  oled.setFontType(0);
  // Gyroscope
  oled.print("Gyro");
  oled.setCursor(0, 12);
  // X
  oled.print("Pit: ");
  oled.print(pitch);
  oled.setCursor(0, 25);
  // Y
  oled.print("Rol: ");
  oled.print(roll);
  oled.setCursor(0, 39);
  // Z
  oled.print("Yaw: ");
  oled.print(yaw);
  oled.display();

}

setup.ino

// Setup
void setup() {

  // Give display time to power on
  delay(100);
  
  // Set up I2C bus
  Wire.begin();

  // Setup Micro OLED
  isSetupMicroOLED();

  // L3G4200D Gyroscope
  isSetupGyroscope();

}

——

People can contact us: https://www.donluc.com/?page_id=1927

Technology Experience

  • Single-Board Microcontrollers (PIC, Arduino, Raspberry Pi,Espressif, etc…)
  • IoT
  • Wireless (Radio Frequency, Bluetooth, WiFi, Etc…)
  • Robotics
  • Camera and Video Capture Receiver Stationary, Wheel/Tank and Underwater Vehicle
  • Unmanned Vehicles Terrestrial and Marine
  • Machine Learning
  • RTOS
  • Research & Development (R & D)

Instructor and E-Mentor

  • IoT
  • PIC Microcontrollers
  • Arduino
  • Raspberry Pi
  • Espressif
  • Robotics

Follow Us

Luc Paquin – Curriculum Vitae – 2022
https://www.donluc.com/luc/

Web: https://www.donluc.com/
Facebook: https://www.facebook.com/neosteam.labs.9/
YouTube: https://www.youtube.com/channel/UC5eRjrGn1CqkkGfZy0jxEdA
Twitter: https://twitter.com/labs_steam
Pinterest: https://www.pinterest.com/NeoSteamLabs/
Instagram: https://www.instagram.com/neosteamlabs/

Don Luc

Categories
Archives