Frusta

Check-in [947f22cec0]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:fixed G2/G3 (arcs) last commit had deg/rad mistake
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:947f22cec0d7a1d20c8f98fcefef266c56b0a5c9
User & Date: rmelton 2013-10-24 23:03:04
Context
2013-10-25
00:26
fixed G2 (arcs) check-in: fb1a2763ee user: rmelton tags: trunk
2013-10-24
23:03
fixed G2/G3 (arcs) last commit had deg/rad mistake check-in: 947f22cec0 user: rmelton tags: trunk
17:08
fixed G2/G3 (arcs) to gcode reader check-in: ec0df6213a user: rmelton tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to lib/gcode/gcode_0.1.tcl.

77
78
79
80
81
82
83

84
85
86
87
88
89
90
91
92
93

94
95
96
97
98
99
100
...
129
130
131
132
133
134
135



136
137

138
139



140





141
142










143
144
145
146
147
148
149
150
151
152
153
154
155







156
157
158
159
160
161
162
163
164
165
166
167

168
169
170
171
172
173
174
...
203
204
205
206
207
208
209



210
211

212
213



214





215
216










217
218

219
220




221
222
223
224
225
226
227
228
229







230
231
232
233
234
235
236
                            [list $cfg(X) $cfg(Y) $cfg(Z)]\
                            [list $new(X) $new(Y) $new(Z)]\
                            ]
      lassign [list $new(X) $new(Y) $new(Z) $new(E)] cfg(X) cfg(Y) cfg(Z) cfg(E)
    }
    
    proc G2 {args} {

      ## clockwise arc (ref http://linuxcnc.org/docs/html/gcode/gcode.html#sec:G2-G3-Arc)
      ## note this code is only valid for XY arcs
      variable cfg
      variable pathList
      set new(X) $cfg(X)
      set new(Y) $cfg(Y)
      set new(Z) $cfg(Z)
      set new(E) $cfg(E)
      set offsetI 0    ;# x offset to center
      set offsetJ 0    ;# y offset to center

      while {[llength $args] > 0} {
        set args [lassign $args cmd]
        switch -regexp -matchvar matchvar -- $cmd {
          {X(-?[[:digit:]]+(\.[[:digit:]]+)?)} {
            set new(X) [expr {$cfg(scale)*[lindex $matchvar 1]+($cfg(relP) ? $cfg(X) : 0)}]
            #puts stderr "X $matchvar"
          }
................................................................................
      } else {
        set type [list move Layer_$cfg(layerNum)]
        set color yellow
        set width 1
      }
      set Xc [expr {$cfg(X)+$offsetI}]
      set Yc [expr {$cfg(Y)+$offsetJ}]



      #compute "start" angle
      set theta0 [expr {atan((0-$offsetJ)/(0-$offsetI))}]

      #compute "stop" angle
      set theta1 [expr {atan(($new(Y)-$Yc)/($new(X)-$Xc))}]



      set stepAngle -5





      lassign [list $cfg(X) $cfg(Y)] x0 y0
      set Pi 3.14159265










      for {set deg [expr {$theta0+$stepAngle}]} {$deg >= $theta1} {incr deg $stepAngle} {
        set rads [expr {$deg/360.0*2*$Pi}]
        set x1 [expr {cos($rads)*$radius+$Xc}]
        set y1 [expr {sin($rads)*$radius+$Yc}]
        lappend pathList [list \
                              $width\
                              $type\
                              $color\
                              [list $x0 $y0 $cfg(Z)]\
                              [list $x1 $y1 $cfg(Z)]\
                             ]
        lassign [list $x1 $y1] x0 y0
      }







      lassign [list $new(X) $new(Y) $new(E)] cfg(X) cfg(Y) cfg(E)
    }
    proc G3 {args} {
      ## counter-clockwise arc (ref http://linuxcnc.org/docs/html/gcode/gcode.html#sec:G2-G3-Arc)
      variable cfg
      variable pathList
      set new(X) $cfg(X)
      set new(Y) $cfg(Y)
      set new(Z) $cfg(Z)
      set new(E) $cfg(E)
      set offsetI 0    ;# x offset to center
      set offsetJ 0    ;# y offset to center

      while {[llength $args] > 0} {
        set args [lassign $args cmd]
        switch -regexp -matchvar matchvar -- $cmd {
          {X(-?[[:digit:]]+(\.[[:digit:]]+)?)} {
            set new(X) [expr {$cfg(scale)*[lindex $matchvar 1]+($cfg(relP) ? $cfg(X) : 0)}]
            #puts stderr "X $matchvar"
          }
................................................................................
      } else {
        set type [list move Layer_$cfg(layerNum)]
        set color yellow
        set width 1
      }
      set Xc [expr {$cfg(X)+$offsetI}]
      set Yc [expr {$cfg(Y)+$offsetJ}]



      #compute "start" angle
      set theta0 [expr {atan((0-$offsetJ)/(0-$offsetI))}]

      #compute "stop" angle
      set theta1 [expr {atan(($new(Y)-$Yc)/($new(X)-$Xc))}]



      set stepAngle 5





      lassign [list $cfg(X) $cfg(Y)] x0 y0
      set Pi 3.14159265










      for {set deg [expr {$theta0+$stepAngle}]} {$deg <= $theta1} {incr deg $stepAngle} {
        set rads [expr {$deg/360.0*2*$Pi}]

        set x1 [expr {cos($rads)*$radius+$Xc}]
        set y1 [expr {sin($rads)*$radius+$Yc}]




        lappend pathList [list \
                              $width\
                              $type\
                              $color\
                              [list $x0 $y0 $cfg(Z)]\
                              [list $x1 $y1 $cfg(Z)]\
                             ]
        lassign [list $x1 $y1] x0 y0
      }







      lassign [list $new(X) $new(Y) $new(E)] cfg(X) cfg(Y) cfg(E)
    }

    proc G28 {args} {;## move to origin
      variable cfg
      if {($cfg(X) != 0) || ($cfg(Y) != 0) || ($cfg(Z) != 0)} {
        ## move to 0







>










>







 







>
>
>

<
>

<
>
>
>
|
>
>
>
>
>

<
>
>
>
>
>
>
>
>
>
>
|












>
>
>
>
>
>
>












>







 







>
>
>

<
>

<
>
>
>
|
>
>
>
>
>

<
>
>
>
>
>
>
>
>
>
>
|

>
|
|
>
>
>
>









>
>
>
>
>
>
>







77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
...
131
132
133
134
135
136
137
138
139
140
141

142
143

144
145
146
147
148
149
150
151
152
153

154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
...
232
233
234
235
236
237
238
239
240
241
242

243
244

245
246
247
248
249
250
251
252
253
254

255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
                            [list $cfg(X) $cfg(Y) $cfg(Z)]\
                            [list $new(X) $new(Y) $new(Z)]\
                            ]
      lassign [list $new(X) $new(Y) $new(Z) $new(E)] cfg(X) cfg(Y) cfg(Z) cfg(E)
    }
    
    proc G2 {args} {
      return
      ## clockwise arc (ref http://linuxcnc.org/docs/html/gcode/gcode.html#sec:G2-G3-Arc)
      ## note this code is only valid for XY arcs
      variable cfg
      variable pathList
      set new(X) $cfg(X)
      set new(Y) $cfg(Y)
      set new(Z) $cfg(Z)
      set new(E) $cfg(E)
      set offsetI 0    ;# x offset to center
      set offsetJ 0    ;# y offset to center
      set saveArgs $args
      while {[llength $args] > 0} {
        set args [lassign $args cmd]
        switch -regexp -matchvar matchvar -- $cmd {
          {X(-?[[:digit:]]+(\.[[:digit:]]+)?)} {
            set new(X) [expr {$cfg(scale)*[lindex $matchvar 1]+($cfg(relP) ? $cfg(X) : 0)}]
            #puts stderr "X $matchvar"
          }
................................................................................
      } else {
        set type [list move Layer_$cfg(layerNum)]
        set color yellow
        set width 1
      }
      set Xc [expr {$cfg(X)+$offsetI}]
      set Yc [expr {$cfg(Y)+$offsetJ}]
      set r0 [expr {sqrt(($offsetI*$offsetI)+($offsetJ*$offsetJ))}]
      set r1 [expr {sqrt(($new(X)-$Xc)*($new(X)-$Xc)+($new(Y)-$Yc)*($new(Y)-$Yc))}]
      set Pi 3.14159265
      #compute "start" angle

      set theta0 [expr {180.0*atan2((0-$offsetJ),(0-$offsetI))/$Pi}]
      #compute "stop" angle

      set theta1 [expr {180.0*atan2(($new(Y)-$Yc),($new(X)-$Xc))/$Pi}]
      if {$theta1 > $theta0} {set theta0 [expr {$theta0+360}]}
      set theta [expr {$theta0-$theta1}]
      set stepAngle -30
      if       {$theta > 270} {set stepAngle [expr {$theta/-20}]
      } elseif {$theta > 180} {set stepAngle [expr {$theta/-12}]
      } elseif {$theta >  90} {set stepAngle [expr {$theta/-9}]
      } elseif {$theta >  45} {set stepAngle [expr {$theta/-5}]
      } else                  {set stepAngle [expr {$theta/-3}]}
      lassign [list $cfg(X) $cfg(Y)] x0 y0

      if {abs(1.0-($r1/$r0)) > 0.1} {
        set color red
        log::Puts "WARNING, non-circular G2 arc squish=[expr abs(1.0-($r1/$r0))*100.0]% @ line??\n $saveArgs"
      } else {
        set radius $r0
      }
      if {($theta0+$stepAngle) < $theta1} {
        ## arc is smaller than stepsize
        set stepAngle [expr {$theta1-$theta0}]
      }
      for {set deg [expr {$theta0+$stepAngle}]} {$deg >= $theta1} {set deg [expr {$deg+$stepAngle}]} {
        set rads [expr {$deg/360.0*2*$Pi}]
        set x1 [expr {cos($rads)*$radius+$Xc}]
        set y1 [expr {sin($rads)*$radius+$Yc}]
        lappend pathList [list \
                              $width\
                              $type\
                              $color\
                              [list $x0 $y0 $cfg(Z)]\
                              [list $x1 $y1 $cfg(Z)]\
                             ]
        lassign [list $x1 $y1] x0 y0
      }
      lappend pathList [list \
                            $width\
                            $type\
                            white\
                            [list $x0     $y0     $cfg(Z)]\
                            [list $new(X) $new(Y) $cfg(Z)]\
                           ]
      lassign [list $new(X) $new(Y) $new(E)] cfg(X) cfg(Y) cfg(E)
    }
    proc G3 {args} {
      ## counter-clockwise arc (ref http://linuxcnc.org/docs/html/gcode/gcode.html#sec:G2-G3-Arc)
      variable cfg
      variable pathList
      set new(X) $cfg(X)
      set new(Y) $cfg(Y)
      set new(Z) $cfg(Z)
      set new(E) $cfg(E)
      set offsetI 0    ;# x offset to center
      set offsetJ 0    ;# y offset to center
      set saveArgs $args
      while {[llength $args] > 0} {
        set args [lassign $args cmd]
        switch -regexp -matchvar matchvar -- $cmd {
          {X(-?[[:digit:]]+(\.[[:digit:]]+)?)} {
            set new(X) [expr {$cfg(scale)*[lindex $matchvar 1]+($cfg(relP) ? $cfg(X) : 0)}]
            #puts stderr "X $matchvar"
          }
................................................................................
      } else {
        set type [list move Layer_$cfg(layerNum)]
        set color yellow
        set width 1
      }
      set Xc [expr {$cfg(X)+$offsetI}]
      set Yc [expr {$cfg(Y)+$offsetJ}]
      set r0 [expr {sqrt(($offsetI*$offsetI)+($offsetJ*$offsetJ))}]
      set r1 [expr {sqrt(($new(X)-$Xc)*($new(X)-$Xc)+($new(Y)-$Yc)*($new(Y)-$Yc))}]
      set Pi 3.14159265
      #compute "start" angle

      set theta0 [expr {180.0*atan2((0-$offsetJ),(0-$offsetI))/$Pi}]
      #compute "stop" angle

      set theta1 [expr {180.0*atan2(($new(Y)-$Yc),($new(X)-$Xc))/$Pi}]
      if {$theta1 < $theta0} {set theta1 [expr {$theta1+360}]}
      set theta [expr {$theta1-$theta0}]
      set stepAngle 30
      if       {$theta > 270} {set stepAngle [expr {$theta/20}]
      } elseif {$theta > 180} {set stepAngle [expr {$theta/12}]
      } elseif {$theta >  90} {set stepAngle [expr {$theta/9}]
      } elseif {$theta >  45} {set stepAngle [expr {$theta/5}]
      } else                  {set stepAngle [expr {$theta/3}]}
      lassign [list $cfg(X) $cfg(Y)] x0 y0

      if {abs(1.0-($r1/$r0)) > 0.1} {
        set color red
        log::Puts "WARNING, non-circular G3 arc squish=[expr abs(1.0-($r1/$r0))*100.0]% @ line??\n $saveArgs"
      } else {
        set radius $r0
      }
      if {($theta0+$stepAngle) > $theta1} {
        ## arc is smaller than stepsize
        set stepAngle [expr {$theta1-$theta0}]
      }
      for {set deg [expr {$theta0+$stepAngle}]} {$deg <= $theta1} {set deg [expr {$deg+$stepAngle}]} {
        set rads [expr {$deg/360.0*2*$Pi}]
        if {[info exists radius]} {
          set x1 [expr {cos($rads)*$radius+$Xc}]
          set y1 [expr {sin($rads)*$radius+$Yc}]
        } else {
          set x1 [expr {cos($rads)*(($r1-$r0)*(($deg-$theta0)/($theta1-$theta0))+$r0)+$Xc}]
          set y1 [expr {sin($rads)*(($r1-$r0)*(($deg-$theta0)/($theta1-$theta0))+$r0)+$Yc}]
        }
        lappend pathList [list \
                              $width\
                              $type\
                              $color\
                              [list $x0 $y0 $cfg(Z)]\
                              [list $x1 $y1 $cfg(Z)]\
                             ]
        lassign [list $x1 $y1] x0 y0
      }
      lappend pathList [list \
                            $width\
                            $type\
                            lightgreen\
                            [list $x0     $y0     $cfg(Z)]\
                            [list $new(X) $new(Y) $cfg(Z)]\
                           ]
      lassign [list $new(X) $new(Y) $new(E)] cfg(X) cfg(Y) cfg(E)
    }

    proc G28 {args} {;## move to origin
      variable cfg
      if {($cfg(X) != 0) || ($cfg(Y) != 0) || ($cfg(Z) != 0)} {
        ## move to 0