Subversion Repositories Projects

Rev

Rev 303 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 303 Rev 333
Line 10... Line 10...
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"
Line -... Line 17...
-
 
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;
Line 24... Line 27...
24
#endif
27
#endif
Line -... Line 28...
-
 
28
 
-
 
29
uint16_t Error = 0;
-
 
30
 
-
 
31
typedef enum
-
 
32
{
-
 
33
        STATE_UNDEFINED,
Line 25... Line 34...
25
 
34
        STATE_IDLE,
26
#define GPS_RX_TIMEOUT  0x0001
35
        STATE_SEND_FOLLOWME
27
 
36
} SysState_t;
28
 
37
 
Line 29... Line 38...
29
int main (void)
38
int main (void)
30
{
39
{
Line 31... Line 40...
31
        static uint16_t GPS_Timer = 0;
40
        static uint16_t FollowMe_Timer = 0;
Line 49... Line 58...
49
        ADC_Init();
58
        ADC_Init();
50
        Button_Init();
59
        Button_Init();
51
        // enable interrupts global
60
        // enable interrupts global
52
        sei();
61
        sei();
Line -... Line 62...
-
 
62
 
-
 
63
        Fat16_Init();
53
 
64
 
54
        LEDRED_OFF;
-
 
55
        #ifdef USE_FOLLOWME
65
        LEDRED_OFF;
56
        LEDGRN_ON;
-
 
Line 57... Line 66...
57
        #endif
66
        LEDGRN_ON;
58
 
67
 
Line 59... Line 68...
59
        // try to initialize the FAT 16 filesystem on the SD-Card
68
        // try to initialize the FAT 16 filesystem on the SD-Card
Line 68... Line 77...
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");
Line 71... Line -...
71
 
-
 
72
 
80
 
73
        #ifdef USE_FOLLOWME
-
 
Line 74... Line 81...
74
        //BeepTime = 2000;
81
 
Line 75... Line 82...
75
        #endif
82
        //BeepTime = 2000;
-
 
83
 
76
 
84
    LCD_Clear();
77
    LCD_Clear();
85
 
-
 
86
        FollowMe_Timer = SetDelay(FOLLOWME_INTERVAL);
-
 
87
 
-
 
88
        while (1)
78
 
89
        {
79
        GPS_Timer = SetDelay(1000);
90
                // get gps data to update the follow me position
80
        while (1)
91
                GPS_Update();
81
        {
92
 
-
 
93
                // check for button action and change state resectively
82
                // check for button action
94
                if(GetButton())
-
 
95
                {
-
 
96
                        BeepTime = 200;
-
 
97
 
-
 
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
 
Line 83... Line 108...
83
                if(GetButton())
108
                                default:
Line -... Line 109...
-
 
109
                                        SysState = STATE_IDLE;
-
 
110
                                        break;
-
 
111
                        }
-
 
112
 
-
 
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
84
                {
150
                                SysState = STATE_IDLE;
85
                        BeepTime = 200;
151
                                break;
86
                        Fat16_Init();
152
 
87
 
153
                }
88
                }
154
 
Line 105... Line 171...
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
 
-
 
Line 137... Line 195...
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
Line -... Line 213...
-
 
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
    }