ESP8266 and a BerryIMU – simple web server

The ESP8266 is another good microcontroller which can be used with the BerryIMU.

The ESP8266 is small,  requires minimal power and includes Wifi.

 

In this guide we will setup of the ESP8266 to provide a web page which we can then use to read the  accelerometer, gyroscope and compass values from the BerryIMU. We will also force this webpage to refresh every 1 seconds.

Accessing the ESP8266 from an iPhone

 

 

The ESP8266 Arduino core to program our ESP8266. This allows you to use the Arduino IDE to program and upload to the ESP8266.

We have used the Adafruit Feather Huzzah and the Sparkfun Thing Dev board in this guide. Both are excellent boards with included USB to serial converters. Just plug in and upload. This guide can also be used with other ESP8266 boards, just take note of the pins used.

Sparkfun Thing and BerryIMU
Sparkfun Thing and BerryIMU
Adafruit Feather Hazzuh! and BerryIMU
Adafruit Feather Hazzuh! and BerryIMU

Hook Up

The below diagrams show how to connect a BerryIMU to an ESP8266 microcontroller, in this case the  Adafruit Feather Huzzah and the Sparkfun Thing Dev board.

 

Adafruit Huzzah IMU
Adafruit Huzzah and BerryIMU
Sparkfun Thing IMU
Sparkfun Thing Dev and BerryIMU

 

Prepare Arduino IDE

The Arduino IDE needs to be updated with the board packages for the ESP8266.  This is very easy to do and both Sparkfun & Adafruit have detailed guides on how to do this;
Sparkfun Arduino IDE and ESP8266
Adafruit Arduino IDE and ESP8266

BerryIMU Raspberry Pi Gyroscope Accelerometer

The Code

The code can be found here . Download the entire Git repo. The code for this guide can be found under the directory
ESP8266-BerryIMU/BerryIMU_ESP8266_simple_web/
The file you load into the Arduino IDE is BerryIMU_ESP8266_simple_web.ino.

This guide will only cover the specific to the ESP8266. There is another guide here http://ozzmaker.com/berryimu/ which covers the code used to calculate the angles and heading from the BerryIMU.

The first thing to do is update the code with your wireless network settings.

const char* ssid = "******";
const char* password = "***************";

Further down you can see where we define the web server and what port to listen on

ESP8266WebServer server(80);

There is then a function called handleroot(). This is what builds the web page and sends it to the client when the client requests it E.g. When a web browser requests for a page.
Looking at the line which contains the meta tag, you can see where the refresh timer is set to 1 seconds.

I have also hilighted the variables which store the angles and heading from the BerryIMU.

void handleroot()
{
  //Create webpage with BerryIMU data which is updated every 1 seconds
  server.sendContent("HTTP/1.1 200 OK\r\n"); //send new p\r\nage
  server.sendContent("Content-Type: text/html\r\n");
  server.sendContent("\r\n");
  server.sendContent
  ("<html><head><meta http-equiv='refresh' content='1'</meta>"
  "<h3 style=text-align:center;font-size:200%;color:RED;>BerryIMU and ESP8266</h3>"
  "<h3 style=text-align:center;font-size:100%;>accelerometer, gyroscope, magnetometer</h3>"
  "<h3 style=text-align:center;font-family:courier new;><a href=http://ozzmaker.com/ target=_blank>http://ozzmaker.com</a></h3><hr>");
  server.sendContent
  ("<h2 style=text-align:center;> Filtered X angle= " + String(<strong><span style="color: #ff0000;">CFangleX</span></strong>));
  server.sendContent
  ("<h2 style=text-align:center;> Filtered Y angle= " + String(<span style="color: #ff0000;"><strong>CFangleY</strong></span>));
  server.sendContent
  ("</h2><h2 style=text-align:center;> Heading = " + String(<span style="color: #ff0000;"><strong>heading</strong></span>));
  server.sendContent
  ("</h2><h2 style=text-align:center;> Tilt compensated heading =  " + String(<strong><span style="color: #ff0000;">headingComp</span></strong>));
}

Within setup(), we define what pins are used for I2C to communicated with the BerryIMU.

Wire.begin(4,5);

The first value is the SDA pin and the second specifies the SCL pin. Any pin on the ESP8266 can be used for I2C.

