/
live.ml
1464 lines (1266 loc) · 54.8 KB
/
live.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(*
* Real time handling of flying A/Cs
*
* Copyright (C) 2004-2006 ENAC, Pascal Brisset, Antoine Drouin
*
* This file is part of paparazzi.
*
* paparazzi is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* paparazzi is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with paparazzi; see the file COPYING. If not, write to
* the Free Software Foundation, 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*
*)
module G = MapCanvas
open Latlong
module LL = Latlong
open Printf
module Tele_Pprz = Pprz.Messages(struct let name = "telemetry" end)
module Ground_Pprz = Pprz.Messages(struct let name = "ground" end)
module Alert_Pprz = Pprz.Messages(struct let name = "alert" end)
let (//) = Filename.concat
let gcs_id = "GCS"
let approaching_alert_time = 3.
let track_size = ref 500
let _auto_hide_fp = ref false
let min_height = 200
let lines_height = 30
let is_int = fun x ->
try let _ = int_of_string x in true with _ -> false
let ok_modes = ["MANUAL"; "AUTO1"; "AUTO2"]
let rotate = fun a (x, y) ->
let cosa = cos a and sina = sin a in
(cosa *.x +. sina *.y, -. sina*.x +. cosa *. y)
let rec list_casso x = function
[] -> raise Not_found
| (a,b)::abs -> if x = b then a else list_casso x abs
let rec list_iter3 = fun f l1 l2 l3 ->
match l1, l2, l3 with
[], [], [] -> ()
| x1::x1s, x2::x2s, x3::x3s ->
f x1 x2 x3;
list_iter3 f x1s x2s x3s
| _ -> invalid_arg "list_iter3"
type color = string
type aircraft = {
ac_name : string;
ac_speech_name : string;
config : Pprz.values;
track : MapTrack.track;
color: color;
fp_group : MapFP.flight_plan;
fp_show : GMenu.check_menu_item;
wp_HOME : MapWaypoints.waypoint option;
fp : Xml.xml;
blocks : (int * string) list;
mutable last_ap_mode : string;
mutable last_stage : int * int;
ir_page : Pages.infrared;
gps_page : Pages.gps;
pfd_page : Horizon.pfd;
link_page : Pages.link;
misc_page : Pages.misc;
dl_settings_page : Page_settings.settings option;
rc_settings_page : Pages.rc_settings option;
pages : GObj.widget;
notebook_label : GMisc.label;
strip : Strip.t;
mutable first_pos : bool;
mutable last_block_name : string;
mutable in_kill_mode : bool;
mutable speed : float;
mutable alt : float;
mutable target_alt : float;
mutable flight_time : int;
mutable wind_speed : float;
mutable wind_dir : float; (* Rad, clockwise from North *)
mutable ground_prox : bool;
mutable got_track_status_timer : int;
mutable last_dist_to_wp : float;
mutable dl_values : string option array;
mutable last_unix_time : float;
mutable airspeed : float;
mutable version : string;
}
let aircrafts = Hashtbl.create 3
exception AC_not_found
let find_ac = fun ac_id ->
try
Hashtbl.find aircrafts ac_id
with
Not_found -> raise AC_not_found
let active_ac = ref ""
let get_ac = fun vs ->
let ac_id = Pprz.string_assoc "ac_id" vs in
find_ac ac_id
let show_fp = fun ac ->
ac.fp_group#show ();
ac.fp_show#set_active true
let hide_fp = fun ac ->
ac.fp_group#hide ();
ac.fp_show#set_active false
(* callback for FP check button in menu *)
let show_mission = fun ac on_off ->
let a = find_ac ac in
if on_off then
a.fp_group#show ()
else
a.fp_group#hide ()
let auto_hide_fp = fun hide ->
let _hide_fp = fun () ->
Hashtbl.iter (fun _ a -> hide_fp a) aircrafts;
if !active_ac <> "" then begin
let a = find_ac !active_ac in
show_fp a
end;
in
_auto_hide_fp := hide;
if hide then _hide_fp () else Hashtbl.iter (fun _ a -> show_fp a) aircrafts
let select_ac = fun acs_notebook ac_id ->
if !active_ac <> ac_id then
let ac = Hashtbl.find aircrafts ac_id in
(* Show the buttons in the active strip and hide the previous active one *)
ac.strip#show_buttons ();
if !active_ac <> "" then begin
let ac' = find_ac !active_ac in
ac'.strip#hide_buttons ();
ac'.notebook_label#set_width_chars (String.length ac'.notebook_label#text);
if !_auto_hide_fp then hide_fp ac'
end;
(* Set the new active *)
active_ac := ac_id;
if !_auto_hide_fp then show_fp ac;
(* Select and enlarge the label of the A/C notebook *)
let n = acs_notebook#page_num ac.pages in
acs_notebook#goto_page n;
ac.notebook_label#set_width_chars 20;
module M = Map.Make (struct type t = string let compare = compare end)
let log =
let last = ref M.empty in
fun ?(say = false) (a:Pages.alert) ac_id s ->
if not (M.mem ac_id !last) || M.find ac_id !last <> s then begin
last := M.add ac_id s (M.remove ac_id !last);
if say then Speech.say s;
a#add s
end
let log_and_say = fun a ac_id s -> log ~say:true a ac_id s
let resize_track = fun ac track ->
match
GToolbox.input_string ~text:(string_of_int track#size) ~title:ac "Track size"
with
None -> ()
| Some s -> track#resize (int_of_string s)
let send_move_waypoint_msg = fun ac i w ->
let wgs84 = w#pos in
let vs = ["ac_id", Pprz.String ac;
"wp_id", Pprz.Int i;
"lat", Pprz.Float ((Rad>>Deg)wgs84.posn_lat);
"long", Pprz.Float ((Rad>>Deg)wgs84.posn_long);
"alt", Pprz.Float w#alt
] in
Ground_Pprz.message_send "gcs" "MOVE_WAYPOINT" vs
let commit_changes = fun ac ->
let a = find_ac ac in
List.iter
(fun w ->
let (i, w) = a.fp_group#index w in
if w#moved then
send_move_waypoint_msg ac i w)
a.fp_group#waypoints
let center = fun geomap track () ->
match track#last with
None -> ()
| Some geo ->
geomap#center geo;
geomap#canvas#misc#draw None
let blocks_of_stages = fun stages ->
let blocks = ref [] in
List.iter (fun x ->
let name = ExtXml.attrib x "block_name"
and id = ExtXml.int_attrib x "block" in
if not (List.mem_assoc id !blocks) then
blocks := (id, name) :: !blocks)
(Xml.children stages);
List.sort compare !blocks
let jump_to_block = fun ac_id id ->
Ground_Pprz.message_send "gcs" "JUMP_TO_BLOCK"
["ac_id", Pprz.String ac_id; "block_id", Pprz.Int id]
let dl_setting = fun ac_id idx value ->
let vs = ["ac_id", Pprz.String ac_id; "index", Pprz.Int idx;"value", Pprz.Float value] in
Ground_Pprz.message_send "dl" "DL_SETTING" vs
let get_dl_setting = fun ac_id idx ->
let vs = ["ac_id", Pprz.String ac_id; "index", Pprz.Int idx] in
Ground_Pprz.message_send "dl" "GET_DL_SETTING" vs
let menu_entry_of_block = fun ac_id (id, name) ->
let send_msg = fun () -> jump_to_block ac_id id in
`I (name, send_msg)
let reset_waypoints = fun fp () ->
List.iter (fun w ->
let (_i, w) = fp#index w in
w#reset_moved ())
fp#waypoints
let icon = ref None
let show_snapshot = fun (geomap:G.widget) geo_FL geo_BR point pixbuf name ev ->
match ev with
| `BUTTON_PRESS _ev ->
let image = GMisc.image ~pixbuf () in
let icon = image#coerce in
begin
match GToolbox.question_box ~title:name ~buttons:["Delete"; "Close"] ~icon "" with
1 ->
point#destroy ()
| _ -> ()
end;
true
| `LEAVE_NOTIFY _ev ->
begin
match !icon with
None -> ()
| Some i -> i#destroy ()
end;
false
| `ENTER_NOTIFY _ev ->
let w = GdkPixbuf.get_width pixbuf
and h = GdkPixbuf.get_height pixbuf in
icon := Some (geomap#display_pixbuf ((0,0), geo_FL) ((w,h), geo_BR) pixbuf);
point#raise_to_top ();
false
| _ -> false
let mark = fun (geomap:G.widget) ac_id track plugin_frame ->
let i = ref 1 in fun () ->
match track#last with
Some geo ->
begin
let group = geomap#background in
let point = geomap#circle ~group ~fill_color:"blue" geo 5. in
point#raise_to_top ();
let lat = (Rad>>Deg)geo.posn_lat
and long = (Rad>>Deg)geo.posn_long in
Tele_Pprz.message_send ac_id "MARK"
["ac_id", Pprz.String ac_id;
"lat", Pprz.Float lat;
"long", Pprz.Float long];
let frame =
match plugin_frame with
None -> geomap#canvas#coerce
| Some pf -> pf#coerce in
let width, height = Gdk.Drawable.get_size frame#misc#window in
let dest = GdkPixbuf.create width height() in
GdkPixbuf.get_from_drawable ~dest ~width ~height frame#misc#window;
let name = sprintf "Snapshot-%s-%d_%f_%f_%f.png" ac_id !i lat long (track#last_heading) in
let png = sprintf "%s/var/logs/%s" Env.paparazzi_home name in
GdkPixbuf.save png "png" dest;
incr i;
(* Computing the footprint: front_left and back_right *)
let cam_aperture = 2.4/.1.9 in (* width over distance FIXME *)
let alt = track#last_altitude -. float (Srtm.of_wgs84 geo) in
let width = cam_aperture *. alt in
let height = width *. 3. /. 4. in
let utm = utm_of WGS84 geo in
let a = (Deg>>Rad)track#last_heading in
let (xfl,yfl) = rotate a (-.width/.2., height/.2.)
and (xbr,ybr) = rotate a (width/.2., -.height/.2.) in
let geo_FL = of_utm WGS84 (utm_add utm (xfl,yfl))
and geo_BR = of_utm WGS84 (utm_add utm (xbr,ybr)) in
ignore (point#connect#event (show_snapshot geomap geo_FL geo_BR point dest name))
end
| None -> ()
(** Light display of attributes in the flight plan. *)
let attributes_pretty_printer = fun attribs ->
(* Remove the optional attributes *)
let valid = fun a ->
let a = String.lowercase a in
a <> "no" && a <> "strip_icon" && a <> "strip_button" && a <> "pre_call"
&& a <> "post_call" && a <> "key" && a <> "group" in
let sprint_opt = fun b s ->
if String.length b > 0 then
sprintf " %s%s%s" s b s
else
""
in
let elt = Xml.Element("", attribs, []) in
let pre_call = ExtXml.attrib_or_default elt "pre_call" ""
and post_call = ExtXml.attrib_or_default elt "post_call" "" in
let attribs = List.filter (fun (a, _) -> valid a) attribs in
(* Don't print the name of the attribute if there is only one *)
match attribs with
[(_, v)] -> v ^ sprint_opt pre_call "]" ^ sprint_opt post_call "["
| _ -> XmlEdit.string_of_attribs attribs
(** Load a mission. Returns the XML window *)
let load_mission = fun ?editable color geomap xml ->
Map2d.set_georef_if_none geomap (MapFP.georef_of_xml xml);
new MapFP.flight_plan ~format_attribs:attributes_pretty_printer ?editable ~show_moved:true geomap color Env.flight_plan_dtd xml
let get_bat_levels = fun af_xml ->
let default_catastrophic_level = 9.
and default_max_level = 12.5 in
try
let bat_section = ExtXml.child af_xml ~select:(fun x -> Xml.attrib x "name" = "BAT") "section" in
let fvalue = fun name default ->
try ExtXml.float_attrib (ExtXml.child bat_section ~select:(fun x -> ExtXml.attrib x "name" = name) "define") "value" with _ -> default in
fvalue "CATASTROPHIC_BAT_LEVEL" default_catastrophic_level, fvalue "MAX_BAT_LEVEL" default_max_level
with _ -> (default_catastrophic_level, default_max_level)
let get_alt_shift = fun af_xml ->
let default_plus_plus = 30.
and default_plus = 5.
and default_minus = -5. in
try
let gcs_section = ExtXml.child af_xml ~select:(fun x -> Xml.attrib x "name" = "GCS") "section" in
let fvalue = fun name default ->
try ExtXml.float_attrib (ExtXml.child gcs_section ~select:(fun x -> ExtXml.attrib x "name" = name) "define") "value" with _ -> default in
fvalue "ALT_SHIFT_PLUS_PLUS" default_plus_plus,
fvalue "ALT_SHIFT_PLUS" default_plus,
fvalue "ALT_SHIFT_MINUS" default_minus
with _ -> (default_plus_plus, default_plus, default_minus)
let get_speech_name = fun af_xml def_name ->
let default_speech_name = def_name in
try
let gcs_section = ExtXml.child af_xml ~select:(fun x -> Xml.attrib x "name" = "GCS") "section" in
let fvalue = fun name default ->
try ExtXml.attrib (ExtXml.child gcs_section ~select:(fun x -> ExtXml.attrib x "name" = name) "define") "value" with _ -> default in
fvalue "SPEECH_NAME" default_speech_name
with _ -> default_speech_name
let get_icon_and_track_size = fun af_xml ->
(* firmware name as default if fixedwing or rotorcraft *)
let firmware = ExtXml.child af_xml "firmware" in
let firmware_name = ExtXml.attrib firmware "name" in
try
(* search AC_ICON in GCS section *)
let gcs_section = ExtXml.child af_xml ~select:(fun x -> Xml.attrib x "name" = "GCS") "section" in
let ac_icon = ExtXml.child gcs_section ~select:(fun x -> ExtXml.attrib x "name" = "AC_ICON") "define" in
match ExtXml.attrib ac_icon "value" with
| "home" -> ("home", 1) (* no track for home icon *)
| x -> (x, !track_size)
with _ -> (firmware_name, !track_size)
let key_press_event = fun keys do_action ev ->
try
let (modifiers, action) = List.assoc (GdkEvent.Key.keyval ev) keys in
let ev_modifiers = GdkEvent.Key.state ev in
if List.for_all (fun m -> List.mem m ev_modifiers) modifiers then begin
do_action action;
true
end else
false
with
| _ -> false
(*****************************************************************************)
let create_ac = fun alert (geomap:G.widget) (acs_notebook:GPack.notebook) (ac_id:string) config ->
let color = Pprz.string_assoc "default_gui_color" config
and name = Pprz.string_assoc "ac_name" config in
(** Get the flight plan **)
let fp_url = Pprz.string_assoc "flight_plan" config in
let fp_file = Http.file_of_url fp_url in
let fp_xml_dump = ExtXml.parse_file ~noprovedtd:true fp_file in
let stages = ExtXml.child fp_xml_dump "stages" in
let blocks = blocks_of_stages stages in
(** Get the airframe file *)
let af_url = Pprz.string_assoc "airframe" config in
let af_file = Http.file_of_url af_url in
let af_xml = ExtXml.parse_file af_file in
(** Get an alternate speech name if available *)
let speech_name = get_speech_name af_xml name in
(* Aicraft menu decorated with a colored box *)
let image = GBin.event_box ~width:10 ~height:10 () in
image#coerce#misc#modify_bg [`NORMAL, `NAME color];
let ac_mi = GMenu.image_menu_item ~label:name ~image ~packing:geomap#menubar#append () in
let ac_menu = GMenu.menu () in
ac_mi#set_submenu ac_menu;
let ac_menu_fact = new GMenu.factory ac_menu in
let fp_show = ac_menu_fact#add_check_item "Fligh Plan" ~active:true in
ignore (fp_show#connect#toggled (fun () -> show_mission ac_id fp_show#active));
let (icon, size) = get_icon_and_track_size af_xml in
let track = new MapTrack.track ~size ~icon ~name ~color:color ac_id geomap in
track#set_event_cb (select_ac acs_notebook);
geomap#register_to_fit (track:>MapCanvas.geographic);
let center_ac = center geomap track in
ignore (ac_menu_fact#add_item "Center A/C" ~callback:center_ac);
ignore (ac_menu_fact#add_item "Clear Track" ~callback:(fun () -> track#clear_map2D));
ignore (ac_menu_fact#add_item "Resize Track" ~callback:(fun () -> resize_track ac_id track));
let reset_wp_menu = ac_menu_fact#add_item "Reset Waypoints" in
let jump_block_entries = List.map (menu_entry_of_block ac_id) blocks in
let commit_moves = fun () ->
commit_changes ac_id in
let sm = ac_menu_fact#add_submenu "Datalink" in
let dl_menu = [
`M ("Jump to block", jump_block_entries);
`I ("Commit Moves", commit_moves)] in
GToolbox.build_menu sm ~entries:dl_menu;
let cam = ac_menu_fact#add_check_item "Cam footprint" ~active:false in
ignore (cam#connect#toggled (fun () -> track#set_cam_state cam#active));
let params = ac_menu_fact#add_check_item "A/C label" ~active:false in
ignore (params#connect#toggled (fun () -> track#set_params_state params#active));
(** Add a new tab in the A/Cs notebook, with a colored label *)
let eb = GBin.event_box () in
let _label = GMisc.label ~text:name ~packing:eb#add () in
eb#coerce#misc#modify_bg [`NORMAL, `NAME color;`ACTIVE, `NAME color];
(** Put a notebook for this A/C *)
let ac_frame = GBin.frame () in
ignore (acs_notebook#append_page ~tab_label:eb#coerce ac_frame#coerce);
let ac_notebook = GPack.notebook ~packing: ac_frame#add () in
let visible = fun w ->
ac_notebook#page_num w#coerce = ac_notebook#current_page in
(** Add a strip *)
let min_bat, max_bat = get_bat_levels af_xml in
let alt_shift_plus_plus, alt_shift_plus, alt_shift_minus = get_alt_shift af_xml in
let param = { Strip.color = color; min_bat = min_bat; max_bat = max_bat;
alt_shift_plus_plus = alt_shift_plus_plus;
alt_shift_plus = alt_shift_plus;
alt_shift_minus = alt_shift_minus; } in
(*let strip = Strip.add config color min_bat max_bat in*)
let strip = Strip.add config param in
strip#connect (fun () -> select_ac acs_notebook ac_id);
strip#connect_mark (mark geomap ac_id track !Plugin.frame);
(** Build the XML flight plan, connect then "jump_to_block" *)
let fp_xml = ExtXml.child fp_xml_dump "flight_plan" in
let fp = load_mission ~editable:false color geomap fp_xml in
fp#connect_activated (fun node ->
if XmlEdit.tag node = "block" then
let block = XmlEdit.attrib node "name" in
let id = list_casso block blocks in
jump_to_block ac_id id);
ignore (reset_wp_menu#connect#activate (reset_waypoints fp));
(** Monitor waypoints changes *)
List.iter
(fun w ->
let (i, w) = fp#index w in
w#set_commit_callback (fun () -> send_move_waypoint_msg ac_id i w))
fp#waypoints;
(** Add waypoints as geo references *)
List.iter
(fun w ->
let (_i, w) = fp#index w in
geomap#add_info_georef (sprintf "%s.%s" name w#name) (w :> < pos : Latlong.geographic >))
fp#waypoints;
(** Add the short cut buttons in the strip *)
let tooltips = GData.tooltips () in
let keys = ref [] in (* Associations between keys and block ids *)
List.iter (fun block ->
let id = ExtXml.int_attrib block "no" in
begin (* Is it a key short cut ? *)
try
let key, modifiers = GtkData.AccelGroup.parse (Pprz.key_modifiers_of_string (Xml.attrib block "key")) in
keys := (key, (modifiers, id)) :: !keys
with
_ -> ()
end;
try (* Is it a strip button ? *)
let label = ExtXml.attrib block "strip_button"
and block_name = ExtXml.attrib block "name"
and group = ExtXml.attrib_or_default block "group" "" in
let b =
try (* Is it an icon ? *)
let icon = Xml.attrib block "strip_icon" in
let b = GButton.button () in
let pixbuf = GdkPixbuf.from_file (Env.gcs_icons_path // icon) in
ignore (GMisc.image ~pixbuf ~packing:b#add ());
(* Drag for Drop *)
let papget = Papget_common.xml "goto_block" "button"
[ "block_name", block_name;
"sender", ac_id;
"icon", icon] in
Papget_common.dnd_source b#coerce papget;
(* Associates the label as a tooltip *)
tooltips#set_tip b#coerce ~text:label;
b
with
Xml.No_attribute _ -> (* It's not an icon *)
GButton.button ~label ()
| exc ->
fprintf stderr "Error: '%s' Using a standard button" (Printexc.to_string exc);
GButton.button ~label ()
in
strip#add_widget b#coerce ~group;
ignore (b#connect#clicked (fun _ -> jump_to_block ac_id id))
with
_ -> ())
(Xml.children (ExtXml.child (ExtXml.child fp_xml_dump "flight_plan") "blocks"));
(** Handle key shortcuts for block selection *)
let key_press = key_press_event !keys (fun block_id -> jump_to_block ac_id block_id) in
ignore (geomap#canvas#event#connect#after#key_press key_press);
(** Insert the flight plan tab *)
let fp_label = GMisc.label ~text: "Flight Plan" () in
ignore ((ac_notebook:GPack.notebook)#append_page ~tab_label:fp_label#coerce fp#window#coerce);
let infrared_label = GMisc.label ~text: "Infrared" () in
let infrared_frame = GBin.frame ~show:false ~shadow_type:`NONE () in
ignore (ac_notebook#append_page ~tab_label: infrared_label#coerce infrared_frame#coerce);
let ir_page = new Pages.infrared infrared_frame in
let gps_label = GMisc.label ~text: "GPS" () in
let gps_frame = GBin.frame ~shadow_type: `NONE () in
ignore (ac_notebook#append_page ~tab_label: gps_label#coerce gps_frame#coerce);
let gps_page = new Pages.gps ~visible gps_frame in
let pfd_label = GMisc.label ~text: "PFD" () in
let pfd_frame = GBin.frame ~shadow_type: `NONE () in
ignore (ac_notebook#append_page ~tab_label: pfd_label#coerce pfd_frame#coerce);
let pfd_page = new Horizon.pfd pfd_frame
and _pfd_page_num = ac_notebook#page_num pfd_frame#coerce in
let link_label = GMisc.label ~text: "Link" () in
let link_frame = GBin.frame ~shadow_type: `NONE () in
ignore (ac_notebook#append_page ~tab_label: link_label#coerce link_frame#coerce);
let link_page = new Pages.link ~visible link_frame in
let misc_label = GMisc.label ~text: "Misc" () in
let misc_frame = GBin.frame ~shadow_type: `NONE () in
ignore (ac_notebook#append_page ~tab_label:misc_label#coerce misc_frame#coerce);
let misc_page = new Pages.misc ~packing:misc_frame#add misc_frame in
let settings_url = Pprz.string_assoc "settings" config in
let settings_file = Http.file_of_url settings_url in
let settings_xml =
try
if String.compare "replay" settings_file <> 0 then
ExtXml.parse_file ~noprovedtd:true settings_file
else
Xml.Element("empty", [], [])
with exc ->
prerr_endline (Printexc.to_string exc);
Xml.Element("empty", [], [])
in
let dl_setting_callback = fun idx value ->
if classify_float value = FP_normal || classify_float value = FP_zero then
dl_setting ac_id idx value
else
get_dl_setting ac_id idx
in
let dl_settings_page =
try
let xml_settings = Xml.children (ExtXml.child settings_xml "dl_settings") in
let settings_tab = new Page_settings.settings ~visible xml_settings dl_setting_callback (fun group x -> strip#add_widget ~group x) in
(** Connect key shortcuts *)
let key_press = fun ev ->
key_press_event settings_tab#keys (fun commit -> commit ()) ev in
ignore (geomap#canvas#event#connect#after#key_press key_press);
let tab_label = GPack.hbox () in
let _label = (GMisc.label ~text:"Settings" ~packing:tab_label#pack ()) in
let button_save_settings = GButton.button ~packing:tab_label#pack () in
ignore (GMisc.image ~stock:`SAVE ~packing:button_save_settings#add ());
button_save_settings#set_border_width 0;
ignore (button_save_settings#connect#clicked (fun () -> settings_tab#save af_file));
ignore (ac_notebook#append_page ~tab_label:tab_label#coerce settings_tab#widget);
Some settings_tab
with exc ->
log alert ac_id (Printexc.to_string exc);
None in
let rc_settings_page =
try
let xml_settings = Xml.children (ExtXml.child settings_xml "rc_settings") in
if xml_settings = [] then
raise Exit
else
let settings_tab = new Pages.rc_settings ~visible xml_settings in
let tab_label = (GMisc.label ~text:"RC Settings" ())#coerce in
ignore (ac_notebook#append_page ~tab_label settings_tab#widget);
Some settings_tab
with _ -> None in
let wp_HOME =
let rec loop = function
[] -> None
| w::ws ->
let (_i, w) = fp#index w in
if w#name = "HOME" then Some w else loop ws in
loop fp#waypoints in
let ac = { track = track; color = color; last_dist_to_wp = 0.;
fp_group = fp; fp_show = fp_show ; config = config ;
wp_HOME = wp_HOME; fp = fp_xml;
ac_name = name; ac_speech_name = speech_name;
blocks = blocks; last_ap_mode= "";
last_stage = (-1,-1);
ir_page = ir_page; flight_time = 0;
gps_page = gps_page;
pfd_page = pfd_page;
link_page = link_page;
misc_page = misc_page;
dl_settings_page = dl_settings_page;
rc_settings_page = rc_settings_page;
strip = strip; first_pos = true;
last_block_name = ""; alt = 0.; target_alt = 0.;
in_kill_mode = false; speed = 0.;
wind_dir = 42.; ground_prox = true;
wind_speed = 0.;
pages = ac_frame#coerce;
notebook_label = _label;
got_track_status_timer = 1000;
dl_values = [||]; last_unix_time = 0.;
airspeed = 0.;
version = ""
} in
Hashtbl.add aircrafts ac_id ac;
select_ac acs_notebook ac_id;
(** Periodically send the wind estimation through
a WIND_INFO message packed into a RAW_DATALINK *)
let send_wind = fun () ->
if misc_page#periodic_send then begin
(* FIXME: Disabling the timer would be preferable *)
try
let a = (pi/.2. -. ac.wind_dir)
and w = ac.wind_speed in
let wind_east = sprintf "%.1f" (-. cos a *. w)
and wind_north = sprintf "%.1f" (-. sin a *. w)
and airspeed = sprintf "%.1f" ac.airspeed in
let msg_items = ["WIND_INFO"; ac_id; "42"; wind_east; wind_north;airspeed] in
let value = String.concat ";" msg_items in
let vs = ["ac_id", Pprz.String ac_id; "message", Pprz.String value] in
Ground_Pprz.message_send "dl" "RAW_DATALINK" vs;
with
exc -> log alert ac_id (sprintf "send_wind (%s): %s" ac_id (Printexc.to_string exc))
end;
true
in
if is_int ac_id then
ignore (Glib.Timeout.add 10000 send_wind);
begin
match dl_settings_page with
Some settings_tab ->
(** Connect the strip buttons *)
let connect = fun ?(warning=true) setting_name strip_connect ->
try
let id = settings_tab#assoc setting_name in
strip_connect (fun x -> dl_setting_callback id x)
with Not_found ->
if warning then
fprintf stderr "Warning: %s not setable from GCS strip (i.e. not listed in the xml settings file)\n" setting_name in
connect "flight_altitude" (fun f -> ac.strip#connect_shift_alt (fun x -> f (ac.target_alt+.x)));
connect "launch" ~warning:false ac.strip#connect_launch;
connect "kill_throttle" ac.strip#connect_kill;
(* try to connect either pprz_mode (fixedwing) or autopilot_mode (rotorcraft) *)
connect "pprz_mode" ~warning:false (ac.strip#connect_mode 2.);
connect "autopilot_mode" ~warning:false (ac.strip#connect_mode 13.);
connect "nav_shift" ~warning:false ac.strip#connect_shift_lateral;
connect "autopilot_flight_time" ac.strip#connect_flight_time;
let get_ac_unix_time = fun () -> ac.last_unix_time in
connect ~warning:false "snav_desired_tow" (ac.strip#connect_apt get_ac_unix_time);
begin (* Periodically update the appointment *)
try
let id = settings_tab#assoc "snav_desired_tow" in
let set_appointment = fun _ ->
begin try
let v = match ac.dl_values.(id) with None -> raise Not_found | Some x -> int_of_string x in
let t = Unix.gmtime (Latlong.unix_time_of_tow v) in
ac.strip#set_label "apt" (sprintf "%d:%02d:%02d" t.Unix.tm_hour t.Unix.tm_min t.Unix.tm_sec)
with _ -> () end;
true
in
ignore (Glib.Timeout.add 1000 set_appointment)
with Not_found -> ()
end;
(** Connect the GPS reset button *)
begin
try
let gps_reset_id = settings_tab#assoc "gps.reset" in
gps_page#connect_reset
(fun x -> dl_setting_callback gps_reset_id (float x))
with Not_found -> ()
end
| None -> ()
end;
(* Monitor track status *)
let monitor_track_status = fun () ->
ac.got_track_status_timer <- ac.got_track_status_timer + 1;
if ac.got_track_status_timer > 5 then
ac.track#delete_desired_track ();
true in
ignore (Glib.Timeout.add 1000 monitor_track_status);;
(* since tcl8.6 "green" refers to "darkgreen" and the former "green" is now "lime", but that is not available in older versions, so hardcode the color to #00ff00*)
let ok_color = "#00ff00"
let warning_color = "orange"
let alert_color = "red"
(** Bind to message while catching all the esceptions of the callback *)
let safe_bind = fun msg cb ->
let safe_cb = fun sender vs ->
try cb sender vs with
AC_not_found -> () (* A/C not yet registed; silently ignore *)
| x -> fprintf stderr "%s: safe_bind (%s:%a): %s\n%!" Sys.argv.(0) msg (fun c vs -> List.iter (fun (_,v) -> fprintf c "%s " (Pprz.string_of_value v)) vs) vs (Printexc.to_string x) in
ignore (Ground_Pprz.message_bind msg safe_cb)
let alert_bind = fun msg cb ->
let safe_cb = fun sender vs ->
try cb sender vs with _ -> () in
ignore (Alert_Pprz.message_bind msg safe_cb)
let tele_bind = fun msg cb ->
let safe_cb = fun sender vs ->
try cb sender vs with
AC_not_found -> () (* A/C not yet registed; silently ignore *)
| x -> fprintf stderr "tele_bind (%s): %s\n%!" msg (Printexc.to_string x) in
ignore (Tele_Pprz.message_bind msg safe_cb)
let ask_config = fun alert geomap fp_notebook ac ->
let get_config = fun _sender values ->
if not (Hashtbl.mem aircrafts ac) then
create_ac alert geomap fp_notebook ac values
in
Ground_Pprz.message_req "gcs" "CONFIG" ["ac_id", Pprz.String ac] get_config
let one_new_ac = fun alert (geomap:G.widget) fp_notebook ac ->
if not (Hashtbl.mem aircrafts ac) then
ask_config alert geomap fp_notebook ac
let get_wind_msg = fun (geomap:G.widget) _sender vs ->
let ac = get_ac vs in
let value = fun field_name -> Pprz.float_assoc field_name vs in
let airspeed = value "mean_aspeed" in
ac.airspeed <- airspeed;
ac.strip#set_airspeed airspeed;
ac.misc_page#set_value "Mean airspeed" (sprintf "%.1f" airspeed);
ac.wind_speed <- value "wspeed";
let deg_dir = value "dir" in
ac.wind_dir <- (Deg>>Rad)deg_dir;
ac.misc_page#set_value "Wind speed" (sprintf "%.1f" ac.wind_speed);
ac.misc_page#set_value "Wind direction" (sprintf "%.1f" deg_dir);
let ac_id = Pprz.string_assoc "ac_id" vs in
if !active_ac = ac_id && ac.wind_speed > 1. then begin
geomap#wind_sock#set_color ac.color;
geomap#wind_sock#item#show ();
geomap#set_wind_sock deg_dir (sprintf "%.1f" ac.wind_speed)
end
let get_fbw_msg = fun alarm _sender vs ->
let ac = get_ac vs in
let status = Pprz.string_assoc "rc_status" vs
and rate = (Pprz.int_assoc "rc_rate" vs) / 5 in
(* divide by 5 to have normal values between 0 and 10 *)
(* RC rate max approx. 50 Hz *)
ac.strip#set_rc rate status;
let mode = Pprz.string_assoc "rc_mode" vs in
if mode = "FAILSAFE" then begin
log_and_say alarm ac.ac_name (sprintf "%s, mayday, AP Failure. Switch to manual." ac.ac_speech_name)
end
let get_telemetry_status = fun alarm _sender vs ->
let ac = get_ac vs in
let link_id = Pprz.string_assoc "link_id" vs in
(* Update color and lost time in the strip *)
let time_lost = Pprz.float_assoc "time_since_last_msg" vs in
let (links_up, total_links) = ac.link_page#links_ratio () in
let link_ratio_string =
if ac.link_page#multiple_links () then sprintf "%i/%i" links_up total_links else "" in
ac.strip#set_label "telemetry_status" (if time_lost > 2. then sprintf "%.0f" time_lost else link_ratio_string);
ac.strip#set_color "telemetry_status" (if time_lost > 5. then alert_color else if links_up < total_links then warning_color else ok_color);
(* Update link page *)
let rx_msgs_rate = Pprz.float_assoc "rx_bytes_rate" vs
and downlink_bytes_rate = Pprz.int_assoc "downlink_rate" vs
and uplink_lost_time = Pprz.int_assoc "uplink_lost_time" vs in
let ping_time = Pprz.float_assoc "ping_time" vs in
if (not (ac.link_page#link_exists link_id)) then begin
ac.link_page#add_link link_id;
log_and_say alarm ac.ac_name (sprintf "%s, new link detected: %s" ac.ac_speech_name link_id)
end;
let link_changed = ac.link_page#update_link link_id time_lost ping_time rx_msgs_rate downlink_bytes_rate uplink_lost_time in
let (links_up, _) = ac.link_page#links_ratio () in
match (link_changed, links_up) with
(_, 0) -> log_and_say alarm ac.ac_name (sprintf "%s, all links lost" ac.ac_speech_name)
| (Pages.Linkup, _)-> log_and_say alarm ac.ac_name (sprintf "%s, link %s re-connected" ac.ac_speech_name link_id)
| (Pages.Nochange, _) -> ()
| (Pages.Linkdown, _) -> log_and_say alarm ac.ac_name (sprintf "%s, link %s lost" ac.ac_speech_name link_id)
let get_engine_status_msg = fun _sender vs ->
let ac = get_ac vs in
ac.strip#set_throttle ~kill:ac.in_kill_mode (Pprz.float_assoc "throttle" vs);
ac.strip#set_bat (Pprz.float_assoc "bat" vs)
let get_if_calib_msg = fun _sender vs ->
let ac = get_ac vs in
match ac.rc_settings_page with
None -> ()
| Some p ->
p#set_rc_setting_mode (Pprz.string_assoc "if_mode" vs);
p#set (Pprz.float_assoc "if_value1" vs) (Pprz.float_assoc "if_value2" vs)
let listen_wind_msg = fun (geomap:G.widget) ->
safe_bind "WIND" (get_wind_msg geomap)
let listen_fbw_msg = fun a ->
safe_bind "FLY_BY_WIRE" (get_fbw_msg a)
let listen_engine_status_msg = fun () ->
safe_bind "ENGINE_STATUS" get_engine_status_msg
let listen_if_calib_msg = fun () ->
safe_bind "INFLIGH_CALIB" get_if_calib_msg
let listen_telemetry_status = fun a ->
safe_bind "TELEMETRY_STATUS" (get_telemetry_status a)
let list_separator = Str.regexp ","
let aircrafts_msg = fun alert (geomap:G.widget) fp_notebook acs ->
let acs = Pprz.string_assoc "ac_list" acs in
let acs = Str.split list_separator acs in
List.iter (one_new_ac alert geomap fp_notebook) acs
let listen_dl_value = fun () ->
let get_dl_value = fun _sender vs ->
let ac = get_ac vs in
match ac.dl_settings_page with
Some settings ->
let csv = Pprz.string_assoc "values" vs in
let string_value = fun v -> match v with "?" -> None | _ -> Some v in
let values = Array.map string_value (Array.of_list (Str.split list_separator csv)) in
ac.dl_values <- values;
for i = 0 to min (Array.length values) settings#length - 1 do
try
settings#set i values.(i)
with _ -> ()
done
| None -> () in
safe_bind "DL_VALUES" get_dl_value
let highlight_fp = fun ac b s ->
if (b, s) <> ac.last_stage then begin
ac.last_stage <- (b, s);
ac.fp_group#highlight_stage b s
end
let check_approaching = fun ac geo alert ->
match ac.track#last with
None -> ()
| Some ac_pos ->
let d = LL.wgs84_distance ac_pos geo in
if d < ac.speed *. approaching_alert_time then
log_and_say alert ac.ac_name (sprintf "%s, approaching" ac.ac_speech_name)
let ac_alt_graph = [14,0;-5,0;-7,-6]
let translate = fun l dx dy -> List.map (fun (x, y) -> (x + dx, y + dy)) l
let rotate_and_translate = fun l angle dx dy ->
translate (List.map (fun (x, y) -> (
truncate ((cos angle) *. (float x) -. (sin angle) *. (float y)),
truncate ((sin angle) *. (float x) +. (cos angle) *. (float y)))
) l) dx dy
let flip = fun l -> List.map (fun (x, y) -> (-x, y)) l
let draw_altgraph = fun (da_object:Gtk_tools.pixmap_in_drawin_area) (geomap:MapCanvas.widget) aircrafts ->
(** First estimate the coverage of the window *)
let width_c, height_c = Gdk.Drawable.get_size geomap#canvas#misc#window in
let (xc0, yc0) = geomap#canvas#get_scroll_offsets in
let (east, _y0) = geomap#window_to_world (float xc0) (float (yc0+height_c))
and (west, _y1) = geomap#window_to_world (float (xc0+width_c)) (float yc0) in
let da = da_object#drawing_area in
let width, height = Gdk.Drawable.get_size da#misc#window in
let dr = da_object#get_pixmap () in
dr#set_background `BLACK;
dr#set_foreground `BLACK;
(* Background *)
dr#rectangle ~x:0 ~y:0 ~width ~height ~filled:true ();
(* Text *)
let context = da#misc#create_pango_context in
let print_string = fun x y string color ->
let layout = context#create_layout in
let from_codeset = "ISO-8859-15"
and to_codeset = "UTF-8" in
Pango.Layout.set_text layout (Glib.Convert.convert ~from_codeset ~to_codeset string);
let (_w,h) = Pango.Layout.get_pixel_size layout in
dr#put_layout ~x ~y:(y-h) ~fore:(`NAME color) layout in
(* find min and max alt *)
let max_alt = ref 0
and min_alt = ref 35786000 in
Hashtbl.iter (fun _ac_id ac ->
let track = ac.track in
let alt = (truncate track#last_altitude) in
let ground_alt = alt - (truncate (track#height ())) in
if ground_alt < !min_alt then min_alt := ground_alt;
if alt > !max_alt then max_alt := alt
) aircrafts;
min_alt := min !min_alt !max_alt;
if (!min_alt mod lines_height) < (min 10 (lines_height / 2)) then
min_alt := (!min_alt / lines_height - 1) * lines_height
else
min_alt := (!min_alt / lines_height) * lines_height;
min_alt := max !min_alt 0;
let height_alt = max (!max_alt - !min_alt + 10) min_height in
(* lines *)
dr#set_foreground (`NAME "grey");
let n = height_alt / lines_height in
for i = 0 to n do