0,0 → 1,97 |
//----- Include Files --------------------------------------------------------- |
#include <avr/io.h> // include I/O definitions (port names, pin names, etc) |
#include <avr/interrupt.h> // include interrupt support |
#include <math.h> |
|
#include "global.h" // include our global settings |
#include "uart2.h" // include uart function library |
#include "rprintf.h" // include printf function library |
#include "timer.h" // include timer function library (timing, PWM, etc) |
#include "a2d.h" // include A/D converter function library |
|
//----- Begin Code ------------------------------------------------------------ |
#define BUFLEN 64 |
|
void radtodeg(double fi, u16 *deg, u08 *min, u08 *sec) //convert radians to degrees mins and seconds |
{ |
double pom; |
|
fi=fi*180/PI; |
*deg=(u16)trunc(fi); |
pom=(fi-(*deg))*60; |
*min=(u08)trunc(pom); |
*sec=(u08)round((pom-(*min))*60); |
} |
|
inline double quadraticerror(double average, double buf[], u16 size) // compute average quadratic error |
{ |
u16 i; |
double err=0; |
|
for(i=0; i<size; i++) err += square(buf[i]-average); // sum quadratic errors |
err = sqrt((1/(double)size)*err); |
return err; |
} |
|
int main(void) |
{ |
u16 i,x,y; |
double fi, err, fibuf[BUFLEN]; // buffer for recorded and computed values |
u08 fi_min, fi_sec, err_min, err_sec; // computed angles |
u16 fi_deg, err_deg; // computed angles in whole degrees |
|
// initialize some libraries |
// initialize the UART (serial port) |
uartInit(); |
uartSetBaudRate(0,9600); |
// make all rprintf statements use uart for output |
rprintfInit(uart0SendByte); |
// initialize the timer system |
timerInit(); |
// turn on and initialize A/D converter |
a2dInit(); |
// configure a2d port (PORTA) as input |
// so we can receive analog signals |
DDRF = 0x00; |
// make sure pull-up resistors are turned off |
PORTF = 0x00; |
|
// set the a2d prescaler (clock division ratio) |
// - a lower prescale setting will make the a2d converter go faster |
// - a higher setting will make it go slower but the measurements |
// will be more accurate |
// - other allowed prescale values can be found in a2d.h |
a2dSetPrescaler(ADC_PRESCALE_DIV128); |
|
// set the a2d reference |
// - the reference is the voltage against which a2d measurements are made |
// - other allowed reference values can be found in a2d.h |
a2dSetReference(ADC_REFERENCE_AREF); |
|
// use a2dConvert8bit(channel#) to get an 8bit a2d reading |
// use a2dConvert10bit(channel#) to get a 10bit a2d reading |
|
rprintf("inklinometr 2009\r\n"); |
|
while(1) |
{ |
fi=0; |
err=0; |
for(i=0; i<BUFLEN; i++) |
{ |
x = a2dConvert10bit(ADC_CH_ADC0); |
y = a2dConvert10bit(ADC_CH_ADC1); |
fibuf[i] = atan2((double)x-511,(double)y-511); // record computed angles to buffer for post processing |
} |
for(i=0; i<BUFLEN; i++) fi += fibuf[i]; // sum recorded angles |
fi = (fi/BUFLEN)+PI; // average recorded angles and expand product to whole circle |
|
err=quadraticerror((fi-PI),fibuf,BUFLEN); |
radtodeg(fi,&fi_deg,&fi_min,&fi_sec); //translate radians to degrees |
radtodeg(err,&err_deg,&err_min,&err_sec); |
|
rprintf("fi:%d.%d.%d +- %d.%d.%d \r\n", fi_deg, fi_min, fi_sec, err_deg, err_min, err_sec); |
delay_ms(20); |
} |
return 0; |
} |
Property changes: |
Added: svn:mergeinfo |