From 8255d76580631ccf7bbb9a72b742152850b4488e Mon Sep 17 00:00:00 2001 From: arne Date: Sat, 5 Aug 2023 15:54:59 +0200 Subject: [PATCH] Extend and fix bugs in some gcode helpers --- src/heyarne/line_us/connection.clj | 6 +++--- src/heyarne/line_us/gcode.clj | 2 +- src/heyarne/line_us/helpers.clj | 23 +++++++++++------------ 3 files changed, 15 insertions(+), 16 deletions(-) diff --git a/src/heyarne/line_us/connection.clj b/src/heyarne/line_us/connection.clj index f9b5771..012242e 100644 --- a/src/heyarne/line_us/connection.clj +++ b/src/heyarne/line_us/connection.clj @@ -33,7 +33,7 @@ (throw (IllegalArgumentException. (str ~(name coord) " should be in range [" ~min-v# ", " ~max-v# "] but is " ~coord)))))) -(defn- send-command! [^Socket line-us ^String raw-cmd] +(defn send-command! [^Socket line-us ^String raw-cmd] ;; this is basically taken from the Processing example code and the processing ;; "Client" class (doto (io/output-stream line-us) @@ -42,7 +42,7 @@ ;; wait for the response (let [res (read-response line-us)] (if-not (re-find #"^(ok|hello)" res) - (throw (Exception. res)) + #dbg (throw (Exception. res)) res))) (defn send-movement! @@ -52,7 +52,7 @@ (validate-coord x) (validate-coord y) (validate-coord z) - (send-command! line-us (str "G01 X" x " Y" y " Z" z)) + (send-command! line-us (format "G01 X%.0f Y%.0f Z%.0f" (double x) (double y) (double z))) coords) (defn move-home! [^Socket line-us] diff --git a/src/heyarne/line_us/gcode.clj b/src/heyarne/line_us/gcode.clj index 59ab580..4feeeef 100644 --- a/src/heyarne/line_us/gcode.clj +++ b/src/heyarne/line_us/gcode.clj @@ -48,7 +48,7 @@ An optional parameter `{:res 10}` can be used to adjust the sampling resolution for the shape, if the shape supports it." - (comp gcode-verts->gcode-seq ->gcode-verts)) + (comp #_gcode-verts->gcode-seq ->gcode-verts)) (defn- geom? [x] (str/starts-with? (pr-str x) "#thi.ng.geom.")) diff --git a/src/heyarne/line_us/helpers.clj b/src/heyarne/line_us/helpers.clj index ea62133..3ecc579 100644 --- a/src/heyarne/line_us/helpers.clj +++ b/src/heyarne/line_us/helpers.clj @@ -2,31 +2,30 @@ (:require [thi.ng.geom.rect :as rect] [thi.ng.math.core :as m])) -(defn g01-bounds +(defn coord-seq-bounds "Returns a rect representing the drawing bounds of a sequence of g01 coords" - [g01] + [coords] (let [bounds [Double/MAX_VALUE Double/MIN_VALUE Double/MIN_VALUE Double/MAX_VALUE] [top right bottom left] (reduce (fn [[top right bottom left] [x y _]] [(min top y) (max right x) (max bottom y) (min left x)]) bounds - g01)] + coords)] (rect/rect [left bottom] [right top]))) (defn rescale "Returns a new g01 sequence that is proportionally scaled to fit into the bounding box passed in as the second argument. Assumes that the top left in g01-seq is at [0 0]." - [g01-seq [top right bottom left]] - (let [s-bounds (g01-bounds g01-seq) - t-bounds (rect/rect [left bottom] [right top]) - ;; we need to translate the bounding box - [translate-x translate-y] (m/+ (:p s-bounds) (:p t-bounds)) + [into-bounds g01-seq ] + (let [src-bounds (coord-seq-bounds g01-seq) +;; we need to translate the bounding box + [translate-x translate-y] (m/+ (:p src-bounds) (:p into-bounds)) ;; and scale it - [s-x s-y] (:p s-bounds) - [t-x t-y] (:p t-bounds) - [s-width s-height] (:size s-bounds) - [t-width t-height] (:size t-bounds) + [s-x s-y] (:p src-bounds) + [t-x t-y] (:p into-bounds) + [s-width s-height] (:size src-bounds) + [t-width t-height] (:size into-bounds) factor (min (/ (+ t-x t-width) (+ s-x s-width)) (/ (+ t-y t-height) (+ s-y s-height)))] (map (fn [coord] (-> (update coord 0 #(+ translate-x (* % factor)))