[saveSettings] automatic conversion are now described in units.xml

This commit is contained in:
Gautier Hattenberger
2012-07-18 18:42:43 +02:00
parent 9d4bc8f588
commit c028e7a296
4 changed files with 22 additions and 19 deletions
+2 -2
View File
@@ -1,9 +1,9 @@
<!-- Table of default units convertion -->
<!-- used to convert from unit to alt_unit (messages) or code_unit (airframe) -->
<units>
<unit from="deg" to="rad" coef="0.0174532925"/>
<unit from="deg" to="rad" coef="0.0174532925" auto="true"/>
<unit from="rad" to="deg" coef="57.2957795131"/>
<unit from="deg/s" to="rad/s" coef="0.0174532925"/>
<unit from="deg/s" to="rad/s" coef="0.0174532925" auto="true"/>
<unit from="rad/s" to="deg/s" coef="57.2957795131"/>
<unit from="m" to="cm" coef="100."/>
<unit from="cm" to="m" coef="0.01"/>
+8 -3
View File
@@ -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
+4
View File
@@ -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
+8 -14
View File
@@ -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"));