Pages

Friday, December 4, 2015

Adafruit 10-DOF IMU: Getting It to work on Raspberry Pi... With Python!!!

I recently purchased the Adafruit 10-DOF IMU with much anticipation.










http://www.adafruit.com/product/1604

This puppy combines 3 sensors into one package giving you 11 axes of data in one package! The various readings can be combined to create a complete Altitude and Heading Reference System (AHRS).  Barometric Pressure and Temperature readings can be combined with standard physics equations to calculate its height above sea level.

Getting it Connected Up and Running


After some initial fumbling around I finally got this thing to work. So let's begin by connecting it up. This step is pretty straight-forward if you follow the instructions linked below.
SaveFromTheNet
https://learn.adafruit.com/adafruit-10-dof-imu-breakout-lsm303-l3gd20-bmp180/connecting-it-up













Next, I had to enable I2C on my Raspberry Pi 2. To do this I simply followed the instructions below. Note that on the RPI2 I had to use the >i2cdetect -y 1 command instead of >i2cdetect -y 0 to detect if i2c was working. I guess bus 1 is enabled by default on the RPI2 instead of bus 0.

http://www.instructables.com/id/Raspberry-Pi-I2C-Python/step2/Enable-I2C/


Next, install the RTIMULib2 from the link below.

https://github.com/richards-tech/RTIMULib2

To install the package, do the following:
1) download the package
2) unzip
3) cd to ~/TRIMULib2-master/Linux/python
4) run >sudo python setup.py build
5) run >sudo python setup.py install

That's it!

Now let's try it out.
> cd ~/RTIMULib2-master/Linux/python/tests
> sudo python Fusion.py

Your terminal should start rolling off number like the screen-shot below:













And that's it!  Take a look at the code for Fusion.py, Fusion10.py, and Fusion11.py to understand how to call the functions to access the IMU.

Thursday, December 19, 2013

Passing Data From Matlab to Python and Back

The following python and Matlab scripts will:
    1) Pass two variables form Matlab to python,
    2) process the variables in python, and
    3) return the result back to Matlab

The idea is to use the Matlab system() function to call a pre-defined python script (multiplySctipt.py) and then use the python sys.stdout.write() method to send the result back to Matlab. The result is returned to Matlab as text.

Note that this method is fine for small amounts of data, however, passing large amounts of data can take a long time.

---------------------------------------------------------------------
# Simple Python Command Line Script to take 2 numbers as inputs and
# output the product of the two numbers.  I saved this file as
# '/MyDocuments/python/multiplyScript.py'.

import sys, getopt

def main(argv):
    try:
        myopts, args = getopt.getopt(sys.argv[1:],'x:y:')
    except getopt.getoptError:
        print(str(getopt.getoptError))
        print("Usage: %s -x <1st number> -y <2nd number>" % sys.argv[0])
        print("Example: %s -x 3.14 -y 2.0" % sys.argv[0])
        sys.exit(2)

    #######################
    # o == option
    # a == argument
    for o,a in myopts:
        if o == '-x':
            x = float(a)
        if o == '-y':
            y = float(a)
    sys.stdout.write(str(x*y))

if __name__ == "__main__":

    main(sys.argv[1:])
---------------------------------------------------------------------

---------------------------------------------------------------------
% Simple Matlab script to run python script and get output
pythonScript = ['/MyDocuments/python/multiplyScript.py'];
x = num2str(3.14);
y = num2str(2.0);
[status,cmdout] = system(['python ',pythonScript,' -x ',x,' -y ',y]);
cmdout = str2num(cmdout);
---------------------------------------------------------------------

Wednesday, December 4, 2013

How to Programmatically Format Command Window Text

References: http://undocumentedmatlab.com/blog/changing-matlab-command-window-colors-part2/
                    http://undocumentedmatlab.com/blog/bold-color-text-in-the-command-window/
                    http://undocumentedmatlab.com/blog/another-command-window-text-color-hack/


stringVar = 3.142; % some variable to use in the fprintf string function

% Red Text
fprintf(2, 'this is some text %1.3f', stringVar); % the 2 indicates an error message

% Blue Underlined Text (i.e. hyperlink format):
fprintf('this is some <a href="">blue underlined text %1.3f</a>', stringVar);

% Bold Text
fprintf('this is some <strong>bold text %1.3f</strong>', stringVar);

% Orange Text
fprintf('this is some [\borange text %1.3f]\b', stringVar);

How to Programatically Change the Background and Foreground Color of the Matlab Command Window

Reference: http://undocumentedmatlab.com/blog/changing-matlab-command-window-colors/

Code:

import com.mathworks.mde.cmdwin.CmdWinDocument.getInstance;
cmdWinDoc = com.mathworks.mde.cmdwin.CmdWinDocument.getInstance;
listeners = cmdWinDoc.getDocmentListeners;
for ii = 1:length(listeners)
    if ~isempty(regexp(char(listeners(ii).toString()),'JTextArea'))
        jTextArea = listeners(ii);
        break;
    end
end
jTextArea.setBackground(java.awt.Color.yellow);
jTextArea.setBackground(java.awt.Color(1,1,0));
set(jTextArea,'Background',[1,1,0]);
set(jTextArea,'Foreground',[0,1,1]);