Subversion Repositories HomeAutomation

Rev

Rev 226 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 226 Rev 227
1
/**
1
/**
2
 * CanGPS.
2
 * CanGPS.
-
 
3
 *
-
 
4
 * Messages: <byte0><byte1>...<byte7>
-
 
5
 * $GPGGA: <GGA_MSG><UTC><Latitude><Latitude dec.><N/S><Longitude><Longitude dec.><E/W>
-
 
6
 * $GPVTG: <VTG_MSG><Course true><Course dec. true><Course magnetic><Course dec. magnetic><Speed km/h><Speed dec. km/h>
-
 
7
 * $GPZDA: <ZDA_MSG><hh><mm><ss><day><month><year>
3
 *
8
 *
4
 * @date    2001-01-09
9
 * @date    2001-01-09
5
 * @author  Erik Larsson
10
 * @author  Erik Larsson
6
 */
11
 */
7
 
12
 
8
#define DEBUG           0 /* Prints debug messages to uart, only use this function if output is not connected to GPS */
13
#define DEBUG           0 /* Prints debug messages to uart, only use this function if output is not connected to GPS */
9
 
14
 
10
/*-----------------------------------------------------------------------------
15
/*-----------------------------------------------------------------------------
11
 * Includes
16
 * Includes
12
 *---------------------------------------------------------------------------*/
17
 *---------------------------------------------------------------------------*/
13
/* system files */
18
/* system files */
14
#include <avr/io.h>
19
#include <avr/io.h>
15
#include <avr/interrupt.h>
20
#include <avr/interrupt.h>
16
#include <avr/wdt.h>
21
#include <avr/wdt.h>
17
#include <stdio.h>
22
#include <stdio.h>
18
#include <string.h>
23
#include <string.h>
19
/* lib files */
24
/* lib files */
20
#include <can.h>
25
#include <can.h>
21
#include <serial.h>
26
#include <serial.h>
22
#include <uart.h>
27
#include <uart.h>
23
#include <timebase.h>
28
#include <timebase.h>
24
 
29
 
25
/* defines */
30
/* defines */
26
#define GPS_CMD_GET     0x1400
31
#define GPS_CMD_GET     0x1400
27
#define GPS_CMD_SEND    0x1401
32
#define GPS_CMD_SEND    0x1401
28
#define STATUS_SEND_PERIOD 500 /* milliseconds */
33
#define STATUS_SEND_PERIOD 500 /* milliseconds */
-
 
34
 
-
 
35
#define GGA_MSG     0x01
-
 
36
#define VTG_MSG     0x02
-
 
37
#define ZDA_MSG     0x03
-
 
38
#define GGA_MSG_BYTES 8
-
 
39
#define VTG_MSG_BYTES 7
-
 
40
#define ZDA_MSG_BYTES 7
29
 
41
 
30
#define TRUE 1
42
#define TRUE 1
31
#define FALSE !TRUE
43
#define FALSE !TRUE
32
#define CR 0x0D
44
#define CR 0x0D
33
#define LF 0x0A
45
#define LF 0x0A
-
 
46
#define GPS_MSG_DELIMITER ","
34
 
47
 
35
/*-------------------------------------------------------------------------
48
/*-------------------------------------------------------------------------
36
 * Global variables
49
 * Global variables
37
 * -----------------------------------------------------------------------*/
50
 * -----------------------------------------------------------------------*/
38
 
51
 
39
/*----------------------------------------------------------------------------
52
/*----------------------------------------------------------------------------
40
 * Functions
53
 * Functions
41
 * -------------------------------------------------------------------------*/
54
 * -------------------------------------------------------------------------*/
42
 
55
 
43
/*----------------------------------------------------------------------------
56
/*----------------------------------------------------------------------------
44
 * Interrupt routines
57
 * Interrupt routines
45
 * -------------------------------------------------------------------------*/
58
 * -------------------------------------------------------------------------*/
46
ISR( PCINT2_vect )
59
ISR( PCINT2_vect )
47
{
60
{
48
}
61
}
49
 
62
 
50
/*-----------------------------------------------------------------------------
63
/*-----------------------------------------------------------------------------
51
 * Main Program
64
 * Main Program
52
 *---------------------------------------------------------------------------*/
65
 *---------------------------------------------------------------------------*/