Wireless is then enabled  and then we try and connect to the wireless network. The IP address is then printed to the serial console.

  WiFi.begin(ssid, password);

  // Wait for connection
  while (WiFi.status() != WL_CONNECTED)
  {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("Connected to ");
  Serial.println(ssid);

  //Print IP to console
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
  delay(3);

And finally, the web server is started and match on root of the web server and then run handleroot().

server.on("/", handleroot);

You need to add the below line in the main loop to handle web requests.

server.handleClient(); //Handler for client connections

Boot Raspberry Pi from USB – in Beta

The Raspberry Pi 3 can now be booted from a USB drive or from over the Network.  It is still in beta and somewhat complicated to setup.

Raspberry Pi Network Boot

 

However, once the Raspberry Foundation has ironed out some of the bugs and have made it easier to configure, I think these two features will be used more frequently, specifically booting from USB.

 

Raspberry Pi USB

 

 

Info here;
Raspberry Pi boot modes

And below;

Pi 3 booting part I: USB mass storage boot beta

 

Testing points on a Raspberry Pi

Below is a list of test points which can be found on Raspberry Pi 2, 3 and some are also on b+.

With the use of a multimeter, these test points can help with troubleshooting hardware issues.

I have yet to find any formal documentation about these test points. However, I do know they exist.

PP3 GND
PP4 GND
PP5 GND
PP6 GND
PP7 5V after polyfuse
PP8 3V3
PP9 1V8
PP10 Goes from 3V3 to 2V on brownout
PP11 DAC_2V5 (for composite video DAC)
PP12 AUD_2V5 (for PWM audio drivers)
PP13 Goes from 3V3 to 2V on ACT activity
PP14 SD_CLK
PP15 SD_CMD
PP16 SD_DAT0
PP17 SD_DAT1
PP18 SD_DAT2
PP19 SD_DAT13
PP20 H5V
PP21 RUN signal (reset)
PP22 Goes from 3V3 to 2V on activity of green (link) ethernet jack LED
PP23 Goes from 3V3 to 2V on activity of yellow (speed) ethernet jack LED
PP24 COMPVID
PP25 AUDIO_L
PP26 AUDI_R
PP27 VBUS (USB 5V power)
PP28 ETH_CLK (25.000 MHz)
PP29 VC_TMS
PP30 VC_TRST_N
PP31 VC_CLK
PP32 VC_TDI
PP33 VC_TDO
PP34 GND
PP35 GPIO6 of LAN9514
PP36 GPIO7 of LAN9514
PP37 CAM_GPIO0
PP38 CAM_GPIO1
PP39 SCL0
PP40 SDA0

 

Below is an example of how you can use PP9 to confirm that  the regulator is supplying 1.8v correctly.

Ground can be sourced from the SD card slot;Pi3TestPointsProbs-1000

And the exact location of PP9;

Raspberry Pi test point 9

 

Adafruit has some great information covering the Raspberry Pi power circuitry. link

 

 

How to Check the Software and Hardware Version of a Raspberry Pi

There are a number of commands which can be used to check the hardware and software versions on a Raspberry Pi.

Version of Debian;

cat /etc/debian_version can be used to see what version of Debian you are running.

pi@raspberrypi ~ $ cat /etc/debian_version
7.8

2015-05-05-raspbian-wheezy

pi@raspberrypi ~ $ cat /etc/debian_version
8.0

2016-02-03-raspbian-jessie

OS Release Notes;

cat /etc/os-release can be used to see OS release notes

pi@raspberrypi ~ $ cat /etc/os-release
PRETTY_NAME=”Raspbian GNU/Linux 7 (wheezy)”
NAME=”Raspbian GNU/Linux”
VERSION_ID=”7″
VERSION=”7 (wheezy)”
ID=raspbian
ID_LIKE=debian
ANSI_COLOR=”1;31″
HOME_URL=”http://www.raspbian.org/”
SUPPORT_URL=”http://www.raspbian.org/RaspbianForums”
BUG_REPORT_URL=”http://www.raspbian.org/RaspbianBugs”

2015-05-05-raspbian-wheezy

pi@raspberrypi ~ $ cat /etc/os-release
PRETTY_NAME=”Raspbian GNU/Linux 8 (jessie)”
NAME=”Raspbian GNU/Linux”
VERSION_ID=”8″
VERSION=”8 (jessie)”
ID=raspbian
ID_LIKE=debian
HOME_URL=”http://www.raspbian.org/”
SUPPORT_URL=”http://www.raspbian.org/RaspbianForums”
BUG_REPORT_URL=”http://www.raspbian.org/RaspbianBugs”

2016-02-03-raspbian-jessie

Kernel Version;

uname -a can be used to see what kernel version is running

pi@raspberrypi ~ $ uname -a
Linux raspberrypi 3.18.11-v7+ #781 SMP PREEMPT Tue Apr 21 18:07:59 BST 2015 armv7l GNU/Linux

2015-05-05-raspbian-wheezy

pi@raspberrypi ~ $ uname -a
Linux raspberrypi 4.1.19-v7+ #858 SMP Tue Mar 15 15:56:00 GMT 2016 armv7l GNU/Linux

2016-02-03-raspbian-jessie

 

BerryIMU Raspberry Pi Gyroscope Accelerometer

To check the hardware version;

cat /proc/cpuinfo can be used to see what hardware you are using. Take note of the revision number in the second last line and then refer to the table below. The output below is from a Pi 2

pi@raspberrypi ~ $ cat /proc/cpuinfo
processor : 0
model name : ARMv7 Processor rev 5 (v7l)
BogoMIPS : 38.40
Features : half thumb fastmult vfp edsp neon vfpv3 tls vfpv4 idiva idivt vfpd32 lpae evtstrm
CPU implementer : 0x41
CPU architecture: 7
CPU variant : 0x0
CPU part : 0xc07
CPU revision : 5processor : 1
model name : ARMv7 Processor rev 5 (v7l)
BogoMIPS : 38.40
Features : half thumb fastmult vfp edsp neon vfpv3 tls vfpv4 idiva idivt vfpd32 lpae evtstrm
CPU implementer : 0x41
CPU architecture: 7
CPU variant : 0x0
CPU part : 0xc07
CPU revision : 5processor : 2
model name : ARMv7 Processor rev 5 (v7l)
BogoMIPS : 38.40
Features : half thumb fastmult vfp edsp neon vfpv3 tls vfpv4 idiva idivt vfpd32 lpae evtstrm
CPU implementer : 0x41
CPU architecture: 7
CPU variant : 0x0
CPU part : 0xc07
CPU revision : 5processor : 3
model name : ARMv7 Processor rev 5 (v7l)
BogoMIPS : 38.40
Features : half thumb fastmult vfp edsp neon vfpv3 tls vfpv4 idiva idivt vfpd32 lpae evtstrm
CPU implementer : 0x41
CPU architecture: 7
CPU variant : 0x0
CPU part : 0xc07
CPU revision : 5Hardware : BCM2709
Revision : a21041
Serial : 00000000c15e9432
pi@raspberrypi ~ $

 

 

Model and Pi Revision 256MB Hardware Revision Code from cpuinfo
Model B Revision 1.0 256MB 0002
Model B Revision 1.0 + ECN0001 (no fuses, D14 removed) 256MB 0003
Model B Revision 2.0
Mounting holes
256MB 0004
0005
0006
Model A
Mounting holes
256MB 0007
0008
0009
Model B Revision 2.0
Mounting holes
512MB 000d
000e
000f
Model B+ 512MB 0010
Compute Module 512MB 0011
Model A+ 256MB 0012
Pi 2 Model B 1GB a01041 (Sony, UK)
a21041 (Embest, China)
PiZero 512MB 900092(no camera connector)
900093(camera connector)
Pi 3 Model B 1GB a02082 (Sony, UK)
a22082 (Embest, China)

Converting values from an Accelerometer to Gs

In this post I will show how to convert the raw values read from an accelerometer to ‘Gs’.

 

An accelerometer measures proper acceleration, which is the acceleration it experiences relative to freefall. This is most commonly called “G-Force” (G)

For example, an accelerometer at resting on a table would measure 1G ( 9.81 m/s2) straight upwards. By contrast, accelerometers in free fall and accelerating due to the gravity of Earth will measure zero.

The accelerometer used by the BerryIMU is a MEMS sensors(LSM9DS0), which outputs the raw readings as mg/LSB.
Most MEMS accelerometers use this output format.

mg = milli-G’s (just like milliliters)
1mG = 0.001 G’s of acceleration, so 1000mG = 1G.
LSB = Least Significant bit, which is the last bit on the right.

BerryIMU Raspberry Pi Gyroscope Accelerometer

The LSM9DS0 outputs a 16 bit value for the accelerometer readings.

If you look at the characteristics of the LSM9DS0 in the datasheet, you can see the sensitivity levels for the accelerometer highlighted in red below and the corresponding values for the LSB, which are highlighted in blue. You can download the datasheet here;

The raw values from the accelerometer are  multiplied by the sensitive level to get the value in G.

LSM9DS1 BerryIMU

Let’s use FS ±2 g as an example sensitivity level.  As the range is -2 to +2, this would be a total of 4g.  Or 4,000 Milli-Gs.
The output is 16 bits. 16 bits equals 65,535.   This means we can get 65,535 different readings for the range  between -2 and +2. (or -2,000 MilliGs and +2,000 MilliGs)

 4,000 MilliGs / 65,535 = 0.061

Each time the LSB changes by one, the value changes by 0.061, which is the value highlighted in blue in the table above.

For FS ±8 g, the range would be -8 to +8, which is a total of 16,000 MilliGs.
 16,000 MilliGs / 65,535 = 0.244

Example when using ±2g sensitivity
In the table below, every time the raw values increments by one, the final calculated value(which is MilliG) increments by 0.061

RAW  	BINARY	LSB value for +/-2G	Calc MilliG
16	10000		0.061		0.976
17	10001		0.061		1.037
18	10010		0.061		1.098

The above values of 16,17 and 18 above a very low and only used for illustration.
If your accelerometer is horizontal and resting and at rest when using a sensitive level of ±2g, the raw value for Z should hover  around 16,500.
16,500 X 0.061 = 1006 MilliGs or 1G

Example when using ±8g sensitivity
In the table below, every time the raw values increments by one, the final calculated value(which is MilliG) increments by 0.244

RAW  	BINARY	LSB value for +/-2G	Calc MilliG
16	10000		0.244		3.904
17	10001		0.244		4.148
18	10010		0.244		4.392

If you accelerometer is horizontal and at rest, when using a sensitive level of ±8g, the raw value for Z should hover  around 4,475.

4,475
 X 0.224 = 1002.4 MilliGs or 1G

 

The Code

The two above examples are easy to implement in python;
±8g Sensitivity

writeACC(CTRL_REG2_XM, 0b00010000) #+/- 8G full scale
print("G Value for Z axis %f G" % ((ACCz * 0.224)/1000))

The first line above is used to initialise the accelerometer with a sensitivity level of ±2g.
The second line prints the calculated value as G using the raw values from the accelerometer.

±2g Sensitivity

writeACC(CTRL_REG2_XM, 0b00000000) #+/- 2G full scale
print("G Value for Z axis %f G" % ((ACCz * 0.061)/1000))

The first line above is used to initialise the accelerometer with a sensitivity level of ±2g.
The second line prints the calculated value as G uses using raw values from the accelerometer.
Below is a snippet from the main program;

import smbus
import time
import math
from LSM9DS0 import *
import datetime
bus = smbus.SMBus(1)




def writeACC(register,value):
        bus.write_byte_data(ACC_ADDRESS , register, value)
        return -1




def readACCx():
        acc_l = bus.read_byte_data(ACC_ADDRESS, OUT_X_L_A)
        acc_h = bus.read_byte_data(ACC_ADDRESS, OUT_X_H_A)
	acc_combined = (acc_l | acc_h <<8)

	return acc_combined  if acc_combined < 32768 else acc_combined - 65536


def readACCy():
        acc_l = bus.read_byte_data(ACC_ADDRESS, OUT_Y_L_A)
        acc_h = bus.read_byte_data(ACC_ADDRESS, OUT_Y_H_A)
	acc_combined = (acc_l | acc_h <<8)

	return acc_combined  if acc_combined < 32768 else acc_combined - 65536


def readACCz():
        acc_l = bus.read_byte_data(ACC_ADDRESS, OUT_Z_L_A)
        acc_h = bus.read_byte_data(ACC_ADDRESS, OUT_Z_H_A)
	acc_combined = (acc_l | acc_h <<8)

	return acc_combined  if acc_combined < 32768 else acc_combined - 65536




	
#initialise the accelerometer
writeACC(CTRL_REG1_XM, 0b01100111) #z,y,x axis enabled, continuos update,  100Hz data rate
writeACC(CTRL_REG2_XM, 0b00010000) #+/- 8G full scale



while True:
	
	
	#Read the accelerometer,gyroscope and magnetometer values
	ACCx = readACCx()
	ACCy = readACCy()
	ACCz = readACCz()

	print("##### X = %f G  #####" % ((ACCx * 0.224)/1000)),
	print(" Y =   %fG  #####" % ((ACCy * 0.224)/1000)),
	print(" Z =  %fG  #####" % ((ACCz * 0.224)/1000))

	
	
	#slow program down a bit, makes the output more readable
	time.sleep(0.03)

The complete code is in our Git repository here
The code can be pulled down to your Raspberry Pi with;

pi@raspberrypi ~ $ git clone http://github.com/mwilliams03/BerryIMU.git

Raspberry Pi Digital Spirit Level

 

In this post we show how to create a Digital Spirit Level using a Raspberry Pi and python.

The code moves that bubbles on the display in relation to the angle read from the IMU.
Parts used in this project;

Any IMU or TFT can be used, however the code would need to be updated to accommodate the different devices. It is best to use a 480×320 TFT as the images are scaled to fit this resolution.

This guide assumes that some basic understanding of an IMU(Accelerometer and Gyroscope)  is already known. And you have one already working with your Raspberry Pi.

If you don’t, we do have some guides which covers this.

BerryIMU Raspberry Pi Gyroscope Accelerometer

 

We have used our existing python code to read the values from the IMU, however we have removed the code related to the magnetometer as it isn’t needed for this project.

Git repository here
The code can be pulled down to your Raspberry Pi with;

pi@raspberrypi ~ $ git clone http://github.com/mwilliams03/BerryIMU.git

 

Placement of IMU

The IMU can be attached anywhere, however it is best to place it in the same orientation as shown below. If you do change the orientation, you will need to update the code accordingly.

spiritLevel1-600

Continue reading Raspberry Pi Digital Spirit Level

BerryIMU Python Code Update – Kalman Filter and More

We have updated to the python code in our git repo.

It now includes;

  • The elusive Kalman filter.
  • Math needed when the IMU is upside down
  • Automatically calculate loop period.
  • A lot more comments.

What is a Kalman filter?  In a nutshell;
A Kalman filter is, it is an algorithm which uses a series of measurements observed over time, in this context an accelerometer and a gyroscope. These measurements will contain noise that will contribute to the error of the measurement. The Kalman filter will then try to estimate the state of the system, based on the current and previous states, that tend to be more precise that than the measurements alone.

A Kalman filter is more precise than a Complementary filter. This can be seen in the image below, which is the output of a complementary filter (CFangleX) and a Kalman filter (kalmanX) from the X axis plotted in a graph.

The red line (KalmanX) is better at filtering out noisep;

Python Kalman filter Raspberry Pi

 

The code can be found here in our Git repository here
And  can be pulled down to your Raspberry Pi with;

pi@raspberrypi ~ $ git clone https://github.com/mwilliams03/BerryIMU.git

BerryIMU Raspberry Pi Gyroscope Accelerometer

A summary of the code;


def kalmanFilterY ( accAngle, gyroRate, DT):
        y=0.0
        S=0.0

        global KFangleY
        global Q_angle
        global Q_gyro
        global y_bias
        global XP_00
        global XP_01
        global XP_10
        global XP_11
        global YP_00
        global YP_01
        global YP_10
        global YP_11

        KFangleY = KFangleY + DT * (gyroRate - y_bias)

        YP_00 = YP_00 + ( - DT * (YP_10 + YP_01) + Q_angle * DT )
        YP_01 = YP_01 + ( - DT * YP_11 )
        YP_10 = YP_10 + ( - DT * YP_11 )
        YP_11 = YP_11 + ( + Q_gyro * DT )

        y = accAngle - KFangleY
        S = YP_00 + R_angle
        K_0 = YP_00 / S
        K_1 = YP_10 / S

        KFangleY = KFangleY + ( K_0 * y )
        y_bias = y_bias + ( K_1 * y )

        YP_00 = YP_00 - ( K_0 * YP_00 )
        YP_01 = YP_01 - ( K_0 * YP_01 )
        YP_10 = YP_10 - ( K_1 * YP_00 )
        YP_11 = YP_11 - ( K_1 * YP_01 )

        return KFangleY

Blip, blop, bloop…