1 |
/**** GPS skrysohledac ****/ |
1 |
/**** GPS skrysohledac ****/ |
2 |
#define ID "$Id: gps.c 984 2008-01-06 23:41:31Z kakl $" |
2 |
#define ID "$Id: gps.c 984 2008-01-06 23:41:31Z kakl $" |
3 |
|
3 |
|
4 |
#include "gpsrl.h" |
4 |
#include "gpsrl.h" |
5 |
|
5 |
|
6 |
#include <math.h> |
6 |
#include <math.h> |
7 |
#include <stdlib.h> |
7 |
#include <stdlib.h> |
8 |
#include <stdlibm.h> |
8 |
#include <stdlibm.h> |
9 |
|
9 |
|
- |
|
10 |
#define SCINTILAK 1 //kanal adc pro scintilacni detektor NB3201 |
- |
|
11 |
#define RANGE PIN_A4 // vystup detektoru urcujici rozsah mereni. |
- |
|
12 |
|
- |
|
13 |
|
10 |
#define LCD_RS PIN_B1 // LCD control |
14 |
#define LCD_RS PIN_B1 // LCD control |
11 |
#define LCD_E PIN_B2 // LCD enable |
15 |
#define LCD_E PIN_B2 // LCD enable |
12 |
#define LCD_DATA_LSB PIN_B4 // LSB data bit LCD |
16 |
#define LCD_DATA_LSB PIN_B4 // LSB data bit LCD |
13 |
#include "MYLCD.C" |
17 |
#include "MYLCD.C" |
14 |
|
18 |
|
- |
|
19 |
#define TL1 PIN_C0 |
- |
|
20 |
#define TL2 PIN_C1 |
- |
|
21 |
#define TL3 PIN_C2 |
- |
|
22 |
|
15 |
// NMEA |
23 |
// NMEA |
16 |
#define TIME 6 // For NMEA parsing |
24 |
#define TIME 6 // For NMEA parsing |
17 |
#define TIME_LEN 10 |
25 |
#define TIME_LEN 10 |
18 |
#define LATDEG 19 |
26 |
#define LATDEG 19 |
19 |
#define LATMIN 21 |
27 |
#define LATMIN 21 |
20 |
#define LONDEG 32 |
28 |
#define LONDEG 32 |
21 |
#define LONMIN 34 |
29 |
#define LONMIN 34 |
- |
|
30 |
#define SAT 36 |
22 |
#define DEG_LEN 2 |
31 |
#define DEG_LEN 2 |
23 |
#define MIN_LEN 7 |
32 |
#define MIN_LEN 7 |
24 |
#define AZIMUTH 46 |
33 |
#define AZIMUTH 46 |
25 |
#define NMEA_LINESIZE 60 |
34 |
#define NMEA_LINESIZE 79 |
26 |
|
35 |
|
27 |
#define MAXGC 4 |
36 |
//$GPRMC,105815.503,V,4915.5877,N,01442.3896,E,0.0,0.00,060108,,,N*44 |
28 |
struct geocache |
- |
|
29 |
{ |
- |
|
30 |
float lat; |
- |
|
31 |
float lon; |
- |
|
32 |
char name[8]; |
- |
|
33 |
}; |
- |
|
34 |
|
37 |
|
35 |
struct geocache const gc[MAXGC] = |
38 |
void main() |
36 |
{ |
39 |
{ |
37 |
{49*60+15.5740,14*60+42.3310,"DOMA"}, // Testovaci souradnice: cisticka |
- |
|
38 |
{48*60+57.966,14*60+28.655,"GC16JMH"}, |
- |
|
39 |
{48*60+57.837,14*60+28.599,"GC16F8Y"}, |
- |
|
40 |
{49*60+9.542,14*60+56.417,"GC-----"} |
- |
|
41 |
}; |
- |
|
42 |
|
40 |
|
- |
|
41 |
setup_adc_ports(AN0); |
- |
|
42 |
setup_adc(ADC_CLOCK_INTERNAL); |
- |
|
43 |
setup_spi(FALSE); |
43 |
float lat,lon; // Latitude, Longitude, Azimuth |
44 |
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1); |
- |
|
45 |
setup_timer_1(T1_DISABLED); |
- |
|
46 |
setup_timer_2(T2_DISABLED,0,1); |
- |
|
47 |
setup_comparator(NC_NC_NC_NC); |
- |
|
48 |
setup_vref(VREF_LOW|-2); |
44 |
int16 az; |
49 |
lcd_init(); |
- |
|
50 |
|
- |
|
51 |
delay_ms(100); |
- |
|
52 |
lcd_gotoxy(5,1); |
- |
|
53 |
printf(lcd_putc,"GPSRL01B \n"); |
45 |
int8 gcnum; // Selection of the GC point |
54 |
printf(lcd_putc,"(c) Kaklik 2008"); |
- |
|
55 |
delay_ms(300); |
- |
|
56 |
printf(lcd_putc,"\f"); |
46 |
|
57 |
|
47 |
//$GPRMC,105815.503,V,4915.5877,N,01442.3896,E,0.0,0.00,060108,,,N*44 |
58 |
set_adc_channel(SCINTILAK); |
- |
|
59 |
|
48 |
inline void read_NMEA() // NMEA parsing |
60 |
while(TRUE) |
49 |
{ |
61 |
{ |
- |
|
62 |
|
50 |
auto char line[NMEA_LINESIZE]; |
63 |
auto char line[NMEA_LINESIZE]; |
51 |
auto char item[16]; |
64 |
char item[16]; |
52 |
auto char *ptr; |
65 |
char *ptr; |
53 |
// auto char *line; |
- |
|
54 |
auto int8 n; |
66 |
int8 n; |
55 |
auto char c; |
67 |
char c; |
- |
|
68 |
|
56 |
|
69 |
|
57 |
// line=malloc(NMEA_LINESIZE); // Space for one line |
70 |
// line=malloc(NMEA_LINESIZE); // Space for one line |
58 |
// if (line==NULL) {printf(lcd_putc,"Error"); sleep();}; |
71 |
// if (line==NULL) {printf(lcd_putc,"Error"); sleep();}; |
59 |
line[NMEA_LINESIZE-1]=0; |
72 |
line[NMEA_LINESIZE-1]=0; |
60 |
|
73 |
|
61 |
while(fgetc(NMEA)!='$'); // Waiting for start character |
74 |
while(fgetc(NMEA)!='$'); // Waiting for start character |
62 |
for(n=0;n<(NMEA_LINESIZE-1);n++) // Read line up to checksum |
75 |
for(n=0;n<(NMEA_LINESIZE-1);n++) // Read line up to checksum |
63 |
{ |
76 |
{ |
64 |
c=fgetc(NMEA); |
77 |
c=fgetc(NMEA); |
65 |
if(c=='*') |
78 |
if(c=='*') |
66 |
{ |
79 |
{ |
67 |
line[n]=0; |
80 |
line[n]=0; |
68 |
break; |
81 |
break; |
69 |
}; |
82 |
}; |
70 |
line[n]=c; |
83 |
line[n]=c; |
71 |
}; |
84 |
}; |
72 |
|
85 |
|
73 |
lcd_gotoxy(12,4); |
86 |
printf(lcd_putc,"\f"); |
74 |
printf(lcd_putc,"%c %06.0g ",line[17],strtod(&line[TIME],&ptr)); |
- |
|
75 |
|
87 |
|
- |
|
88 |
if(!input(TL1)) |
- |
|
89 |
{ |
76 |
lcd_gotoxy(1,3); |
90 |
lcd_gotoxy(1,1); |
77 |
strncpy(item,&line[LATDEG],DEG_LEN+MIN_LEN); item[DEG_LEN+MIN_LEN]=0; |
91 |
strncpy(item,&line[LATDEG],DEG_LEN+MIN_LEN); item[DEG_LEN+MIN_LEN]=0; |
78 |
printf(lcd_putc,"%s*",item); |
92 |
printf(lcd_putc,"%s \n",item); |
- |
|
93 |
|
79 |
strncpy(item,&line[LONDEG],DEG_LEN+MIN_LEN); item[DEG_LEN+MIN_LEN]=0; |
94 |
strncpy(item,&line[LONDEG],DEG_LEN+MIN_LEN); item[DEG_LEN+MIN_LEN]=0; |
80 |
printf(lcd_putc,"%s*",item); |
95 |
printf(lcd_putc,"%s",item); |
81 |
|
96 |
|
82 |
// lcd_gotoxy(1,2); |
- |
|
83 |
lat=strtod(&line[LATMIN],&ptr); |
- |
|
84 |
line[LATMIN]=0; |
- |
|
85 |
lat+=60*strtod(&line[LATDEG],&ptr); |
- |
|
86 |
lon=strtod(&line[LONMIN],&ptr); |
- |
|
87 |
line[LONMIN]=0; |
- |
|
88 |
lon+=60*strtod(&line[LONDEG],&ptr); |
- |
|
89 |
strtod(&line[AZIMUTH],&ptr); |
- |
|
90 |
az=strtoul(ptr+1,&ptr,10); |
- |
|
91 |
// printf(lcd_putc,"%.3g*%.3g*%03Lu",lat,lon,az); |
- |
|
92 |
// free(line); |
- |
|
93 |
} |
97 |
} |
94 |
|
- |
|
95 |
void main() |
- |
|
96 |
{ |
98 |
else |
97 |
|
- |
|
98 |
setup_adc_ports(NO_ANALOGS); |
- |
|
99 |
setup_adc(ADC_OFF); |
- |
|
100 |
setup_spi(FALSE); |
- |
|
101 |
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1); |
- |
|
102 |
setup_timer_1(T1_DISABLED); |
- |
|
103 |
setup_timer_2(T2_DISABLED,0,1); |
- |
|
104 |
setup_comparator(NC_NC_NC_NC); |
- |
|
105 |
setup_vref(VREF_LOW|-2); |
- |
|
106 |
lcd_init(); |
- |
|
107 |
|
- |
|
108 |
gcnum++; // Next GC |
- |
|
109 |
if (gcnum>(MAXGC-1)) gcnum=0; |
- |
|
110 |
|
- |
|
111 |
delay_ms(200); |
- |
|
112 |
lcd_gotoxy(1,2); |
- |
|
113 |
printf(lcd_putc,"%s",gc[gcnum].name); // Print GC name |
- |
|
114 |
|
- |
|
115 |
while(TRUE) |
- |
|
116 |
{ |
99 |
{ |
- |
|
100 |
lcd_gotoxy(1,1); |
- |
|
101 |
printf(lcd_putc,"Time %06.0g \n",strtod(&line[TIME],&ptr)); |
117 |
read_NMEA(); // Read one NMEA line and parse |
102 |
printf(lcd_putc,"sats %06.0g ",strtod(&line[SAT],&ptr)); |
- |
|
103 |
} |
118 |
|
104 |
|
119 |
lon=(gc[gcnum].lon-lon)*1214; // Kakona's projection :) |
- |
|
120 |
lat=(gc[gcnum].lat-lat)*1854; |
105 |
puts(line,PC); |
121 |
|
106 |
|
122 |
lcd_gotoxy(1,4); |
- |
|
123 |
printf(lcd_putc,"%.0gm ",sqrt((lon*lon) + (lat*lat))); // Distance |
- |
|
124 |
lcd_gotoxy(1,1); |
- |
|
125 |
if (lon==0) lon=0.001; // Divided by zero cure |
- |
|
126 |
lon=3-(6/PI*atan2(lat,lon)); // Bearing |
- |
|
127 |
if (lon<0) lon+=12; // overflow cure |
- |
|
128 |
if (lon>12) lon-=12; |
107 |
// free(line); |
129 |
printf(lcd_putc,"BE%2.0gh*",lon); |
- |
|
130 |
lat=12.0/360*az; // Azimuth of the movement |
- |
|
131 |
lon=lon-lat; // Heading |
- |
|
132 |
if (lon<0) lon+=12; // overflow cure |
- |
|
133 |
if (lon>12) lon-=12; |
- |
|
134 |
printf(lcd_putc,"HE%2.0gh*AZ%2.0gh*",lon,lat); |
- |
|
135 |
} |
108 |
} |
136 |
} |
109 |
} |
137 |
|
110 |
|