Friday, June 10, 2011

My altimeter code

/////////**********My Altimeter Project***************/////////

Try this code on mbed for dislaying Altitude and Pressure on 16X2 LCD display.
This program copied from mbed cookbook

#include "mbed.h"
#include "math.h"
#include "TextLCD.h"

TextLCD lcd(p15, p16, p17, p18, p19, p20); // rs, e, d4-d7
I2C i2c(p9, p10);
Serial pc(USBTX, USBRX); // tx, rx

int I2C_ADDRESS = 0xEE; // sensor address

// oversampling setting
// 0 = ultra low power
// 1 = standard
// 2 = high
// 3 = ultra high resolution
const unsigned char oversampling_setting = 3; //oversampling for measurement
const unsigned char pressure_conversiontime[4] = {
4.5, 7.5, 13.5, 25.5 }; // delays for oversampling settings 0, 1, 2 and 3

// sensor registers from the BOSCH BMP085 datasheet
short ac1;
short ac2;
short ac3;
unsigned short ac4;
unsigned short ac5;
unsigned short ac6;
short b1;
short b2;
short mb;
short mc;
short md;

// variables to keep the values
float temperature = 0;
float pressure = 0;


void writeRegister(unsigned char r, unsigned char v)
{
char cmd1[2];
cmd1[0] = r;
cmd1[1] = v;
i2c.write(I2C_ADDRESS,cmd1, 2);
}

// read a 16 bit register
int readIntRegister(unsigned char r)
{
char cmd1[2];
char cmd2[1];
//unsigned char msb, lsb;
cmd2[0] = r;
i2c.write(I2C_ADDRESS,cmd2, 1);
i2c.read(I2C_ADDRESS, cmd1, 2);
return (((int)cmd1[0]<<8) | ((int)cmd1[1]));
}

// read uncompensated temperature value
unsigned int readUT() {
writeRegister(0xf4,0x2e);
wait(0.0045); // the datasheet suggests 4.5 ms
return readIntRegister(0xf6);

}

// read uncompensated pressure value
long readUP() {
writeRegister(0xf4,0x34+(oversampling_setting<<6));
wait(pressure_conversiontime[oversampling_setting]*0.001);

//unsigned char msb, lsb, xlsb;
char cmd1[3];
char cmd0[1];
cmd0[0] = 0xf6;
i2c.write(I2C_ADDRESS,cmd0, 1);
i2c.read(I2C_ADDRESS, cmd1, 3);
return (((long)cmd1[0]<<16) | ((long)cmd1[1]<<8) | ((long)cmd1[2])) >>(8-oversampling_setting);

}

// Below there are the utility functions to get data from the sensor.

// read temperature and pressure from sensor
void readSensor() {

long ut= readUT();
long up = readUP();


int x1, x2, x3, b3, b5, b6, p;
unsigned int b4, b7;
//calculate true temperature
x1 = ((long)ut - ac6) * ac5 >> 15;
x2 = ((long) mc << 11) / (x1 + md);
b5 = x1 + x2;
temperature = (b5 + 8) >> 4;

//calculate true pressure
b6 = b5 - 4000;
x1 = (b2 * (b6 * b6 >> 12)) >> 11;
x2 = ac2 * b6 >> 11;
x3 = x1 + x2;

if (oversampling_setting == 3) b3 = ((int32_t) ac1 * 4 + x3 + 2) << 1;
if (oversampling_setting == 2) b3 = ((int32_t) ac1 * 4 + x3 + 2);
if (oversampling_setting == 1) b3 = ((int32_t) ac1 * 4 + x3 + 2) >> 1;
if (oversampling_setting == 0) b3 = ((int32_t) ac1 * 4 + x3 + 2) >> 2;

x1 = ac3 * b6 >> 13;
x2 = (b1 * (b6 * b6 >> 12)) >> 16;
x3 = ((x1 + x2) + 2) >> 2;
b4 = (ac4 * (unsigned long) (x3 + 32768)) >> 15;
b7 = ((unsigned long) up - b3) * (50000 >> oversampling_setting);
p = b7 < 0x80000000 ? (b7 * 2) / b4 : (b7 / b4) * 2;

x1 = (p >> 8) * (p >> 8);
x1 = (x1 * 3038) >> 16;
x2 = (-7357 * p) >> 16;
pressure = p + ((x1 + x2 + 3791) >> 4);

}



void getCalibrationData() {
pc.printf("Reading Calibration Data");
ac1 = readIntRegister(0xAA);
pc.printf("AC1: %d\r\n",ac1);

ac2 = readIntRegister(0xAC);
pc.printf("AC2: %d\r\n",ac2);

ac3 = readIntRegister(0xAE);
pc.printf("AC3: %d\r\n",ac3);

ac4 = readIntRegister(0xB0);
pc.printf("AC4: %d\r\n",ac4);

ac5 = readIntRegister(0xB2);
pc.printf("AC5: %d\r\n",ac5);

ac6 = readIntRegister(0xB4);
pc.printf("AC6: %d\r\n",ac6);

b1 = readIntRegister(0xB6);
pc.printf("B1: %d\r\n",b1);

b2 = readIntRegister(0xB8);
pc.printf("B2: %d\r\n",b2);

mb = readIntRegister(0xBA);
pc.printf("MB: %d\r\n",mb);

mc = readIntRegister(0xBC);
pc.printf("MC: %d\r\n",mc);

md = readIntRegister(0xBE);
pc.printf("MD: %d\r\n",md);

}

float altitud(float p)
{
float To=298;
float ho=0;
float Po=101325;
//ecuacion
float c=(To-0.0065*ho);
float e=(p/Po);
float d=exp(0.19082*log(e));
float b=c*d;
float alt=153.84615*(To-b);
return(alt);
}



