Translate

Wednesday, November 4, 2015

Embedded RPOf

The next thing to do about the RPOf is to embed it in a piggy back board over the Raspberry Pi.
The Raspberry Pi GPIO connector has both the 3.3V and 5V rail available, so to have a more compact system, embedding the RPOf on the "shield" connected to the Raspberry Pi, can helps to reduce the space and number of boards.

Friday, October 30, 2015

Working on the iRis project

[To be updated !]

A step-by-step guide to work on git for the iRis project.
If you are new on git and github, read this article first.

The iRis project is on github, so if somebody wants to join it, or just follow the progress, can clone the git repo for iRis.

Working on RPOf project

Here a step-by-step guide to work on git for the RPOf project.
If you are new on git and github, read this article first.

Working on my projects

Here a step-by-step guide to work on git for some projects I maintain.

This guide assumes:

  • you like Linux, you USE Linux
  • you know how to work with the terminal
  • the instructions are based on Ubuntu, but they should be easily used on other systems.
    The main difference could be the application to install code. apt-get for Ubuntu, yum for Red Hat/CentOS, ecc.
  • you know how to set up a cross compiler for different microcontroller under Linux

Introduction


The most important thing to remember working with git, is that normally ALL the operations are LOCAL to your machine !
Only few specific and explicitly called operations operate on a remote server, in our case, github.

Specifically :

  • The first time, when we clone the project from github
  • Every time we want to update our code from github
  • Every time we want to save our work on github

First time

git Installation


The first time we want to clone the project from github.
How we do that ?
First of all we must have git on our machine.
So if you don't already installed it, open a terminal and type :

  • sudo apt-get update
  • sudo apt-get install git


