diff --git a/data/option.go b/data/option.go index 5d081a0..bbce068 100644 --- a/data/option.go +++ b/data/option.go @@ -160,7 +160,7 @@ func (gch GCodeHunk) DoesInstructionContainCodes(targetCodes []string) bool { break } } - if contained == true { + if contained { break } } @@ -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.") diff --git a/gcode/builder.go b/gcode/builder.go index f0b5896..2b829fd 100644 --- a/gcode/builder.go +++ b/gcode/builder.go @@ -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 @@ -89,11 +91,11 @@ 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") @@ -101,14 +103,17 @@ func (g *Builder) AddMoveSpeed(p data.MicroVec3, extrusion data.Millimeter, spee 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 { @@ -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) { diff --git a/gcode/builder_test.go b/gcode/builder_test.go index fb8ea49..f45c014 100644 --- a/gcode/builder_test.go +++ b/gcode/builder_test.go @@ -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) { @@ -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) { @@ -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) { @@ -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) { @@ -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": { @@ -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": { @@ -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": { @@ -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", }, }