mirror of
https://github.com/paparazzi/paparazzi.git
synced 2026-05-21 11:55:41 +08:00
Merge pull request #1204 from paparazzi/dynamic_sectors
Dynamic sectors close #643 and also close #605 because it is working on concave hulls as well
This commit is contained in:
@@ -0,0 +1,83 @@
|
||||
<!DOCTYPE flight_plan SYSTEM "flight_plan.dtd">
|
||||
|
||||
<flight_plan alt="260" ground_alt="185" lat0="43.4622" lon0="1.2729" max_dist_from_home="1500" name="Dynamic sectors demo" qfu="270" security_height="25" home_mode_height="50">
|
||||
<header>
|
||||
#include "subsystems/datalink/datalink.h"
|
||||
</header>
|
||||
<waypoints>
|
||||
<waypoint name="HOME" x="0" y="0"/>
|
||||
<waypoint name="STDBY" x="20" y="80"/>
|
||||
<waypoint name="1" x="44.8" y="102.2"/>
|
||||
<waypoint alt="215.0" name="AF" x="200" y="-10"/>
|
||||
<waypoint alt="185.0" name="TD" x="80.0" y="20.0"/>
|
||||
<waypoint name="BASELEG" x="26.9" y="-23.0"/>
|
||||
<waypoint name="C1" x="-100" y="0"/>
|
||||
<waypoint name="C2" x="-100" y="200"/>
|
||||
<waypoint name="C3" x="100" y="200"/>
|
||||
<waypoint name="C4" x="100" y="0"/>
|
||||
<waypoint name="CLIMB" x="-122.5" y="35.4"/>
|
||||
</waypoints>
|
||||
<sectors>
|
||||
<sector name="Square" type="dynamic">
|
||||
<corner name="C1"/>
|
||||
<corner name="C2"/>
|
||||
<corner name="C3"/>
|
||||
<corner name="C4"/>
|
||||
</sector>
|
||||
</sectors>
|
||||
<exceptions/>
|
||||
<blocks>
|
||||
<block name="Wait GPS">
|
||||
<set value="1" var="kill_throttle"/>
|
||||
<while cond="!GpsFixValid()"/>
|
||||
</block>
|
||||
<block name="Geo init">
|
||||
<while cond="LessThan(NavBlockTime(), 10)"/>
|
||||
<call fun="NavSetGroundReferenceHere()"/>
|
||||
<!--call fun="NavSetAltitudeReferenceHere()"/-->
|
||||
</block>
|
||||
<block name="Holding point">
|
||||
<set value="1" var="kill_throttle"/>
|
||||
<attitude roll="0" throttle="0" vmode="throttle"/>
|
||||
</block>
|
||||
<block name="Takeoff" strip_icon="takeoff.png" strip_button="Takeoff (wp CLIMB)">
|
||||
<exception cond="GetPosAlt() > GetAltRef()+25" deroute="Standby"/>
|
||||
<set value="0" var="kill_throttle"/>
|
||||
<go wp="CLIMB"/>
|
||||
</block>
|
||||
<block name="Standby" strip_button="Standby" strip_icon="home.png">
|
||||
<circle radius="nav_radius" wp="STDBY"/>
|
||||
</block>
|
||||
<block name="Fly in Square">
|
||||
<exception cond="! InsideSquare(GetPosX(), GetPosY())" deroute="Come back wp 1"/>
|
||||
<attitude alt="GetAltRef()+75" roll="0" vmode="alt"/>
|
||||
</block>
|
||||
<block name="Come back wp 1">
|
||||
<exception cond="InsideSquare(GetPosX(), GetPosY())" deroute="Fly in Square"/>
|
||||
<go wp="1"/>
|
||||
<deroute block="Fly in Square"/>
|
||||
</block>
|
||||
<block name="Land Right AF-TD" strip_button="Land right (wp AF-TD)" strip_icon="land-right.png">
|
||||
<set value="DEFAULT_CIRCLE_RADIUS" var="nav_radius"/>
|
||||
<deroute block="land"/>
|
||||
</block>
|
||||
<block name="Land Left AF-TD" strip_button="Land left (wp AF-TD)" strip_icon="land-left.png">
|
||||
<set value="-DEFAULT_CIRCLE_RADIUS" var="nav_radius"/>
|
||||
<deroute block="land"/>
|
||||
</block>
|
||||
<block name="land">
|
||||
<call fun="nav_compute_baseleg(WP_AF, WP_TD, WP_BASELEG, nav_radius)"/>
|
||||
<circle radius="nav_radius" until="NavCircleCount() > 0.5" wp="BASELEG"/>
|
||||
<set value="V_CTL_AUTO_THROTTLE_MIN_CRUISE_THROTTLE" var="v_ctl_auto_throttle_cruise_throttle"/>
|
||||
<circle radius="nav_radius" until="NavQdrCloseTo(DegOfRad(baseleg_out_qdr)-(nav_radius/fabs(nav_radius))*10) && 10 > fabs(GetPosAlt() - WaypointAlt(WP_BASELEG))" wp="BASELEG"/>
|
||||
</block>
|
||||
<block name="final">
|
||||
<exception cond="GetAltRef() + 10 > GetPosAlt()" deroute="flare"/>
|
||||
<go from="AF" hmode="route" vmode="glide" wp="TD"/>
|
||||
</block>
|
||||
<block name="flare">
|
||||
<go approaching_time="0" from="AF" hmode="route" throttle="0.0" vmode="throttle" wp="TD"/>
|
||||
<attitude roll="0.0" throttle="0.0" until="FALSE" vmode="throttle"/>
|
||||
</block>
|
||||
</blocks>
|
||||
</flight_plan>
|
||||
@@ -77,7 +77,8 @@ height CDATA #IMPLIED>
|
||||
|
||||
<!ATTLIST sector
|
||||
name CDATA #REQUIRED
|
||||
color CDATA #IMPLIED>
|
||||
color CDATA #IMPLIED
|
||||
type CDATA #IMPLIED>
|
||||
|
||||
<!ATTLIST corner
|
||||
name CDATA #REQUIRED>
|
||||
|
||||
@@ -1332,7 +1332,8 @@ let listen_waypoint_moved = fun () ->
|
||||
try
|
||||
let w = ac.fp_group#get_wp wp_id in
|
||||
w#set_ground_alt ground_alt;
|
||||
w#set ~altitude ~update:true geo
|
||||
w#set ~altitude ~update:true geo;
|
||||
ac.fp_group#update_sectors w#name
|
||||
with
|
||||
Not_found -> () (* Silently ignore unknown waypoints *)
|
||||
in
|
||||
|
||||
+36
-9
@@ -180,9 +180,11 @@ let georef_of_xml = fun xml ->
|
||||
|
||||
let display_lines = fun ?group color (geomap:MapCanvas.widget) points ->
|
||||
let n = Array.length points in
|
||||
let l = ref [] in
|
||||
for i = 0 to n - 1 do
|
||||
ignore (geomap#segment ?group ~width:3 ~fill_color:color points.(i) points.((i+1)mod n))
|
||||
done
|
||||
l := !l @ [(geomap#segment ?group ~width:3 ~fill_color:color points.(i) points.((i+1)mod n))]
|
||||
done;
|
||||
!l
|
||||
|
||||
let space_regexp = Str.regexp " "
|
||||
let comma_regexp = Str.regexp ","
|
||||
@@ -209,7 +211,7 @@ let display_kml = fun ?group color geomap xml ->
|
||||
[Xml.PCData text] ->
|
||||
let points = Str.split space_regexp text in
|
||||
let points = List.map wgs84_of_kml_point points in
|
||||
display_lines ?group color geomap (Array.of_list points)
|
||||
ignore(display_lines ?group color geomap (Array.of_list points))
|
||||
| _ -> failwith "coordinates expected"
|
||||
end
|
||||
|
||||
@@ -257,14 +259,16 @@ class flight_plan = fun ?format_attribs ?editable ~show_moved geomap color fp_dt
|
||||
w in
|
||||
|
||||
(* The sectors *)
|
||||
let _ =
|
||||
(* Parse sectors and store dynamic ones *)
|
||||
let sectors =
|
||||
let waypoints = ExtXml.child xml "waypoints" in
|
||||
try
|
||||
List.iter (fun x ->
|
||||
List.fold_left (fun l x ->
|
||||
match String.lowercase (Xml.tag x) with
|
||||
"kml" ->
|
||||
let file = ExtXml.attrib x "file" in
|
||||
display_kml ~group:wpts_group#group color geomap (ExtXml.parse_file (Env.flight_plans_path // file))
|
||||
display_kml ~group:wpts_group#group color geomap (ExtXml.parse_file (Env.flight_plans_path // file));
|
||||
l
|
||||
| "sector" ->
|
||||
let wgs84 = fun wp_name ->
|
||||
let wp_name = Xml.attrib wp_name "name" in
|
||||
@@ -275,10 +279,15 @@ class flight_plan = fun ?format_attribs ?editable ~show_moved geomap color fp_dt
|
||||
let points = List.map wgs84 (Xml.children x) in
|
||||
let points = Array.of_list points in
|
||||
let color_sector = ExtXml.attrib_or_default x "color" color in
|
||||
display_lines ~group:wpts_group#group color_sector geomap points
|
||||
let segments = display_lines ~group:wpts_group#group color_sector geomap points in
|
||||
let wp_names = List.map (fun wp -> Xml.attrib wp "name") (Xml.children x) in
|
||||
if ExtXml.attrib_or_default x "type" "" = "dynamic" then
|
||||
[(wp_names, segments, color_sector)] @ l
|
||||
else
|
||||
l
|
||||
| _ -> failwith "Unknown sectors child")
|
||||
(Xml.children (ExtXml.child xml "sectors"))
|
||||
with Not_found -> () in
|
||||
[] (Xml.children (ExtXml.child xml "sectors"))
|
||||
with Not_found -> [] in
|
||||
|
||||
(* The waypoints *)
|
||||
let _ = List.iter
|
||||
@@ -368,6 +377,24 @@ object
|
||||
|
||||
method connect_activated = fun cb -> XmlEdit.connect_activated xml_tree_view cb
|
||||
|
||||
method update_sectors = fun wp_name ->
|
||||
List.iter (fun (wps_name, segments, color) ->
|
||||
let wp_in_sector = List.exists (fun name -> name = wp_name) wps_name in
|
||||
if wp_in_sector then begin
|
||||
(* Build WP array *)
|
||||
let points = List.map (fun n -> let (_, w) = Hashtbl.find yaws n in w#pos) wps_name in
|
||||
let points = Array.of_list points in
|
||||
let segments = Array.of_list segments in
|
||||
let n = Array.length points in
|
||||
(* Update segments *)
|
||||
for i = 0 to n - 1 do
|
||||
let (x1, y1) = geomap#world_of points.(i)
|
||||
and (x2, y2) = geomap#world_of (points.((i+1)mod n)) in
|
||||
segments.(i)#set [`POINTS [|x1; y1; x2; y2 |]]
|
||||
done
|
||||
end
|
||||
) sectors
|
||||
|
||||
initializer (
|
||||
(** Create a graphic waypoint when it is created from the xml editor *)
|
||||
XmlEdit.connect xml_wpts (function XmlEdit.New_child node -> ignore (create_wp node) | _ -> ())
|
||||
|
||||
@@ -45,6 +45,7 @@ class flight_plan :
|
||||
method insert_path : (MapWaypoints.waypoint * float) list -> unit
|
||||
method highlight_stage : int -> int -> unit
|
||||
method connect_activated : (XmlEdit.node->unit) -> unit
|
||||
method update_sectors : string -> unit
|
||||
end
|
||||
|
||||
(** Extracts [lat0] and [Lon0] attributes *)
|
||||
|
||||
@@ -698,8 +698,8 @@ let dummy_waypoint =
|
||||
[])
|
||||
|
||||
|
||||
|
||||
let print_inside_polygon = fun pts ->
|
||||
let (_, pts) = List.split pts in
|
||||
let layers = Geometry_2d.slice_polygon (Array.of_list pts) in
|
||||
let rec f = fun i j ->
|
||||
if i = j then
|
||||
@@ -723,25 +723,51 @@ let print_inside_polygon = fun pts ->
|
||||
in
|
||||
f 0 (Array.length layers - 1);;
|
||||
|
||||
|
||||
|
||||
let print_inside_sector = fun (s, pts) ->
|
||||
lprintf "static inline bool_t %s(float _x, float _y) { \\\n" (inside_function s);
|
||||
let print_inside_polygon_global = fun pts ->
|
||||
lprintf "uint8_t i, j;\n";
|
||||
lprintf "bool_t c = FALSE;\n";
|
||||
(* build array of wp id *)
|
||||
let (ids, _) = List.split pts in
|
||||
lprintf "const uint8_t nb_pts = %d;\n" (List.length pts);
|
||||
lprintf "const uint8_t wps_id[] = { %s };\n\n" (String.concat ", " ids);
|
||||
(* start algo *)
|
||||
lprintf "for (i = 0, j = nb_pts - 1; i < nb_pts; j = i++) {\n";
|
||||
right ();
|
||||
print_inside_polygon pts;
|
||||
lprintf "if (((WaypointY(wps_id[i]) > _y) != (WaypointY(wps_id[j]) > _y)) &&\n";
|
||||
lprintf " (_x < (WaypointX(wps_id[j])-WaypointX(wps_id[i])) * (_y-WaypointY(wps_id[i])) / (WaypointY(wps_id[j])-WaypointY(wps_id[i])) + WaypointX(wps_id[i]))) {\n";
|
||||
right ();
|
||||
lprintf "if (c == TRUE) { c = FALSE; } else { c = TRUE; }\n";
|
||||
left();
|
||||
lprintf "}\n";
|
||||
left();
|
||||
lprintf "}\n";
|
||||
lprintf "return c;\n"
|
||||
|
||||
|
||||
type sector_type = StaticSector | DynamicSector
|
||||
|
||||
let print_inside_sector = fun t (s, pts) ->
|
||||
lprintf "static inline bool_t %s(float _x, float _y) {\n" (inside_function s);
|
||||
right ();
|
||||
begin
|
||||
match t with
|
||||
| StaticSector -> print_inside_polygon pts
|
||||
| DynamicSector -> print_inside_polygon_global pts
|
||||
end;
|
||||
left ();
|
||||
lprintf "}\n"
|
||||
|
||||
|
||||
let parse_wpt_sector = fun waypoints xml ->
|
||||
let parse_wpt_sector = fun indexes waypoints xml ->
|
||||
let sector_name = ExtXml.attrib xml "name" in
|
||||
let p2D_of = fun x ->
|
||||
let name = name_of x in
|
||||
try
|
||||
let wp = List.find (fun wp -> name_of wp = name) waypoints in
|
||||
let i = get_index_waypoint name indexes in
|
||||
let x = float_attrib wp "x"
|
||||
and y = float_attrib wp "y" in
|
||||
{G2D.x2D = x; G2D.y2D = y }
|
||||
(i, {G2D.x2D = x; G2D.y2D = y })
|
||||
with
|
||||
Not_found -> failwith (sprintf "Error: corner '%s' of sector '%s' not found" name sector_name)
|
||||
in
|
||||
@@ -870,16 +896,18 @@ let () =
|
||||
Xml2h.define "HOME_MODE_HEIGHT" (sof home_mode_height);
|
||||
Xml2h.define "MAX_DIST_FROM_HOME" (sof mdfh);
|
||||
|
||||
lprintf "\n#ifdef NAV_C\n\n";
|
||||
|
||||
let index_of_waypoints =
|
||||
let i = ref (-1) in
|
||||
List.map (fun w -> incr i; (name_of w, !i)) waypoints in
|
||||
|
||||
let sectors_element = try ExtXml.child xml "sectors" with Not_found -> Xml.Element ("", [], []) in
|
||||
let sectors = List.filter (fun x -> String.lowercase (Xml.tag x) = "sector") (Xml.children sectors_element) in
|
||||
let sectors = List.map (parse_wpt_sector waypoints) sectors in
|
||||
List.iter print_inside_sector sectors;
|
||||
let sectors_type = List.map (fun x -> match ExtXml.attrib_or_default x "type" "static" with "dynamic" -> DynamicSector | _ -> StaticSector) sectors in
|
||||
let sectors = List.map (parse_wpt_sector index_of_waypoints waypoints) sectors in
|
||||
List.iter2 print_inside_sector sectors_type sectors;
|
||||
|
||||
lprintf "#ifdef NAV_C\n";
|
||||
lprintf "\nstatic inline void auto_nav(void) {\n";
|
||||
right ();
|
||||
List.iter print_exception global_exceptions;
|
||||
|
||||
Reference in New Issue
Block a user