Frusta

Check-in [ec0df6213a]
Login

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

Overview
Comment:fixed G2/G3 (arcs) to gcode reader
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:ec0df6213ab5f9c02c27450562dcd5d1f5399fc5
User & Date: rmelton 2013-10-24 17:08:25
Context
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
2013-10-23
19:16
fixed exercise to work at z0 check-in: 80ef4bd8f0 user: rmelton tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

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

75
76
77
78
79
80
81























































































































































82
83
84
85
86
87
88
                            $type\
                            $color\
                            [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 G28 {args} {;## move to origin
      variable cfg
      if {($cfg(X) != 0) || ($cfg(Y) != 0) || ($cfg(Z) != 0)} {
        ## move to 0
      }
      lassign {0 0 0} cfg(X) cfg(Y) cfg(Z)
    }







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







75
76
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
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
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
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
237
238
239
                            $type\
                            $color\
                            [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"
          }
          {Y(-?[[:digit:]]+(\.[[:digit:]]+)?)} {
            set new(Y) [expr {$cfg(scale)*[lindex $matchvar 1]+($cfg(relP) ? $cfg(Y) : 0)}]
            #puts stderr "Y $matchvar"
          }
          {F(-?[[:digit:]]+(\.[[:digit:]]+)?)} {
            #puts stderr "F $matchvar"
          }
          {E(-?[[:digit:]]+(\.[[:digit:]]+)?)} {
            set new(E) [expr {$cfg(scale)*[lindex $matchvar 1]+($cfg(relE) ? $cfg(E) : 0)}]
            #puts stderr "E $matchvar"
          }
          {I(-?[[:digit:]]+(\.[[:digit:]]+)?)} {
            set offsetI [expr {$cfg(scale)*[lindex $matchvar 1]}]
          }
          {J(-?[[:digit:]]+(\.[[:digit:]]+)?)} {
            set offsetJ [expr {$cfg(scale)*[lindex $matchvar 1]}]
          }
          default {error "G2 bad cmd=$cmd"}
        }
      }
      if {$new(E) > $cfg(E)} {
        set type [list fill Layer_$cfg(layerNum)]
        set color purple
        if {$new(Z) > $cfg(layerZ)} {
          incr cfg(layerNum)
          set cfg(layerZ) $new(Z)
        }
        set width 1
      } 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"
          }
          {Y(-?[[:digit:]]+(\.[[:digit:]]+)?)} {
            set new(Y) [expr {$cfg(scale)*[lindex $matchvar 1]+($cfg(relP) ? $cfg(Y) : 0)}]
            #puts stderr "Y $matchvar"
          }
          {F(-?[[:digit:]]+(\.[[:digit:]]+)?)} {
            #puts stderr "F $matchvar"
          }
          {E(-?[[:digit:]]+(\.[[:digit:]]+)?)} {
            set new(E) [expr {$cfg(scale)*[lindex $matchvar 1]+($cfg(relE) ? $cfg(E) : 0)}]
            #puts stderr "E $matchvar"
          }
          {I(-?[[:digit:]]+(\.[[:digit:]]+)?)} {
            set offsetI [expr {$cfg(scale)*[lindex $matchvar 1]}]
          }
          {J(-?[[:digit:]]+(\.[[:digit:]]+)?)} {
            set offsetJ [expr {$cfg(scale)*[lindex $matchvar 1]}]
          }
          default {error "G3 bad cmd=$cmd"}
        }
      }
      if {$new(E) > $cfg(E)} {
        set type [list fill Layer_$cfg(layerNum)]
        set color purple
        if {$new(Z) > $cfg(layerZ)} {
          incr cfg(layerNum)
          set cfg(layerZ) $new(Z)
        }
        set width 1
      } 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
      }
      lassign {0 0 0} cfg(X) cfg(Y) cfg(Z)
    }