Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/3.4-dev' into v3-chrishamm
Browse files Browse the repository at this point in the history
  • Loading branch information
chrishamm committed Sep 27, 2021
2 parents 130708f + e76c1a5 commit e84003a
Show file tree
Hide file tree
Showing 16 changed files with 268 additions and 109 deletions.
11 changes: 9 additions & 2 deletions src/CAN/CanMotion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ void CanMotion::AddExtruderMovement(const PrepParams& params, DriverId canDriver
#endif

// This is called by DDA::Prepare when all DMs for CAN drives have been processed. Return the calculated move time in steps, or 0 if there are no CAN moves
uint32_t CanMotion::FinishMovement(uint32_t moveStartTime) noexcept
uint32_t CanMotion::FinishMovement(uint32_t moveStartTime, bool simulating) noexcept
{
boardsActiveInLastMove.ClearAll();
CanMessageBuffer *buf = movementBufferList;
Expand Down Expand Up @@ -207,7 +207,14 @@ uint32_t CanMotion::FinishMovement(uint32_t moveStartTime) noexcept
buf->dataLength = buf->msg.moveLinear.GetActualDataLength();
#endif
CanMessageBuffer * const nextBuffer = buf->next; // must get this before sending the buffer, because sending the buffer releases it
CanInterface::SendMotion(buf); // queues the buffer for sending and frees it when done
if (simulating)
{
CanMessageBuffer::Free(buf);
}
else
{
CanInterface::SendMotion(buf); // queues the buffer for sending and frees it when done
}
#if 0
++numMotionMessagesSentLast;
#endif
Expand Down
2 changes: 1 addition & 1 deletion src/CAN/CanMotion.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ namespace CanMotion
#else
void AddMovement(const PrepParams& params, DriverId canDriver, int32_t steps, bool usePressureAdvance = false) noexcept;
#endif
uint32_t FinishMovement(uint32_t moveStartTime) noexcept;
uint32_t FinishMovement(uint32_t moveStartTime, bool simulating) noexcept;
bool CanPrepareMove() noexcept;
CanMessageBuffer *GetUrgentMessage() noexcept;

Expand Down
30 changes: 15 additions & 15 deletions src/GCodes/GCodes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ void GCodes::Reset() noexcept
}
triggersPending.Clear();

simulationMode = 0;
simulationMode = SimulationMode::off;
exitSimulationWhenFileComplete = updateFileWhenSimulationComplete = false;
simulationTime = 0.0;
lastDuration = 0;
Expand Down Expand Up @@ -990,7 +990,7 @@ void GCodes::DoPause(GCodeBuffer& gb, PauseReason reason, const char *msg, uint1
pauseRestorePoint.fanSpeed = lastDefaultFanSpeed;

#if HAS_MASS_STORAGE || HAS_LINUX_INTERFACE
if (simulationMode == 0)
if (!IsSimulating())
{
SaveResumeInfo(false); // create the resume file so that we can resume after power down
}
Expand Down Expand Up @@ -1190,7 +1190,7 @@ bool GCodes::DoEmergencyPause() noexcept
// Try to pause the current SD card print, returning true if successful, false if needs to be called again
bool GCodes::LowVoltagePause() noexcept
{
if (simulationMode != 0)
if (IsSimulating())
{
return true; // ignore the low voltage indication
}
Expand Down Expand Up @@ -2099,7 +2099,7 @@ bool GCodes::DoStraightMove(GCodeBuffer& gb, bool isCoordinated, const char *& e
// As soon as we set segmentsLeft nonzero, the Move process will assume that the move is ready to take, so this must be the last thing we do.
const Kinematics& kin = reprap.GetMove().GetKinematics();
const SegmentationType st = kin.GetSegmentationType();
if (st.useSegmentation && simulationMode != 1 && (moveState.hasPositiveExtrusion || moveState.isCoordinated || st.useG0Segmentation))
if (st.useSegmentation && simulationMode != SimulationMode::normal && (moveState.hasPositiveExtrusion || moveState.isCoordinated || st.useG0Segmentation))
{
// This kinematics approximates linear motion by means of segmentation
float moveLengthSquared = fsquare(moveState.currentUserPosition[X_AXIS] - initialUserPosition[X_AXIS]) + fsquare(moveState.currentUserPosition[Y_AXIS] - initialUserPosition[Y_AXIS]);
Expand Down Expand Up @@ -3294,7 +3294,7 @@ void GCodes::StartPrinting(bool fromStart) noexcept
lastFilamentError = FilamentSensorStatus::ok;
reprap.GetPrintMonitor().StartedPrint();
platform.MessageF(LogWarn,
(simulationMode == 0) ? "Started printing file %s\n" : "Started simulating printing file %s\n",
(IsSimulating()) ? "Started simulating printing file %s\n" : "Started printing file %s\n",
reprap.GetPrintMonitor().GetPrintingFilename());
if (fromStart)
{
Expand Down Expand Up @@ -3338,7 +3338,7 @@ GCodeResult GCodes::DoDwell(GCodeBuffer& gb) THROWS(GCodeException)
}
#endif

if ( simulationMode != 0 // if we are simulating then simulate the G4...
if ( IsSimulating() // if we are simulating then simulate the G4...
&& &gb != daemonGCode // ...unless it comes from the daemon...
&& &gb != triggerGCode // ...or a trigger...
&& (&gb == fileGCode || !exitSimulationWhenFileComplete) // ...or we are simulating a file and this command doesn't come from the file
Expand Down Expand Up @@ -3410,7 +3410,7 @@ GCodeResult GCodes::SetOrReportOffsets(GCodeBuffer &gb, const StringRef& reply,
if (gb.Seen('R'))
{
settingTemps = true;
if (simulationMode == 0)
if (!IsSimulating())
{
float standby[MaxHeaters];
gb.GetFloatArray(standby, hCount, true);
Expand All @@ -3423,7 +3423,7 @@ GCodeResult GCodes::SetOrReportOffsets(GCodeBuffer &gb, const StringRef& reply,
if (gb.Seen('S'))
{
settingTemps = true;
if (simulationMode == 0)
if (!IsSimulating())
{
float activeTemps[MaxHeaters];
gb.GetFloatArray(activeTemps, hCount, true);
Expand All @@ -3444,7 +3444,7 @@ GCodeResult GCodes::SetOrReportOffsets(GCodeBuffer &gb, const StringRef& reply,
if (gb.Seen('F'))
{
settingOther = true;
if (simulationMode == 0)
if (!IsSimulating())
{
tool->SetSpindleRpm(gb.GetUIValue());
}
Expand Down Expand Up @@ -4163,7 +4163,7 @@ void GCodes::StopPrint(StopPrintReason reason) noexcept
#endif

exitSimulationWhenFileComplete = false;
simulationMode = 0; // do this after we append the simulation info to the file so that DWC doesn't try to reload the file info too soon
simulationMode = SimulationMode::off; // do this after we append the simulation info to the file so that DWC doesn't try to reload the file info too soon
reprap.GetMove().Simulate(simulationMode);
EndSimulation(nullptr);

Expand Down Expand Up @@ -4225,7 +4225,7 @@ void GCodes::StopPrint(StopPrintReason reason) noexcept
(reason == StopPrintReason::normalCompletion) ? "Finished" : "Cancelled",
printingFilename, printMinutes/60u, printMinutes % 60u);
#if HAS_MASS_STORAGE || HAS_LINUX_INTERFACE
if (reason == StopPrintReason::normalCompletion && simulationMode == 0)
if (reason == StopPrintReason::normalCompletion && !IsSimulating())
{
platform.DeleteSysFile(RESUME_AFTER_POWER_FAIL_G);
}
Expand Down Expand Up @@ -4458,7 +4458,7 @@ bool GCodes::AllAxesAreHomed() const noexcept
// Tell us that the axis is now homed
void GCodes::SetAxisIsHomed(unsigned int axis) noexcept
{
if (simulationMode == 0)
if (!IsSimulating())
{
axesHomed.SetBit(axis);
axesVirtuallyHomed = axesHomed;
Expand All @@ -4469,7 +4469,7 @@ void GCodes::SetAxisIsHomed(unsigned int axis) noexcept
// Tell us that the axis is not homed
void GCodes::SetAxisNotHomed(unsigned int axis) noexcept
{
if (simulationMode == 0)
if (!IsSimulating())
{
axesHomed.ClearBit(axis);
axesVirtuallyHomed = axesHomed;
Expand All @@ -4484,7 +4484,7 @@ void GCodes::SetAxisNotHomed(unsigned int axis) noexcept
// Flag all axes as not homed
void GCodes::SetAllAxesNotHomed() noexcept
{
if (simulationMode == 0)
if (!IsSimulating())
{
axesHomed.Clear();
axesVirtuallyHomed = axesHomed;
Expand Down Expand Up @@ -4724,7 +4724,7 @@ OutputBuffer *GCodes::GenerateJsonStatusResponse(int type, int seq, ResponseSour
void GCodes::StartToolChange(GCodeBuffer& gb, int toolNum, uint8_t param) noexcept
{
newToolNumber = toolNum;
toolChangeParam = (simulationMode != 0) ? 0 : param;
toolChangeParam = (IsSimulating()) ? 0 : param;
gb.SetState(GCodeState::toolChange0);
}

Expand Down
14 changes: 11 additions & 3 deletions src/GCodes/GCodes.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,14 @@ struct M675Settings
float minDistance; // the position we reached when probing towards minimum
};

enum class SimulationMode : uint8_t
{ off = 0, // not simulating
debug, // simulating step generation
normal, // not generating steps, just timing
partial, // generating DDAs but doing nothing with them
highest = partial
};

class LinuxInterface;

// The GCode interpreter
Expand Down Expand Up @@ -162,7 +170,7 @@ class GCodes

bool IsReallyPrinting() const noexcept; // Return true if we are printing from SD card and not pausing, paused or resuming
bool IsReallyPrintingOrResuming() const noexcept;
bool IsSimulating() const noexcept { return simulationMode != 0; }
bool IsSimulating() const noexcept { return simulationMode != SimulationMode::off; }
bool IsDoingToolChange() const noexcept { return doingToolChange; }
bool IsHeatingUp() const noexcept; // Return true if the SD card print is waiting for a heater to reach temperature
bool IsRunningConfigFile() const noexcept { return runningConfigFile; }
Expand Down Expand Up @@ -466,7 +474,7 @@ class GCodes
GCodeResult ReceiveI2c(GCodeBuffer& gb, const StringRef &reply) THROWS(GCodeException); // Handle M261
#if HAS_MASS_STORAGE || HAS_LINUX_INTERFACE || HAS_EMBEDDED_FILES
GCodeResult SimulateFile(GCodeBuffer& gb, const StringRef &reply, const StringRef& file, bool updateFile) THROWS(GCodeException); // Handle M37 to simulate a whole file
GCodeResult ChangeSimulationMode(GCodeBuffer& gb, const StringRef &reply, uint32_t newSimulationMode) THROWS(GCodeException); // Handle M37 to change the simulation mode
GCodeResult ChangeSimulationMode(GCodeBuffer& gb, const StringRef &reply, SimulationMode newSimMode) THROWS(GCodeException); // Handle M37 to change the simulation mode
#endif
GCodeResult WaitForPin(GCodeBuffer& gb, const StringRef &reply) THROWS(GCodeException); // Handle M577

Expand Down Expand Up @@ -639,7 +647,7 @@ class GCodes
// Simulation and print time
float simulationTime; // Accumulated simulation time
uint32_t lastDuration; // Time or simulated time of the last successful print or simulation, in seconds
uint8_t simulationMode; // 0 = not simulating, 1 = simulating, >1 are simulation modes for debugging
SimulationMode simulationMode; // see description of enum SimulationMode
bool exitSimulationWhenFileComplete; // true if simulating a file
bool updateFileWhenSimulationComplete; // true if simulated time should be appended to the file

Expand Down
24 changes: 12 additions & 12 deletions src/GCodes/GCodes2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ bool GCodes::HandleGcode(GCodeBuffer& gb, const StringRef& reply) THROWS(GCodeEx
{
GCodeResult result = GCodeResult::ok;
const int code = gb.GetCommandNumber();
if (simulationMode != 0 && code > 4 && code != 10 && code != 11 && code != 20 && code != 21 && (code < 53 || code > 59) && (code < 90 || code > 92))
if (IsSimulating() && code > 4 && code != 10 && code != 11 && code != 20 && code != 21 && (code < 53 || code > 59) && (code < 90 || code > 92))
{
HandleReply(gb, result, "");
return true; // we only simulate some gcodes
Expand Down Expand Up @@ -236,7 +236,7 @@ bool GCodes::HandleGcode(GCodeBuffer& gb, const StringRef& reply) THROWS(GCodeEx

if (modifyingTool)
{
if (simulationMode != 0)
if (IsSimulating())
{
break;
}
Expand Down Expand Up @@ -452,7 +452,7 @@ bool GCodes::HandleMcode(GCodeBuffer& gb, const StringRef& reply) THROWS(GCodeEx
{
const int code = gb.GetCommandNumber();

if ( simulationMode != 0
if ( IsSimulating()
&& (code < 20 || code > 37)
&& code != 0 && code != 1 && code != 82 && code != 83 && code != 105 && code != 109 && code != 111 && code != 112 && code != 122
&& code != 200 && code != 204 && code != 207 && code != 408 && code != 409 && code != 486 && code != 999)
Expand Down Expand Up @@ -1155,15 +1155,15 @@ bool GCodes::HandleMcode(GCodeBuffer& gb, const StringRef& reply) THROWS(GCodeEx
else
{
uint32_t newSimulationMode;
gb.TryGetUIValue('S', newSimulationMode, seen);
gb.TryGetLimitedUIValue('S', newSimulationMode, seen, (uint32_t)SimulationMode::highest + 1);
if (seen)
{
result = ChangeSimulationMode(gb, reply, newSimulationMode);
result = ChangeSimulationMode(gb, reply, (SimulationMode)newSimulationMode);
}
else
{
reply.printf("Simulation mode: %s, move time: %.1f sec, other time: %.1f sec",
(simulationMode != 0) ? "on" : "off", (double)reprap.GetMove().GetSimulationTime(), (double)simulationTime);
(IsSimulating()) ? "on" : "off", (double)reprap.GetMove().GetSimulationTime(), (double)simulationTime);
}
}
}
Expand Down Expand Up @@ -1541,7 +1541,7 @@ bool GCodes::HandleMcode(GCodeBuffer& gb, const StringRef& reply) THROWS(GCodeEx

// Set the heater temperatures for that tool. We set the standby temperatures as well as the active ones,
// because any slicer that uses M109 doesn't understand that there are separate active and standby temperatures.
if (simulationMode == 0)
if (!IsSimulating())
{
SetToolHeaters(applicableTool.Ptr(), temperature, true); // this may throw
}
Expand All @@ -1556,7 +1556,7 @@ bool GCodes::HandleMcode(GCodeBuffer& gb, const StringRef& reply) THROWS(GCodeEx
}

newToolNumber = applicableTool->Number();
toolChangeParam = (simulationMode != 0) ? 0 : DefaultToolChangeParam;
toolChangeParam = (IsSimulating()) ? 0 : DefaultToolChangeParam;
gb.SetState(GCodeState::m109ToolChange0);
result = GCodeResult::ok;
}
Expand All @@ -1566,15 +1566,15 @@ bool GCodes::HandleMcode(GCodeBuffer& gb, const StringRef& reply) THROWS(GCodeEx
{
// Even though the tool is selected, we may have turned it off e.g. when upgrading the WiFi firmware or following a heater fault that has been cleared.
// So make sure the tool heaters are on.
reprap.SelectTool(applicableTool->Number(), simulationMode != 0);
reprap.SelectTool(applicableTool->Number(), IsSimulating());
}
else
{
// If we already have an active tool and we are setting temperatures for a different tool, set that tool's heaters to standby in case it is off
reprap.StandbyTool(applicableTool->Number(), simulationMode != 0);
reprap.StandbyTool(applicableTool->Number(), IsSimulating());
}

if (code == 109 && simulationMode == 0)
if (code == 109 && !IsSimulating())
{
gb.SetState(GCodeState::m109WaitForTemperature);
result = GCodeResult::ok;
Expand Down Expand Up @@ -4689,7 +4689,7 @@ bool GCodes::HandleTcode(GCodeBuffer& gb, const StringRef& reply)
{
// Even though the tool is selected, we may have turned it off e.g. when upgrading the WiFi firmware or following a heater fault that has been cleared.
// So make sure the tool heaters are on.
reprap.SelectTool(toolNum, simulationMode != 0);
reprap.SelectTool(toolNum, IsSimulating());
}
}
}
Expand Down
18 changes: 9 additions & 9 deletions src/GCodes/GCodes3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ GCodeResult GCodes::SetPositions(GCodeBuffer& gb) THROWS(GCodeException)
ToolOffsetInverseTransform(moveState.coords, moveState.currentUserPosition); // make sure the limits are reflected in the user position
}
reprap.GetMove().SetNewPosition(moveState.coords, true);
if (simulationMode == 0)
if (!IsSimulating())
{
axesHomed |= reprap.GetMove().GetKinematics().AxesAssumedHomed(axesIncluded);
axesVirtuallyHomed = axesHomed;
Expand Down Expand Up @@ -424,7 +424,7 @@ GCodeResult GCodes::SimulateFile(GCodeBuffer& gb, const StringRef &reply, const
QueueFileToPrint(file.c_str(), reply))
# endif
{
if (simulationMode == 0)
if (!IsSimulating())
{
axesVirtuallyHomed = AxesBitmap::MakeLowestNBits(numVisibleAxes); // pretend all axes are homed
SavePosition(simulationRestorePoint, gb);
Expand All @@ -437,7 +437,7 @@ GCodeResult GCodes::SimulateFile(GCodeBuffer& gb, const StringRef &reply, const
# else
updateFileWhenSimulationComplete = updateFile;
# endif
simulationMode = 1;
simulationMode = SimulationMode::normal;
reprap.GetMove().Simulate(simulationMode);
reprap.GetPrintMonitor().StartingPrint(file.c_str());
StartPrinting(true);
Expand All @@ -449,22 +449,22 @@ GCodeResult GCodes::SimulateFile(GCodeBuffer& gb, const StringRef &reply, const
}

// Handle M37 to change the simulation mode
GCodeResult GCodes::ChangeSimulationMode(GCodeBuffer& gb, const StringRef &reply, uint32_t newSimulationMode)
GCodeResult GCodes::ChangeSimulationMode(GCodeBuffer& gb, const StringRef &reply, SimulationMode newSimMode) THROWS(GCodeException)
{
if (newSimulationMode != simulationMode)
if (newSimMode != simulationMode)
{
if (!LockMovementAndWaitForStandstill(gb))
{
return GCodeResult::notFinished;
}

if (newSimulationMode == 0)
if (newSimMode == SimulationMode::off)
{
EndSimulation(&gb);
}
else
{
if (simulationMode == 0)
if (!IsSimulating())
{
// Starting a new simulation, so save the current position
axesVirtuallyHomed = AxesBitmap::MakeLowestNBits(numVisibleAxes); // pretend all axes are homed
Expand All @@ -473,8 +473,8 @@ GCodeResult GCodes::ChangeSimulationMode(GCodeBuffer& gb, const StringRef &reply
simulationTime = 0.0;
}
exitSimulationWhenFileComplete = updateFileWhenSimulationComplete = false;
simulationMode = (uint8_t)newSimulationMode;
reprap.GetMove().Simulate(simulationMode);
simulationMode = newSimMode;
reprap.GetMove().Simulate(newSimMode);
}
return GCodeResult::ok;
}
Expand Down
6 changes: 3 additions & 3 deletions src/GCodes/GCodes4.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ void GCodes::RunStateMachine(GCodeBuffer& gb, const StringRef& reply) noexcept
const Tool * const oldTool = reprap.GetCurrentTool();
if (oldTool != nullptr)
{
reprap.StandbyTool(oldTool->Number(), simulationMode != 0);
reprap.StandbyTool(oldTool->Number(), IsSimulating());
UpdateCurrentUserPosition(); // the tool offset may have changed, so get the current position
}
gb.AdvanceState();
Expand All @@ -362,7 +362,7 @@ void GCodes::RunStateMachine(GCodeBuffer& gb, const StringRef& reply) noexcept
case GCodeState::m109ToolChange2: // select the new tool if it exists and run tpost
if (LockMovementAndWaitForStandstill(gb)) // wait for tpre.g to finish executing
{
reprap.SelectTool(newToolNumber, simulationMode != 0);
reprap.SelectTool(newToolNumber, IsSimulating());
UpdateCurrentUserPosition(); // get the actual position of the new tool

gb.AdvanceState();
Expand Down Expand Up @@ -432,7 +432,7 @@ void GCodes::RunStateMachine(GCodeBuffer& gb, const StringRef& reply) noexcept
break;

case GCodeState::m109WaitForTemperature:
if (cancelWait || simulationMode != 0 || ToolHeatersAtSetTemperatures(reprap.GetCurrentTool(), gb.LatestMachineState().waitWhileCooling, TEMPERATURE_CLOSE_ENOUGH))
if (cancelWait || IsSimulating() || ToolHeatersAtSetTemperatures(reprap.GetCurrentTool(), gb.LatestMachineState().waitWhileCooling, TEMPERATURE_CLOSE_ENOUGH))
{
cancelWait = isWaiting = false;
gb.SetState(GCodeState::normal);
Expand Down
Loading

0 comments on commit e84003a

Please sign in to comment.