diff --git a/sw/ground_segment/cockpit/saveSettings.ml b/sw/ground_segment/cockpit/saveSettings.ml index 20463a7259..7c06daa0b1 100644 --- a/sw/ground_segment/cockpit/saveSettings.ml +++ b/sw/ground_segment/cockpit/saveSettings.ml @@ -43,21 +43,6 @@ let floats_not_equal = fun f1 f2 -> let r = abs_float (f1 /. f2) in 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 *) 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" and unit_airframe = 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 _ -> 1. in let val_list = Str.split (Str.regexp "[ ()]+") airframe_value in diff --git a/sw/lib/ocaml/pprz.ml b/sw/lib/ocaml/pprz.ml index 77f51ab82c..02192104db 100644 --- a/sw/lib/ocaml/pprz.ml +++ b/sw/lib/ocaml/pprz.ml @@ -73,6 +73,7 @@ let (//) = Filename.concat let messages_file = Env.paparazzi_src // "conf" // "messages.xml" let lazy_messages_xml = lazy (Xml.parse_file messages_file) 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 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 *) +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 -> try Xml.attrib xml "alt_unit_coef" with _ -> let u = try Xml.attrib xml "unit" with _ -> "" in let au = try Xml.attrib xml "alt_unit" with _ -> "" in - match (u, au) with - ("deg", "rad") | ("deg/s", "rad/s") -> string_of_float (pi /. 180.) - | ("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 coef = try string_of_float (scale_of_units u au) with _ -> "1." in + coef let pipe_regexp = Str.regexp "|" let field_of_xml = fun xml -> diff --git a/sw/lib/ocaml/pprz.mli b/sw/lib/ocaml/pprz.mli index 3ab239c9ca..3eeec6ae4b 100644 --- a/sw/lib/ocaml/pprz.mli +++ b/sw/lib/ocaml/pprz.mli @@ -86,21 +86,17 @@ val int32_assoc : string -> values -> Int32.t val hex_of_int_array : value -> string (** 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 (** 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 (** [Unknown_msg_name (name, class_name)] Raised if message [name] is not diff --git a/sw/tools/gen_airframe.ml b/sw/tools/gen_airframe.ml index 8f656bbe4e..d0ae43979a 100644 --- a/sw/tools/gen_airframe.ml +++ b/sw/tools/gen_airframe.ml @@ -26,8 +26,6 @@ let max_pprz = 9600. (* !!!! MAX_PPRZ From paparazzi.h !!!! *) -exception Undefined_scale - open Printf open Xml2h @@ -84,18 +82,9 @@ let define_integer name v n = let code_unit_scale_of_tag = function t -> let u = try ExtXml.attrib t "unit" with _ -> "" in - let cu = try ExtXml.attrib t "code_unit" with _ -> "" in - match (u, cu) with - ("deg", "rad") | ("deg/s", "rad/s") -> Latlong.pi /. 180. - | ("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 + (* default value for code_unit is rad when unit is deg *) + let cu = try ExtXml.attrib t "code_unit" with _ -> if u = "deg" then "rad" else "" in + Pprz.scale_of_units u cu let parse_element = fun prefix s -> match Xml.tag s with