From: Jérémie D. <Ba...@us...> - 2011-03-16 23:17:32
|
This is an automated email from the git hooks/post-receive script. It was generated because a ref change was pushed to the repository containing the project "krobot". The branch, master has been updated via c8e4360b8d33d3ece5eda6020e207d1ffd4cd71f (commit) via ac782540fa94043e31d790e4dd4ad60cd2a00957 (commit) from ee17197988c142797ab6828aebeca793214cb402 (commit) Those revisions listed above that are new to this repository have not appeared on any other notification email; so we list those revisions in full, below. - Log ----------------------------------------------------------------- commit c8e4360b8d33d3ece5eda6020e207d1ffd4cd71f Author: Jérémie Dimino <je...@di...> Date: Thu Mar 17 00:15:58 2011 +0100 [info] make krobot-plot display a graph with cairo commit ac782540fa94043e31d790e4dd4ad60cd2a00957 Author: Jérémie Dimino <je...@di...> Date: Wed Mar 16 23:17:20 2011 +0100 [info] replace E.notify_s by E.map_s and E.keep ----------------------------------------------------------------------- Changes: diff --git a/info/control2011/_oasis b/info/control2011/_oasis index 8c23795..dd67350 100644 --- a/info/control2011/_oasis +++ b/info/control2011/_oasis @@ -62,7 +62,7 @@ Executable "krobot-driver" BuildDepends: krobot.can-bus, lwt.syntax # +-------------------------------------------------------------------+ -# | Tools | +# | Tools | # +-------------------------------------------------------------------+ Executable "krobot-plot" @@ -70,7 +70,7 @@ Executable "krobot-plot" Install: true CompiledObject: best MainIs: krobot_plot.ml - BuildDepends: krobot.can-bus, lwt.syntax + BuildDepends: krobot.can-bus, lwt.syntax, cairo.lablgtk2, lwt.glib # +-------------------------------------------------------------------+ # | Examples | diff --git a/info/control2011/_tags b/info/control2011/_tags index fedbe71..fee2129 100644 --- a/info/control2011/_tags +++ b/info/control2011/_tags @@ -2,7 +2,7 @@ <src/interfaces/*.ml>: -syntax_camlp4o # OASIS_START -# DO NOT EDIT (digest: 714f24c8f8cd15c9da08a3b463f1d17c) +# DO NOT EDIT (digest: 38a5a55661f6998cf1a3f011ff7ecfd3) # Library krobot "src/lib": include <src/lib/*.ml{,i}>: pkg_obus @@ -57,11 +57,15 @@ <tools/krobot_plot.{native,byte}>: pkg_obus <tools/krobot_plot.{native,byte}>: pkg_lwt.unix <tools/krobot_plot.{native,byte}>: pkg_lwt.syntax +<tools/krobot_plot.{native,byte}>: pkg_lwt.glib <tools/krobot_plot.{native,byte}>: pkg_krobot.interfaces +<tools/krobot_plot.{native,byte}>: pkg_cairo.lablgtk2 <tools/*.ml{,i}>: use_krobot-can-bus <tools/*.ml{,i}>: use_krobot <tools/*.ml{,i}>: pkg_obus <tools/*.ml{,i}>: pkg_lwt.unix <tools/*.ml{,i}>: pkg_lwt.syntax +<tools/*.ml{,i}>: pkg_lwt.glib <tools/*.ml{,i}>: pkg_krobot.interfaces +<tools/*.ml{,i}>: pkg_cairo.lablgtk2 # OASIS_STOP diff --git a/info/control2011/setup.ml b/info/control2011/setup.ml index e003f47..02aa273 100644 --- a/info/control2011/setup.ml +++ b/info/control2011/setup.ml @@ -1,7 +1,7 @@ (* setup.ml generated for the first time by OASIS v0.2.0~alpha1 *) (* OASIS_START *) -(* DO NOT EDIT (digest: 003c94954a115118c0158a0ee154e6cc) *) +(* DO NOT EDIT (digest: a60a31dc8dbfa28c3584c2830b23531f) *) (* Regenerated by OASIS v0.2.0 Visit http://oasis.forge.ocamlcore.org for more information and @@ -5249,7 +5249,9 @@ let setup_t = bs_build_depends = [ InternalLibrary "krobot-can-bus"; - FindlibPackage ("lwt.syntax", None) + FindlibPackage ("lwt.syntax", None); + FindlibPackage ("cairo.lablgtk2", None); + FindlibPackage ("lwt.glib", None) ]; bs_build_tools = [ExternalTool "ocamlbuild"]; bs_c_sources = []; diff --git a/info/control2011/src/driver/krobot_driver.ml b/info/control2011/src/driver/krobot_driver.ml index 71d36ff..5ab3ea7 100644 --- a/info/control2011/src/driver/krobot_driver.ml +++ b/info/control2011/src/driver/krobot_driver.ml @@ -27,7 +27,8 @@ lwt () = (* D-Bus --> CAN *) lwt () = OBus_signal.connect (Krobot_can.frames bus) - >|= E.notify_s (Krobot_can_bus.send can) + >|= E.map_s (Krobot_can_bus.send can) + >|= E.keep in (* CAN --> D-Bus *) diff --git a/info/control2011/tools/krobot_plot.ml b/info/control2011/tools/krobot_plot.ml index 0629f22..3e8b1f9 100644 --- a/info/control2011/tools/krobot_plot.ml +++ b/info/control2011/tools/krobot_plot.ml @@ -10,53 +10,130 @@ open Lwt open Krobot_can -type data = { - coder_pos : int; - coder_dir : int; +type direction = Forward | Backward + +type point = { time : float; position : int; direction : direction } + +(* +-----------------------------------------------------------------+ + | Graphs | + +-----------------------------------------------------------------+ *) + +let graph_duration = 10.0 + (* Which amount of data to keep in graphs. *) + +(* Type of graphs. *) +type graph = { + points : point Queue.t array; + (* Queue of points with their time. They are ordered by increasing + date. *) + mutable max : int; + (* The maximum reached. *) } -let decode_frame frame = +(* Remove old points. *) +let update_graph graph time = + Array.iter + (fun q -> + while not (Queue.is_empty q) && (Queue.top q).time +. graph_duration < time do + ignore (Queue.take q) + done) + graph.points + +(* +-----------------------------------------------------------------+ + | Plotting | + +-----------------------------------------------------------------+ *) + +let rec colors = (1., 0., 0.) :: (0., 1., 0.) :: (0., 0., 1.) :: (1., 1., 0.) :: colors + +let plot ctx width height graph time = + Cairo.set_source_rgb ctx 1. 1. 1.; + Cairo.rectangle ctx 0. 0. width height; + Cairo.fill ctx; + let colors = ref colors in + Array.iter + (fun q -> + let r, g, b = List.hd !colors in + colors := List.tl !colors; + Cairo.set_source_rgb ctx r g b; + let prev = ref None in + Queue.iter + (fun point -> + let x = (point.time -. (time -. graph_duration)) /. graph_duration *. width + and y = height -. height *. (float point.position /. float graph.max) in + match !prev with + | None -> + prev := Some(x, y) + | Some(x', y') -> + prev := Some(x, y); + Cairo.move_to ctx x' y'; + Cairo.line_to ctx x y; + Cairo.stroke ctx) + q) + graph.points + +(* +-----------------------------------------------------------------+ + | Decoding frames | + +-----------------------------------------------------------------+ *) + +let decode_frame time frame = let data = Krobot_can.data frame in - ({ coder_pos = get_uint16 data 0; - coder_dir = get_uint8 data 4 }, - { coder_pos = get_uint16 data 2; - coder_dir = get_uint8 data 5 }) - -let rec loop bus oc = - let time = Unix.gettimeofday () in - lwt coder1, coder2 = Krobot_can_bus.recv bus >|= decode_frame in - lwt coder3, coder4 = Krobot_can_bus.recv bus >|= decode_frame in - lwt () = - Lwt_io.fprintlf oc - "%f %d %d %d %d %d %d %d %d" - time - coder1.coder_pos coder1.coder_dir - coder2.coder_pos coder2.coder_dir - coder3.coder_pos coder3.coder_dir - coder4.coder_pos coder4.coder_dir - in - lwt () = - Lwt_io.printlf "time: %f - coder1: pos = %d, dir = %d - coder2: pos = %d, dir = %d - coder3: pos = %d, dir = %d - coder4: pos = %d, dir = %d -" - time - coder1.coder_pos coder1.coder_dir - coder2.coder_pos coder2.coder_dir - coder3.coder_pos coder3.coder_dir - coder4.coder_pos coder4.coder_dir - in - loop bus oc + ({ time = time; + position = get_uint16 data 0; + direction = if get_uint8 data 4 = 0 then Forward else Backward }, + { time = time; + position = get_uint16 data 2; + direction = if get_uint8 data 5 = 0 then Forward else Backward }) + +(* +-----------------------------------------------------------------+ + | Main-loop | + +-----------------------------------------------------------------+ *) lwt () = if Array.length Sys.argv <> 2 then begin print_endline "usage: krobot-plot <interface>"; exit 2; end; + ignore (GMain.init ()); + Lwt_glib.install (); + + let window = GWindow.window ~title:"Krobot coders positions" () in + ignore (window#connect#destroy ~callback:(fun () -> exit 0)); + window#show (); + + let graph = { points = Array.init 4 (fun _ -> Queue.create ()); max = 1 } in + try_lwt - lwt bus = Krobot_can_bus.open_can Sys.argv.(1) and oc = Lwt_io.open_file ~mode:Lwt_io.output "data" in - loop bus oc + lwt bus = Krobot_can_bus.open_can Sys.argv.(1) in + join [ + (* Data reading. *) + while_lwt true do + (* Read coder positions. *) + lwt coder1, coder2 = Krobot_can_bus.recv bus >|= (fun frame -> decode_frame (Unix.gettimeofday ()) frame) in + lwt coder3, coder4 = Krobot_can_bus.recv bus >|= (fun frame -> decode_frame (Unix.gettimeofday ()) frame) in + (* Compute the new maximum. *) + graph.max <- List.fold_left max graph.max [coder1.position; coder2.position; coder3.position; coder4.position]; + (* Add points to the graph. *) + Queue.push coder1 graph.points.(0); + Queue.push coder2 graph.points.(1); + Queue.push coder3 graph.points.(2); + Queue.push coder4 graph.points.(3); + (* Remove old points. *) + update_graph graph (Unix.gettimeofday ()); + return () + done; + (* Drawing. *) + while_lwt true do + let { Gtk.width; Gtk.height } = window#misc#allocation in + let surface = Cairo.image_surface_create Cairo.FORMAT_ARGB32 width height in + let ctx = Cairo.create surface in + plot ctx (float width) (float height) graph (Unix.gettimeofday ()); + let ctx = Cairo_lablgtk.create window#misc#window in + Cairo.set_source_surface ctx surface 0. 0.; + Cairo.rectangle ctx 0. 0. (float width) (float height); + Cairo.fill ctx; + Cairo.surface_finish surface; + Lwt_unix.sleep 0.05 + done; + ] with Unix.Unix_error(error, func, arg) -> Lwt_log.error_f "'%s' failed with: %s" func (Unix.error_message error) hooks/post-receive -- krobot |