(note, I'm using for this article, the git version 1.9.1 on a Ubuntu 14.04 LTS)

After the installation, is better to set it up with some basic info :

  • git config --global user.name "your name"
  • git config --global user.email "youremail@domain.com"
  • git config --list will show the configuration for your git
For example :

git config --global user.name "John Smith"
git config --global user.email "jsmith@mystery.com"

I'm not sure if is valid for every type of operation, but it is possible github will require a SSH key in order to clone/update a project.
In that case I strongly recommend to follow this brief tutorial about how to generate and deploy an ssh key with github.

At this point you are ready to clone the project from github !

Clone a project

Cloning is the act that duplicate on your hard drive what is present in a remote git repository, in this case github.
The cloning is strongly suggested in case you want to collaborate to the project.
Cloning the project recreate on your local machine the project and it's history and allows to merge it back on github if you do modifications.

First of all create or go on a directory that will host the project.
For example, create a directory called SteveProjects on your home environment :
  • cd ~
  • mkdir SteveProjects
  • cd SteveProjects
Now is possible to use git to clone a project, with the command :

  • git clone URL_repository
That's it !
On your directory Projects, will exist a directory called as the project name, containing the project.

Download a project


In case you are not interested to collaborate to the project but wants to have the project on your machine, or in case you don't want to install git or generate SSH keys, from github is possible to download the project as a zip file.

Update project

Now that you have the project on your machine, to keep it updated is even easier.
Simply go in the project directory and issue the command : git pull

  • cd ~/Projects/project_name
  • git pull
That's it !  If the code on the github is changed, it will be updated on your machine

Working on the project

Commit the project


If you want to collaborate to a project, you can modify the code on your machine, and keep track of the modifications locally, using git.
Issuing the command git status returns the status of the modifications made to the code.

Every time you modify a file, git status will indicate a modification.
Every time you will have to add it with:

git add file_name

git add * will add all the modified files.

Then it will be possible to commit the file with:

git commit file_name

All these operations are LOCAL to your machine !

If you are a registered collaborator to the project (ask me), then you should be able to "push" your modifications in the github with:

git push

Working protocol


In order to reduce the conflict between developer, I suggest this protocol:


  1. Clone the project
    git clone URL_of_github_project
  2. Create a branch
    git checkout -b branch_name
  3. Work on your modifications, add and commit regularly
  4. When is time to push the modifications:
    git push origin branch_name
  5. If allowed, merge the branch on the master.



Tuesday, October 13, 2015

iRis project - an introduction

The purpose of the iRis project is to have a "head" for medical training purpose related to eyes illness.
Basically the idea is to build a "eyes simulator" capable to react to the light like an eye does, in order to simulate  different types of illness retina/nerve related.

Friday, August 14, 2015

Cooling the server area

Problem, cool down the server area.

The server is placed under a stair and the server area is insulated.
The area is in contact with the garage and the laundry room and of course the garage of course is not an insulated area, but the laundry it is.
Thus the idea : drill a hole in the stairs and connect a fan to aspire the fresh air from the laundry room.

Tuesday, August 11, 2015

MSP430 - Energia and Grove


I do have few Arduino around.
Like many people I bought some of them because ... well, I don't know, I guess at the time it was the news, to see what it is.
And I didn't like it much.


Saturday, August 8, 2015

MSP430 - I2C library

There are many different library out there to handle I2C with the MSP430.
Some of the libraries are also "official", from TI.

I found a nice and simple C library to handle the I2C with USI is on github and apparently is easy to deploy and use.
This article contains some notes about how to deploy and use the library, closing some missing practical information.

Let's start with ... the hardware.

Hardware


The library was originally developed and tested on an MSP430g2452.
It surely can work on any MSP430 family member that has a USI, that's was anyway my initial intent.
I tried to use an MSP430f2013 but in the end I opted to buy some MSP430g2452 because the 2013 doesn't have enough memory (only 2K ROM) to handle protocol and the rest of the code needed for a project.
The MSP430g2452 has 8K ROM.

After choosing the microcontroller, the attention was concentrated on the I/O pin to use.
Note ! Initially I'm experiment using a Launchpad.
The library expects to use the USI pins, i.e. the P1.6 for the SCL and the P1.7 for the SDA.
Apparently there is NO need to bother with the I/O ports to inform the MSP430 that the 2 pins are dedicated to the USI in I2C mode.
Is enough to program the USI control register to do that, and that is taken care in the i2c_init function provided by the library.

Important !!! The P1.6 on the Launchpad is connected to the green LED !
Remove the jumper on that LED in order to have something working !

Software

Compile it


The library presumably is written for IAR or other commercial system.
In order to compile it with Mspgcc few modifications are necessary.


  1. Remove the specific include of the msp430g2452 header from usi_i2c.c
  2. Add the generic include of msp430 in the usi_i2c.h
  3. Remove the include of the stdint.h header from usi_i2c.c and move in the usi_i2c.h
  4. Change the interrupt declaration, from:
    #pragma vector = USI_VECTOR
    __interrupt void USI_TXRX(void)
    {
      switch(__even_in_range(i2c_state,12))

    to

    __attribute__((__interrupt__(USI_VECTOR)))
    void Usi_txrx (void){  switch(__even_in_range(i2c_state,12))
  5. Move the inline function i2c_done() from the header file (usi_i2c.h) into the module (usi_i2c.c) and leave a function prototype for that function in the usi_i2c.h header file
With these modifications I was able to compile the library using mspgcc.

I also fixed a bug that was preventing the library to read more than 8 bit at a time.
In the usi_i2c.c file, in the interrupt function Usi_txrx change the highlighted line :

..................................................................
..................................................................
case I2C_RECEIVED_DATA:       // received data, send ACK/NACK
    *i2c_receive_buffer = USISRL;
    i2c_receive_buffer++;
    USICTL0 |= USIOE;           // SDA = output
    if(i2c_sequence_length > 1) {    
      // If this is not the last byte
      USISRL = 0x00;                // ACK 
..................................................................
..................................................................

with

..................................................................
..................................................................
case I2C_RECEIVED_DATA:       // received data, send ACK/NACK
    *i2c_receive_buffer = USISRL;
    i2c_receive_buffer++;
    USICTL0 |= USIOE;           // SDA = output
    if(i2c_sequence_length > 0) {   // TheFwGuy MOD !!!    
      // If this is not the last byte
      USISRL = 0x00;                // ACK 
..................................................................
..................................................................


Use it


The library has only three "public" functions and act as Master.
Let see how to use it.

i2c_init


The first public function is called i2c_init and like the name imply, is used to initialize the USI and set up the MSP430 to use it.
It must be called once, preferably after other basic I/O initializations happens.
After that, the "system" should be able to send out I2C messages.

Example:

        i2c_init(USIDIV_5, USISSEL_2);

The first parameter determine the division applied to the input clock.
USIDEV_0 divide by 1, USIDEV_1 divide by 2, USIDEV_2 divide by 4 and so on (see MSP430 Family user guide - USICKCTL register).
The second parameter determine the source of the clock (see MSP430 Family user guide - USICKCTL register).
In the example above the source for the clock is the SMCLK divided by 32.

i2c_done


This function returns TRUE (1) or FALSE (0) to indicate if a transaction is running.
The I2C messages are sent via interrupt so it is possible that the main program is ready to send another sequence when the system is still handling the previous sequence.
So before to perform any request, better to see if the system is ready to accept it.
Example:

      if(i2c_done())
         i2c_send_sequence((uint16_t *)seq1, 2, (uint8_t *)recseq, 0);

or
      while(!i2c_done());

Strongly suggested to use it to prevent to start a sequence when another one is still running.

i2c_send_sequence

This is the main function and the most complex to handle.
Simply put, this function can send and receive information via I2C using a "sequence".
The sequence  is a series of word and commands.
i2c_usi.h  defines two commands :
  • I2C_RESTART
  • I2C_READ
The function has these inputs:

  • sequence
    sequence is a pointer to an array containing the I2C operation sequence that should be performed.
    It can include any number of writes, restarts and reads.
    The sequence is composed of uint16_t, not uint8_t elements.
    This is because the need  to support out-of-band signalling of I2C_RESTART and I2C_READ operations, while still passing through 8-bit data.
    The sequence uses the Bus Pirate I2C convention.
    The address must be prepared manually, adding the R/W bit.
    The address is prepared shifting the 7-bit I2C address to the left and add the R/W bit (0 to write, 1 to read).
    The examples above communicate with a device whose I2C address is 0x1c, which shifted left gives 0x38.
    For reads we use 0x39, which is (0x1c<<1)|1.

    So for example, to write at the address 0x39, the sequence would be:

    • 0x39 << 1 = 0x72
    And to read from the address 0x39:
    • (0x39 << 1) | 1 = 0x73
  • sequence length
    sequence_length is the number of sequence elements (not bytes).
    Sequences of arbitrary (well, 32-bit) length are supported.
  • received data
    received_data should point to a buffer that can hold as many bytes as there are I2C_READ operations in the sequence.
    If there are no reads, 0 can be passed, as this parameter will not be used.
  • wake up sr bits
    Used if you want to use the MSP430 in low power mode.

Building sequences


The sequences to send depends strictly to a specific component so there is no a generic suggestion if not to carefully read the datasheet of the component.


Thursday, July 23, 2015

My quick'ndirty installation of ZeroMq


ZeroMQ supports different languages and thus allow an easy interprocess data exchange between different programs.
For example a C program can easily exchange data with a Python or Java program.

Install ZeroMQ

To install ZeroMQ, is necessary to compile the source code and generate the library.

To do so, under ubuntu :
  1. sudo apt-get install build-essential
  2. sudo apt-get install libtool
  3. sudo apt-get install automake
  4. sudo apt-get install autoconf
  5. sudo apt-get install uuid-dev
  6. Download the tarball for the libsodium from the Libsodium website
  7. Extract the tarball in a directory
  8. go in the directory
  9. ./configure
  10. make
  11. sudo make install
  12. Download the tarball from the ZeroMQ website
  13. extract the tarball in a directory
  14. go in the directory
  15. ./configure
  16. make
  17. sudo make install
At this point is better to close all the terminals and re-open them.

On some systems is better to run :
sudo ldconfig to update the system library cache, otherwise it is requested a reboot of the machine.

Install Czmq

In order to bind the library with the C language is necessary to compile the library Czmq


  1.  git clone https://github.com/zeromq/czmq
  2.  cd czmq
  3.  ./autogen.sh
  4.  ./configure
  5.  make
  6.  sudo make install
  7.  sudo ldconfig

Use ZeroMQ in your code


Usually the libraries by default are placed in /usr/local/lib.

In order to use ZeroMQ in a project is enough to add to the linker phase the two main libraries, zeroMQ and the binding one.
Usually the name is the name of the library minus the "lib" part and the extension.
So for example, if the zeroMQ main library is called libzmq.so.3, the library name to be used in the linker will be zmq.

For example in a makefile :

LIB0MQ = -l zmq -l czmq      # define variable LIB0MQ containing the libraries directive
................
................
CC = gcc $(CFLAGS) -c
CC1 = gcc $(CFLAGS)

LD = $(LIB0MQ)
AS = as $(AFLAGS)

Then in the main preparation of the code :

projectName: $(projectName_objs)
$(CC1) -o $(APPNAME) $(projectName_objs) $(LD)


Cross compile for ARM

To cross compile for ARM, be sure to have installed a ARM toolchain other the packages described above, and when ready to compile ZeroMQ, starts with :

  1. ./configure --host=arm-none-linux-gnueabi
  2. make
Then copy the library on the target ... TBD

Compile the examples


To compile the examples is possible to use the build script provided with the examples.
However it can be overshooting.

I suggest to use this approach :

  1. copy the examples in a local directory
  2. run : gcc -Wall -g name_example.c -lzmq -o name_example
If everything is installed correctly, the program will execute.

Sunday, April 26, 2015

Monitoring health data - populating Food tables

This article continue the discussion about the building of the system to collect my health numbers.

There are already many data stored in LibreOffice Calc tables and other LibreOffice documents.
Because of that when/where possible macro will be used to generate the mySQL commands to populate the tables.

Populate T_FOOD_TYPE

Populating this table it requires more work because it is necessary to find the information about each specific food used. In order to simplify the input mechanism, a new spreadsheet that collect the information to be put in the database, is created.
Unfortunately there is no easy way to populate the spreadsheet, i.e. it must be done manually, copying the data from the existing LibreOffice word documents as described in the Monitoring health data - my way  #2 article.

Let's examine the spreadsheet used to format the insert statement.

The spreadsheet has two tabs:
  • Food type
  • Food
The Food type tab, is structured as the database, raw.
A hash key is prepared as index, using the Food name, the brand and the number of carbs.
This should allow to generate a unique key for the each record.

Here the fields of the tab:

Index (food type) Name 1 Serving Serving unit Serv X cont Carbs Calories Protein Fat Sodium Potassium Brand Notes


The index in the cell A2 use the Hash macro (it is implied the third Party Hash macro is installed):

=TEXTHASH(CONCATENATE(B2,L2,F2),"MD5")

The goal is to have a unique index and to do so the hash is calculated using the name of the food (B2), the value in carbs (F2) and the brand (L2).
The field in the table are mostly following the SQL table structure (see Monitoring health data - an improvement).
For extra info there are these fields:
  • Serv X cont
    Serving per container - the number of serving you can expect from the container or how many servings can be obtained from a recipe
  • Notes
    Just extra notes about the food
After the table, there is the formula for the formatting of the SQL insert statement.
First of all, a cell (O1) contains the starting structure of the statement :

Insert into t_food_type values (

Then below, this formula is applied :
=IF(B2="","",(CONCATENATE($O$1,"""",A2,"""", ",", """", B2, """", ",", C2, ",", """", D2, """", ",", F2, ",", G2, ",", H2, ",", I2, ",", J2, ",", K2, ",",  """", L2, """", ",", """", M2, """", ");")))
The meaning is :
  • if the field "Name" exists (not blank) then 
    • create a string using the field O1, plus the other fields, starting from the field A2 (hash key) and ending with the field M2 (Notes)
  • otherwise create a blank entry
The formula is then copied in all subsequent cells.

Here an example of the insert statement generation starting with the data on the table :

Insert into t_food_type values ("ce7993e549b4d010c825766dfd034a99","Pork Patties",2,"patty",2,270,11,24,500,0,"Jimmy Dean","");
Insert into t_food_type values ("981a21f5a752cad048a834b9fd6cd258","Cooked Ham",4,"slices",1,60,8,2,580,0,"Land O'Frost","");
Insert into t_food_type values ("1a52451ea65def964ddb3c2be50811ce","Big Cup Noodles Homestyle Beef Flavor Ramen Noodle",1,"1/2 container",24,190,3,8,760,0,"Nissin","");
Insert into t_food_type values ("74ccbb6a8f0e507375ca5f727074a4e1","Texas Style Angus Beef Chili with Black Beans Soup",1,"cup",21,210,15,5,790,0,"Campbell","");
Insert into t_food_type values ("76d5f224ea4a78e3d737caa4eba0d1e","iesta Chicken Lime Tortilla Soup with White Meat Chicken",1,"cup",13,110,9,1.5,790,0,"Campbell","");
.............................................................
.............................................................

After the table is manually populated, we end up with a list of insert capable to fill up the T_FOOD_TYPE table.

Populate T_FOOD

The same mechanism used  to populate the T_FOOD_TYPE table, is used to populate the T_FOOD table too.
Like before, a new spreadsheet that collect the information to be put in the database is created (still refer to the Monitoring health data - my way  #2 article)

Let's examine the spreadsheet used to format the insert statement.

The spreadsheet has two tabs:
  • Food type
  • Food
The Food tab, is structured as the database, raw.
Here the table structure:

Index Date Meal Type Meal FoodIndex Food name Servings Calculated carbs Calculated salt

The Calculated carbs and Calculated Salt are populated using data from the previous table.

Calculated carbs is :

=VLOOKUP(D2,'Food type'.$A$2:$M$156, 6, 0)*F2

The meaning is to look in the all previous tab (Food type) of the spreadsheet for the FoodIndex key, retrieve the number of carbs per serving and multiply for the number of serving (field Servings on this tab).

Similarly for the Calculated salt :

=VLOOKUP(D2,'Food type'.$A$2:$M$156, 10, 0)*F2

The meaning is to look in the all previous tab (Food type) of the spreadsheet for the FoodIndex key, retrieve the number of mg of salt per serving and multiply for the number of serving (field Servings on this tab).
Just to have a quick idea about the amount of salt of the selected food.

The Index is an hash key using the Date Meal, the FoodIndex and the Food name fields. 
This should allow to generate a unique key for the each record.
The cell A2 contains this formula :

=TEXTHASH(CONCATENATE(B2,C2,D2,E2),"MD5")

Again the purpose is to obtain a unique key for each entry, to do so it used the date of the meal (B2), the type of the meal (C2, e.g. Breakfast or Lunch or Dinner or Snack), the food index (D2, foreign key) and the food name (E2).
Note that the food name field is populated from the other Calc table (see above).

After the table, there is the formula for the formatting of the SQL insert statement.
First of all, a cell (I1) contains the starting structure of the statement :

Insert into t_food values (

Then below, this formula is applied :

=IF(B2="","",(CONCATENATE($H$1, """", A2, """", ", str_to_date('",(TEXT(B2,"mm/dd/yy")), "', ", """", "%m/%d/%Y", """", "), ",        """",C2,"""",", ","""",D2,"""", ", " ,F2, ");")))


The meaning is :
  • if the field "Date Meal" exists (not blank) then 
    • create a string using the field H1, plus the other fields, starting from the field A2 (hash key) and ending with the field F2 (Servings)
  • otherwise create a blank entry
The formula is then copied in all subsequent cells.

Here an example of the insert statement generation starting with the data on the table :

Insert into t_food values (84280b208c8c61a7aab5c4be5fb25385, str_to_date('12/29/14', "%m/%d/%Y"), "Breakfast", "ac118ebecb401ae2e9fe42b8f7b1da8c", 2);
Insert into t_food values (16df74f3237c526a71186884b0cd7aa1, str_to_date('12/29/14', "%m/%d/%Y"), "Breakfast", "254a86d2153bfbc149f51d63c7113952", 1);
Insert into t_food values (532061254f4761e69bcf627c4916f3c2, str_to_date('12/29/14', "%m/%d/%Y"), "Lunch", "1b4f22d0d92b9185406cf6d815fd2c4c", 1);
Insert into t_food values (34c6041c3126488e599aa467e8a0d4de, str_to_date('12/29/14', "%m/%d/%Y"), "Lunch", "af3a803a27460603422bf47322ba6d4", 2);
Insert into t_food values (532061254f4761e69bcf627c4916f3c2, str_to_date('12/29/14', "%m/%d/%Y"), "Dinner", "1b4f22d0d92b9185406cf6d815fd2c4c", 2);
....................................................................
....................................................................


Basically after the table is manually populated, we end up with a list of insert capable to fill up the T_FOOD table.