Skip to content
This repository was archived by the owner on Jul 9, 2020. It is now read-only.

Commit 85a33c7

Browse files
wilrikerdc42
authored andcommitted
Implement G38.x (2 <= x <= 5) (Duet3D#324)
* Implement G38.x (2 <= x <= 5) * Improve code readability * Remove accidentally committed change to Endstop.h * Fix missing initialization and deduplicate code * Use GCodeResult::error instead of GCodeResult::badOrMissingParameter * Fix filename in comment, add description and revert changes in RepRapFirmware.h
1 parent 25e05cf commit 85a33c7

File tree

13 files changed

+392
-15
lines changed

13 files changed

+392
-15
lines changed

src/Endstops/EndstopsManager.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,12 +91,13 @@ void EndstopsManager::EnableAxisEndstops(AxesBitmap axes, bool forHoming)
9191
}
9292

9393
// Set up the active endstops for Z probing
94-
void EndstopsManager::EnableZProbe(size_t probeNumber)
94+
void EndstopsManager::EnableZProbe(size_t probeNumber, bool probingAway)
9595
{
9696
activeEndstops = nullptr;
9797
isHomingMove = false;
9898
if (probeNumber < MaxZProbes && zProbes[probeNumber] != nullptr)
9999
{
100+
zProbes[probeNumber]->SetProbingAway(probingAway);
100101
AddToActive(*zProbes[probeNumber]);
101102
}
102103
}

src/Endstops/EndstopsManager.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@ class EndstopsManager
3030
void EnableAxisEndstops(AxesBitmap axes, bool forHoming);
3131

3232
// Set up the active endstops for Z probing
33-
void EnableZProbe(size_t probeNumber);
33+
void EnableZProbe(size_t probeNumber, bool probingAway = false);
3434

3535
// Set up the active endstops for Z probing with the current probe
36-
void EnableCurrentZProbe() { EnableZProbe(currentZProbeNumber); }
36+
void EnableCurrentZProbe(bool probingAway = false) { EnableZProbe(currentZProbeNumber, probingAway); }
3737

3838
// Enable extruder endstops
3939
void EnableExtruderEndstop(size_t extruder);
@@ -56,6 +56,7 @@ class EndstopsManager
5656
GCodeResult HandleG31(GCodeBuffer& gb, const StringRef& reply); // G31
5757

5858
ZProbe& GetCurrentZProbe() const;
59+
const size_t GetCurrentZProbeNumber() const { return currentZProbeNumber; }
5960
ZProbe *GetZProbe(size_t num) const;
6061
void SetZProbeDefaults();
6162
GCodeResult ProgramZProbe(GCodeBuffer& gb, const StringRef& reply);

