Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Problem with code

Status
Not open for further replies.

leo123

New Member
Im currently trying out kalman's filter but got stuck at the very beginning. I cant even display any angle output by my 5DOF. When

accDegreeY[ 0 ] = (adcValue[ 4 ]*5.0)/1023;
accDegreeY[ 1 ] = (accDegreeY[ 0 ]-1.6)/0.3;

accDegreeZ[ 0 ] = (adcValue[ 5 ]*5.0)/1023;
accDegreeZ[ 1 ] = (accDegreeZ[ 0 ]-1.6)/0.3;

is added, i cant compile it. This means that there is a problem with my declaration but i cant find it. My lcd display can only display positive numbers. As you can see in my code, i randomly put in my gyro and accelerometer to know if it is working. I still have one problem because i dont know how to include math calculations in programs. Examples : sin, cos, tan, arcsin,atan2, square root. Im still new in programming PIC. Help needed. Thank you.

Using this math equation to solve my kalman filter.
A Guide To using IMU (Accelerometer and Gyroscope Devices) in Embedded Applications. « Starlino Electronics

This is my code.

#include <stdio.h>
#include <htc.h>
#include <math.h>
#include "lcd.h"

__CONFIG(0x3f71);

void main ( void )
{
char LCDString [ 17 ];
unsigned int adcValue[7];
float accDegreeX[ 3 ],accDegreeY[ 3 ],accDegreeZ[ 3 ],gyroRateX[ 3 ],gyroRateY[ 3 ];
unsigned char tmp;

// Initializations
lcd_init();

lcd_goto ( 0x00 );
lcd_puts ( "ADC:" );
lcd_goto ( 0x40 );
lcd_puts ( "Degree:" );

PORTB = 0x00;

TRISA = 0xff;
TRISB = 0x00;
ADCON1 = 0xc8;

while ( 1 )
{
// Body
ADCON0 = 0x81;
ADGO = 1;
while ( ADGO )
continue;
adcValue [ 0 ] = ( ADRESH << 8 ) | ADRESL;

ADCON0 = 0x89;
ADGO = 1;
while ( ADGO )
continue;
adcValue [ 1 ] = ( ADRESH << 8 ) | ADRESL;

ADCON0 = 0xa1;
ADGO = 1;
while ( ADGO )
continue;
adcValue [ 2 ] = ( ADRESH << 8 ) | ADRESL;

ADCON0 = 0xa9;
ADGO = 1;
while ( ADGO )
continue;
adcValue [ 3 ] = ( ADRESH << 8 ) | ADRESL;

ADCON0 = 0xb1;
ADGO = 1;
while ( ADGO )
continue;
adcValue [ 4 ] = ( ADRESH << 8 ) | ADRESL;

ADCON0 = 0xb9;
ADGO = 1;
while ( ADGO )
continue;
adcValue [ 5 ] = ( ADRESH << 8 ) | ADRESL;

gyroRateX[ 0 ] = (adcValue[ 0 ]*5.0)/1023;
gyroRateX[ 1 ] = (gyroRateX[ 0 ]-1.5)/0.002;

gyroRateY[ 0 ] = (adcValue[ 1 ]*5.0)/1023;
gyroRateY[ 1 ] = (gyroRateY[ 0 ]-1.5)/0.002;

accDegreeX[ 0 ] = (adcValue[ 3 ]*5.0)/1023;
accDegreeX[ 1 ] = (accDegreeX[ 0 ]-1.6)/0.3;

lcd_goto ( 0x05 );
sprintf ( LCDString, "%d ", adcValue[ 0 ] );
lcd_puts ( LCDString );

lcd_goto ( 0x48 );
tmp = ( unsigned char ) gyroRateX[ 1 ];
gyroRateX[ 1 ] = ( gyroRateX[ 1 ] - tmp ) * 10;
lcd_putch ( tmp + '0' );
lcd_putch ( '.' );
tmp = ( unsigned char ) gyroRateX[ 1 ];
gyroRateX[ 1 ] = ( gyroRateX[ 1 ] - tmp ) * 10;
lcd_putch ( tmp + '0' );
tmp = ( unsigned char ) gyroRateX[ 1 ];
gyroRateX[ 1 ] = ( gyroRateX[ 1 ] - tmp ) * 10;
lcd_putch ( tmp + '0' );
lcd_puts ( " G" );
}
}
 
