Subversion Repositories Projects

Rev

Rev 426 | Rev 434 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
426 killagreg 1
#include <avr/boot.h>
2
 
3
#include <avr/io.h>
4
#include <avr/interrupt.h>
5
 
6
#include "main.h"
7
#include "timer0.h"
8
#include "uart0.h"
9
#include "uart1.h"
10
#include "fat16.h"
11
#include "led.h"
12
#include "menu.h"
13
#include "printf_P.h"
14
#include "analog.h"
15
#include "gps.h"
16
#include "button.h"
17
#include "logging.h"
18
#include "settings.h"
19
 
20
#define FOLLOWME_INTERVAL 1000 // 1 second update
21
#define CELLUNDERVOLTAGE 32 // lowest allowed voltage/cell; 32 = 3.2V
22
 
23
#ifdef USE_FOLLOWME
24
int16_t UBat = 120;
25
int16_t Zellenzahl = 0;
26
int16_t PowerOn = 0;
27
int16_t i = 0;
28
int16_t delay = 0;
29
#endif
30
 
31
uint16_t Error = 0;
32
SysState_t SysState = STATE_UNDEFINED;
33
 
34
int main (void)
35
{
36
	static uint16_t FollowMe_Timer = 0;
37
 
38
	// disable interrupts global
39
	cli();
40
 
41
	// disable watchdog
42
    MCUSR &=~(1<<WDRF);
43
    WDTCSR |= (1<<WDCE)|(1<<WDE);
44
    WDTCSR = 0;
45
 
46
	// initalize modules
47
	LED_Init();
48
	LEDRED_ON;
49
    TIMER0_Init();
50
	USART0_Init();
51
	UBX_Init();
52
	USART1_Init();
53
	ADC_Init();
54
	Button_Init();
55
	// enable interrupts global
56
	sei();
57
 
58
	LEDRED_OFF;
59
	LEDGRN_ON;
60
 
61
	// try to initialize the FAT 16 filesystem on the SD-Card
62
	Fat16_Init();
63
 
64
	// initialize the settings
65
	Settings_Init();
66
	// initialize logging (needs settings)
67
	Logging_Init();
68
 
69
  	#ifdef USE_SDLOGGER
70
	printf("\r\n\r\nHW: SD-Logger");
71
	#endif
72
	#ifdef USE_FOLLOWME
73
	printf("\r\n\r\nHW: Follow-Me");
74
	#endif
75
	printf("\r\nFollow Me\n\rSoftware:V%d.%d%c ",VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH + 'a');
76
	printf("\r\n------------------------------");
77
	printf("\r\n");
78
 
79
 
80
	//BeepTime = 2000;
81
 
82
    LCD_Clear();
83
 
84
	FollowMe_Timer = SetDelay(FOLLOWME_INTERVAL);
85
 
86
	while (1)
87
	{
88
		// get gps data to update the follow me position
89
		GPS_Update();
90
 
91
		// update logging
92
		Logging_Update();
93
 
94
		// check for button action and change state resectively
95
		if(GetButton())
96
		{
97
			BeepTime = 200;
98
 
99
			switch(SysState)
100
			{
101
				case STATE_IDLE:
102
					if(!Error) SysState = STATE_SEND_FOLLOWME; // activate followme only of no error has occured
103
					break;
104
 
105
				case STATE_SEND_FOLLOWME:
106
					SysState = STATE_IDLE;
107
					break;
108
 
109
				default:
110
					SysState = STATE_IDLE;
111
					break;
112
			}
113
 
114
		}
115
 
116
		// state machine
117
		switch(SysState)
118
		{
119
			case STATE_SEND_FOLLOWME:
120
				if(CheckDelay(FollowMe_Timer)) // time for next message?
121
				{
122
					if(FollowMe.Position.Status == NEWDATA)        // if new
123
					{   // update remaining data
124
						FollowMe_Timer = SetDelay(FOLLOWME_INTERVAL);  // reset timer
125
						FollowMe.Heading = -1;			// invalid heading
126
						FollowMe.ToleranceRadius = 1;   // 1 meter
127
						FollowMe.HoldTime = 60;         // go home after 60s without any update
128
						FollowMe.Event_Flag = 0;        // no event
129
						FollowMe.reserve[0] = 0;		// reserve
130
						FollowMe.reserve[1] = 0;		// reserve
131
						FollowMe.reserve[2] = 0;		// reserve
132
						FollowMe.reserve[3] = 0;		// reserve
133
						Request_SendFollowMe = 1;       // triggers serial tranmission
134
 
135
					}
136
					else // now new position avalable (maybe bad gps signal condition)
137
					{
138
						FollowMe_Timer = SetDelay(FOLLOWME_INTERVAL/4);  // reset timer on higer frequency
139
					}
140
					LEDGRN_TOGGLE;						// indication of active follow me
141
				}
142
				break;
143
 
144
			case STATE_IDLE:
145
				// do nothing
146
				LEDGRN_ON;
147
				break;
148
 
149
			default:
150
				// triger to idle state
151
				SysState = STATE_IDLE;
152
				break;
153
 
154
		}
155
 
156
 
157
		// restart ADConversion if ready
158
		if(ADReady)
159
		{
160
			DebugOut.Analog[0] = Adc0;
161
			DebugOut.Analog[1] = Adc1;
162
			DebugOut.Analog[2] = Adc2;
163
			DebugOut.Analog[3] = Adc3;
164
			DebugOut.Analog[4] = Adc4;
165
			DebugOut.Analog[5] = Adc5;
166
			DebugOut.Analog[6] = Adc6;
167
			DebugOut.Analog[7] = Adc7;
168
 
169
			#ifdef USE_FOLLOWME
170
			// AVcc = 5V --> 5V = 1024 counts
171
			// the voltage at the voltage divider reference point is 0.8V less that the UBat
172
			// because of the silicon diode inbetween.
173
			// voltage divider R2=10K, R3=3K9
174
			// UAdc4 = R3/(R3+R2)*UBat= 3.9/(3.9+10)*UBat = UBat/3.564
175
			UBat = (3 * UBat + (64 * Adc4) / 368) / 4;
176
			DebugOut.Analog[8] = UBat;
177
 
178
			// check for zellenzahl
179
			if(PowerOn < 100)
180
			{
181
				if(UBat<=84) Zellenzahl = 2;
182
				else Zellenzahl = 3;
183
				PowerOn++;
184
			}
185
			DebugOut.Analog[16] = Zellenzahl;
186
			DebugOut.Analog[17] = PowerOn;
187
 
188
			//show recognised Zellenzahl to user
189
			if(i < Zellenzahl && PowerOn >= 100 && BeepTime == 0 && delay > 1000)
190
			{
191
				BeepTime = 100;
192
				i++;
193
				delay = 0;
194
			}
195
			if(delay < 1500) delay++;
196
 
197
			// monitor battery undervoltage [...||(UBat<74) as temporary workaround to protect 2s lipo packs]
198
			if(((UBat < Zellenzahl * CELLUNDERVOLTAGE)||(UBat < 74)) && (PowerOn >= 100))
199
			{   // sound for low battery
200
				BeepModulation = 0x0300;
201
				if(!BeepTime)
202
				{
430 killagreg 203
					BeepTime = 6000; // 0.6 seconds
426 killagreg 204
				}
205
				Error |= ERROR_LOW_BAT;
206
			}
207
			else
208
			{
209
				Error &= ~ERROR_LOW_BAT;
210
			}
211
			#endif
212
			ADReady = 0;
213
			ADC_Enable(); // restart ad conversion sequence
214
		}
215
 
216
		// serial communication
217
		USART0_ProcessRxData();
218
		USART0_TransmitTxData();
219
 
220
		// indicate error, blinking code tbd.
221
		if(Error)	LEDRED_ON;
222
		else 		LEDRED_OFF;
223
 
224
    }
225
 	return (1);
226
}
227