Friday, March 20, 2015

MPU6050 with Intel Galileo (Python interface)

The MPU6050 is an integrated 6-axis Motion-tracking device that combines a 3-axis gyroscope, 3-axis accelerometer, and Digital Motion Processor (Datasheet and Register Mapping).
It is accurate, as it contains 16-bits analog to digital conversion hardware for each channel. Therefore it captures x, y, and z channel at the same time. The sensor has an I2C-bus so we can communicate with this device over I2C.

MPU6050 with Galileo (Arduino Environment):


This is pretty simple, for this you need to boot small SPI flash image (default one). Several sources are already available in internet but if you couldn't find it please check this out.

MPU6050 with Galileo - Python interface (Linux Environment):


To do this, you need to boot bigger linux image.

Connecting the sensor: 




To do this, your image needs to have python-smbus module. python-smbus is a Python module allows SMBus access through the I2C interface on Linux hosts. The host kernel must have I2C support, I2C device interface support, and a bus adapter driver.

The official image doesnot contain python-smbus module.

Workaround for python-smbus on Intel Galileo:  

  • First, you need to use an image with dev-tools installed on it (I’m using an image provided by Intel). Download the image from here.
  • Manually download I2C library from here and extract the tar file.
  • Before compilation, you have to patch a single line.
  • Go to the extracted directory and edit smbusmodule.c file in py-smbus directory. 
  • cd py-smbus
    vi smbusmodule.c
  • Change the constant I2C_SLAVE to I2C_SLAVE_FORCE in line 159 and then compile the source code.
  • make EXTRA="py-smbus"
    make install     # This will create the i2c-tools
    #The tools like i2cdetect and i2cget should be work now
    #Then go into the py-symbus directory and call
    python setup.py install
  • Now you should be able to execute a python script like this.
  • from smbus import SMBus
    bus = SMBus(0)
    bus.write_byte_data(0x20, 0x29, 0x04)

Note: IO mapping in Gen 2 is different from Gen 1.

For Galileo Gen 1:

If you want to communicate with external I2C slaves (like MPU6050, TSL2561), then you need to change the value of GPIO 29 to 0 (default is 1).
echo -n "29" > /sys/class/gpio/export
cd /sys/class/gpio
cat /gpio29/value # check the value of GPIO 29, it is 1 change it to 0
echo -n "out" > /sys/class/gpio/gpio29/direction
echo -n "0" > /sys/class/gpio/gpio29/value # After this, A4 and A5 lines act like
#SDA and SCL lines respectively 

For Galileo Gen 2:
echo -n "60" > /sys/class/gpio/export
cd /sys/class/gpio
cat /gpio60/value # check the value of GPIO 60, it is 1 change it to 0
echo -n "out" > /sys/class/gpio/gpio60/direction
echo -n "0" > /sys/class/gpio/gpio60/value # After this, A4 and A5 lines act like
#SDA and SCL lines respectively 

For IO Mapping, please check the below links.

Python program for MPU6050
#!/usr/bin/python

import smbus
import sys

# This is the address value read via the i2cdetect command
# if AD0 pin is connected to ground, address will be 0x68
# if AD0 pin is connected to vcc, address will be 0x69
 
ADDRESS_LOW      = 0x68
ADDRESS_HIGH     = 0x69

# Power management registers
power_mgmt_1  = 0x6B
power_mgmt_2  = 0x6C
SMPLRT_DIV       = 0x19
#Registers from which read accel, temp and gyro sensor values
ACCEL_XOUT_H  = 0X3B
ACCEL_YOUT_H  = 0X3D
ACCEL_ZOUT_H  = 0X3F

TEMP_OUT_H  = 0X41

GYRO_XOUT_H   = 0X43
GYRO_YOUT_H  = 0X45
GYRO_ZOUT_H   = 0X47

address = ADDRESS_HIGH

def write_byte(radd, val):
    bus.write_byte_data(address, radd, val)

def read_byte(radd):
    return bus.read_byte_data(address, radd)