It would help if you posted the compiler error. Also, use code tags so that we can understand the file more easily.
 
where did you insert
accDegreeY[ 0 ] = (adcValue[ 4 ]*5.0)/1023;
accDegreeY[ 1 ] = (accDegreeY[ 0 ]-1.6)/0.3;

accDegreeZ[ 0 ] = (adcValue[ 5 ]*5.0)/1023;
accDegreeZ[ 1 ] = (accDegreeZ[ 0 ]-1.6)/0.3;
?

and what is the error message ? Also mention the controller you are using and the version of the compiler.
 
Im using a MPlab Compiler(PIC16F877A)
MPLAB IDE v7.61

This is the error
Clean: Deleting intermediary and output files.
Clean: Deleted file "C:\Users\Tio Pit Sin\Desktop\FYP Programming\delay.obj".
Clean: Deleted file "C:\Users\Tio Pit Sin\Desktop\FYP Programming\delay.cce".
Clean: Deleted file "C:\Users\Tio Pit Sin\Desktop\FYP Programming\lcd.obj".
Clean: Deleted file "C:\Users\Tio Pit Sin\Desktop\FYP Programming\lcd.cce".
Clean: Deleted file "C:\Users\Tio Pit Sin\Desktop\FYP Programming\Program1.obj".
Clean: Deleted file "C:\Users\Tio Pit Sin\Desktop\FYP Programming\Program1.cce".
Clean: Deleted file "Program1.cof".
Clean: Deleted file "C:\Users\Tio Pit Sin\Desktop\FYP Programming\Program1.mcs".
Clean: Done.
Executing: "C:\Program Files\HI-TECH Software\PICC-Lite\9.50\bin\picl.exe" -C -E"delay.cce" "delay.c" -O"delay.obj" -Zg9 -O -ASMLIST -Q -MPLAB -16F877A
Executing: "C:\Program Files\HI-TECH Software\PICC-Lite\9.50\bin\picl.exe" -C -E"lcd.cce" "lcd.c" -O"lcd.obj" -Zg9 -O -ASMLIST -Q -MPLAB -16F877A
Executing: "C:\Program Files\HI-TECH Software\PICC-Lite\9.50\bin\picl.exe" -C -E"Program1.cce" "Program1.c" -O"Program1.obj" -Zg9 -O -ASMLIST -Q -MPLAB -16F877A
Executing: "C:\Program Files\HI-TECH Software\PICC-Lite\9.50\bin\picl.exe" -E"Program1.lde" "C:\Users\Tio Pit Sin\Desktop\FYP Programming\delay.obj" "C:\Users\Tio Pit Sin\Desktop\FYP Programming\lcd.obj" "C:\Users\Tio Pit Sin\Desktop\FYP Programming\Program1.obj" -M"Program1.map" -O"Program1.cof" -O"Program1.hex" -Q -MPLAB -16F877A
BUILD FAILED: Sat Aug 14 23:21:21 2010


This is the code with accDegreeY and accDegreeZ included. You could easily spot it as my code is short.
#include <stdio.h>
#include <htc.h>
#include <math.h>
#include "lcd.h"

__CONFIG(0x3f71);

