From c028e7a2967cb1346a0c727857f64d9ba2f7ef1b Mon Sep 17 00:00:00 2001 From: Gautier Hattenberger Date: Wed, 18 Jul 2012 18:42:43 +0200 Subject: [PATCH] [saveSettings] automatic conversion are now described in units.xml --- conf/units.xml | 4 ++-- sw/lib/ocaml/pprz.ml | 11 ++++++++--- sw/lib/ocaml/pprz.mli | 4 ++++ sw/tools/gen_airframe.ml | 22 ++++++++-------------- 4 files changed, 22 insertions(+), 19 deletions(-) diff --git a/conf/units.xml b/conf/units.xml index 022537d529..bae0ba146d 100644 --- a/conf/units.xml +++ b/conf/units.xml @@ -1,9 +1,9 @@ - + - + diff --git a/sw/lib/ocaml/pprz.ml b/sw/lib/ocaml/pprz.ml index 28b1c33039..aca2607534 100644 --- a/sw/lib/ocaml/pprz.ml +++ b/sw/lib/ocaml/pprz.ml @@ -160,6 +160,7 @@ let payload_size_of_message = fun message -> exception Unit_conversion_error of string exception Unknown_conversion of string * string +exception No_automatic_conversion of string let scale_of_units = fun from_unit to_unit -> if (from_unit = to_unit) then @@ -171,15 +172,18 @@ let scale_of_units = fun from_unit to_unit -> let _unit = List.find (fun u -> (* will raise Xml.No_attribute if not a valid attribute *) let f = Xml.attrib u "from" - and t = Xml.attrib u "to" in - if from_unit = f && to_unit = t then true else false + and t = Xml.attrib u "to" + and a = String.lowercase (ExtXml.attrib_or_default u "auto" "false") in + if f = from_unit && (t = to_unit || a = "true") then true else false ) (Xml.children units_xml) in (* return coef, raise Failure if coef is not a numerical value *) float_of_string (Xml.attrib _unit "coef") with Xml.File_not_found _ -> raise (Unit_conversion_error ("Parse error of conf/units.xml")) | Xml.No_attribute _ | Xml.Not_element _ -> raise (Unit_conversion_error ("File conf/units.xml has errors")) | Failure "float_of_string" -> raise (Unit_conversion_error ("Unit coef is not numerical value")) - | Not_found -> raise (Unknown_conversion (from_unit, to_unit)) + | Not_found -> + if to_unit = "" then raise (No_automatic_conversion from_unit) + else raise (Unknown_conversion (from_unit, to_unit)) | _ -> raise (Unknown_conversion (from_unit, to_unit)) @@ -195,6 +199,7 @@ let alt_unit_coef_of_xml = function xml -> in coef + let pipe_regexp = Str.regexp "|" let field_of_xml = fun xml -> let t = ExtXml.attrib xml "type" in diff --git a/sw/lib/ocaml/pprz.mli b/sw/lib/ocaml/pprz.mli index 7dad2be584..2700a8a66d 100644 --- a/sw/lib/ocaml/pprz.mli +++ b/sw/lib/ocaml/pprz.mli @@ -90,6 +90,10 @@ exception Unit_conversion_error of string (** Unit_conversion_error raised when parsing error occurs *) exception Unknown_conversion of string * string (** Unknown_conversion raised when conversion fails *) +exception No_automatic_conversion of string +(** No_automatic_conversion raised when no conversion found + * and to_unit is an empty string + *) val scale_of_units : string -> string -> float (** scale_of_units from to diff --git a/sw/tools/gen_airframe.ml b/sw/tools/gen_airframe.ml index a1d2e8a7fb..aa58e5ad54 100644 --- a/sw/tools/gen_airframe.ml +++ b/sw/tools/gen_airframe.ml @@ -80,21 +80,15 @@ let define_integer name v n = in continious_frac (truncate v) v (1, (truncate v)) (0, 1) -let code_unit_scale_of_tag = function t -> +let code_unit_coef_of_xml = function xml -> (* if unit attribute is not specified don't even attempt to convert the units *) - let u = try ExtXml.attrib t "unit" with _ -> failwith "Unit conversion error" in - let cu = try ExtXml.attrib t "code_unit" with _ -> "" in + let u = try Xml.attrib xml "unit" with _ -> failwith "Unit conversion error" in + let cu = ExtXml.attrib_or_default xml "code_unit" "" in (* default value for code_unit is rad[/s] when unit is deg[/s] *) - try match (u, cu) with - ("deg", "") -> Pprz.scale_of_units u "rad" (* implicit conversion to rad *) - | ("deg/s", "") -> Pprz.scale_of_units u "rad/s" (* implicit conversion to rad/s *) - | (_, "") -> failwith "Unit conversion error" (* code unit is not defined and no implicit conversion *) - | (_,_) -> Pprz.scale_of_units u cu (* try to convert *) - with - Pprz.Unit_conversion_error s -> prerr_endline (sprintf "Unit conversion error: %s" s); flush stderr; exit 1 - | Pprz.Unknown_conversion (su, scu) -> prerr_endline (sprintf "Warning: unknown unit conversion: from %s to %s" su scu); flush stderr; failwith "Unknown unit conversion" - | _ -> failwith "Unit conversion error" - + try Pprz.scale_of_units u cu with + | Pprz.Unit_conversion_error s -> prerr_endline (sprintf "Unit conversion error: %s" s); flush stderr; exit 1 + | Pprz.Unknown_conversion (su, scu) -> prerr_endline (sprintf "Warning: unknown unit conversion: from %s to %s" su scu); flush stderr; failwith "Unknown unit conversion" + | Pprz.No_automatic_conversion _ | _ -> failwith "Unit conversion error" let parse_element = fun prefix s -> match Xml.tag s with @@ -104,7 +98,7 @@ let parse_element = fun prefix s -> (* fail if units conversion is not found and just copy value instead, this is important for integer values, you can't just multiply them with 1.0 *) try - let value = (ExtXml.float_attrib s "value") *. (code_unit_scale_of_tag s) in + let value = (ExtXml.float_attrib s "value") *. (code_unit_coef_of_xml s) in define (prefix^ExtXml.attrib s "name") (string_of_float value); with _ -> define (prefix^ExtXml.attrib s "name") (ExtXml.display_entities (ExtXml.attrib s "value"));