From: Jérémie D. <Ba...@us...> - 2010-05-11 12:17:31
|
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 258c436c669b76ab50c63192ad60224f9c649eb8 (commit) from 18bb10e639922efb98e3b2273013c37042cf2db4 (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 258c436c669b76ab50c63192ad60224f9c649eb8 Author: Jérémie Dimino <je...@di...> Date: Tue May 11 14:17:00 2010 +0200 better replay ----------------------------------------------------------------------- Changes: diff --git a/info/control/clients/joy_control.ml b/info/control/clients/joy_control.ml index 7d980a6..a9823cf 100644 --- a/info/control/clients/joy_control.ml +++ b/info/control/clients/joy_control.ml @@ -129,43 +129,33 @@ let axis_coef_turn = 4 let accelerations = (400, 400) let duration = 0.2 -let try_call action f = +let record_oc = + if Array.length Sys.argv >= 2 && Sys.argv.(1) = "record" then + Lwt_io.open_file ~mode:Lwt_io.output "krobot.record" + else + Lwt_io.null + +let date_last_command = ref (Unix.gettimeofday ()) + +let robot_call record f = + let date = Unix.gettimeofday () in + let delta = date -. !date_last_command in + date_last_command := date; + lwt () = Lwt_io.fprintf record_oc "%Ld\n%s\n" (Int64.bits_of_float delta) record in try_lwt f () with Failure msg -> - lwt () = Lwt_log.error_f "action %s failed with: %s" action msg in + lwt () = Lwt_log.error_f "action << %s >> failed with: %s" record msg in return () -let record = - if Array.length Sys.argv >= 2 && Sys.argv.(1) = "record" then begin - let oc = Lwt_io.open_file ~mode:Lwt_io.output "krobot.record" in - let prev = ref (Unix.gettimeofday ()) in - fun cmd -> - let date = Unix.gettimeofday () in - let delta = date -. !prev in - prev := date; - lwt () = - if delta > 0. then - Lwt_io.fprintlf oc "sleep duration=%f" delta - else - return () - in - Lwt_io.write_line oc cmd - end else begin - fun cmd -> return () - end - let rec set_velocities krobot velocities = lwt () = Lwt_log.info_f "set-velocities: left=%d right=%d" (fst velocities) (snd velocities) in lwt () = - try_call "set-velocities" + robot_call + (Printf.sprintf + "set-velocities velocity-left=%d velocity-right=%d acceleration-left=%d acceleration-right=%d duration=%f" + (fst velocities) (snd velocities) (fst accelerations) (snd accelerations) duration) (fun () -> - lwt () = - Printf.ksprintf - record - "set-velocities velocity-left=%d velocity-right=%d acceleration-left=%d acceleration-right=%d duration=%f" - (fst velocities) (snd velocities) (fst accelerations) (snd accelerations) duration - in Krobot.Motors.set_velocities krobot ~velocities ~accelerations ~duration) in if velocities = (0, 0) then @@ -190,6 +180,7 @@ let parent_loop krobot pipe = (!laxis_v * axis_coef - !raxis_h * axis_coef_turn, !laxis_v * axis_coef + !raxis_h * axis_coef_turn) in + let enable_claws = lazy(robot_call "claws.enable" (fun () -> Krobot.Claws.enable krobot)) in let rec loop () = Lwt_io.read_value pipe >>= function | KeyPressed KEY_ESCAPE -> @@ -213,18 +204,18 @@ let parent_loop krobot pipe = | JoyButtonPressed ButtonSquare -> stop := true; cancel !thread; - lwt () = try_call "stop-motors" (fun () -> Krobot.Motors.stop krobot ~mode:`Abrupt) in + lwt () = robot_call "stop-motors mode=abrupt" (fun () -> Krobot.Motors.stop krobot ~mode:`Abrupt) in loop () | JoyButtonReleased ButtonSquare -> stop := false; loop () | JoyButtonPressed ButtonCross -> - lwt () = record "claws.take" in - lwt () = try_call "claws.take" (fun () -> Krobot.Claws.take krobot) in + lwt () = Lazy.force enable_claws in + lwt () = robot_call "claws.take" (fun () -> Krobot.Claws.take krobot) in loop () | JoyButtonPressed ButtonCircle -> - lwt () = record "claws.open" in - lwt () = try_call "claws.open" (fun () -> Krobot.Claws.open_ krobot) in + lwt () = Lazy.force enable_claws in + lwt () = robot_call "claws.open" (fun () -> Krobot.Claws.open_ krobot) in loop () | _ -> loop () @@ -254,8 +245,6 @@ let () = Unix.close fd_w; Lwt_main.run begin lwt krobot = Krobot.create () in - lwt () = Krobot.Claws.enable krobot in - lwt () = Krobot.Claws.close krobot in lwt () = Lwt_log.notice "ready to process event" in parent_loop krobot (Lwt_io.of_unix_fd ~mode:Lwt_io.input fd_r) end diff --git a/info/control/clients/replay.ml b/info/control/clients/replay.ml index cc9060f..ba004a4 100644 --- a/info/control/clients/replay.ml +++ b/info/control/clients/replay.ml @@ -15,16 +15,25 @@ lwt () = exit 2 end; - let ic = Lwt_io.open_file ~mode:Lwt_io.input Sys.argv.(1) in + lwt lines = Lwt_stream.to_list (Lwt_io.lines_of_file Sys.argv.(1)) in lwt krobot = Krobot.create () in - let rec loop () = - Lwt_io.read_line_opt ic >>= function - | None -> - return () - | Some line -> - lwt () = Lwt_io.printl line in - lwt () = Script.exec ~krobot ~logger:Lwt_term.printlc ~command:line in - loop () + let rec loop = function + | [] -> + return () + | [_] -> + failwith "invalid record file" + | delta :: command :: lines -> + let delta = Int64.float_of_bits (Int64.of_string delta) in + lwt () = Lwt_io.printl command in + ignore ( + try_lwt + Script.exec ~krobot ~logger:Lwt_term.printlc ~command + with exn -> + lwt () = Lwt_log.error_f "failure: %s" (Printexc.to_string exn) in + exit 1 + ); + lwt () = Lwt_unix.sleep delta in + loop lines in - loop () + loop lines hooks/post-receive -- krobot |