Subversion Repositories Projects

Rev

Rev 303 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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