Skip to content

Commit

Permalink
Add Zhop to cli params, fix test and make sure that the very first mo…
Browse files Browse the repository at this point in the history
…ve comands of each direction gets included even if it is 0
  • Loading branch information
aligator committed Nov 7, 2022
1 parent 1ca10ad commit e68c54d
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 58 deletions.
4 changes: 2 additions & 2 deletions data/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ func (gch GCodeHunk) DoesInstructionContainCodes(targetCodes []string) bool {
break
}
}
if contained == true {
if contained {
break
}
}
Expand Down Expand Up @@ -527,7 +527,7 @@ func ParseFlags() Options {
flag.IntVar(&options.Filament.InitialTemperatureLayerCount, "initial-temperature-layer-count", options.Filament.InitialTemperatureLayerCount, "The number of layers which use the initial temperatures. After this amount of layers, the normal temperatures are used.")
flag.Var(&options.Filament.RetractionSpeed, "retraction-speed", "The speed used for retraction in mm/s.")
flag.Var(&options.Filament.RetractionLength, "retraction-length", "The amount to retract in millimeter.")
flag.Var(&options.Filament.RetractionZHop, "retraction-zhop", "The amount to lift the head when retracting in millimeter.")
flag.Var(&options.Filament.RetractionZHop, "retraction-z-hop", "The amount to lift head when retracting in millimeter.")
flag.Var(&options.Filament.FanSpeed, "fan-speed", "Comma separated layer/primary-fan-speed. eg. --fan-speed 3=20,10=40 indicates at layer 3 set fan to 20 and at layer 10 set fan to 40. Fan speed can range from 0-255.")
flag.IntVar(&options.Filament.ExtrusionMultiplier, "extrusion-multiplier", options.Filament.ExtrusionMultiplier, "The multiplier in % used to change the amount of filament being extruded. Can be used to mitigate under/over extrusion.")

Expand Down
18 changes: 11 additions & 7 deletions gcode/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ type Builder struct {
extrusionAmount data.Millimeter
extrusionPerMM data.Millimeter
currentPosition data.MicroVec3
notFirstMove bool
notFirstMoveX bool
notFirstMoveY bool
notFirstMoveZ bool
moveSpeed, extrudeSpeed, currentSpeed, extrudeSpeedOverride int

retractionSpeed int
Expand Down Expand Up @@ -89,26 +91,29 @@ func (g *Builder) AddComment(comment string, args ...interface{}) {
}

func (g *Builder) AddMoveSpeed(p data.MicroVec3, extrusion data.Millimeter, speed int) {
notFirstMove := g.notFirstMoveX || g.notFirstMoveY || g.notFirstMoveZ
// Ignore moves which are of zero length.
if g.notFirstMove && g.currentPosition.X() == p.X() && g.currentPosition.Y() == p.Y() && g.currentPosition.Z() == p.Z() && extrusion == 0 {
if notFirstMove && g.currentPosition.X() == p.X() && g.currentPosition.Y() == p.Y() && g.currentPosition.Z() == p.Z() && extrusion == 0 {
return
}
g.notFirstMove = true

if extrusion != 0 {
g.buf.WriteString("G1")
} else {
g.buf.WriteString("G0")
}

if p.X() != g.currentPosition.X() {
if p.X() != g.currentPosition.X() || !g.notFirstMoveX {
g.buf.WriteString(fmt.Sprintf(" X%0.2f", p.X().ToMillimeter()))
g.notFirstMoveX = true
}
if p.Y() != g.currentPosition.Y() {
if p.Y() != g.currentPosition.Y() || !g.notFirstMoveY {
g.buf.WriteString(fmt.Sprintf(" Y%0.2f", p.Y().ToMillimeter()))
g.notFirstMoveY = true
}
if p.Z() != g.currentPosition.Z() {
if p.Z() != g.currentPosition.Z() || !g.notFirstMoveZ {
g.buf.WriteString(fmt.Sprintf(" Z%0.2f", p.Z().ToMillimeter()))
g.notFirstMoveZ = true
}

if g.currentSpeed != speed {
Expand All @@ -123,7 +128,6 @@ func (g *Builder) AddMoveSpeed(p data.MicroVec3, extrusion data.Millimeter, spee
g.buf.WriteString("\n")

g.currentPosition = p

}

func (g *Builder) AddMove(p data.MicroVec3, extrusion data.Millimeter) {
Expand Down
100 changes: 51 additions & 49 deletions gcode/builder_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package gcode_test

import (
"testing"

"github.com/aligator/goslice/data"
"github.com/aligator/goslice/gcode"
"github.com/aligator/goslice/util/test"
"testing"
)

func TestGCodeBuilder(t *testing.T) {
Expand Down Expand Up @@ -70,15 +71,16 @@ func TestGCodeBuilder(t *testing.T) {
}, 100, false)
test.Ok(t, err)
},
expected: "G0 X0.00 Y0.00 Z0.10\n" +
"G0 X0.10 Y0.00\n" +
"G0 X0.10 Y0.10\n" +
"G0 X0.00 Y0.10\n" +
"G0 X0.00 Y0.00\n" +
"G0 X0.05 Y0.00\n" +
"G0 X0.05 Y0.05\n" +
"G0 X0.00 Y0.05\n" +
"G0 X0.00 Y0.00\n",
expected: `G0 X0.00 Y0.00 Z0.10
G0 X0.10
G0 Y0.10
G0 X0.00
G0 Y0.00
G0 X0.05
G0 Y0.05
G0 X0.00
G0 Y0.00
`,
},
"some moves": {
exec: func(b *gcode.Builder) {
Expand All @@ -93,15 +95,15 @@ func TestGCodeBuilder(t *testing.T) {
b.AddMove(data.NewMicroVec3(0, 20, 0), 4)
b.AddMove(data.NewMicroVec3(0, 0, 30), 5)
},
expected: "G0 X0.00 Y0.00\n" +
"G0 X0.01 Y0.00\n" +
expected: "G0 X0.00 Y0.00 Z0.00\n" +
"G0 X0.01\n" +
"G1 X0.00 Y0.02 E5.0000\n" +
"G0 X0.00 Y0.00 Z0.03\n" +
"G0 X0.01 Y0.00\n" +
"G1 X0.00 Y0.00 Z0.00 E7.0000\n" +
"G1 X0.01 Y0.00 E10.0000\n" +
"G0 Y0.00 Z0.03\n" +
"G0 X0.01\n" +
"G1 X0.00 Z0.00 E7.0000\n" +
"G1 X0.01 E10.0000\n" +
"G1 X0.00 Y0.02 E14.0000\n" +
"G1 X0.00 Y0.00 Z0.03 E19.0000\n",
"G1 Y0.00 Z0.03 E19.0000\n",
},
"moves with zero length and no extrusion get ignored": {
exec: func(b *gcode.Builder) {
Expand All @@ -121,13 +123,13 @@ func TestGCodeBuilder(t *testing.T) {
b.AddMove(data.NewMicroVec3(0, 0, 0), 2)
b.AddMove(data.NewMicroVec3(10, 0, 0), 3)
},
expected: "G0 X0.00 Y0.00\n" +
"G0 X0.01 Y0.00\n" +
expected: "G0 X0.00 Y0.00 Z0.00\n" +
"G0 X0.01\n" +
"G1 X0.00 Y0.02 E5.0000\n" +
"G0 X0.00 Y0.00 Z0.03\n" +
"G1 X0.00 Y0.00 E10.0000\n" +
"G1 X0.00 Y0.00 Z0.00 E12.0000\n" +
"G1 X0.01 Y0.00 E15.0000\n",
"G0 Y0.00 Z0.03\n" +
"G1 E10.0000\n" +
"G1 Z0.00 E12.0000\n" +
"G1 X0.01 E15.0000\n",
},
"different speeds": {
exec: func(b *gcode.Builder) {
Expand Down Expand Up @@ -163,26 +165,26 @@ func TestGCodeBuilder(t *testing.T) {
b.AddMove(data.NewMicroVec3(40, 0, 0), 5)
b.AddMove(data.NewMicroVec3(10, 0, 0), 0)
},
expected: "G0 X0.00 Y0.00 F12000\n" +
"G1 X0.01 Y0.00 F6000 E5.0000\n" +
"G1 X0.04 Y0.00 E10.0000\n" +
"G0 X0.01 Y0.00 F12000\n" +
"G0 X0.00 Y0.00\n" +
"G1 X0.01 Y0.00 F9000 E15.0000\n" +
"G1 X0.04 Y0.00 E20.0000\n" +
"G0 X0.01 Y0.00 F12000\n" +
"G0 X0.00 Y0.00\n" +
"G1 X0.01 Y0.00 F6000 E25.0000\n" +
"G1 X0.04 Y0.00 E30.0000\n" +
"G0 X0.01 Y0.00 F12000\n" +
"G0 X0.00 Y0.00 F36000\n" +
"G1 X0.01 Y0.00 F6000 E35.0000\n" +
"G1 X0.04 Y0.00 E40.0000\n" +
"G0 X0.01 Y0.00 F36000\n" +
"G0 X0.00 Y0.00\n" +
"G1 X0.01 Y0.00 F30000 E45.0000\n" +
"G1 X0.04 Y0.00 E50.0000\n" +
"G0 X0.01 Y0.00 F36000\n",
expected: "G0 X0.00 Y0.00 Z0.00 F12000\n" +
"G1 X0.01 F6000 E5.0000\n" +
"G1 X0.04 E10.0000\n" +
"G0 X0.01 F12000\n" +
"G0 X0.00\n" +
"G1 X0.01 F9000 E15.0000\n" +
"G1 X0.04 E20.0000\n" +
"G0 X0.01 F12000\n" +
"G0 X0.00\n" +
"G1 X0.01 F6000 E25.0000\n" +
"G1 X0.04 E30.0000\n" +
"G0 X0.01 F12000\n" +
"G0 X0.00 F36000\n" +
"G1 X0.01 F6000 E35.0000\n" +
"G1 X0.04 E40.0000\n" +
"G0 X0.01 F36000\n" +
"G0 X0.00\n" +
"G1 X0.01 F30000 E45.0000\n" +
"G1 X0.04 E50.0000\n" +
"G0 X0.01 F36000\n",
},

"set extrusion": {
Expand All @@ -194,8 +196,8 @@ func TestGCodeBuilder(t *testing.T) {
}, 0, true)
test.Ok(t, err)
},
expected: "G0 X0.00 Y0.00\n" +
"G1 X0.00 Y10.00 E0.3326\n",
expected: "G0 X0.00 Y0.00 Z0.00\n" +
"G1 Y10.00 E0.3326\n",
},

"set extrusion with over extrusion": {
Expand All @@ -208,8 +210,8 @@ func TestGCodeBuilder(t *testing.T) {
}, 0, true)
test.Ok(t, err)
},
expected: "G0 X0.00 Y0.00\n" +
"G1 X0.00 Y10.00 E0.4989\n",
expected: "G0 X0.00 Y0.00 Z0.00\n" +
"G1 Y10.00 E0.4989\n",
},

"set extrusion with under extrusion": {
Expand All @@ -222,8 +224,8 @@ func TestGCodeBuilder(t *testing.T) {
}, 0, true)
test.Ok(t, err)
},
expected: "G0 X0.00 Y0.00\n" +
"G1 X0.00 Y10.00 E0.1663\n",
expected: "G0 X0.00 Y0.00 Z0.00\n" +
"G1 Y10.00 E0.1663\n",
},
}

Expand Down

0 comments on commit e68c54d

Please sign in to comment.