def read_word(radd):
    high = bus.read_byte_data(address, radd)
    low = bus.read_byte_data(address, radd+1)
    val = (high << 8) + low
    return val

def read_word_2c(radd):
    val = read_word(radd)
    if (val >= 0x8000):
        return -((65535 - val) + 1)
    else:
        return val

try:
    bus = smbus.SMBus(0)
    # Now wake the 6050 up as it starts in sleep mode
    write_byte(power_mgmt_1, 0x01)

    while(True):
        try:

            accel_xout = read_word_2c(ACCEL_XOUT_H)
            accel_yout = read_word_2c(ACCEL_YOUT_H)
            accel_zout = read_word_2c(ACCEL_ZOUT_H)

            gyro_xout = read_word_2c(GYRO_XOUT_H)
            gyro_yout = read_word_2c(GYRO_YOUT_H)
            gyro_zout = read_word_2c(GYRO_ZOUT_H)

            temp_val = read_word_2c(TEMP_OUT_H)
   
            print "######################################"
            print "accelerometer data"
            print "------------------"  

            print "accel_xout: ", accel_xout
            print "accel_yout: ", accel_yout
            print "accel_zout: ", accel_zout

            print "gyro data"
            print "---------"

            print "gyro_xout: ", gyro_xout
            print "gyro_yout: ", gyro_yout
            print "gyro_zout: ", gyro_zout

            temp_dT = (temp_val / 340)+36.53
            print "temp val: ", temp_dT

        except KeyboardInterrupt:
            write_byte(power_mgmt_1, 0x41) # Sleep mode
            print "Keyboard exception arises.
            print "Program interrupted."
            print "Program is going to terminate."
            break

except IOError:
    print "Program IO Error."

except:
    print "Program General Exception." 
 
sys.exit()  

Reference:

Friday, March 6, 2015

Galileo with bigger linux image

Galileo is capable of running two kinds of images (operating system).

  • Small Linux for SPI Flash
  • Full-featured Linux for SD card

There is a fundamental difference. Galileo has an on-board flash memory of size 8Mb, limited amount of space to store its Linux kernel (as well as sketch). The small linux image only allows execution of Arduino sketches. It should be noted that the current firmware will not store sketch in the flash, so it will be lost on power cycle.

As such, the default Linux image is limited in terms of extra features. Fortunately Intel supplies Board Support package, which allows to include everything needed to rebuild the Linux image using Yocto.

But with an SD card, we can boot the Galileo with a bigger Linux image, which provides access to the following: 

  • WiFi drivers – The Galileo supports Intel-chipset WiFi cards with these drivers. Plug them in, and they should be recognized immediately.
  • Python – Python is our favorite when it comes to scripting languages. With Python you can easily post to Twitter, check for unread emails, and perform all sorts of other activities.
  • Node.js – Node.js is a popular JavaScript-based scripting language. A good alternative to Python, if you’re more comfortable with JS.
  • SSH – Secure Shell (SSH) is a very useful network tool that allows you to log into and control a device remotely. With SSH running on your network-connected Galileo, and remotely control the board from any device on the network.
  • openCV – OpenCV is an open-source computer vision application. You can plug a USB webcam into the Galileo’s USB host port, and use OpenCV to track and recognize objects.
  • V4L2Video4Linux2 is a video record and play utility for Linux. You’ll need a USB webcam attached to your Galileo to make use of this.

To boot the Galileo off the bigger Linux image, you’ll need an SD card that is at least 1GB (but less than 32GB). You’ll also need to download the bigger image from Intel’s Drivers page. The file is about 48 MB.

Download the linux image, extract it and copy the contents to your SD card.

Plug in the µSD card and then power it up. The first boot may take a little longer than average.

 

Connecting to Galileo via linux console



  • I would prefer to use ssh to login into Galileo. For that you need to connect your galileo as well as your host machine to the same network.
    • Get the ip address of Galileo using nmap/arp-scan tools in linux.
    • Once you get the ip address you can do ssh.
    • ssh <username>@<ip-address-of-galileo>
      
  • If you have serial console cable, you will be able to see everything including boot sequence.
  • There are many ways. Check this out.


