Rev 211 | Go to most recent revision | Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
206 | ligi | 1 | /********************************************* |
2 | * |
||
3 | * class representing the DebugData Structure |
||
4 | * |
||
5 | * Author: Marcus -LiGi- Bueschleb |
||
6 | * |
||
7 | * see README for further Infos |
||
8 | * |
||
9 | * Some code taken from here: |
||
10 | * http://www.koders.com/java/fidFC75A641A87B51BB757E9CD3136C7886C491487F.aspx |
||
11 | * |
||
12 | * and |
||
13 | * http://www.movable-type.co.uk/scripts/latlong.html |
||
14 | * |
||
15 | * thanx a lot for sharing! |
||
16 | * |
||
17 | ********************************************/ |
||
18 | |||
19 | package org.ligi.ufo; |
||
20 | |||
21 | |||
22 | import java.lang.Math; |
||
23 | public class MKGPSPosition |
||
24 | { |
||
25 | public final byte GPS_FORMAT_DECIMAL=0; |
||
26 | public final byte GPS_FORMAT_MINSEC=1; |
||
27 | public final byte GPS_FORMAT_COUNT=2; |
||
28 | |||
29 | byte act_gps_format=GPS_FORMAT_DECIMAL; |
||
30 | |||
31 | public final static int MAX_WAYPOINTS=100; |
||
32 | |||
33 | public int[] LongWP; |
||
34 | public int[] LatWP; |
||
35 | public String[] NameWP; |
||
36 | |||
37 | int UBatt; |
||
38 | |||
39 | public int last_wp=0; |
||
40 | |||
41 | public int Longitude; |
||
42 | public int Latitude; |
||
43 | public int Altitude; |
||
44 | |||
45 | public int TargetLongitude; |
||
46 | public int TargetLatitude; |
||
47 | public int TargetAltitude; |
||
48 | |||
49 | public int HomeLongitude; |
||
50 | public int HomeLatitude; |
||
51 | public int HomeAltitude; |
||
52 | |||
53 | public int Distance2Target; |
||
54 | public int Angle2Target; |
||
55 | |||
56 | public int Distance2Home; |
||
57 | public int Angle2Home; |
||
58 | |||
59 | public byte SatsInUse=-1; |
||
60 | public byte WayPointNumber=-1; |
||
61 | public byte WayPointIndex=-1; |
||
62 | |||
63 | public int AngleNick = -1; |
||
64 | public int AngleRoll = -1; |
||
65 | public int SenderOkay = -1; |
||
66 | public int MKFlags= -1; |
||
67 | public int NCFlags= -1; |
||
68 | public int ErrorCode= -1; |
||
69 | |||
70 | |||
71 | |||
72 | public int Altimeter=-1; // hight according to air pressure |
||
73 | public int Variometer=-1; // climb(+) and sink(-) rate |
||
74 | public int FlyingTime=-1; |
||
75 | |||
76 | public int GroundSpeed=-1; |
||
77 | public int Heading=-1; |
||
78 | public int CompasHeading=-1; |
||
79 | |||
80 | |||
81 | |||
82 | //#if cldc11=="on" |
||
83 | public static final double PI = Math.PI; |
||
84 | public static final double PI_div2 = PI / 2.0; |
||
85 | public static final double PI_div4 = PI / 4.0; |
||
86 | public static final double RADIANS = PI / 180.0; |
||
87 | public static final double DEGREES = 180.0 / PI; |
||
88 | |||
89 | private static final double p4 = 0.161536412982230228262e2; |
||
90 | private static final double p3 = 0.26842548195503973794141e3; |
||
91 | private static final double p2 = 0.11530293515404850115428136e4; |
||
92 | private static final double p1 = 0.178040631643319697105464587e4; |
||
93 | private static final double p0 = 0.89678597403663861959987488e3; |
||
94 | private static final double q4 = 0.5895697050844462222791e2; |
||
95 | private static final double q3 = 0.536265374031215315104235e3; |
||
96 | private static final double q2 = 0.16667838148816337184521798e4; |
||
97 | private static final double q1 = 0.207933497444540981287275926e4; |
||
98 | private static final double q0 = 0.89678597403663861962481162e3; |
||
99 | |||
100 | |||
101 | |||
102 | private static double _ATAN(double X) |
||
103 | { |
||
104 | if (X < 0.414213562373095048802) |
||
105 | return _ATANX(X); |
||
106 | else if (X > 2.414213562373095048802) |
||
107 | return PI_div2 - _ATANX(1.0 / X); |
||
108 | else |
||
109 | return PI_div4 + _ATANX((X - 1.0) / (X + 1.0)); |
||
110 | } |
||
111 | |||
112 | |||
113 | private static double _ATANX(double X) |
||
114 | { |
||
115 | double XX = X * X; |
||
116 | return X * ((((p4 * XX + p3) * XX + p2) * XX + p1) * XX + p0)/ (((((XX + q4) * XX + q3) * XX + q2) * XX + q1) * XX + q0); |
||
117 | } |
||
118 | |||
119 | |||
120 | |||
121 | public double aTan2(double Y, double X) |
||
122 | { |
||
123 | |||
124 | if (X == 0.0) { |
||
125 | if (Y > 0.0) |
||
126 | return PI_div2; |
||
127 | |||
128 | else if (Y < 0.0) |
||
129 | return -PI_div2; |
||
130 | else |
||
131 | return 0.0; |
||
132 | } |
||
133 | |||
134 | // X<0 |
||
135 | else if (X < 0.0) { |
||
136 | if (Y >= 0.0) |
||
137 | return (PI - _ATAN(Y / -X)); // Y>=0,X<0 |Y/X| |
||
138 | else |
||
139 | return -(PI - _ATAN(Y / X)); // Y<0,X<0 |Y/X| |
||
140 | |||
141 | } |
||
142 | |||
143 | // X>0 |
||
144 | else if (X > 0.0) |
||
145 | { |
||
146 | if (Y > 0.0) |
||
147 | return _ATAN(Y / X); |
||
148 | else |
||
149 | return -_ATAN(-Y / X); |
||
150 | |||
151 | } |
||
152 | |||
153 | return 0.0; |
||
154 | |||
155 | } |
||
156 | |||
157 | public int distance2wp(int id) |
||
158 | { |
||
159 | double lat1=(Latitude/10000000.0)*RADIANS; |
||
160 | double long1=(Longitude/10000000.0)*RADIANS; |
||
161 | |||
162 | double lat2=(LatWP[id]/10000000.0)*RADIANS; |
||
163 | double long2=(LongWP[id]/10000000.0)*RADIANS; |
||
164 | |||
165 | |||
166 | double dLat= (lat2-lat1); |
||
167 | double dLon= (long2-long1); |
||
168 | |||
169 | double a = Math.sin(dLat/2.0) * Math.sin(dLat/2.0) + |
||
170 | Math.cos(lat1) * Math.cos(lat2) * |
||
171 | Math.sin(dLon/2.0) * Math.sin(dLon/2.0); |
||
172 | |||
173 | return (int)(( 2.0 * aTan2(Math.sqrt(a), Math.sqrt(1.0-a)) )*6371008.8); |
||
174 | } |
||
175 | |||
176 | |||
177 | |||
178 | |||
179 | public int angle2wp(int id) |
||
180 | { |
||
181 | // TODO reuse from distance |
||
182 | double lat1=(Latitude/10000000.0)*RADIANS; |
||
183 | double long1=(Longitude/10000000.0)*RADIANS; |
||
184 | |||
185 | double lat2=(LatWP[id]/10000000.0)*RADIANS; |
||
186 | double long2=(LongWP[id]/10000000.0)*RADIANS; |
||
187 | |||
188 | |||
189 | double dLat= (lat2-lat1); |
||
190 | double dLon= (long2-long1); |
||
191 | |||
192 | |||
193 | |||
194 | double y = Math.sin(dLon) * Math.cos(lat2); |
||
195 | double x = Math.cos(lat1)*Math.sin(lat2) - Math.sin(lat1)*Math.cos(lat2)*Math.cos(dLon); |
||
196 | return ((int)(aTan2(y, x)*DEGREES)+360)%360; |
||
197 | |||
198 | } |
||
199 | |||
200 | |||
201 | //#endif |
||
202 | |||
203 | //#if cldc11!="on" |
||
204 | //# public int distance2wp(int id) |
||
205 | //# { |
||
206 | //# return -1; |
||
207 | //# } |
||
208 | |||
209 | //# public int angle2wp(int id) |
||
210 | //# { |
||
211 | //# return -1; |
||
212 | //# } |
||
213 | //#endif |
||
214 | |||
215 | public void push_wp() |
||
216 | { |
||
217 | LongWP[last_wp]=Longitude; |
||
218 | LatWP[last_wp]=Latitude; |
||
219 | |||
220 | last_wp++; |
||
221 | } |
||
222 | |||
223 | public void next_gps_format() |
||
224 | { |
||
225 | act_gps_format=(byte)((act_gps_format+1)%GPS_FORMAT_COUNT); |
||
226 | } |
||
227 | |||
228 | public String act_gps_format_str(int val) |
||
229 | { |
||
230 | switch(act_gps_format) |
||
231 | { |
||
232 | case GPS_FORMAT_DECIMAL: |
||
233 | return "" + val/10000000 + "." +val%10000000 ; |
||
234 | case GPS_FORMAT_MINSEC: |
||
235 | return "" + val/10000000 + "^" + ((val%10000000)*60)/10000000 + "'" + ((((val%10000000)*60)%10000000)*60)/10000000 + "." + ((((val%10000000)*60)%10000000)*60)%10000000; |
||
236 | default: |
||
237 | return "invalid format"; |
||
238 | } |
||
239 | } |
||
240 | |||
241 | public String WP_Latitude_str(int id) |
||
242 | { |
||
243 | |||
244 | return act_gps_format_str(LatWP[id]); //+ "''N" ; |
||
245 | } |
||
246 | |||
247 | public String WP_Longitude_str(int id) |
||
248 | { |
||
249 | return act_gps_format_str(LongWP[id]); //+ "''E" ; |
||
250 | |||
251 | } |
||
252 | |||
253 | public String Latitude_str() |
||
254 | { |
||
255 | |||
256 | return act_gps_format_str(Latitude) ; |
||
257 | } |
||
258 | |||
259 | public String Longitude_str() |
||
260 | { |
||
261 | return act_gps_format_str(Longitude) ; |
||
262 | |||
263 | } |
||
264 | |||
265 | |||
266 | public String TargetLatitude_str() |
||
267 | { |
||
268 | return act_gps_format_str(TargetLatitude) ; |
||
269 | } |
||
270 | |||
271 | public String TargetLongitude_str() |
||
272 | { |
||
273 | return act_gps_format_str(TargetLongitude) ; |
||
274 | } |
||
275 | |||
276 | public String HomeLatitude_str() |
||
277 | { |
||
278 | |||
279 | return act_gps_format_str(HomeLatitude) ; |
||
280 | } |
||
281 | |||
282 | public String HomeLongitude_str() |
||
283 | { |
||
284 | return act_gps_format_str(HomeLongitude) ; |
||
285 | } |
||
286 | |||
287 | |||
288 | |||
289 | |||
290 | // Constructor |
||
291 | public MKGPSPosition() |
||
292 | { |
||
293 | |||
294 | LongWP=new int[MAX_WAYPOINTS]; |
||
295 | LatWP=new int[MAX_WAYPOINTS]; |
||
296 | |||
297 | |||
298 | NameWP=new String[MAX_WAYPOINTS]; |
||
299 | // predefined waypoints |
||
300 | |||
301 | /* |
||
302 | LongWP[0]=123230170; |
||
303 | LatWP[0]= 513600170 ; |
||
304 | NameWP[0]="Sicherer PC1"; |
||
305 | |||
306 | LongWP[1]=123269000; |
||
307 | LatWP[1]= 513662670; |
||
308 | NameWP[1]="Sicherer PC2"; |
||
309 | |||
310 | LongWP[2]=123475570; |
||
311 | LatWP[2]= 513569750 ; |
||
312 | NameWP[2]="Treffpunkt Seba"; |
||
313 | */ |
||
314 | |||
315 | last_wp=0; |
||
316 | } |
||
317 | |||
318 | private int parse_arr_4(int offset,int[] in_arr) |
||
319 | { |
||
320 | return ((in_arr[offset+3]<<24) | |
||
321 | (in_arr[offset+2]<<16) | |
||
322 | (in_arr[offset+1]<<8) | |
||
323 | (in_arr[offset+0])); |
||
324 | } |
||
325 | |||
326 | |||
327 | private int parse_arr_2(int offset,int[] in_arr) |
||
328 | { |
||
329 | return ((in_arr[offset+1]<<8) | |
||
330 | (in_arr[offset+0])); |
||
331 | } |
||
332 | |||
333 | |||
334 | |||
335 | public void set_by_mk_data(int[] in_arr,MKVersion version) |
||
336 | { |
||
337 | Longitude=parse_arr_4(0,in_arr); |
||
338 | Latitude=parse_arr_4(4,in_arr); |
||
339 | Altitude=parse_arr_4(8,in_arr); |
||
340 | //status=in_arr[12]; |
||
341 | |||
342 | TargetLongitude=parse_arr_4(13,in_arr); |
||
343 | TargetLatitude=parse_arr_4(17,in_arr); |
||
344 | TargetAltitude=parse_arr_4(21,in_arr); |
||
345 | //Targetstatus=in_arr[25]; |
||
346 | |||
347 | Distance2Target=parse_arr_2(26,in_arr); |
||
348 | Angle2Target=parse_arr_2(28,in_arr); |
||
349 | |||
350 | HomeLongitude=parse_arr_4(30,in_arr); |
||
351 | HomeLatitude=parse_arr_4(34,in_arr); |
||
352 | HomeAltitude=parse_arr_4(38,in_arr); |
||
353 | //Targetstatus=in_arr[42]; |
||
354 | |||
355 | Distance2Home=parse_arr_2(43,in_arr); |
||
356 | Angle2Home=parse_arr_2(45,in_arr); |
||
357 | |||
358 | WayPointIndex=(byte)in_arr[47]; |
||
359 | WayPointNumber=(byte)in_arr[48]; |
||
360 | |||
361 | SatsInUse=(byte)in_arr[49]; |
||
362 | |||
363 | |||
364 | Altimeter=parse_arr_2(50,in_arr); // hight according to air pressure |
||
365 | Variometer=parse_arr_2(52,in_arr);; // climb(+) and sink(-) rate |
||
366 | FlyingTime=parse_arr_2(54,in_arr);; |
||
367 | |||
368 | UBatt= in_arr[56]; |
||
369 | |||
370 | GroundSpeed= parse_arr_2(57,in_arr); |
||
371 | Heading= parse_arr_2(59,in_arr); |
||
372 | CompasHeading= parse_arr_2(61,in_arr); |
||
373 | |||
374 | AngleNick = in_arr[63]; |
||
375 | AngleRoll = in_arr[64]; |
||
376 | SenderOkay = in_arr[65]; |
||
377 | |||
378 | MKFlags=in_arr[66]; |
||
379 | NCFlags=in_arr[67]; |
||
380 | |||
381 | ErrorCode=in_arr[67]; |
||
382 | |||
383 | |||
384 | // ground_speed 54 / 55 |
||
385 | /* |
||
386 | if (version.compare(0,11)==version.VERSION_PREVIOUS) |
||
387 | { |
||
388 | |||
389 | TargetLongitude=parse_arr(8,in_arr); |
||
390 | TargetLatitude=parse_arr(12,in_arr); |
||
391 | Distance2Target=parse_arr(16,in_arr); |
||
392 | Angle2Target=parse_arr(20,in_arr); |
||
393 | Used_Sat=(byte)in_arr[24]; |
||
394 | } |
||
395 | else |
||
396 | { |
||
397 | |||
398 | Longitude=parse_arr(0,in_arr); |
||
399 | Latitude=parse_arr(4,in_arr); |
||
400 | |||
401 | TargetLongitude=parse_arr(13,in_arr); |
||
402 | TargetLatitude=parse_arr(17,in_arr); |
||
403 | |||
404 | Distance2Target=-23 ; //parse_arr(16,in_arr); |
||
405 | Angle2Target=parse_arr(20,in_arr); |
||
406 | |||
407 | WayPointNumber=-1; |
||
408 | WayPointIndex=-1; |
||
409 | Used_Sat=(byte)in_arr[24]; |
||
410 | |||
411 | |||
412 | |||
413 | } |
||
414 | */ |
||
415 | } |
||
416 | |||
417 | |||
418 | |||
419 | } |