void main ( void )
{
char LCDString [ 17 ];
unsigned int adcValue[7];
float accDegreeX[ 3 ],accDegreeY[ 3 ],accDegreeZ[ 3 ],gyroRateX[ 3 ],gyroRateY[ 3 ];
unsigned char tmp;

// Initializations
lcd_init();

lcd_goto ( 0x00 );//Printing ADC: on the top LCD
lcd_puts ( "ADC:" );
lcd_goto ( 0x40 );//Printing Degree: on the bottom LCD
lcd_puts ( "Degree:" );

PORTB = 0x00;

TRISA = 0xff;
TRISB = 0x00;
ADCON1 = 0xc8;//Selecting type of ADC pins

while ( 1 )
{
// Body
ADCON0 = 0x81;
ADGO = 1;
while ( ADGO )
continue;
adcValue [ 0 ] = ( ADRESH << 8 ) | ADRESL;//Getting ADC from RA0(gyroRateX)

ADCON0 = 0x89;
ADGO = 1;
while ( ADGO )
continue;
adcValue [ 1 ] = ( ADRESH << 8 ) | ADRESL;//Getting ADC from RA1(gyroRateY)

ADCON0 = 0xa1;
ADGO = 1;
while ( ADGO )
continue;
adcValue [ 2 ] = ( ADRESH << 8 ) | ADRESL;//Getting ADC from RA5

ADCON0 = 0xa9;
ADGO = 1;
while ( ADGO )
continue;
adcValue [ 3 ] = ( ADRESH << 8 ) | ADRESL;//Getting ADC from RE0(accDegreeX)

ADCON0 = 0xb1;
ADGO = 1;
while ( ADGO )
continue;
adcValue [ 4 ] = ( ADRESH << 8 ) | ADRESL;//Getting ADC from RE1(accDegreeY)

ADCON0 = 0xb9;
ADGO = 1;
while ( ADGO )
continue;
adcValue [ 5 ] = ( ADRESH << 8 ) | ADRESL;//Getting ADC from RE2(accDegreeZ)

gyroRateX[ 0 ] = (adcValue[ 0 ]*5.0)/1023;
gyroRateX[ 1 ] = (gyroRateX[ 0 ]-1.5)/0.002;//Basic calculation to get d/dt for gyroRateX

gyroRateY[ 0 ] = (adcValue[ 1 ]*5.0)/1023;
gyroRateY[ 1 ] = (gyroRateY[ 0 ]-1.5)/0.002;//Basic calculation to get d/dt for gyroRateY

accDegreeX[ 0 ] = (adcValue[ 3 ]*5.0)/1023;
accDegreeX[ 1 ] = (accDegreeX[ 0 ]-1.6)/0.3;//Basic calculation to get g for accDegreeX

accDegreeY[ 0 ] = (adcValue[ 4 ]*5.0)/1023;
accDegreeY[ 1 ] = (accDegreeY[ 0 ]-1.6)/0.3;//Basic calculation to get g for accDegreeY

accDegreeZ[ 0 ] = (adcValue[ 5 ]*5.0)/1023;
accDegreeZ[ 1 ] = (accDegreeZ[ 0 ]-1.6)/0.3;//Basic calculation to get g for accDegreeZ


lcd_goto ( 0x05 );
sprintf ( LCDString, "%d ", adcValue[ 0 ] );//Printing random adc taken from and of those five calculated value. I manually change just to make sure it is working.
lcd_puts ( LCDString );

lcd_goto ( 0x48 );
tmp = ( unsigned char ) gyroRateX[ 1 ];
gyroRateX[ 1 ] = ( gyroRateX[ 1 ] - tmp ) * 10;
lcd_putch ( tmp + '0' );
lcd_putch ( '.' );
tmp = ( unsigned char ) gyroRateX[ 1 ];
gyroRateX[ 1 ] = ( gyroRateX[ 1 ] - tmp ) * 10;
lcd_putch ( tmp + '0' );
tmp = ( unsigned char ) gyroRateX[ 1 ];
gyroRateX[ 1 ] = ( gyroRateX[ 1 ] - tmp ) * 10;
lcd_putch ( tmp + '0' );
lcd_puts ( " G" );
}
}

Thank you for all your help!
 
Status
Not open for further replies.

Latest threads

Back
Top