Note:

  • The console UART port is different in Gen1 and Gen2. Please check this link.


Useful links: 

Galileo intial setup to run Arduino sketches

Powering the Galileo


Gen 1: 


  • The Galileo Gen1 should be powered through the barrel jack, using a regulated 5V DC power supply. 
  • After plug-in, just wait for 2 green LEDs to light up (ON and USB).
  • Connect the micro-USB cable to the Intel® Galileo.
  • Connect the other end to your computer.

Gen 2: 


  • The Galileo Gen2 should be powered through the barrel jack, using a 7-15V DC power supply.
  • After plug-in, just wait for 2 green LEDs to light up (ON and USB).
  • Connect the micro-USB cable to the Intel® Galileo.
  • Connect the other end to your computer.



Run the sketch (for linux)


IDE download and setup

  • Download the ide from here and extract the tar.gz file.
  • Go to extracted directory and run the arduino file
  • Open a terminal and run
  • ./arduino
    
  •  If everything is successful, Arduino IDE will be launched (you might need to install java as well to your host machine).


  • Power up the galileo, attach it to the host machine using micro-USB cable.
  • Open a terminal and type
  • ls /dev/ttyACM*
    
  • In IDE, click on Tools menu, select the board (Gen1, Gen2, Edison..) and serial port like /dev/ttyACM0 or /dev/ttyACM1.


  • If the serial port is disabled, you need to give some permission. So type
  • sudo chmod a+rw /dev/ttyACM*
    

Updating the firmware

Before trying to upload any sketch, first you must update the firmware.
  • To update the board firmware go to Help > Firmware Update. Then click Yes to proceed.
  • Make sure you don’t unplug either power or USB from the Galileo. The update procedure will take about five minutes.




As always, the first program to be uploaded to a board is the “Hello, world” of microcontrollers - Blink.
  • To open the Blink example, go to the File > Examples > 01.Basics > Blink.


  • Make sure the Serial Port and Board selections are still correct. Then click the Upload button.
After the upload completes, you should see a tiny, green LED blinking on and off every second. This LED is connected to pin 13 of the Galileo.


Intel Galileo Getting started


Intel Galileo is the first Arduino-certified development board based on Intel x86 architecture designed to be hardware and software pin-compatible with Arduino shields designed for the Arduino Uno R3. Digital pins, Analog inputs, the power header, ICSP header, and the UART port pins are all in the same locations as on the Arduino Uno R3. Galileo is a microcontroller board based on the Intel® Quark SoC X1000 Application Processor, a 32-bit Intel Pentium-class system on a chip (datasheet). Galileo has been designed for the makers, students, educators and DIY electronics enthusiasts.


Galileo Gen1
Galileo Gen2

Intel Galileo features the Intel Quark SoC X1000, 32-bit processor can run at up to 400MHz, and it has 512 KB SRAM built-in. The Galileo board supports the Quark with a wide range of external peripherals.

Galileo has 8MB Flash (to store firmware as well as sketch), an 11KB EEPROM (non-volatile memory), and a µSD slot (which supports up to 32GB). In addition to the memory, contains all sorts of peripherals: 10/100Mb Ethernet, USB 2.0 host and client ports, an RS-232 port, and a mini PCI Express (mPCIE) slot.

The Arduino pins – including six analog inputs, SPI, I2C, UART, and PWM outputs – are all exactly placed where an experienced Arduino user would expect them to be.

Galileo is designed to support shields that operate at either 3.3V or 5V. The core operating voltage of Galileo is 3.3V. However, a jumper on the board enables voltage translation to 5V at the I/O pins. This provides support for 5V Uno shields and is the default behavior. By switching the jumper position, the voltage translation can be disabled to provide 3.3V operation at the I/O pins.

Intel has developed an IDE which is exactly similar to Arduino Software Development Environment (IDE), write the sketch in your host machine, compile and transfer the binary to galileo over usb client port.



Useful links:



Fun with RGB LED

People often need home lighting control in their lives. These individuals can easily find relevant products which often use different meth...