mirror of
https://github.com/paparazzi/paparazzi.git
synced 2026-05-29 19:17:28 +08:00
factorize unit conversion tools and extend it;
the file conf/units.xml describes all the possible conversions
This commit is contained in:
@@ -43,21 +43,6 @@ let floats_not_equal = fun f1 f2 ->
|
|||||||
let r = abs_float (f1 /. f2) in
|
let r = abs_float (f1 /. f2) in
|
||||||
r < 0.999 || r > 1.001
|
r < 0.999 || r > 1.001
|
||||||
|
|
||||||
(* Unit conversions *)
|
|
||||||
let scale_of_units = fun u1 u2 ->
|
|
||||||
match (u1, u2) with
|
|
||||||
("deg", "rad") | ("deg/s", "rad/s") -> 180. /. Latlong.pi
|
|
||||||
| ("rad", "deg") | ("rad/s", "deg/s") -> Latlong.pi /. 180.
|
|
||||||
| ("m", "cm") | ("m/s", "cm/s") -> 100.
|
|
||||||
| ("cm", "m") | ("cm/s", "m/s") -> 0.01
|
|
||||||
| ("m", "mm") | ("m/s", "mm/s") -> 1000.
|
|
||||||
| ("mm", "m") | ("mm/s", "m/s") -> 0.001
|
|
||||||
| ("decideg", "deg") -> 0.1
|
|
||||||
| ("deg", "decideg") -> 10.
|
|
||||||
| u1, u2 when u1 = u2 -> 1.
|
|
||||||
| _ -> invalid_arg (Printf.sprintf "SaveSettings.scale_of_units %s %s" u1 u2)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
(** The save file dialog box *)
|
(** The save file dialog box *)
|
||||||
let save_airframe = fun w filename save ->
|
let save_airframe = fun w filename save ->
|
||||||
@@ -145,7 +130,7 @@ let fill_data = fun (model:GTree.tree_store) settings airframe_xml ->
|
|||||||
let unit_setting = attrib "unit"
|
let unit_setting = attrib "unit"
|
||||||
and unit_airframe =
|
and unit_airframe =
|
||||||
match unit with Some u -> u | None -> raise Exit in
|
match unit with Some u -> u | None -> raise Exit in
|
||||||
scale_of_units unit_setting unit_airframe
|
Pprz.scale_of_units unit_setting unit_airframe
|
||||||
with
|
with
|
||||||
_ -> 1. in
|
_ -> 1. in
|
||||||
let val_list = Str.split (Str.regexp "[ ()]+") airframe_value in
|
let val_list = Str.split (Str.regexp "[ ()]+") airframe_value in
|
||||||
|
|||||||
+20
-10
@@ -73,6 +73,7 @@ let (//) = Filename.concat
|
|||||||
let messages_file = Env.paparazzi_src // "conf" // "messages.xml"
|
let messages_file = Env.paparazzi_src // "conf" // "messages.xml"
|
||||||
let lazy_messages_xml = lazy (Xml.parse_file messages_file)
|
let lazy_messages_xml = lazy (Xml.parse_file messages_file)
|
||||||
let messages_xml = fun () -> Lazy.force lazy_messages_xml
|
let messages_xml = fun () -> Lazy.force lazy_messages_xml
|
||||||
|
let units_file = Env.paparazzi_src // "conf" // "units.xml"
|
||||||
|
|
||||||
external float_of_bytes : string -> int -> float = "c_float_of_indexed_bytes"
|
external float_of_bytes : string -> int -> float = "c_float_of_indexed_bytes"
|
||||||
external double_of_bytes : string -> int -> float = "c_double_of_indexed_bytes"
|
external double_of_bytes : string -> int -> float = "c_double_of_indexed_bytes"
|
||||||
@@ -158,21 +159,30 @@ let payload_size_of_message = fun message ->
|
|||||||
2 (** + message id + aircraft id *)
|
2 (** + message id + aircraft id *)
|
||||||
|
|
||||||
|
|
||||||
|
let scale_of_units = fun from_unit to_unit ->
|
||||||
|
try
|
||||||
|
let units_xml = Xml.parse_file units_file in
|
||||||
|
(* find the first occurence of matching units or raise Not_found *)
|
||||||
|
let _unit = List.find (fun u ->
|
||||||
|
try
|
||||||
|
if from_unit = (Xml.attrib u "from") && to_unit = (Xml.attrib u "to") then
|
||||||
|
true
|
||||||
|
else
|
||||||
|
false
|
||||||
|
with _ -> false (* not a valid unit declaration *)
|
||||||
|
) (Xml.children units_xml) in
|
||||||
|
(* return coef *)
|
||||||
|
float_of_string (Xml.attrib _unit "coef")
|
||||||
|
with _ -> invalid_arg "Unit conversion failed"
|
||||||
|
|
||||||
|
|
||||||
let alt_unit_coef_of_xml = function xml ->
|
let alt_unit_coef_of_xml = function xml ->
|
||||||
try Xml.attrib xml "alt_unit_coef"
|
try Xml.attrib xml "alt_unit_coef"
|
||||||
with _ ->
|
with _ ->
|
||||||
let u = try Xml.attrib xml "unit" with _ -> "" in
|
let u = try Xml.attrib xml "unit" with _ -> "" in
|
||||||
let au = try Xml.attrib xml "alt_unit" with _ -> "" in
|
let au = try Xml.attrib xml "alt_unit" with _ -> "" in
|
||||||
match (u, au) with
|
let coef = try string_of_float (scale_of_units u au) with _ -> "1." in
|
||||||
("deg", "rad") | ("deg/s", "rad/s") -> string_of_float (pi /. 180.)
|
coef
|
||||||
| ("rad", "deg") | ("rad/s", "deg/s") -> string_of_float (180. /. pi)
|
|
||||||
| ("m", "cm") | ("m/s", "cm/s") -> "100."
|
|
||||||
| ("cm", "m") | ("cm/s", "m/s") -> "0.01"
|
|
||||||
| ("m", "mm") | ("m/s", "mm/s") -> "1000."
|
|
||||||
| ("mm", "m") | ("mm/s", "m/s") -> "0.001"
|
|
||||||
| ("decideg", "deg") -> "0.1"
|
|
||||||
| ("deg", "decideg") -> "10."
|
|
||||||
| (_, _) -> "1."
|
|
||||||
|
|
||||||
let pipe_regexp = Str.regexp "|"
|
let pipe_regexp = Str.regexp "|"
|
||||||
let field_of_xml = fun xml ->
|
let field_of_xml = fun xml ->
|
||||||
|
|||||||
+9
-13
@@ -86,21 +86,17 @@ val int32_assoc : string -> values -> Int32.t
|
|||||||
val hex_of_int_array : value -> string
|
val hex_of_int_array : value -> string
|
||||||
(** Returns the hexadecimal string of an array of integers *)
|
(** Returns the hexadecimal string of an array of integers *)
|
||||||
|
|
||||||
|
val scale_of_units : string -> string -> float
|
||||||
|
(** scale_of_units from to
|
||||||
|
* Returns conversion factor between two units
|
||||||
|
* The possible conversions are described in conf/units.xml
|
||||||
|
* May raise Invalid_argument if one of the unit is not valid
|
||||||
|
* or if units.xml is not valid
|
||||||
|
*)
|
||||||
|
|
||||||
val alt_unit_coef_of_xml : Xml.xml -> string
|
val alt_unit_coef_of_xml : Xml.xml -> string
|
||||||
(** Return coef for alternate unit
|
(** Return coef for alternate unit
|
||||||
Default possible corrections:
|
*)
|
||||||
deg -> rad
|
|
||||||
rad -> deg
|
|
||||||
m -> cm
|
|
||||||
cm -> m
|
|
||||||
m -> mm
|
|
||||||
mm -> m
|
|
||||||
m/s -> cm/s
|
|
||||||
cm/s -> m/s
|
|
||||||
m/s -> mm/s
|
|
||||||
mm/s -> m/s
|
|
||||||
decideg -> deg
|
|
||||||
*)
|
|
||||||
|
|
||||||
exception Unknown_msg_name of string * string
|
exception Unknown_msg_name of string * string
|
||||||
(** [Unknown_msg_name (name, class_name)] Raised if message [name] is not
|
(** [Unknown_msg_name (name, class_name)] Raised if message [name] is not
|
||||||
|
|||||||
@@ -26,8 +26,6 @@
|
|||||||
|
|
||||||
let max_pprz = 9600. (* !!!! MAX_PPRZ From paparazzi.h !!!! *)
|
let max_pprz = 9600. (* !!!! MAX_PPRZ From paparazzi.h !!!! *)
|
||||||
|
|
||||||
exception Undefined_scale
|
|
||||||
|
|
||||||
open Printf
|
open Printf
|
||||||
open Xml2h
|
open Xml2h
|
||||||
|
|
||||||
@@ -84,18 +82,9 @@ let define_integer name v n =
|
|||||||
|
|
||||||
let code_unit_scale_of_tag = function t ->
|
let code_unit_scale_of_tag = function t ->
|
||||||
let u = try ExtXml.attrib t "unit" with _ -> "" in
|
let u = try ExtXml.attrib t "unit" with _ -> "" in
|
||||||
let cu = try ExtXml.attrib t "code_unit" with _ -> "" in
|
(* default value for code_unit is rad when unit is deg *)
|
||||||
match (u, cu) with
|
let cu = try ExtXml.attrib t "code_unit" with _ -> if u = "deg" then "rad" else "" in
|
||||||
("deg", "rad") | ("deg/s", "rad/s") -> Latlong.pi /. 180.
|
Pprz.scale_of_units u cu
|
||||||
| ("deg", "") | ("deg/s", "") -> Latlong.pi /. 180.
|
|
||||||
| ("rad", "deg") | ("rad/s", "deg/s") -> 180. /. Latlong.pi
|
|
||||||
| ("m", "cm") | ("m/s", "cm/s") -> 100.
|
|
||||||
| ("cm", "m") | ("cm/s", "m/s") -> 0.01
|
|
||||||
| ("m", "mm") | ("m/s", "mm/s") -> 1000.
|
|
||||||
| ("mm", "m") | ("mm/s", "m/s") -> 0.001
|
|
||||||
| ("decideg", "deg") -> 0.1
|
|
||||||
| ("deg", "decideg") -> 10.
|
|
||||||
| (_, _) -> raise Undefined_scale
|
|
||||||
|
|
||||||
let parse_element = fun prefix s ->
|
let parse_element = fun prefix s ->
|
||||||
match Xml.tag s with
|
match Xml.tag s with
|
||||||
|
|||||||
Reference in New Issue
Block a user