int main()
{
int z;
pc.baud(9600);
getCalibrationData();
lcd.printf(" ");
while(1)
{

readSensor();
pc.printf("Temperature: %.f Pressure: %.f Altitud: %.1f\r",temperature,pressure, altitud(pressure));
lcd.locate(0,0);
lcd.printf("Pressur:%.f",pressure);
lcd.locate(0,1);
lcd.printf("Altitud:%.1fm",altitud(pressure));
wait(1);
lcd.printf(" ");

}
}

Connect the sensor and lcd display to the mbed controller to the described pins in definition part..

Its working really very nice..

Seshu..

Thursday, June 2, 2011

IMU Design

Dear all,

I have developed an IMU using two breakout boards available in Sparkfun electronics,which are ITG3200 and ADXL345, the prototype is really working good. The code for my prototype is attached below.

references for the sensors: http://www.sparkfun.com/products/9694
: http://www.sparkfun.com/products/9836

sample code and example programs are also available from the above links. Infact i used those sample programs to develop this code.


#include "mbed.h"
#include "ADXL345.h"
#include "ITG3200.h"

Serial pc(USBTX, USBRX);
ITG3200 gyro(p28, p27);


ADXL345 accelerometer(p11, p12, p13, p14);


int main() {
int readings[3] = {0, 0, 0};
pc.printf("Now starting ITG-3200 test...\n");

//Set highest bandwidth.
gyro.setLpBandwidth(LPFBW_42HZ);

pc.printf("Starting ADXL345 test...\n");
pc.printf("Device ID is: 0x%02x\n", accelerometer.getDevId());

//Go into standby mode to configure the device.
accelerometer.setPowerControl(0x00);

//Full resolution, +/-16g, 4mg/LSB.
accelerometer.setDataFormatControl(0x0B);

//3.2kHz data rate.
accelerometer.setDataRate(ADXL345_3200HZ);

//Measurement mode.
accelerometer.setPowerControl(0x08);
while (1) {

wait(0.1);
accelerometer.getOutput(readings);
unsigned int j,k,l,m,n,o,p,q,r,s,t,u;
//13-bit, sign extended values.
// pc.printf("LeftACC1:%i , LeftACC2:%i , LeftACC3:%i ", (int16_t)readings[0], (int16_t)readings[1], (int16_t)readings[2]);

// pc.printf(" LeftGyroX:%d , LeftGyroY:%d , LeftGyroZ%d \n\r", gyro.getGyroX(),gyro.getGyroY(), gyro.getGyroZ());

j = (int16_t)readings[0];
k = (int16_t)readings[1];
l = (int16_t)readings[2];
m = gyro.getGyroX();
n = gyro.getGyroY();
o = gyro.getGyroZ();


Serial pc(USBTX, USBRX);
ITG3200 gyro(p28, p27);



ADXL345 accelerometer(p11, p12, p13, p14);

accelerometer.getOutput(readings);
gyro.setLpBandwidth(LPFBW_42HZ);

p = (int16_t)readings[0];
q = (int16_t)readings[1];
r = (int16_t)readings[2];
s = gyro.getGyroX();
t = gyro.getGyroY();
u = gyro.getGyroZ();


pc.printf("%i, %i, %i, %i, %i, %i, %i, %i, %i, %i, %i, %i\n", j,k,l,p,q,r,m,n,o,s,t,u);

}
}



Here pc.printf will directly prints the output on ouput console. we can use either windows hyperterminal or any other serial data acquision softwares like real-term or tera term etc..

Development board

mbed is a new developement board available in market cheaply, plenty of applications can be developed using mbed board. Once if you buy the board, you will have access to online compiler.

The main advantage is compiling and executing the program is very easy, flashing can be done simply by dragging binary file into mbed folder.

many application and examples are available in the website
have a look

www.mbed.org

All the best...

Tuesday, May 31, 2011

Useful Links for working with OpenOCD

OpenOCD(On-chip Debugger)
1. On-Chip Debugger for windows
version 0.4.0
[http://www.freddiechopin.info/index.php/en/download/category/4-openocd]


2. Nice file to understand the arm compile and flashing process using jtag andopenocd
[http://csd.xen.ssvl.kth.se/csdlive/sites/default/files/projects/ARM%20-%20compile%20and%20flash%20program.pdf]


3. Another useful link for openocd config..
[http://gandalf.arubi.uni-kl.de/avr_projects/arm_projects/openocd_intro/index.html]

Design own jtag interface for Arm controller

step1: Download latest openocd from web [ i used latest 0.4.0]

On-Chip Debugger for windows
version 0.4.0
[http://www.freddiechopin.info/index.php/en/download/category/4-openocd]

step2: Before installing the software remove older versions.
step3: Install the Openocd
step4: Read the user manual carefully and understand about openocd

Ready!!!! now follow the remaining steps

step5: Now run the configuration files which are downloaded along with openocd.
step6: openocd -> interface -> select the .cfg file of your jtag [ i used amonteckey ][amonteckey.cfg]

step7: If you need to configure the above file all you have to do is give the following command
c:\> openocd -f interface/amonteckey.cfg
you should see the messages which states that it detected your jtag and speed etc..
done.

step8: Now run the target configuration!!!
same process like above...
Openocd -> target -> select your target processor [ i used lpcxpresso lpc 1769 cortex m3]

step9: Now run the configuration file, for this you have to use the following command
c:\> openocd -f target/lpc 1769.cfg
oops!!you will get error.........................!!!!!!!!!!!!!!!!!!!!
because you have to run along with above interface command
c:\> openocd -f interface/amonteckey.cfg -f lpc 1769.cfg
this time you will see messages which stating target detected and break points and watch points

step10: Now if you want to flash any file you can still use openocd commands for flashing.......