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