Saturday 23 November 2013

How to access the peripherals, putting it all together, current state of project

I'll start in reverse order, on how to access the peripheral devices:

To start with, the second I2C bus has to be activated as mentioned on the previous post using the following command:
hipi-i2c e 0 1
Following that, devices can be detected on both I2C buses:
 root@akka-pc:~# i2cdetect -y 0
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- 1e --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --                        
root@akka-pc:~# i2cdetect -y 1
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- 1e --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- 48 -- -- -- -- -- -- --
50: -- -- -- 53 -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --                       
 The aboves shows one HMC5883L-based, triple axis magnetometer at I2C#0 (connected to P5) with I2C address 1e.
Also, it shows three devices being connected on I2C#1
These have as follows:
device 1e: HMC5883L-based, triple axis magnetometer
device 48: 4 Channel ADC, Adafruit ADS1115
device 53: ADXL345-based triple axis accelerometer

The headings of the two compasses can be read using the following code:
$ cat two-compasses
#!/usr/bin/python3
from i2clibraries import i2c_hmc5883l
hmc5883a = i2c_hmc5883l.i2c_hmc5883l(1)
hmc5883a.setContinuousMode()
hmc5883a.setDeclination(0,0)
hmc5883b = i2c_hmc5883l.i2c_hmc5883l(0)
hmc5883b.setContinuousMode()
hmc5883b.setDeclination(0,0)
print (hmc5883a.__str__())
print (hmc5883b.__str__())
On the above code I have highlighted the way we choose which magnetometer (i.e. on which I2C bus) we work with.
Executing this code, we get these results:
$ ./two-compasses
Axis X: -122.36
Axis Y: -809.6
Axis Z: -160.08
Declination: 0 0'
Heading: 261 24'

Axis X: 411.24
Axis Y: -168.36
Axis Z: 215.28
Declination: 0 0'
Heading: 337 44'
Apart of the Compasses, we also have a 3D accelerometer, which can be read using something like:
# cat accelerometer
#!/usr/bin/python3
from i2clibraries import i2c_adxl345
from time import *
adxl345 = i2c_adxl345.i2c_adxl345(1)
while True:
        print(adxl345)
        sleep(1)
and running the above while moving the accelerometer, we get:
# ./accelerometer
X:    0.21875
Y:    -0.625
Z:    0.65625

X:    -0.34375
Y:    0.9375
Z:    0.28125

X:    -0.5625
Y:    -0.25
Z:    -0.78125

X:    -0.03125
Y:    0.6875
Z:    1.125
In order to access these peripherals, I use the Think Bowl I2C Libraries

The third peripheral I have connected is a Adafruit ADS1115, which can be managed using the appropriate Adafruit library (more about it in a later post).

So, the current status can be seen in the following photo:
The components shown have as follows:
  1. The Raspberry PI: This is a model B, but since on the final version Ethernet won't be needed, it might be replaced with a Model A depending the the memory resources needed for the version of software that will eventually be running. Model A is considered for power consumption reasons. More about that below, when discussing item (3)
  2. This is a 12V battery. Since the outcome of this project is intended to work on my boat, I am doing all development with that in mind. 
  3. This a switching voltage converter, that makes the 5V Raspberry needs out of the 12V the battery produces. Being a switching regulator, it has a very high efficiency, which is very important since electrical power on board is always scarce. I have been using a very low cost and light component that is usually being used in electric RC models and it is called "Battery Eliminator Circuit". On the final release of the ShipComputer, (the one I will be use on the boat) I will possibly make use of  2 BECs, one for creating 5V out of 12V and another one for creating 3.3V out of 12V. The Raspberry will be powered from both of these, and the 5V one will also be powering the USB hub that seems to be needed. The linear voltage regulator found on Raspberry, usually responsible for up to one third of the total power consumption, will be removed as it 'll be not longer needed. 
  4. This is a Current Sensor based on the ACS714 Hall effect-based chip that produces a analog voltage based on the current that passes through, and its direction. When there's no current, the sensor produces a voltage of Vcc/2. This output changes by 185 mV per Amp. It will be connected to an analog input of the attached ADC and it will measure consumption and charging.
  5. This is the 3-axis Accelerometer
  6. ADS1115 ADC, I2C-based ADC, by Adafruit.
  7. Magnetometer of bus I2C#1
  8. USB Hub (need more that the 2 USBs provided)
  9. USB GPS, connected to the Raspberry through the USB hub
  10. Wifi USB Dongle. The system is set up as an Access Point, in order to enable connection of the tablet, on which the Primary User Interface will be running.
  11. Bluetooth Dongle for connectivity to the Pebble Smart Watch, on which the secondary UI will be running
  12. A splitter PCB for connecting all three I2C peripherals on the same I2C bus
  13. A PCB that holds the pull-up resistors needed for I2C#0
  14. The second magnetometer on I2C#0
