Improving fan speed reporting

This commit is contained in:
Lars Brubaker 2022-05-03 10:19:19 -07:00
parent 2fd226c155
commit e4cdc106d8
4 changed files with 106 additions and 3 deletions

View file

@ -76,6 +76,13 @@ namespace MatterControl.Printing
public abstract double GetLayerHeight(int layerIndex);
/// <summary>
/// Get the speed of the fan at the conculsion of this layer
/// </summary>
/// <param name="layerIndex">The layer to get the last fan speed for</param>
/// <returns>The fan speed 0 to 255</returns>
public abstract int GetLastFanSpeed(int layerIndex);
public abstract double GetLayerTop(int layerIndex);
public abstract int GetLayerIndex(int instructionIndex);

View file

@ -164,7 +164,12 @@ namespace MatterControl.Printing
throw new NotImplementedException();
}
public override double PercentComplete(int instructionIndex)
public override int GetLastFanSpeed(int layerIndex)
{
return -1;
}
public override double PercentComplete(int instructionIndex)
{
lock(locker)
{

View file

@ -51,6 +51,8 @@ namespace MatterControl.Printing
private double parsingLastZ;
private readonly List<int> toolChanges = new List<int>();
private Dictionary<int, int> lastFanSpeed = new Dictionary<int, int>();
/// <summary>
/// Gets total print time that will leave the heaters on at the conclusion of the print.
/// </summary>
@ -290,6 +292,70 @@ namespace MatterControl.Printing
return total;
}
public override int GetLastFanSpeed(int requestedLayer)
{
if (LayerCount == 0 || requestedLayer < 0)
{
return 0;
}
if (lastFanSpeed.ContainsKey(requestedLayer))
{
return lastFanSpeed[requestedLayer];
}
var currentLayer = requestedLayer;
while (currentLayer >= 0)
{
int startInstruction = GetFirstLayerInstruction(currentLayer);
if (currentLayer == 0)
{
startInstruction = 0;
}
int endInstruction = GetFirstLayerInstruction(currentLayer + 1);
var foundSpeed = false;
var lastSpeed = 0;
for (int i = startInstruction; i < endInstruction; i++)
{
var line = Instruction(i).Line;
if (line.StartsWith("M107")) // fan off
{
foundSpeed = true;
lastSpeed = 0;
}
else if (line.StartsWith("M106")) // fan on
{
double speed = 0;
if (GCodeFile.GetFirstNumberAfter("S", line, ref speed, 0, ""))
{
foundSpeed = true;
lastSpeed = (int)speed;
}
}
}
if (foundSpeed)
{
lastFanSpeed[requestedLayer] = lastSpeed;
return lastSpeed;
}
currentLayer--;
if (lastFanSpeed.ContainsKey(currentLayer))
{
lastFanSpeed[currentLayer + 1] = lastFanSpeed[currentLayer];
lastFanSpeed[requestedLayer] = lastFanSpeed[currentLayer];
return lastFanSpeed[currentLayer];
}
}
lastFanSpeed[requestedLayer] = 0;
return 0;
}
/// <summary>
/// Get the height of this layer (from the top of the previous layer to the top of this layer).
/// </summary>

View file

@ -27,7 +27,6 @@ of the authors and should not be interpreted as representing official policies,
either expressed or implied, of the FreeBSD Project.
*/
using System;
using System.Collections.Generic;
using MatterControl.Printing;
using MatterHackers.MatterControl.SlicerConfiguration;
@ -234,12 +233,38 @@ namespace MatterHackers.MatterControl.PartPreviewWindow
double speed = 0;
if (GCodeFile.GetFirstNumberAfter("S", line, ref speed, 0, ""))
{
fanSpeeds += separator + $"{speed * 100 / 255:0}%";
if (speed == 0)
{
fanSpeeds += separator + "Off";
}
else
{
fanSpeeds += separator + $"{speed * 100 / 255:0}%";
}
separator = ", ";
}
}
}
if (string.IsNullOrEmpty(fanSpeeds))
{
// look back until we find a speed or go back to the begining
var speed = loadedGCode.GetLastFanSpeed(activeLayerIndex - 1);
if (speed >= 0)
{
if (speed == 0)
{
return "Off";
}
return $"{speed * 100 / 255:0}%";
}
else
{
return "---";
}
}
return fanSpeeds;
}