From: Pierre C. <Ba...@us...> - 2013-04-06 22:57:44
|
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 ea4f473d3e5947d777e4cecee84681d00961496c (commit) via 08d02d817b9535f9634e2fd2c9bd64f0a1cc5b24 (commit) from 53fc37f4d093087aa81f0d50668689421da71958 (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 ea4f473d3e5947d777e4cecee84681d00961496c Author: Pierre Chambart <pie...@oc...> Date: Sun Apr 7 00:56:10 2013 +0200 [camlcv] houghlines using type matrix commit 08d02d817b9535f9634e2fd2c9bd64f0a1cc5b24 Author: Pierre Chambart <pie...@oc...> Date: Sun Apr 7 00:54:54 2013 +0200 New beacon reader and transmitter ----------------------------------------------------------------------- Changes: diff --git a/info/control2011/_oasis b/info/control2011/_oasis index 91019c0..f90e326 100644 --- a/info/control2011/_oasis +++ b/info/control2011/_oasis @@ -312,6 +312,13 @@ Executable "krobot-urg" MainIs: krobot_urg.ml BuildDepends: krobot, urg, lwt.syntax, lwt.preemptive, threads +Executable "krobot-beacon-reader" + Path: src/tools + Install: true + CompiledObject: best + MainIs: krobot_beacon_reader.ml + BuildDepends: krobot, lwt.syntax, lwt.unix + # +-------------------------------------------------------------------+ # | Examples | # +-------------------------------------------------------------------+ diff --git a/info/control2011/src/lib/krobot_bus.ml b/info/control2011/src/lib/krobot_bus.ml index b39ed69..34595f3 100644 --- a/info/control2011/src/lib/krobot_bus.ml +++ b/info/control2011/src/lib/krobot_bus.ml @@ -42,6 +42,8 @@ type message = | Collisions of Bezier.curve * (float * (vertice * float) option) list | Coins of vertice list | Urg of int array + | Beacon_raw of (int * int * int * int * int * int + * int * int * int * int * int) type t = { oc : Lwt_io.output_channel; @@ -149,6 +151,8 @@ let string_of_message = function l)) | Urg distances -> sprintf "Urg (many_points...)" + | Beacon_raw _ -> + sprintf "Raw beacon packet" (* +-----------------------------------------------------------------+ | Sending/receiving messages | diff --git a/info/control2011/src/lib/krobot_bus.mli b/info/control2011/src/lib/krobot_bus.mli index ba3122e..ad33c96 100644 --- a/info/control2011/src/lib/krobot_bus.mli +++ b/info/control2011/src/lib/krobot_bus.mli @@ -89,6 +89,9 @@ type message = (** distances mesured by the URG (in millimeters) *) | Urg of int array + | Beacon_raw of (int * int * int * int * int * int + * int * int * int * int * int) + val string_of_message : message -> string (** Returns a string representation of the given message. *) diff --git a/info/control2011/src/tools/krobot_beacon_reader.ml b/info/control2011/src/tools/krobot_beacon_reader.ml new file mode 100644 index 0000000..eb6b934 --- /dev/null +++ b/info/control2011/src/tools/krobot_beacon_reader.ml @@ -0,0 +1,125 @@ +(* + * krobot_beacon_reader.ml + * ---------------- + * Copyright : (c) 2013, Pierre Chambart + * Licence : BSD3 + * + * This file is a part of [kro]bot. + *) + +(* Read beacon messages on serial port and broadcast it on the bus. *) + +open Lwt +open Lwt_react +open Lwt_preemptive +open Krobot_bus +open Krobot_message + +let section = Lwt_log.Section.make "krobot(beacon_reader)" + +let open_serial path rate = + lwt fd = Lwt_unix.openfile path [Unix.O_RDWR; Unix.O_NONBLOCK] 0o660 in + let tio = { + (* taken from minicom *) + Unix.c_ignbrk = true; Unix.c_brkint = false; Unix.c_ignpar = false; + Unix.c_parmrk = false; Unix.c_inpck = false; Unix.c_istrip = false; + Unix.c_inlcr = false; Unix.c_igncr = false; Unix.c_icrnl = false; + Unix.c_ixon = false; Unix.c_ixoff = false; Unix.c_opost = false; + Unix.c_obaud = rate; Unix.c_ibaud = rate; Unix.c_csize = 8; + Unix.c_cstopb = 1; Unix.c_cread = true; Unix.c_parenb = false; + Unix.c_parodd = false; Unix.c_hupcl = false; Unix.c_clocal = true; + Unix.c_isig = false; Unix.c_icanon = false; Unix.c_noflsh = false; + Unix.c_echo = false; Unix.c_echoe = false; Unix.c_echok = false; + Unix.c_echonl = false; Unix.c_vintr = '\000'; Unix.c_vquit = '\000'; + Unix.c_verase = '\000'; Unix.c_vkill = '\000'; Unix.c_veof = '\000'; + Unix.c_veol = '\000'; Unix.c_vmin = 1; Unix.c_vtime = 5; + Unix.c_vstart = '\000'; Unix.c_vstop = '\000' + } in + lwt () = Lwt_unix.tcsetattr fd Unix.TCSAFLUSH tio in + return fd + +let rec get_packet ic = + lwt line = Lwt_io.read_line ic in + try + let s = Scanf.sscanf line "%i %i %i %i %i %i %i %i %i %i %i " + (fun x1 x2 x3 x4 x5 x6 x7 x8 x9 x10 x11 -> + x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11) in + (* let x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11 = s in *) + (* lwt () = Lwt_log.info_f ~section "%i %i %i %i %i %i %i %i %i %i %i " *) + (* x1 x2 x3 x4 x5 x6 x7 x8 x9 x10 x11 in *) + return s + with _ -> get_packet ic + +(* +-----------------------------------------------------------------+ + | read/send loop | + +-----------------------------------------------------------------+ *) + +let loop bus ic = + let rec aux () = + lwt packet = get_packet ic in + let time = Unix.gettimeofday () in + let msg = Beacon_raw packet in + lwt () = Krobot_bus.send bus (time, msg) in + aux () in + aux () + +(* +-----------------------------------------------------------------+ + | Message handling | + +-----------------------------------------------------------------+ *) + +let handle_message (timestamp, message) = + match message with + | Kill "beacon_reader" -> + exit 0 + | _ -> + () + +(* +-----------------------------------------------------------------+ + | Command-line arguments | + +-----------------------------------------------------------------+ *) + +let fork = ref true +let tty = ref "/dev/ttyUSB0" +let baudrate = ref 57600 + +let options = Arg.align [ + "-no-fork", Arg.Clear fork, " Run in foreground"; + "-tty", Arg.Set_string tty, " set tty file"; + "-baudrate", Arg.Set_int baudrate, " set tty baudrate file"; +] + +let usage = "\ +Usage: krobot-beacon-reader [options] +options are:" + +(* +-----------------------------------------------------------------+ + | Entry point | + +-----------------------------------------------------------------+ *) + +lwt () = + Arg.parse options ignore usage; + + (* Display all informative messages. *) + Lwt_log.append_rule "*" Lwt_log.Info; + + (* Open the krobot bus. *) + lwt bus = Krobot_bus.get () in + + lwt fd = open_serial !tty !baudrate in + let ic = Lwt_io.of_fd ~mode:Lwt_io.input fd in + + lwt _ = get_packet ic in + + lwt () = Lwt_log.info ~section "got first packet" in + + (* Fork if not prevented. *) + if !fork then Krobot_daemon.daemonize bus; + + (* Handle krobot message. *) + E.keep (E.map handle_message (Krobot_bus.recv bus)); + + (* Kill any running urg_handler. *) + lwt () = Krobot_bus.send bus (Unix.gettimeofday (), Krobot_bus.Kill "beacon_reader") in + + (* Loop forever. *) + loop bus ic diff --git a/info/control2011/src/tools/krobot_urg.ml b/info/control2011/src/tools/krobot_urg.ml index 00761e1..6b1acaf 100644 --- a/info/control2011/src/tools/krobot_urg.ml +++ b/info/control2011/src/tools/krobot_urg.ml @@ -31,8 +31,8 @@ let to_array (b:Urg.point_data) = let loop bus urg = let rec aux () = - let time = Unix.gettimeofday () in lwt _ = Lwt_preemptive.detach Urg_simple.get urg in + let time = Unix.gettimeofday () in let msg = Urg (to_array urg.Urg_simple.data) in lwt () = Krobot_bus.send bus (time, msg) in lwt () = Lwt_log.info_f "send things" in diff --git a/info/vision/camlcv/_oasis b/info/vision/camlcv/_oasis index 198848e..69e0ed7 100644 --- a/info/vision/camlcv/_oasis +++ b/info/vision/camlcv/_oasis @@ -100,6 +100,12 @@ Executable "test_houghLines" Install: false BuildDepends: cv +Executable "test_houghLines_draw" + Path: test/ + MainIs: test_houghLines_draw.ml + Install: false + BuildDepends: cv + Executable "test_contour_canny" Path: test/ MainIs: test_contour_canny.ml diff --git a/info/vision/camlcv/src/cvCore.ml b/info/vision/camlcv/src/cvCore.ml index c77548b..cabf39e 100644 --- a/info/vision/camlcv/src/cvCore.ml +++ b/info/vision/camlcv/src/cvCore.ml @@ -50,6 +50,19 @@ let depth_f32 = IPL_DEPTH_32F type 'a image_depth = 'a depth +type ('channel) cvMat_float32 = + (float, Bigarray.float32_elt, Bigarray.c_layout) Bigarray.Array3.t + +type ('channel) cvMat_float64 = + (float, Bigarray.float64_elt, Bigarray.c_layout) Bigarray.Array3.t + +type ('channel) cvMat_int = + (int32, Bigarray.int32_elt, Bigarray.c_layout) Bigarray.Array3.t +(* there is no 64 bit type in CvMat... so stick to int32 *) + +type int_mat = + (int, Bigarray.int8_unsigned_elt, Bigarray.c_layout) Bigarray.Array2.t + type color_conversion = | CV_BGR2BGRA | CV_RGB2RGBA @@ -615,6 +628,17 @@ let houghLinesP img ?(minLineLength=0.) ?(maxLineGap=0.) rho theta threshold = houghLinesP' img vec rho theta threshold minLineLength maxLineGap; Array.init (vec4i_vect_size vec) (fun i -> vec4i_vect_get vec i) +external houghLinesP_mat' : + int_mat -> + vec4i_vect -> + float -> float -> int -> float -> float -> unit + = "ocaml_HoughLinesP_mat_bytecode" "ocaml_HoughLinesP_mat" + +let houghLinesP_mat mat ?(minLineLength=0.) ?(maxLineGap=0.) rho theta threshold = + let vec = create_vec4i_vect () in + houghLinesP_mat' mat vec rho theta threshold minLineLength maxLineGap; + Array.init (vec4i_vect_size vec) (fun i -> vec4i_vect_get vec i) + type calib_cb = | CV_CALIB_CB_ADAPTIVE_THRESH | CV_CALIB_CB_NORMALIZE_IMAGE @@ -672,16 +696,6 @@ let find_corner_subpix ?(criteria=default_criteria) ?(winSize=11,11) ?(zeroZone= (** camera calibration *) -type ('channel) cvMat_float32 = - (float, Bigarray.float32_elt, Bigarray.c_layout) Bigarray.Array3.t - -type ('channel) cvMat_float64 = - (float, Bigarray.float64_elt, Bigarray.c_layout) Bigarray.Array3.t - -type ('channel) cvMat_int = - (int32, Bigarray.int32_elt, Bigarray.c_layout) Bigarray.Array3.t -(* there is no 64 bit type in CvMat... so stick to int32 *) - external cvCalibrateCamera2 : [`Channel_3] cvMat_float32 -> [`Channel_2] cvMat_float32 -> [`Channel_1] cvMat_int -> cvSize -> [`Channel_1] cvMat_float32 -> [`Channel_1] cvMat_float32 -> float diff --git a/info/vision/camlcv/src/cvCore.mli b/info/vision/camlcv/src/cvCore.mli index 9c559f3..ef1a5fc 100644 --- a/info/vision/camlcv/src/cvCore.mli +++ b/info/vision/camlcv/src/cvCore.mli @@ -292,6 +292,15 @@ val good_features_to_track : ?maxLineGap:float -> float -> float -> int -> vec4i array (** [houghLinesP img rho theta threshold] *) +type int_mat = + (int, Bigarray.int8_unsigned_elt, Bigarray.c_layout) Bigarray.Array2.t + + val houghLinesP_mat : + int_mat -> + ?minLineLength:float -> + ?maxLineGap:float -> float -> float -> int -> vec4i array +(** [houghLinesP img rho theta threshold] *) + type calib_cb = | CV_CALIB_CB_ADAPTIVE_THRESH | CV_CALIB_CB_NORMALIZE_IMAGE diff --git a/info/vision/camlcv/src/cv_caml.c b/info/vision/camlcv/src/cv_caml.c index f7a0a55..21998a4 100644 --- a/info/vision/camlcv/src/cv_caml.c +++ b/info/vision/camlcv/src/cv_caml.c @@ -312,12 +312,12 @@ static CvMat CvMat_val(value v) { void* data = Data_bigarray_val(v); int rows = Bigarray_val(v)->dim[0]; int cols = Bigarray_val(v)->dim[1]; - int channels = Bigarray_val(v)->dim[2]; + int channels = (Bigarray_val(v)->num_dims == 2) ? 1 : Bigarray_val(v)->dim[2]; int kind = Bigarray_val(v)->flags & BIGARRAY_KIND_MASK; - int type = 0; + int type = -1; switch (kind) { - case BIGARRAY_FLOAT32: + case CAML_BA_FLOAT32: switch (channels) { case 1: type = CV_32FC1; @@ -333,7 +333,7 @@ static CvMat CvMat_val(value v) { } break; - case BIGARRAY_FLOAT64: + case CAML_BA_FLOAT64: switch (channels) { case 1: type = CV_64FC1; @@ -349,7 +349,7 @@ static CvMat CvMat_val(value v) { } break; - case BIGARRAY_INT32: + case CAML_BA_INT32: switch (channels) { case 1: type = CV_32SC1; @@ -365,10 +365,47 @@ static CvMat CvMat_val(value v) { } break; + case CAML_BA_UINT8: + switch (channels) { + case 1: + type = CV_8UC1; + break; + case 2: + type = CV_8UC2; + break; + case 3: + type = CV_8UC3; + break; + default: + break; + } + break; + + case CAML_BA_SINT8: + switch (channels) { + case 1: + type = CV_8SC1; + break; + case 2: + type = CV_8SC2; + break; + case 3: + type = CV_8SC3; + break; + default: + break; + } + break; + default: break; } - if(type == 0) caml_failwith("CvMat_val case not handled"); + + if(type == -1) + { + fflush(stdout); + caml_failwith("CvMat_val case not handled"); + } return (cvMat(rows, cols, type, data)); } @@ -1346,6 +1383,42 @@ extern "C" CAMLprim value ocaml_HoughLinesP_bytecode( value * argv, int argn ) argv[4], argv[5], argv[6] ); } +extern "C" CAMLprim value ocaml_HoughLinesP_mat(value vmat, + value vlines, + value rho, + value theta, + value threshold, + value minLineLength, + value maxLineGap) +{ + CAMLparam5( vmat, vlines, rho, theta, threshold ); + CAMLxparam2( minLineLength, maxLineGap ); + + CvMat mat = CvMat_val(vmat); + Mat m = Mat(&mat); + + /* Size s = m.size(); */ + /* printf("mat: depth %i channels %i eltsize %i w %i h %i\n", m.channels(), m.depth(), m.elemSize(), s.width, s.height); */ + /* fflush(stdout); */ + + ERRWRAP( + HoughLinesP(Mat(&mat), + *Vector_val<Vec4i>(vlines), + Double_val(rho), + Double_val(theta), + Int_val(threshold), + Double_val(minLineLength), + Double_val(maxLineGap))); + + CAMLreturn(Val_unit); +} + +extern "C" CAMLprim value ocaml_HoughLinesP_mat_bytecode( value * argv, int argn ) +{ + return ocaml_HoughLinesP_mat( argv[0], argv[1], argv[2], argv[3], + argv[4], argv[5], argv[6] ); +} + extern "C" CAMLprim value ocaml_CvSeq_info(value vseq) { CAMLparam1(vseq); diff --git a/info/vision/camlcv/test/test_houghLines_draw.ml b/info/vision/camlcv/test/test_houghLines_draw.ml new file mode 100644 index 0000000..a0c6586 --- /dev/null +++ b/info/vision/camlcv/test/test_houghLines_draw.ml @@ -0,0 +1,63 @@ +open CvCore +open CvHighGui +open Test_base + +let pi = 4. *. (atan 1.) + +let x = 500 +let y = 500 + +let img = Bigarray.Array2.create Bigarray.int8_unsigned Bigarray.c_layout 500 500 +let out = create_image ~x ~y depth_u8 channel_3 + +let points = [| 12.3, 3.5; + 2.4, 5.2; + 7.6, 26.8; + 10., 10.; + 11., 11.; + 12., 12.; + 13., 13.; |] + +let rec show () = + (* let x,y = image_size img in *) + (* let points' = Array.map (fun (x,y) -> int_of_float x, int_of_float y) points in *) + + (* draw_points *) + (* ~color:white *) + (* ~size:1 *) + (* img *) + (* points; *) + + (* Array.iter (fun (x,y) -> *) + (* let i, j = int_of_float x, int_of_float y in *) + (* img.{i,j} <- 255) points; *) + + for i = 100 to 300 do + img.{i,i} <- 255; + done; + + for i = 100 to 300 do + img.{i+30,400-i} <- 255; + done; + + let t1 = Unix.gettimeofday () in + let lines = + houghLinesP_mat + ~minLineLength:2. + ~maxLineGap:20. + img 0.1 (pi /. 180.) 50 in + let t2 = Unix.gettimeofday () in + + Printf.printf "lines: %i time: %f\n%!" + (Array.length lines) (t2 -. t1); + + Array.iter (fun (x1,y1,x2,y2) -> + line ~color:red out (x1,y1) (x2,y2)) lines; + + show_image "out" out; + + match wait_key 10 with + | Some 'q' -> () + | _ -> show () + +let _ = show () hooks/post-receive -- krobot |