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