Visualize IMU Data Using the BNO055, ROS, and Jetson Nano

In this tutorial, we will learn how to use an NVIDIA Jetson Nano to read data from the BNO055 Absolute Orientation Sensor and connect that data to ROS using the ros_imu_bno055 package. Here is what you will be able to do by the end of this tutorial:

bno055-ros-in-action

Real-World Applications

IMUs like the BNO055 enable us to measure the acceleration and orientation of a robotic system. IMUs are common in both wheeled robots and aerial robots. 

Prerequisites

You Will Need

In addition to the parts listed in the article I linked to in the Prerequisites, you will need the following components (#ad).

Once you finish this project, you can mount the IMU on anything that moves (drone, robot car, etc.) so you can measure the system’s acceleration and orientation.

Disclosure (#ad): As an Amazon Associate I earn from qualifying purchases.

Soldering (Optional)

To make sure the electrical pins are stuck to the board, you can solder them like I did in this video here with the BNO055 board.

Set Up the Hardware

First, let’s set up the hardware.

Make the following connections between the BNO055 and the USB to Serial Converter:

  • Connect BNO055 Vin to 3.3V (VCC) power.
  • Connect BNO055 GND to ground (GND).
  • Connect BNO055 SDA (UART transmitter TX) to receiver RXD.
  • Connect BNO055 SCL (UART receiver RX) to transmitter TXD.
  • Connect BNO055 PS1 to BNO055 Vin (3.3V).
2021-04-19-16.38.10
2021-04-19-16.38.18-1

Now connect the other end of the USB to Serial Converter to the Jetson Nano.

2021-04-19-17.31.37

If you have a LIDAR connected to your Jetson Nano, disconnect it from the USB port.

Install and Build the BNO055 ROS Package

Let’s get all the software set up.

Turn on your Jetson Nano.

Open a new terminal window.

Install the BNO055 ROS package.

cd ~/catkin_ws/src
git clone https://github.com/RoboticArts/ros_imu_bno055.git

Build the package.

cd ~/catkin_ws/
catkin_make --only-pkg-with-deps ros_imu_bno055

Source the environment.

source devel/setup.bash

Install the ROS IMU plugin.

sudo apt-get install ros-melodic-rviz-imu-plugin

Install pyserial.

pip3 install pyserial

Install the udev rule to identify the device as ttyUSB_IMU.

roscd ros_imu_bno055
sudo cp utils/99-bno055.rules /etc/udev/rules.
sudo udevadm control --reload-rules && sudo udevadm trigger

Reboot your computer.

sudo reboot

View the IMU Data

Open a terminal window.

Install some packages.

sudo apt-get install python-catkin-pkg
sudo apt-get install python3-catkin-pkg-modules
sudo apt-get install python3-rospkg-modules

Get a list of the connected USB devices (there should only be one, which is the BNO055).

ls -la /dev/ttyUSB*

Change permissions of the USB connection.

sudo chmod 666 /dev/ttyUSB0

Open a new terminal, and type the following command to launch visualization of the IMU data in Rviz.

roslaunch ros_imu_bno055 view_imu.launch
1-run-imu-launch

Move your BNO055 around, and you will see the axes move. The red line is the x-axis, the green line is the y-axis, and the blue line is the z-axis.

2-imu_view_launch

Let’s see the active ROS topics. Open up a new terminal and type:

rostopic list

Now let’s see the data.

rostopic echo /imu/data
rostopic-echo

Press CTRL + C in all terminal windows to close everything down.

Another way to launch the BNO055 ROS driver is by using the following command. Notice how we specified the USB port name (ttyUSB0):

roslaunch ros_imu_bno055 imu.launch serial_port:=/dev/ttyUSB0

Let’s see the active ROS topics. Open up a new terminal and type:

rostopic list

Press CTRL + C in all terminal windows to close everything down.

Other Commands

Launch the IMU again.

roslaunch ros_imu_bno055 imu.launch serial_port:=/dev/ttyUSB0

Here are some other commands. To reset the IMU, you can use the following command:

rosservice call /imu/reset_device "{}"

When you type this command, your USB to Serial converter should blink a few times, and then hold steady.

To calibrate the IMU, type the following command:

roslaunch ros_imu_bno055 imu_calibration.launch serial_port:=/dev/ttyUSB0 operation_mode:=NDOF_FMC_OFF
3-calibration-mode

Move your IMU around as explained at the bottom of this page in order to calibrate the gyroscope, accelerometer, and the magnetometer.

That’s it! Keep building!

How to Send Roll, Pitch, & Yaw Data Over I2C From Arduino to Raspberry Pi

In this post, I’ll show you how to send roll, pitch, and yaw data over I2C using Raspberry Pi and Arduino. We’ll also capture GPS data on the Raspberry Pi to make things interesting for when I mount everything on a quadcopter.

Requirements

Here are the requirements:

  • Using the IMU connected to the Arduino, capture roll, pitch, and yaw data.
  • Using the GPS connected to the Raspberry Pi, capture latitude, longitude, and altitude data.
  • Send the IMU data via I2C to the Raspberry Pi.
  • Send the IMU and GPS data via Bluetooth from Raspberry Pi to my host computer (e.g. my personal laptop).
  • Display the data on my host computer.
  • To make things interesting, I mounted all the equipment on a quadcopter.

Design

Hardware

The following components are used in this project. You will need:

Software

Here are the steps for the GPS Poller, responsible for capturing Latitude+Longitude+Altitude data on the Raspberry Pi:

  • Open a new file to log the GPS data
  • Create a GPS Poller class
  • Log the latitude data
  • Log the longitude data
  • Log the altitude in feet
  • Delay 5 seconds
  • Close file

Here are the steps for the IMU I2C program on the Arduino, responsible for capturing Roll+Pitch+Yaw data and sending to the Raspberry Pi:

  • Set the delay between fresh samples
  • Define a flag to stop the program
  • Make the Arduino a slave to the Raspberry Pi by defining a slave address
  • Declare a byte array of size 12, which will be the roll+pitch+yaw data (with the sign) to send back to Raspberry Pi
  • Declare variables used for digit extraction
  • Declare function to end the program
  • Display some basic information on the IMU sensor
  • Display some basic info about the sensor status
  • Display sensor calibration status
  • Create method that sends a byte array (of size 12) when reading request is received from the Raspberry Pi
  • Create method that retrieves the digit of any position in an integer. The rightmost digit has position 0. The second rightmost digit has position 1, etc. e.g. Position 3 of integer 245984 is 5.
  • Create Arduino setup function (automatically called at startup) — 9600 Baud Rate
  • Initialize the sensor
  • Set up the Wire library and make Arduino the slave
  • Define the callbacks for i2c communication
  • Need callback that specifies a function when data is received from the RPi Master
  • Need callback that specifies a function when the Master requests data from the Arduino
  • Arduino loop function, called once ‘setup’ is complete
  • While not done:
    • Get a new sensor event
    • Display the floating point data and capture the roll, pitch, and yaw data. Cast the floats to signed integers.
    • Store each digit of the roll, pitch, and yaw data into a byte array (which will be sent to the RPi)
    • End program
  • While true infinite loop

Here are the steps for the I2C Python program on the Raspberry Pi, responsible for sending messages and requesting IMU data via I2C from the Arduino slave:

  • Open a new text file to log the IMU data
  • Set up slave address in the Arduino Program
  • Read a block of 12 bytes starting at SLAVE_ADDRESS, offset 0
  • Extract the IMU reading data
  • Print the IMU data to the console
  • Write the IMU data to the text file
  • Close text file when done
  • Request IMU data every 5 seconds from the Arduino

Implementation

The most straightforward way to connect the Arduino board to the Raspberry Pi is using the USB cable, as I have done in previous projects. However, we can also use I2C. I2C uses two lines: SDA (data) and SCL (clock). It also uses GND (ground).

Here are the connections that I made between the Raspberry Pi and the Arduino:

  • Raspberry Pi SDA (I2C1 SDA) –> Arduino SDA
  • Raspberry Pi SCL (I2C1 SCL) –> Arduino SCL
  • Raspberry Pi GND –> Arduino GND
imu_i2c_1

Raspberry Pi 3 Pin Mappings. Image Source: Microsoft.com

imu_i2c_2

Arduino Uno Pin Mappings. Image Source: Electronics Schematics

Here is the schematic I followed.

imu_i2c_3

Image Source: Monk (2016)

The BNO055 (IMU) was wired to the Arduino Uno using the solderless breadboard as follows:

  • Connected Vin to the power supply of 5V
  • Connected GND to common power/data ground
  • Connected the SDA pin to the I2C data SDA pin on the Arduino (A4).
  • Connected the SCL pin to the I2C clock SCL pin on the Arduino (A5).

To get started with the implementation, I tested the GPS device to see if I can successfully capture GPS latitude + longitude + altitude data on the Raspberry Pi and save it to a text file. This data will later get sent via Bluetooth from Raspberry Pi to my Host computer (HP Omen laptop with Windows 10).

GPS was connected to the Raspberry Pi via the USB cord. The commands for this are as follows:

To start the GPS stream, I typed:

sudo gpsd /dev/ttyAMA0 -F /var/run/gpsd.sock

To display the GPS data, I typed the following command:

cgps -s

Here is what the display looked like:

imu_i2c_4

Other commands I could have run are gpsmon and xgps.

gpsmon looks like this:

imu_i2c_5

xgps looks like this:

imu_i2c_6

Sometimes the GPS data did not show up immediately. When that occurred, I rebooted the Raspberry Pi by typing the following command:

sudo reboot

I typed the following command to shutdown the GPS stream:

sudo killall gpsd

Now, I want to do the same thing, but this time I want to save the GPS data to a text file. I will use this syntax in the command terminal in order to save the standard output stream to a text file.

command | tee output.txt

More specifically, I will type:

cgps -s | tee /home/pi/Documents/GPS/output.txt

The output.txt was created, and the data was logged in the file. However, it is more useful to output the GPS data in a more user-friendly format. To do this, I will run a Python script that is a GPS polling program. The code for this program is located in the Software section later in this report.

To create the program, I opened the Python IDE (Raspberry Pi -> Programming -> Python 3 (IDLE)).

I clicked File -> New File. I then added the code and saved the file as GPSPoller.py.

imu_i2c_7

To run the script, I typed

python GPSPoller.py

To stop the script, I pressed Ctrl-C. I could have also pressed Ctrl-Pause/Break.

imu_i2c_8

Here is the output of the locations.csv file. For the actual code when I flew the quadcopter, this file was named gps_data.txt.

imu_i2c_9

Here is how gps_data.txt looks:

gps_data

Next I connected Arduino to Raspberry Pi via I2C as pictured earlier in this section. I also connected them via USB in order to provide the Arduino with power and to easily upload sketches to the Arduino. BNO055 connects to Arduino.

Next, I followed the instructions inMonk (2016) to make the Arduino the Slave, and the Raspberry Pi the Master. I needed to write code for both the Arduino and the Raspberry Pi in order for them to communicate with each other via I2C (The code for Arduino and Raspberry Pi are in the Software section later in this report).

After writing the Arduino code for I2C communication and IMU data capture, I uploaded the code to the board. I then needed to enable I2C on the Raspberry Pi. I configured Raspberry Pi accordingly by going to Preferences under the main menu, and then clicking Raspberry Pi Configuration -> Interfaces -> Enable I2C.

I now installed the Python I2C library by using the command:

sudo apt-get install python-smbus

It was already installed. I then clicked:

sudo reboot

I had my Arduino Uno attached to the Raspberry Pi via I2C. I wanted to check that it’s attached and find its I2C address.

From a Terminal window on my Raspberry Pi, I typed the following commands to fetch and install the i2c-tools:

sudo apt-get install i2c-tools

It was already installed.

Next, I ran the following command:

$ sudo i2cdetect -y 1
imu_i2c_10

Next, I needed to write the code in Python that the Raspberry Pi can use to make requests for IMU data from the Arduino. That code, as I mentioned above, is in the Software section of this post. I will run this Python script in the terminal window and redirect the IMU data response from the Arduino slave to a text file.

The command to run the Python program is as follows:

sudo python ardu_pi_i2c_imu.py

A file named imu_data.txt will capture the Roll+Pitch+Yaw data. Here is how the data looks:

imu_data

Hardware

imu_i2c_hw 2
imu_i2c_hw 3

Software

Here is the Python script that logs the GPS latitude + longitude + altitude data into a text file on the Raspberry Pi. Don’t be scared at how long the code is. Just copy and paste it into your file:

from gps import *
import time
import threading

# Source: Donat, Wolfram. "Make a Raspberry Pi-controlled Robot :
# Building a Rover with Python, Linux, Motors, and Sensors.
# Sebastopol, CA: Maker Media, 2014. Print.
# Modified by Addison Sears-Collins
# Date April 17, 2019

# Open a new file to log the GPS data
f = open("gps_data.txt", "w")

gpsd = None

# Create a GPS Poller class. 
class GpsPoller(threading.Thread):
  def __init__(self):
    threading.Thread.__init__(self)
    global gpsd
    gpsd = gps(mode=WATCH_ENABLE)
    self.current_value = None
    self.running = True

  def run(self):
    global gpsd
    while gpsp.running:
      gpsd.next()

if __name__ == '__main__':
  gpsp = GpsPoller()
  try:
    gpsp.start()
    while True:
      f.write("Lat: " + str(gpsd.fix.latitude) # Log the latitude data
      + "\tLon: " + str(gpsd.fix.longitude) # Log the longitude data
      + "\tAlt: " + str(gpsd.fix.altitude / .3048) # Log the altitude in feet
      + "\n")
      time.sleep(5)
  except(KeyboardInterrupt, SystemExit):
    f.close()
    gpsp.running = False
    gpsp.join()

Here is the code for the IMU I2C program on the Arduino, responsible for capturing Roll+Pitch+Yaw data and sending to the Raspberry Pi:

#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BNO055.h>
#include <utility/imumaths.h>

/* This driver uses the Adafruit unified sensor library (Adafruit_Sensor),
   which provides a common 'type' for sensor data and some helper functions.

   To use this driver you will also need to download the Adafruit_Sensor
   library and include it in your libraries folder.

   You should also assign a unique ID to this sensor for use with
   the Adafruit Sensor API so that you can identify this particular
   sensor in any data logs, etc.  To assign a unique ID, simply
   provide an appropriate value in the constructor below (12345
   is used by default in this example).

   Connections
   ===========
   Connect SCL to analog 5
   Connect SDA to analog 4
   Connect VDD to 3-5V DC
   Connect GROUND to common ground

   History
   =======
   2015/MAR/03  - First release (KTOWN)
   2015/AUG/27  - Added calibration and system status helpers

   @Author Modified by Addison Sears-Collins
   @Date   April 17, 2019
*/

/* Set the delay between fresh samples */
#define BNO055_SAMPLERATE_DELAY_MS (100)

Adafruit_BNO055 bno = Adafruit_BNO055(55);

// Flag used to stop the program
bool done = false;

// Make the Arduino a slave to the Raspberry Pi
int SLAVE_ADDRESS = 0X04;

// Toggle in-built LED for verifying program is working
int ledPin = 13;

// Data to send back to Raspberry Pi
byte imu_data[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};

// Variables used for digit extraction
int roll = 0;
int pitch = 0;
int yaw = 0;

// Initialize the LED. This is used for testing.
boolean ledOn = false;

/**************************************************************************/
/*
    This function ends the program
*/
/**************************************************************************/

void end_program() {
    
    // Used for reading data from the serial monitor
    char ch;

    // Check to see if ! is available to be read
    if (Serial.available()) {

    // Read the character
    ch = Serial.read();

    // End the program if exclamation point is entered in the serial monitor
    if (ch == '!') {
      done = true;
      Serial.println("Finished recording Roll+Pitch+Yaw data. Goodbye.");
    }
  } 
}

/**************************************************************************/
/*
    Displays some basic information on this sensor from the unified
    sensor API sensor_t type (see Adafruit_Sensor for more information)
*/
/**************************************************************************/
void displaySensorDetails(void)
{
  sensor_t sensor;
  bno.getSensor(&sensor);
  Serial.println("------------------------------------");
  Serial.print  ("Sensor:       "); Serial.println(sensor.name);
  Serial.print  ("Driver Ver:   "); Serial.println(sensor.version);
  Serial.print  ("Unique ID:    "); Serial.println(sensor.sensor_id);
  Serial.print  ("Max Value:    "); Serial.print(sensor.max_value); Serial.println(" xxx");
  Serial.print  ("Min Value:    "); Serial.print(sensor.min_value); Serial.println(" xxx");
  Serial.print  ("Resolution:   "); Serial.print(sensor.resolution); Serial.println(" xxx");
  Serial.println("------------------------------------");
  Serial.println("");
  delay(500);
}

/**************************************************************************/
/*
    Display some basic info about the sensor status
*/
/**************************************************************************/
void displaySensorStatus(void)
{
  /* Get the system status values (mostly for debugging purposes) */
  uint8_t system_status, self_test_results, system_error;
  system_status = self_test_results = system_error = 0;
  bno.getSystemStatus(&system_status, &self_test_results, &system_error);

  /* Display the results in the Serial Monitor */
  Serial.println("");
  Serial.print("System Status: 0x");
  Serial.println(system_status, HEX);
  Serial.print("Self Test:     0x");
  Serial.println(self_test_results, HEX);
  Serial.print("System Error:  0x");
  Serial.println(system_error, HEX);
  Serial.println("");
  delay(500);
}

/**************************************************************************/
/*
    Display sensor calibration status
*/
/**************************************************************************/
void displayCalStatus(void)
{
  /* Get the four calibration values (0..3) */
  /* Any sensor data reporting 0 should be ignored, */
  /* 3 means 'fully calibrated" */
  uint8_t system, gyro, accel, mag;
  system = gyro = accel = mag = 0;
  bno.getCalibration(&system, &gyro, &accel, &mag);

  /* The data should be ignored until the system calibration is > 0 */
  Serial.print("\t");
  if (!system)
  {
    Serial.print("! ");
  }

  /* Display the individual values */
  Serial.print("Sys:");
  Serial.print(system, DEC);
  Serial.print(" G:");
  Serial.print(gyro, DEC);
  Serial.print(" A:");
  Serial.print(accel, DEC);
  Serial.print(" M:");
  Serial.print(mag, DEC);
}

/**************************************************************************/
/*
    Callback for received data
*/
/**************************************************************************/

void processMessage(int n) {

  char ch = Wire.read();
  if (ch == 'l') {
    toggleLED();
  }
}

/**************************************************************************/
/*
    Method to toggle the LED. This is used for testing.
*/
/**************************************************************************/

void toggleLED() {
  ledOn = ! ledOn;
  digitalWrite(ledPin, ledOn);
  
}

/**************************************************************************/
/*
    Code that executes when request is received from Raspberry Pi
*/
/**************************************************************************/

void sendIMUReading() {
  Wire.write(imu_data, 12); 
}

/**************************************************************************/
/*
    Retrieves the digit of any position in an integer. The rightmost digit
    has position 0. The second rightmost digit has position 1, etc.
    e.g. Position 3 of integer 245984 is 5.
*/
/**************************************************************************/

byte getDigit(int num, int n) {
  int int_digit, temp1, temp2;
  byte byte_digit;

  temp1 = pow(10, n+1);
  int_digit = num % temp1;

  if (n > 0) {
    temp2 = pow(10, n);
    int_digit = int_digit / temp2;
  }

  byte_digit = (byte) int_digit;

  return byte_digit;
}

    
/**************************************************************************/
/*
    Arduino setup function (automatically called at startup)
*/
/**************************************************************************/
void setup(void)
{
  Serial.begin(9600);
  Serial.println("Orientation Sensor Test"); Serial.println("");

  /* Initialise the sensor */
  if(!bno.begin())
  {
    /* There was a problem detecting the BNO055 ... check your connections */
    Serial.print("Ooops, no BNO055 detected ... Check your wiring or I2C ADDR!");
    while(1);
  }

  delay(1000);

  /* Display some basic information on this sensor */
  displaySensorDetails();

  /* Optional: Display current status */
  displaySensorStatus();

  bno.setExtCrystalUse(true);

  pinMode(ledPin, OUTPUT); // This is used for testing.

  Wire.begin(SLAVE_ADDRESS); // Set up the Wire library and make Arduino the slave
  
  /* Define the callbacks for i2c communication */
  Wire.onReceive(processMessage); // Used to specify a function when data received from Master
  Wire.onRequest(sendIMUReading); // Used to specify a function when the Master requests data
  
}

/**************************************************************************/
/*
    Arduino loop function, called once 'setup' is complete
*/
/**************************************************************************/
void loop(void)
{

  while (!done) {
    /* Get a new sensor event */
    sensors_event_t event;
    bno.getEvent(&event);

    /* Display the floating point data */
    Serial.print("Yaw: ");
    yaw = (int) event.orientation.x;
    Serial.print(yaw);
    if (yaw < 0) {
      imu_data[8] = 1;  // Capture the sign information
      yaw = abs(yaw);
    }
    else {
      imu_data[8] = 0;
    }
    if (yaw > 360) {
      yaw = yaw - 360; // Calculate equivalent angle
    } 
    
    Serial.print("\tPitch: ");
    pitch = (int) event.orientation.y;
    Serial.print(pitch);
    if (pitch < 0) {
      imu_data[4] = 1;   // Capture the sign information
      pitch = abs(pitch);
    }
    else {
      imu_data[4] = 0;
    }
    
    Serial.print("\tRoll: ");
    roll = (int) event.orientation.z; 
    Serial.print(roll);
    if (roll < 0) {
      imu_data[0] = 1;    // Capture the sign information
      roll = abs(roll);
    }    
    else {
      imu_data[0] = 0;
    }

    /* Optional: Display calibration status */
    displayCalStatus();

    /* Optional: Display sensor status (debug only) */
    //displaySensorStatus();

    /* New line for the next sample */
    Serial.println("");

    /* Update the IMU data by extracting each digit from the raw data */
    imu_data[1] = getDigit(roll, 2);
    imu_data[2] = getDigit(roll, 1);
    imu_data[3] = getDigit(roll, 0);
    imu_data[5] = getDigit(pitch, 2);
    imu_data[6] = getDigit(pitch, 1);
    imu_data[7] = getDigit(pitch, 0);
    imu_data[9] = getDigit(yaw, 2);
    imu_data[10] = getDigit(yaw, 1);
    imu_data[11] = getDigit(yaw, 0);

    /* Wait the specified delay before requesting nex data */
    delay(BNO055_SAMPLERATE_DELAY_MS);

    end_program();

  }
         
  // Do nothing
  while (true){};

}

Here is the code for the I2C Python program on the Raspberry Pi, responsible for sending messages and requesting IMU data via I2C from the Arduino slave:

import smbus
import time
# Created by Addison Sears-Collins
# April 17, 2019
# Open a new file to log the IMU data
f = open("imu_data.txt", "w")

# for RPI version 1, use bus = smbus.SMBus(0)
bus = smbus.SMBus(1)

# This is the address we setup in the Arduino Program
SLAVE_ADDRESS = 0x04

def request_reading():
  # Read a block of 12 bytes starting at SLAVE_ADDRESS, offset 0
  reading = bus.read_i2c_block_data(SLAVE_ADDRESS, 0, 12)

  # Extract the IMU reading data
  if reading[0] < 1:
    roll_sign = "+"
  else:
    roll_sign = "-"
  roll_1 = reading[1]
  roll_2 = reading[2]
  roll_3 = reading[3]

  if reading[4] < 1:
    pitch_sign = "+"
  else:
    pitch_sign = "-"    
  pitch_1 = reading[5]
  pitch_2 = reading[6]
  pitch_3 = reading[7]

  if reading[8] < 1:
    yaw_sign = "+"
  else:
    yaw_sign = "-" 
  yaw_1 = reading[9]
  yaw_2 = reading[10]
  yaw_3 = reading[11]

  # Print the IMU data to the console
  print("Roll: " + roll_sign + str(roll_1) + str(roll_2) + str(roll_3) +
        "   Pitch: " + pitch_sign + str(pitch_1) + str(pitch_2) + str(pitch_3) +
        "   Yaw: " + yaw_sign + str(yaw_1) + str(yaw_2) + str(yaw_3))

  try:
    f.write("Roll: " + roll_sign + str(roll_1) + str(roll_2) + str(roll_3) +
        "   Pitch: " + pitch_sign + str(pitch_1) + str(pitch_2) + str(pitch_3) +
        "   Yaw: " + yaw_sign + str(yaw_1) + str(yaw_2) + str(yaw_3) + "\n")
  except(KeyboardInterrupt, SystemExit):
    f.close()

# Request IMU data every 5 seconds from the Arduino
while True:
  # Used for testing: command = raw_input("Enter command: l - toggle LED, r - read IMU ")
    # if command == 'l' :
    #   bus.write_byte(SLAVE_ADDRESS, ord('l'))
    # elif command == 'r' :
  request_reading()
  time.sleep(5)

Video

How to Send Roll, Pitch, & Yaw Over Bluetooth | Raspberry Pi and Arduino

In this post, I’ll show you how to send roll, pitch, and yaw data over Bluetooth using Raspberry Pi and Arduino.

Requirements

Here are the requirements:

  • Using the IMU connected to the Arduino, capture roll, pitch, and yaw data.
  • Send the data via Serial over USB to the Raspberry Pi.
  • Send the data via Bluetooth from Raspberry Pi to my host computer (e.g. my personal laptop).
  • Display the data on my host computer.
  • To make things interesting, I mounted the IMU on a quadcopter.

Design

Hardware

The following components are used in this project. You will need:

Software

Here are the steps for the software of the system:

  • Set the delay between fresh samples.
  • Establish a function to end the program when a sentinel is entered (in this case “!”).
  • Define a method that displays some basic information on the sensor.
  • Define a method that displays some basic information about the sensor status.
  • Define a method that displays the sensor calibration status.
  • Setup Function:
    • Initialize serial communication at 9600 bits per second
    • Initialize the sensor.
    • Display some basic information on this sensor.
  • Captures Roll+Pitch+Yaw data from the Adafruit BNO055.
  • Print data to the Serial monitor.
  • Go to new line.
  • Wait a specified delay before requesting the next data.
  • End the program when the sentinel is entered.
  • Infinite loop.

Implementation

My Adafruit BNO055 Absolute Orientation Sensor was already setup from my Wifi roll, pitch, and yaw project. I made some modifications to the software though, which are presented in the Software section.

The BNO055 was wired to the Arduino Uno using the solderless breadboard as follows:

  • Connected Vin to the power supply of 5V
  • Connected GND to common power/data ground
  • Connected the SDA pin to the I2C data SDA pin on the Arduino (A4).
  • Connected the SCL pin to the I2C clock SCL pin on the Arduino (A5).

The challenge in this project is to set up Bluetooth on my Raspberry Pi and send the Roll+Pitch+Yaw data to my host computer.

First, I installed the packages that will enable me to use Bluetooth on my Raspberry Pi.

I opened the Terminal and typed sudo apt-get install bluetooth bluez blueman. I pressed Enter.

1_install_bt_graphical_settings_menu

Here is a description of the packages:

  • Blueman: A full featured Bluetooth manager. It provides a GUI-based setting panel Bluetooth manager.
  • Bluez: Provides the Bluetooth protocol stack and the bluetoothctl utility.
  • Bluetooth: Provides all the plugins supported by Bluez Bluetooth stack.

Once the packages were downloaded and installed, I typed:

sudo reboot

None of that worked, so I tried the steps here to configure the Bluetooth. Bluetooth was back!

I made sure that I turned Bluetooth to on inside my Windows settings.

bluetooth_on
bluetooth_on

I paired with the Raspberry Pi. On the actual Raspberry Pi, I made sure to click “Make Discoverable” under the Bluetooth Manager settings.

3_pairing_correctly_to_external_devicePNG

I went back to my Windows laptop and clicked “Bluetooth” under Add a device.

add_new_device

I added the Raspberry Pi.

make_discoverable
raspberry_pi_paired

The Raspberry Pi and my Windows computer are now paired via Bluetooth.

rpi_connected

To access the Bluetooth graphical settings menu, on the Raspberry Pi desktop, I clicked Menu, located in the upper left corner of the screen, scrolled down to Preferences with the cursor and clicked Bluetooth Manager. From there I clicked “Send Files to Device” to send a test text file via Bluetooth from the Raspberry Pi to the Windows computer.

from_rpi_to_host
from_rpi_to_host

I went to the Bluetooth & other devices settings on my Windows computer and clicked on “Send or receive files via Bluetooth.” I clicked on “Receive files,” which opened up a window. The Windows computer is now ready to receive files.

from_rpi_to_host

I then went back to the Raspberry Pi and selected the test text file and sent it to my Windows computer. I got a prompt on my Windows computer asking where it should be saved. Sending and receiving was successful!

file_sent_successfully

Hardware

I used the same hardware as the Wifi-based roll, pitch, and yaw project.

Software

Here is the code for the program I developed. (Note that after you load the sketch to Arduino, the code runs automatically whenever the Arduino is connected to power. That could be via a battery or power directly from the Raspberry Pi):

#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BNO055.h>
#include <utility/imumaths.h>

/* This driver uses the Adafruit unified sensor library (Adafruit_Sensor),
   which provides a common 'type' for sensor data and some helper functions.

   To use this driver you will also need to download the Adafruit_Sensor
   library and include it in your libraries folder.

   You should also assign a unique ID to this sensor for use with
   the Adafruit Sensor API so that you can identify this particular
   sensor in any data logs, etc.  To assign a unique ID, simply
   provide an appropriate value in the constructor below (12345
   is used by default in this example).

   Connections
   ===========
   Connect SCL to analog 5
   Connect SDA to analog 4
   Connect VDD to 3-5V DC
   Connect GROUND to common ground

   History
   =======
   2015/MAR/03  - First release (KTOWN)
   2015/AUG/27  - Added calibration and system status helpers
   
   Modified by Addison Sears-Collins on May 3, 2019
*/

/* Set the delay between fresh samples */
#define BNO055_SAMPLERATE_DELAY_MS (100)

Adafruit_BNO055 bno = Adafruit_BNO055(55);

// Flag used to stop the program
bool done = false;

/**
 * This function ends the program.
 */
void end_program() {
    
  // Used for reading data from the serial monitor
  char ch;

  // Check to see if ! is available to be read
  if (Serial.available()) {

    // Read the character
    ch = Serial.read();

    // End the program if exclamation point is entered in the serial monitor
    if (ch == '!') {
      done = true;
      Serial.println("Finished recording Roll+Pitch+Yaw data. Goodbye.");
    }
  } 
}

/**************************************************************************/
/*
    Displays some basic information on this sensor from the unified
    sensor API sensor_t type (see Adafruit_Sensor for more information)
*/
/**************************************************************************/
void displaySensorDetails(void)
{
  sensor_t sensor;
  bno.getSensor(&sensor);
  Serial.println("------------------------------------");
  Serial.print  ("Sensor:       "); Serial.println(sensor.name);
  Serial.print  ("Driver Ver:   "); Serial.println(sensor.version);
  Serial.print  ("Unique ID:    "); Serial.println(sensor.sensor_id);
  Serial.print  ("Max Value:    "); Serial.print(sensor.max_value); Serial.println(" xxx");
  Serial.print  ("Min Value:    "); Serial.print(sensor.min_value); Serial.println(" xxx");
  Serial.print  ("Resolution:   "); Serial.print(sensor.resolution); Serial.println(" xxx");
  Serial.println("------------------------------------");
  Serial.println("");
  delay(500);
}

/**************************************************************************/
/*
    Display some basic info about the sensor status
*/
/**************************************************************************/
void displaySensorStatus(void)
{
  /* Get the system status values (mostly for debugging purposes) */
  uint8_t system_status, self_test_results, system_error;
  system_status = self_test_results = system_error = 0;
  bno.getSystemStatus(&system_status, &self_test_results, &system_error);

  /* Display the results in the Serial Monitor */
  Serial.println("");
  Serial.print("System Status: 0x");
  Serial.println(system_status, HEX);
  Serial.print("Self Test:     0x");
  Serial.println(self_test_results, HEX);
  Serial.print("System Error:  0x");
  Serial.println(system_error, HEX);
  Serial.println("");
  delay(500);
}

/**************************************************************************/
/*
    Display sensor calibration status
*/
/**************************************************************************/
void displayCalStatus(void)
{
  /* Get the four calibration values (0..3) */
  /* Any sensor data reporting 0 should be ignored, */
  /* 3 means 'fully calibrated" */
  uint8_t system, gyro, accel, mag;
  system = gyro = accel = mag = 0;
  bno.getCalibration(&system, &gyro, &accel, &mag);

  /* The data should be ignored until the system calibration is > 0 */
  Serial.print("\t");
  if (!system)
  {
    Serial.print("! ");
  }

  /* Display the individual values */
  Serial.print("Sys:");
  Serial.print(system, DEC);
  Serial.print(" G:");
  Serial.print(gyro, DEC);
  Serial.print(" A:");
  Serial.print(accel, DEC);
  Serial.print(" M:");
  Serial.print(mag, DEC);
}

/**************************************************************************/
/*
    Arduino setup function (automatically called at startup)
*/
/**************************************************************************/
void setup(void)
{
  Serial.begin(9600);
  Serial.println("Orientation Sensor Test"); Serial.println("");

  /* Initialise the sensor */
  if(!bno.begin())
  {
    /* There was a problem detecting the BNO055 ... check your connections */
    Serial.print("Ooops, no BNO055 detected ... Check your wiring or I2C ADDR!");
    while(1);
  }

  delay(1000);

  /* Display some basic information on this sensor */
  displaySensorDetails();

  /* Optional: Display current status */
  displaySensorStatus();

  bno.setExtCrystalUse(true);
}

/**************************************************************************/
/*
    Arduino loop function, called once 'setup' is complete (your own code
    should go here)
*/
/**************************************************************************/
void loop(void)
{

  while (!done) {
    /* Get a new sensor event */
    sensors_event_t event;
    bno.getEvent(&event);

    /* Display the floating point data */
    Serial.print("Yaw: ");
    Serial.print(event.orientation.x, 4);
    Serial.print("\tPitch: ");
    Serial.print(event.orientation.y, 4);
    Serial.print("\tRoll: ");
    Serial.print(event.orientation.z, 4);

    /* Optional: Display calibration status */
    displayCalStatus();

    /* Optional: Display sensor status (debug only) */
    //displaySensorStatus();

    /* New line for the next sample */
    Serial.println("");

    /* Wait the specified delay before requesting next data */
    delay(BNO055_SAMPLERATE_DELAY_MS);

    end_program();

  }
         
  // Do nothing
  while (true){};
}

Video