src/Endstops/ZProbe.cpp

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ void ZProbe::SetDefaults()
3232
recoveryTime = 0.0;
3333
tolerance = DefaultZProbeTolerance;
3434
misc.parts.maxTaps = DefaultZProbeTaps;
35-
misc.parts.invertReading = misc.parts.turnHeatersOff = misc.parts.saveToConfigOverride = false;
35+
misc.parts.invertReading = misc.parts.turnHeatersOff = misc.parts.saveToConfigOverride = misc.parts.probingAway = false;
3636
type = ZProbeType::none;
3737
sensor = -1;
3838
}
@@ -136,7 +136,21 @@ EndStopHit ZProbe::Stopped() const
136136
EndstopHitDetails ZProbe::CheckTriggered(bool goingSlow)
137137
{
138138
EndstopHitDetails rslt;
139-
switch (Stopped())
139+
EndStopHit e = Stopped();
140+
141+
// Note: This might need to be moved into Stopped() to not having to duplicate it to ZProbeEndstop
142+
if (misc.parts.probingAway) {
143+
switch (e) {
144+
case EndStopHit::atStop:
145+
e = EndStopHit::noStop;
146+
break;
147+
default:
148+
e = EndStopHit::atStop;
149+
break;
150+
}
151+
}
152+
153+
switch (e)
140154
{
141155
case EndStopHit::atStop:
142156
rslt.SetAction(EndstopHitAction::stopAll);

src/Endstops/ZProbe.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ class ZProbe : public EndstopOrZProbe
4444
bool GetSaveToConfigOverride() const { return misc.parts.saveToConfigOverride; }
4545
int GetAdcValue() const { return adcValue; }
4646
unsigned int GetMaxTaps() const { return misc.parts.maxTaps; }
47+
void SetProbingAway(const bool probingAway) { misc.parts.probingAway = probingAway; }
4748

4849
int GetReading() const;
4950
int GetSecondaryValues(int& v1, int& v2);
@@ -67,10 +68,11 @@ class ZProbe : public EndstopOrZProbe
6768
{
6869
struct
6970
{
70-
uint16_t maxTaps : 5, // maximum probes at each point
71-
invertReading : 1, // true if we need to invert the reading
72-
turnHeatersOff : 1, // true to turn heaters off while probing
73-
saveToConfigOverride : 1; // true if the trigger height should be saved to config-override.g
71+
uint16_t maxTaps : 5, // maximum probes at each point
72+
invertReading : 1, // true if we need to invert the reading
73+
probingAway : 1, // true if we are probing away, i.e. until contact lost
74+
turnHeatersOff : 1, // true to turn heaters off while probing
75+
saveToConfigOverride : 1; // true if the trigger height should be saved to config-override.g
7476
} parts;
7577
uint16_t all;
7678
} misc;

src/GCodes/GCodeMachineState.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,12 @@ enum class GCodeState : uint8_t
8080
probingAtPoint6,
8181
probingAtPoint7,
8282

83+
// These next 4 must be contiguous
84+
straightProbe0,
85+
straightProbe1,
86+
straightProbe2,
87+
straightProbe3,
88+
8389
doingFirmwareRetraction,
8490
doingFirmwareUnRetraction,
8591
loadingFilament,

src/GCodes/GCodes.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2504,18 +2504,23 @@ MessageType GCodes::GetMessageBoxDevice(GCodeBuffer& gb) const
25042504
return mt;
25052505
}
25062506

2507-
// Do a manual bed probe. On entry the state variable is the state we want to return to when the user has finished adjusting the height.
2508-
void GCodes::DoManualProbe(GCodeBuffer& gb)
2507+
void GCodes::DoManualProbe(GCodeBuffer& gb, const char *message, const char *title, const AxesBitmap axes)
25092508
{
25102509
if (Push(gb, true)) // stack the machine state including the file position and set the state to GCodeState::normal
25112510
{
25122511
gb.MachineState().CloseFile(); // stop reading from file if we were
25132512
gb.MachineState().waitingForAcknowledgement = true; // flag that we are waiting for acknowledgement
25142513
const MessageType mt = GetMessageBoxDevice(gb);
2515-
platform.SendAlert(mt, "Adjust height until the nozzle just touches the bed, then press OK", "Manual bed probing", 2, 0.0, MakeBitmap<AxesBitmap>(Z_AXIS));
2514+
platform.SendAlert(mt, message, title, 2, 0.0, axes);
25162515
}
25172516
}
25182517

2518+
// Do a manual bed probe. On entry the state variable is the state we want to return to when the user has finished adjusting the height.
2519+
void GCodes::DoManualBedProbe(GCodeBuffer& gb)
2520+
{
2521+
DoManualProbe(gb, "Adjust height until the nozzle just touches the bed, then press OK", "Manual bed probing", MakeBitmap<AxesBitmap>(Z_AXIS));
2522+
}
2523+
25192524
// Start probing the grid, returning true if we didn't because of an error.
25202525
// Prior to calling this the movement system must be locked.
25212526
GCodeResult GCodes::ProbeGrid(GCodeBuffer& gb, const StringRef& reply)

src/GCodes/GCodes.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,7 @@ class GCodes INHERIT_OBJECT_MODEL
259259
void SetBedEquationWithProbe(int sParam, const StringRef& reply); // Probes a series of points and sets the bed equation
260260
GCodeResult SetOrReportOffsets(GCodeBuffer& gb, const StringRef& reply); // Deal with a G10
261261
GCodeResult SetPositions(GCodeBuffer& gb); // Deal with a G92
262+
GCodeResult StraightProbe(GCodeBuffer& gb, const StringRef& reply); // Deal with a G38.x
262263
GCodeResult DoDriveMapping(GCodeBuffer& gb, const StringRef& reply); // Deal with a M584
263264
GCodeResult ProbeTool(GCodeBuffer& gb, const StringRef& reply); // Deal with a M585
264265
GCodeResult FindCenterOfCavity(GCodeBuffer& gb, const StringRef& reply, const bool towardsMin = true); // Deal with a M675
@@ -350,7 +351,8 @@ class GCodes INHERIT_OBJECT_MODEL
350351
void CopyConfigFinalValues(GCodeBuffer& gb); // Copy the feed rate etc. from the daemon to the input channels
351352

352353
MessageType GetMessageBoxDevice(GCodeBuffer& gb) const; // Decide which device to display a message box on
353-
void DoManualProbe(GCodeBuffer& gb); // Do a manual bed probe
354+
void DoManualProbe(GCodeBuffer&, const char *message, const char *title, const AxesBitmap); // Do manual probe in arbitrary direction
355+
void DoManualBedProbe(GCodeBuffer& gb); // Do a manual bed probe
354356

355357
void AppendAxes(const StringRef& reply, AxesBitmap axes) const; // Append a list of axes to a string
356358

src/GCodes/GCodes2.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,14 @@ bool GCodes::HandleGcode(GCodeBuffer& gb, const StringRef& reply)
304304
DoFileMacro(gb, BED_EQUATION_G, true, 32); // Try to execute bed.g
305305
break;
306306

307+
case 38: // Straight probe - move until either the probe is triggered or the commanded move ends
308+
if (!LockMovementAndWaitForStandstill(gb))
309+
{
310+
return false;
311+
}
312+
result = StraightProbe(gb, reply);
313+
break;
314+
307315
case 53: // Temporarily use machine coordinates
308316
gb.MachineState().g53Active = true;
309317
break;

src/GCodes/GCodes3.cpp

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* This file contains functions that are called form file GCodes2.cpp to execute various G and M codes.
77
*/
88

9+
#include <Movement/StraightProbeSettings.h>
910
#include "GCodes.h"
1011

1112
#include "GCodeBuffer/GCodeBuffer.h"
@@ -676,6 +677,105 @@ GCodeResult GCodes::DoDriveMapping(GCodeBuffer& gb, const StringRef& reply)
676677

677678
return GCodeResult::ok;
678679
}
680+
// Handle G38.[2-5]
681+
GCodeResult GCodes::StraightProbe(GCodeBuffer& gb, const StringRef& reply)
682+
{
683+
const int8_t fraction = gb.GetCommandFraction();
684+
if (fraction < 2 || fraction > 5) {
685+
return GCodeResult::warningNotSupported;
686+
}
687+
/*
688+
* It is an error if:
689+
* # the current point is the same as the programmed point.
690+
* # no axis word is used
691+
* # the feed rate is zero
692+
* # the probe is already in the target state
693+
*/
694+
695+
StraightProbeSettings& sps = reprap.GetMove().GetStraightProbeSettings();
696+
sps.Reset();
697+
698+
699+
switch (fraction)
700+
{
701+
case 2:
702+
sps.SetStraightProbeType(StraightProbeType::towardsWorkpieceErrorOnFailure);
703+
break;
704+
705+
case 3:
706+
sps.SetStraightProbeType(StraightProbeType::towardsWorkpiece);
707+
break;
708+
709+
case 4:
710+
sps.SetStraightProbeType(StraightProbeType::awayFromWorkpieceErrorOnFailure);
711+
break;
712+
713+
case 5:
714+
sps.SetStraightProbeType(StraightProbeType::awayFromWorkpiece);
715+
break;
716+
}
717+
718+
// Get the target coordinates and check if we would move at all
719+
float target[MaxAxes];
720+
ToolOffsetTransform(currentUserPosition, target);
721+
bool seen = false;
722+
bool doesMove = false;
723+
for (size_t axis = 0; axis < MaxAxes; axis++)
724+
{
725+
if (gb.Seen(axisLetters[axis]))
726+
{
727+
seen = true;
728+
const float axisTarget = gb.GetFValue();
729+
if (axisTarget != target[axis])
730+
{
731+
doesMove = true;
732+
}
733+
target[axis] = axisTarget;
734+
sps.AddMovingAxis(axis);
735+
}
736+
}
737+
738+
// No axis letters seen
739+
if (!seen)
740+
{
741+
// Signal error for G38.2 and G38.4
742+
if (sps.SignalError())
743+
{
744+
reply.copy("No axis specified.");
745+
return GCodeResult::error;
746+
}
747+
return GCodeResult::ok;
748+
}
749+
750+
// At least one axis seen but it would not result in movement
751+
else if (!doesMove)
752+
{
753+
// Signal error for G38.2 and G38.4
754+
if (sps.SignalError())
755+
{
756+
reply.copy("Target equals current position.");
757+
return GCodeResult::error;
758+
}
759+
return GCodeResult::ok;
760+
}
761+
sps.SetTarget(target);
762+
763+
// See whether we are using a user-defined Z probe or just current one
764+
size_t probeToUse = platform.GetEndstops().GetCurrentZProbeNumber();
765+
if (gb.Seen('P'))
766+
{
767+
probeToUse = gb.GetUIValue();
768+
if (platform.GetEndstops().GetZProbe(probeToUse) == nullptr)
769+
{
770+
reply.copy("Invalid probe number");
771+
return GCodeResult::error;
772+
}
773+
}
774+
sps.SetZProbeToUse(probeToUse);
775+
776+
gb.SetState(GCodeState::straightProbe0);
777+
return GCodeResult::ok;
778+
}
679779

680780
// Deal with a M585
681781
GCodeResult GCodes::ProbeTool(GCodeBuffer& gb, const StringRef& reply)

0 commit comments

Comments
 (0)