Subversion Repositories svnkaklik

Rev

Rev 507 | Rev 509 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log

Rev 507 Rev 508
Line 17... Line 17...
17
#include <avr/io.h>		// include I/O definitions (port names, pin names, etc)
17
#include <avr/io.h>		// include I/O definitions (port names, pin names, etc)
18
#include <avr/interrupt.h>	// include interrupt support
18
#include <avr/interrupt.h>	// include interrupt support
19
#include <math.h>
19
#include <math.h>
20
 
20
 
21
#include "global.h"		// include our global settings
21
#include "global.h"		// include our global settings
22
#include "uart.h"		// include uart function library
22
#include "uart2.h"		// include uart function library
23
#include "rprintf.h"	// include printf function library
23
#include "rprintf.h"	// include printf function library
24
#include "timer.h"		// include timer function library (timing, PWM, etc)
24
#include "timer.h"		// include timer function library (timing, PWM, etc)
25
#include "a2d.h"		// include A/D converter function library
25
#include "a2d.h"		// include A/D converter function library
26
 
26
 
27
//----- Begin Code ------------------------------------------------------------
27
//----- Begin Code ------------------------------------------------------------
28
#define BUFLEN 32
28
#define BUFLEN 64
29
 
29
 
30
int main(void)
30
int main(void)
31
{
31
{
32
	u08 i=0;
-
 
33
	s16 x=0,y=0;
32
	u16 i,x,y;
34
	double fi;
33
	double fi, err, fibuf[BUFLEN];
35
	s16 fia;
34
	s16 fia, erra;
36
	u16 fib;
35
	u16 fib, errb;
37
 
36
 
38
	// initialize our libraries
37
	// initialize our libraries
39
	// initialize the UART (serial port)
38
	// initialize the UART (serial port)
40
	uartInit();
39
	uartInit();
41
	uartSetBaudRate(9600);
40
	uartSetBaudRate(0,9600);
42
	// make all rprintf statements use uart for output
41
	// make all rprintf statements use uart for output
43
	rprintfInit(uartSendByte);
42
	rprintfInit(uart0SendByte);
44
	// initialize the timer system
43
	// initialize the timer system
45
	timerInit();
44
	timerInit();
46
	// turn on and initialize A/D converter
45
	// turn on and initialize A/D converter
47
	a2dInit();	
46
	a2dInit();	
48
	// configure a2d port (PORTA) as input
47
	// configure a2d port (PORTA) as input
49
	// so we can receive analog signals
48
	// so we can receive analog signals
50
	DDRC = 0x00;
49
	DDRA = 0x00;
51
	// make sure pull-up resistors are turned off
50
	// make sure pull-up resistors are turned off
52
	PORTC = 0x00;
51
	PORTA = 0x00;
53
 
52
 
54
	// set the a2d prescaler (clock division ratio)
53
	// set the a2d prescaler (clock division ratio)
55
	// - a lower prescale setting will make the a2d converter go faster
54
	// - a lower prescale setting will make the a2d converter go faster
56
	// - a higher setting will make it go slower but the measurements
55
	// - a higher setting will make it go slower but the measurements
57
	//   will be more accurate
56
	//   will be more accurate
Line 66... Line 65...
66
	// use a2dConvert8bit(channel#) to get an 8bit a2d reading
65
	// use a2dConvert8bit(channel#) to get an 8bit a2d reading
67
	// use a2dConvert10bit(channel#) to get a 10bit a2d reading
66
	// use a2dConvert10bit(channel#) to get a 10bit a2d reading
68
 
67
 
69
	while(1)
68
	while(1)
70
	{
69
	{
-
 
70
		fi=0;
-
 
71
		err=0;
71
		for(i=0; i<BUFLEN; i++)
72
		for(i=0; i<BUFLEN; i++)
72
		{
73
		{ 
73
			x += a2dConvert10bit(0);
74
			x = a2dConvert10bit(ADC_CH_ADC0);
74
			y += a2dConvert10bit(1);
75
			y = a2dConvert10bit(ADC_CH_ADC1);
-
 
76
			fibuf[i] = atan2((double)x-511,(double)y-511);		// record computed angles to buffer for post processing
75
		}
77
		}
76
		x = x/BUFLEN - 512;
-
 
77
		y = y/BUFLEN - 512;
78
		for(i=0; i<BUFLEN; i++) fi += fibuf[i];		// sum recorded angles
78
 
79
 
-
 
80
		fi = ((fi/BUFLEN)+PI) * 180.0 / PI;		// average recorded angles and convert product to degrees
-
 
81
 
-
 
82
		for(i=0; i<BUFLEN; i++) err += (fibuf[i]-fi)*(fibuf[i]-fi);		// sum cubic errors
-
 
83
		err = sqrt(err/(BUFLEN-1))/sqrt(BUFLEN);	// compute average cubic error
-
 
84
		erra = floor(err);
79
		fi = atan2(y,x) * 180.0 / PI;
85
		errb = floor((err - erra)*1000);
-
 
86
		
80
		fia = floor(fi);
87
		fia = floor(fi);
81
		fib = floor((fi - fia));
88
		fib = floor((fi - fia)*1000);
-
 
89
 
-
 
90
		
82
		rprintf("X:%d Y:%d fi:%d.%d \r\n", x, y, fia, fib);
91
		rprintf("fi:%d.%d  +- %d.%d \r\n", fia, fib, erra, errb);
83
	}
92
	}
84
	
-
 
85
	return 0;
93
	return 0;
86
}
94
}