As of the status of the project, most of the hardware is there, along with the low level libraries needed for accessing the various devices.
Some porting has to be done from Python V2 to Python V3 but Ok, it is feasible, it will be done.
In all, I consider the hardware and low-level software to be in place.
What is left is all host and UI software, along with much needed solutions on some more core issues, for example, the lack of accuracy by the magnetometers! I have two devices that I work in parallel and they produce reading +/- 15 degrees from one another! I will investigate the possibility of providing some form of calibration for these, we 'll see.

More about the software:
Concerning the host software, well, I haven't touch it yet! I have done some tests on reading the sensors but that's it.
For version 1 of the host software, there are not that many things to do, there has to be a loop for reading the sensors,  calculating the computed fields, store results in a DB, push metrics to the UIs etc.
For the Primary UI, I am investigating the possibility of using javascript and HTML5. It's a bit tough because things like websockets are new to me but the result will be something portable and possibly extensible so it is worth the effort.
As for the Secondary UI, programming the Pebble using SDK 1.12 was not that bad, and there is a new V2 SDK that came out a few days ago, it might need some reading but looks OK too.

Having said that, I am now forced to put the project on hold for one or two months, which means that I will probably be back early next year.

Thanks for reading!

G.

UPDATE:
I have totally forgotten about the wind direction and speed sensor! I have access to a Nasa Masthead Wind Sensor which produces two analog values for wind direction and a pulse signal for the wind speed.
I will use two ADC inputs on the ADS1115 for the wind direction and a I/O pin in interrupt mode for counting the length of the wind speed pulses (as shown here).
I have sent a message to Nasa Marine Instruments asking for documentation for the sensor but haven't had a reply yet, so whoever has any info about it, please let me know!



 





Using the second I2C bus on Raspberry PI

Hello again!
Today I am really excited because I am a big step closer to finishing the hardware part of the project.
But since I use this blog as a form of documentation, on this post I will concentrate on how the second i2c port of the raspberry PI can be used.

Just for the record, I need a second I2C bus because I want to use two compasses for which I cannot change the I2C ID.

Version 1 of the Raspberry Pi Model B had I2C#0 coming out on the GPIO. The SoC has an additional I2C bus that on Raspberry Version 1 was very hard to bring out.

Version 2 of Raspberry Model B and all Model As have I2C#1 connected on GPIO, and the I2C#0 accessible through the connector P5:
(this is a Model A but is the same for Model B too)
The P5 connector pins are actually missing, so the easiest way of take advantage of the I2C#0 is by adding the a 2x4 block of connector pins:
Although now the connector for I2C is available, it is still unusable because there are a couple of pull-up resistors missing.
One can find what is missing by comparing the schematics for I2C#0 and I2C#1 which can be found here:
Connector GPIO P1 has these resistors:
 which are missing from connector GPIO P5:
So we add them, that is, add 1.8K pullup resistors to pins 3 & 4 of connector P5 (the other side of the resistors should be connected to 3.3V). The easiest way for me to do that was using an external miniature PCB, someone might want to do a permanent modification to the RaspberryPI.
Now the hardware bit is ready, but we still can't use the I2C#0.
This is because the I2C functionality is an alternative to the default mode of the P5 pins, so somehow the SoC has to be told to activate I2C on these pins.
There are quite a few ways to do that, for various reasons I decided to use the HiPi Library, as described here.  
After installing the HiPi package (using the manual process described here), the I2C#0 can be activated by issuing the command:
hipi-i2c e 0 1
At this point we are ready to start using the second I2C bus on the Raspberry PI!

REFERENCES:
Raspberry PI Version 1 Schematics
Raspberry PI Version 2 Schematics
HiPi Perl Modules for Raspberry PI