53
int main(void) {
66
int main(void) {
54
 
-
 
55
    uint32_t timeStamp = 0, timeStampTurn = 0;
67
    uint32_t timeStamp = 0, timeStampTurn = 0;
56
    uint8_t buffering = FALSE, gpsMsg_received = FALSE, rxGps_element;
68
    uint8_t buffering = FALSE, gpsMsg_received = FALSE, rxGps_element;
57
    char buffer[100], rxGps;
69
    char buffer[100], rxGps;
-
 
70
    /* GPGGA, Global positioning system fix data */
-
 
71
    char gga_utc[7], gga_latitude[9], gga_latitude_NS, gga_longitude[10], gga_longitude_EW;
-
 
72
    /* GPVTG, Course over ground and speed */
-
 
73
    char vtg_course_true[6], vtg_course_magnetic[6], vtg_speed_kmh[6];
-
 
74
    /* GPZDA, Time & Date */
-
 
75
    char zda_time[7], zda_day[3], zda_month[3], zda_year[5];
-
 
76
 
-
 
77
    char *pch;
58
 
78
 
59
    Timebase_Init();
79
    Timebase_Init();
60
    Serial_Init();
80
    Serial_Init();
61
 
81
 
62
    sei();
82
    sei();
63
#if DEBUG
83
#if DEBUG
64
    printf("\n------------------------------------------------------------\n");
84
    printf("\n------------------------------------------------------------\n");
65
    printf(  "   CAN: GPS\n");
85
    printf(  "   CAN: GPS\n");
66
    printf(  "------------------------------------------------------------\n");
86
    printf(  "------------------------------------------------------------\n");
67
 
87
 
68
    printf("CanInit...");
88
    printf("CanInit...");
69
    if (Can_Init() != CAN_OK) {
89
    if (Can_Init() != CAN_OK) {
70
        printf("FAILED!\n");
90
        printf("FAILED!\n");
71
    }else{
91
    }else{
72
        printf("OK!\n");
92
        printf("OK!\n");
73
    }
93
    }
74
#else
94
#else
75
    Can_Init();
95
    Can_Init();
76
#endif
96
#endif
77
 
97
 
78
    Can_Message_t txMsg;
98
    Can_Message_t txMsg;
79
    Can_Message_t rxMsg;
99
    Can_Message_t rxMsg;
80
    txMsg.Id = GPS_CMD_SEND;
100
    txMsg.Id = GPS_CMD_SEND;
81
    txMsg.RemoteFlag = 0;
101
    txMsg.RemoteFlag = 0;
82
    txMsg.ExtendedFlag = 1;
102
    txMsg.ExtendedFlag = 1;
83
    txMsg.DataLength = 3;
103
    txMsg.DataLength = 3;
84
 
104
 
85
    /* main loop */
105
    /* main loop */
86
    while (1) {
106
    while (1) {
87
        /* service the CAN routines */
107
        /* service the CAN routines */
88
        Can_Service();
108
        Can_Service();
89
 
109
 
90
        /* check if any messages have been received */
110
        /* check if any messages have been received */
91
        while (Can_Receive(&rxMsg) == CAN_OK) {
111
        while (Can_Receive(&rxMsg) == CAN_OK) {
92
            /* This node that control a servo is adressed */
112
            /* This node that control a servo is adressed */
93
            if( rxMsg.Id == GPS_CMD_GET ){
113
            if( rxMsg.Id == GPS_CMD_GET ){
94
 
114
 
95
            }
115
            }
96
        }
116
        }
97
 
117
 
98
        if( Timebase_PassedTimeMillis(timeStamp) >= STATUS_SEND_PERIOD ){
118
        if( Timebase_PassedTimeMillis(timeStamp) >= STATUS_SEND_PERIOD ){
99
            timeStamp = Timebase_CurrentTime();
119
            timeStamp = Timebase_CurrentTime();
100
            /* Send blinds status to CAN */
120
            /* Send blinds status to CAN */
101
 
121
 
102
        }
122
        }
103
 
123
 
104
        /* Get data from GPS */
124
        /* Get data from GPS */
105
        rxGps = uart_getc();
125
        rxGps = uart_getc();
106
        while( rxGps != UART_NO_DATA ){
126
        while( rxGps != UART_NO_DATA ){
107
            /* '$' indicates start of message, start buffering */
127
            /* '$' indicates start of message, start buffering */
108
            if( rxGps == '$' ){
128
            if( rxGps == '$' ){
109
                buffering = TRUE;
129
                buffering = TRUE;
110
                rxGps_element = 0;
130
                rxGps_element = 0;
111
            }else if( rxGps == CR ){
131
            }else if( rxGps == CR ){
112
                /* End of message */
132
                /* End of message */
113
                buffer[rxGps_element]='\0';
133
                buffer[rxGps_element]='\0';
114
                buffering = FALSE;
134
                buffering = FALSE;
115
                gpsMsg_received = TRUE;
135
                gpsMsg_received = TRUE;
116
            }
136
            }
117
 
137
 
118
            if( buffering ){
138
            if( buffering ){
119
                buffer[ rxGps_element ] = rxGps;
139
                buffer[ rxGps_element ] = rxGps;
120
                rxGps_element++;
140
                rxGps_element++;
121
            }
141
            }
122
            /* Get next character */
142
            /* Get next character */
123
            rxGps = uart_getc();
143
            rxGps = uart_getc();
124
        }
144
        }
125
 
145
 
126
        if( gpsMsg_received ){
146
        if( gpsMsg_received ){
127
            /* One whole msg is completed, start compare and parse */
147
            /* One whole msg is completed, start compare and parse */
128
            if( !strncmp(buffer, "GPGGA", 5) ){
148
            if( !strncmp(buffer, "GPGGA", 5) ){
129
                /* Standard NMEA message, $GPGGA */
149
                /* Standard NMEA message, $GPGGA */
-
 
150
                strtok(buffer, GPS_MSG_DELIMITER); /* Throw away "GPGGA" */
-
 
151
 
-
 
152
                /* Get UTC */
-
 
153
                pch = strtok(NULL, GPS_MSG_DELIMITER);
-
 
154
                strcpy(gga_utc, pch);
-
 
155
 
-
 
156
                /* Get latitude */
-
 
157
                pch = strtok(NULL, GPS_MSG_DELIMITER);
-
 
158
                strcpy(gga_latitude, pch);
-
 
159
 
-
 
160
                /* Get latitude N/S */
-
 
161
                pch = strtok(NULL, GPS_MSG_DELIMITER);
-
 
162
                strcpy(gga_latitude_NS, pch);
-
 
163
 
-
 
164
                /* Get longitude */
-
 
165
                pch = strtok(NULL, GPS_MSG_DELIMITER);
-
 
166
                strcpy(gga_longitude, pch);
-
 
167
 
-
 
168
                /* Get longitude E/W */
-
 
169
                pch = strtok(NULL, GPS_MSG_DELIMITER);
-
 
170
                strcpy(gga_longitude_EW, pch);
130
 
171
 
131
            }else if( !strncmp(buffer, "GPVTG", 5) ){
172
            }else if( !strncmp(buffer, "GPVTG", 5) ){
132
                /* Standard NMEA message, $GPVTG */
173
                /* Standard NMEA message, $GPVTG */
-
 
174
                strtok(buffer, GPS_MSG_DELIMITER); /* Throw away "GPVTG" */
133
 
175
 
-
 
176
                /* Get course over ground, true degree */
-
 
177
                pch = strtok(NULL, GPS_MSG_DELIMITER);
-
 
178
                strcpy(vtg_course_true, pch);
-
 
179
 
-
 
180
                /* Get course over ground, magnetic degree */
-
 
181
                pch = strtok(NULL, GPS_MSG_DELIMITER);
-
 
182
                strcpy(vtg_course_magnetic, pch);
-
 
183
 
-
 
184
                /* Get speed over ground, km/h */
-
 
185
                pch = strtok(NULL, GPS_MSG_DELIMITER);
-
 
186
                strcpy(vtg_speed_kmh, pch);
-
 
187
 
134
            }else if( !strncmp(buffer, "GPZDA", 5) ){
188
            }else if( !strncmp(buffer, "GPZDA", 5) ){
135
                /* Standard NMEA message, $GPZDA */
189
                /* Standard NMEA message, $GPZDA */
-
 
190
                strtok(buffer, GPS_MSG_DELIMITER); /* Throw away "GPZDA" */
-
 
191
 
-
 
192
                /* Get time */
-
 
193
                pch = strtok(NULL, GPS_MSG_DELIMITER);
-
 
194
                strcpy(zda_time, pch);
-
 
195
 
-
 
196
                /* Get day, 0-31 */
-
 
197
                pch = strtok(NULL, GPS_MSG_DELIMITER);
-
 
198
                strcpy(zda_day, pch);
-
 
199
 
-
 
200
                /* Get month, 1-12 */
-
 
201
                pch = strtok(NULL, GPS_MSG_DELIMITER);
-
 
202
                strcpy(zda_month, pch);
-
 
203
 
-
 
204
                /* Get year */
-
 
205
                pch = strtok(NULL, GPS_MSG_DELIMITER);
-
 
206
                strcpy(zda_year, pch);
136
            }
207
            }
137
 
208
 
138
            gpsMsg_received = FALSE; /* Get ready for next message */
209
            gpsMsg_received = FALSE; /* Get ready for next message */
139
 
210
 
140
        }
211
        }
141
    }
212
    }
142
    return 0;
213
    return 0;
143
}
214
}
144
 
215
 
145
 
216