Skip to content

Commit

Permalink
improve Gcode parsing
Browse files Browse the repository at this point in the history
A StringViewToDouble conversion function is added. With help of this function, improved the Gcode parsing to extract more data for each Gcode command. Now it correctly identifies and extracts all relevant command data for each command line, including optional parameters, and stores them in corresponding classes' member variables. More specifically, the changes were made in classes representing G0, G1, m104, and other Gcode commands. Additionally, for each updated class a std::optional field was added for each optional command parameter which can appear in actual Gcode command, allowing to check if certain parameter was set in the given Gcode command.

Contribute to CURA-10561
  • Loading branch information
jellespijker committed Oct 17, 2023
1 parent d021414 commit 8d36970
Show file tree
Hide file tree
Showing 15 changed files with 410 additions and 67 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ set(DULCIFICUM_SRC
src/proto_path/translate.cpp

src/utils/io.cpp
src/utils/svtod.cpp
src/command_types.cpp
)
add_library(dulcificum ${DULCIFICUM_SRC})
Expand Down
19 changes: 13 additions & 6 deletions include/dulcificum/gcode/ast/acceleration.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,23 @@

#include "dulcificum/gcode/ast/entry.h"

#include <optional>

namespace dulcificum::gcode::ast
{
/*!
* /brief Set the starting acceleration for moves by type.
* P = Printing acceleration. Used for moves that include extrusion
* T = Travel acceleration. Used for moves that include no extrusion
*/
class M204 : public Entry<R"(M204((?:\sP(?<P>\d+(?:\.\d+)?))|(?:\sT(?<T>\d+(?:\.\d+)?)))*$)">
class M204 : public Entry<R"(M204((?:\sP(?<P>\d+(?:\.\d+)?))|(?:\sT(?<T>\d+(?:\.\d+)?))|(?:\sS(?<S>\d+(?:\.\d+)?)))*$)">
{
public:
M204() = delete;
M204(size_t index, std::string line)
: Entry{ index, std::move(line) } {};
M204(size_t index, std::string line);
std::optional<double> P;
std::optional<double> T;
std::optional<double> S;
};

/*!
Expand All @@ -25,12 +29,15 @@ class M204 : public Entry<R"(M204((?:\sP(?<P>\d+(?:\.\d+)?))|(?:\sT(?<T>\d+(?:\.
* Z = Z max jerk (units/s)
* E = E max jerk (units/s)
*/
class M205 : public Entry<R"(M205((?:\sX(?<X>\d+(?:\.\d+)?))|(?:\sY(?<Y>\d+(?:\.\d+)?))|(?:\sZ(?<Z>\d+(?:\.\d+)?))|(?:\sE(?<E>\d+(?:\.\d+)?)))*$)">
class M205 : public Entry<R"(M205((?:\sX(?<X>-?\d+(?:\.\d+)?))|(?:\sY(?<Y>-?\d+(?:\.\d+)?))|(?:\sZ(?<Z>-?\d+(?:\.\d+)?))|(?:\sE(?<E>-?\d+(?:\.\d+)?)))*$)">
{
public:
M205() = delete;
M205(size_t index, std::string line)
: Entry{ index, std::move(line) } {};
M205(size_t index, std::string line);
std::optional<double> X;
std::optional<double> Y;
std::optional<double> Z;
std::optional<double> E;
};

} // namespace dulcificum::gcode::ast
Expand Down
11 changes: 7 additions & 4 deletions include/dulcificum/gcode/ast/bed_temperature.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

#include "dulcificum/gcode/ast/entry.h"

#include <optional>

namespace dulcificum::gcode::ast
{
/*!
Expand All @@ -13,8 +15,8 @@ class M140 : public Entry<R"(M140((?:\sS(?<S>\d+(?:\.\d+)?)))*$)">
{
public:
M140() = delete;
M140(size_t index, std::string line)
: Entry{ index, std::move(line) } {};
M140(size_t index, std::string line);
std::optional<double> S;
};

/*!
Expand All @@ -26,8 +28,9 @@ class M190 : public Entry<R"(M190((?:\sS(?<S>\d+(?:\.\d+)?))|(?:\sR(?<R>\d+(?:\.
{
public:
M190() = delete;
M190(size_t index, std::string line)
: Entry{ index, std::move(line) } {};
M190(size_t index, std::string line);
std::optional<double> S;
std::optional<double> R;
};
} // namespace dulcificum::gcode::ast

Expand Down
29 changes: 16 additions & 13 deletions include/dulcificum/gcode/ast/comment_commands.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,20 @@

#include "dulcificum/gcode/ast/entry.h"

#include <optional>

namespace dulcificum::gcode::ast
{
/*!
* /brief The layer index
* L = index
*/
class Layer : public Entry<R"(;LAYER:(?<L>\d*))">
class Layer : public Entry<R"(;LAYER:(?<L>-?\d+))">
{
public:
Layer() = delete;
Layer(size_t index, std::string line)
: Entry{ index, std::move(line) } {};
Layer(size_t index, std::string line);
std::optional<size_t> L;
};

/*!
Expand All @@ -25,8 +27,8 @@ class Mesh : public Entry<R"(;MESH:(?<M>.*))">
{
public:
Mesh() = delete;
Mesh(size_t index, std::string line)
: Entry{ index, std::move(line) } {};
Mesh(size_t index, std::string line);
std::optional<std::string_view> M;
};

/*!
Expand All @@ -37,8 +39,8 @@ class FeatureType : public Entry<R"(;TYPE:(?<T>.*))">
{
public:
FeatureType() = delete;
FeatureType(size_t index, std::string line)
: Entry{ index, std::move(line) } {};
FeatureType(size_t index, std::string line);
std::optional<std::string_view> T;
};

/*!
Expand All @@ -50,8 +52,9 @@ class InitialTemperatureExtruder : public Entry<R"(;EXTRUDER_TRAIN\.(?<T>\d)\.IN
{
public:
InitialTemperatureExtruder() = delete;
InitialTemperatureExtruder(size_t index, std::string line)
: Entry{ index, std::move(line) } {};
InitialTemperatureExtruder(size_t index, std::string line);
std::optional<size_t> T;
std::optional<double> S;
};

/*!
Expand All @@ -62,8 +65,8 @@ class InitialTemperatureBuildPlate : public Entry<R"(;BUILD_PLATE.INITIAL_TEMPER
{
public:
InitialTemperatureBuildPlate() = delete;
InitialTemperatureBuildPlate(size_t index, std::string line)
: Entry{ index, std::move(line) } {};
InitialTemperatureBuildPlate(size_t index, std::string line);
std::optional<double> S;
};

/*!
Expand All @@ -74,8 +77,8 @@ class BuildVolumeTemperature : public Entry<R"(;BUILD_VOLUME.TEMPERATURE:(?<S>([
{
public:
BuildVolumeTemperature() = delete;
BuildVolumeTemperature(size_t index, std::string line)
: Entry{ index, std::move(line) } {};
BuildVolumeTemperature(size_t index, std::string line);
std::optional<double> S;
};

} // namespace dulcificum::gcode::ast
Expand Down
21 changes: 15 additions & 6 deletions include/dulcificum/gcode/ast/entry.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,44 @@

#include "dulcificum/utils/char_range_literal.h"

#include <spdlog/spdlog.h>

#include <array>
#include <cstddef>
#include <ctll/fixed_string.hpp>
#include <ctre.hpp>
#include <map>
#include <string>
#include <string_view>
#include <tuple>
#include <unordered_map>
#include <utility>
#include <variant>

namespace dulcificum::gcode::ast
{

template<dulcificum::utils::CharRangeLiteral Pattern>
template<utils::CharRangeLiteral Pattern>
class Entry
{
public:
using value_type = std::map<std::string_view, std::variant<double, int, std::string_view>>;

Entry() = delete;
Entry(size_t index, std::string line)
: index{ index }
, line{ std::move(line) } {};

size_t index;
std::string line;
static inline constexpr ctll::fixed_string pattern{ Pattern.value };

constexpr auto get()
{
return ctre::match<Pattern.value>(line);
return ctre::match<pattern>(line);
};

virtual constexpr void operator()()
virtual value_type operator()()
{
spdlog::info("lino: {} -> {}", index, line);
return {};
};
};

Expand Down
17 changes: 11 additions & 6 deletions include/dulcificum/gcode/ast/extruder_temperature.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,36 @@

#include "dulcificum/gcode/ast/entry.h"

#include <optional>

namespace dulcificum::gcode::ast
{
/*!
* /brief Set a new target hot end temperature (non-blocking).
* S = Target temperature
*/
class M104 : public Entry<R"(M104((?:\sS(?<S>\d+(?:\.\d+)?)))*$)">
class M104 : public Entry<R"(M104((?:\sS(?<S>\d+(?:\.\d+)?))|(?:\sT(?<T>\d+(?:\.\d+)?)))*$)">
{
public:
M104() = delete;
M104(size_t index, std::string line)
: Entry{ index, std::move(line) } {};
M104(size_t index, std::string line);
std::optional<double> S;
std::optional<size_t> T;
};

/*!
* /brief Wait for the hot end to reach its target (blocking).
* R = Target temperature (wait for cooling or heating).
* S = Target temperature (wait only when heating)
*/
class M109 : public Entry<R"(M109((?:\sS(?<S>\d+(?:\.\d+)?))|(?:\sR(?<R>\d+(?:\.\d+)?)))*$)">
class M109 : public Entry<R"(M109((?:\sS(?<S>\d+(?:\.\d+)?))|(?:\sR(?<R>\d+(?:\.\d+)?))|(?:\sT(?<T>\d+(?:\.\d+)?)))*$)">
{
public:
M109() = delete;
M109(size_t index, std::string line)
: Entry{ index, std::move(line) } {};
M109(size_t index, std::string line);
std::optional<double> S;
std::optional<double> R;
std::optional<size_t> T;
};
} // namespace dulcificum::gcode::ast

Expand Down
6 changes: 4 additions & 2 deletions include/dulcificum/gcode/ast/fan.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

#include "dulcificum/gcode/ast/entry.h"

#include <optional>

namespace dulcificum::gcode::ast
{
/*!
Expand All @@ -13,8 +15,8 @@ class M106 : public Entry<R"(M106((?:\sS(?<S>\d+(?:\.\d+)?)))*$)">
{
public:
M106() = delete;
M106(size_t index, std::string line)
: Entry{ index, std::move(line) } {};
M106(size_t index, std::string line);
std::optional<double> S;
};

/*!
Expand Down
11 changes: 8 additions & 3 deletions include/dulcificum/gcode/ast/position.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

#include "dulcificum/gcode/ast/entry.h"

#include <optional>

namespace dulcificum::gcode::ast
{
/*!
Expand All @@ -12,12 +14,15 @@ namespace dulcificum::gcode::ast
* Z = New Z axis position
* E New extruder position
*/
class G92 : public Entry<R"(G92((?:\sX(?<X>\d+(?:\.\d+)?))|(?:\sY(?<Y>\d+(?:\.\d+)?))|(?:\sZ(?<Z>\d+(?:\.\d+)?))|(?:\sE(?<E>\d+(?:\.\d+)?)))*$)">
class G92 : public Entry<R"(G92((?:\sX(?<X>-?\d+(?:\.\d+)?))|(?:\sY(?<Y>-?\d+(?:\.\d+)?))|(?:\sZ(?<Z>-?\d+(?:\.\d+)?))|(?:\sE(?<E>-?\d+(?:\.\d+)?)))*$)">
{
public:
G92() = delete;
G92(size_t index, std::string line)
: Entry{ index, std::move(line) } {};
G92(size_t index, std::string line);
std::optional<double> X;
std::optional<double> Y;
std::optional<double> Z;
std::optional<double> E;
};
} // namespace dulcificum::gcode::ast

Expand Down
6 changes: 4 additions & 2 deletions include/dulcificum/gcode/ast/purge.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

#include "dulcificum/gcode/ast/entry.h"

#include <optional>

namespace dulcificum::gcode::ast
{
/*!
Expand All @@ -12,8 +14,8 @@ class G280 : public Entry<R"(G280((?:\sS(?<S>\d+(?:\.\d+)?)))*$)">
{
public:
G280() = delete;
G280(size_t index, std::string line)
: Entry{ index, std::move(line) } {};
G280(size_t index, std::string line);
std::optional<size_t> S;
};
} // namespace dulcificum::gcode::ast

Expand Down
6 changes: 3 additions & 3 deletions include/dulcificum/gcode/ast/toolchange.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ namespace dulcificum::gcode::ast
/*!
* /brief Switch to the specified tool
*/
class T : public Entry<R"(T(?<T>\d))">
class T : public Entry<R"(T(?<S>\d))">
{
public:
T() = delete;
T(size_t index, std::string line)
: Entry{ index, std::move(line) } {};
T(size_t index, std::string line);
std::optional<size_t> S;
};
} // namespace dulcificum::gcode::ast

Expand Down
28 changes: 18 additions & 10 deletions include/dulcificum/gcode/ast/translate.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@

#include "dulcificum/gcode/ast/entry.h"

#include <cstddef>
#include <optional>
#include <string>


namespace dulcificum::gcode::ast
{
Expand All @@ -14,14 +18,16 @@ namespace dulcificum::gcode::ast
* E = An absolute or relative coordinate on the E axis (in current units).
* F = The maximum movement rate of the move between the start and end point.
*/
class G0 : public Entry<R"(G0((?:\sX(?<X>\d+(?:\.\d+)?))|(?:\sY(?<Y>\d+(?:\.\d+)?))|(?:\sZ(?<Z>\d+(?:\.\d+)?))|(?:\sE(?<E>\d+(?:\.\d+)?))|(?:\sF(?<F>\d+(?:\.\d+)?)))*$)">
class G0 : public Entry<R"(G0((?:\sX(?<X>-?\d+(?:\.\d+)?))|(?:\sY(?<Y>-?\d+(?:\.\d+)?))|(?:\sZ(?<Z>-?\d+(?:\.\d+)?))|(?:\sE(?<E>-?\d+(?:\.\d+)?))|(?:\sF(?<F>-?\d+(?:\.\d+)?)))*$)">
{
public:
G0() = delete;
G0(size_t index, std::string line)
: Entry{ index, std::move(line) } {};

void operator()() override final;
G0(size_t index, std::string line);
std::optional<double> X;
std::optional<double> Y;
std::optional<double> Z;
std::optional<double> E;
std::optional<double> F;
};

/*!
Expand All @@ -32,14 +38,16 @@ class G0 : public Entry<R"(G0((?:\sX(?<X>\d+(?:\.\d+)?))|(?:\sY(?<Y>\d+(?:\.\d+)
* E = An absolute or relative coordinate on the E axis (in current units).
* F = The maximum movement rate of the move between the start and end point.
*/
class G1 : public Entry<R"(G1((?:\sX(?<X>\d+(?:\.\d+)?))|(?:\sY(?<Y>\d+(?:\.\d+)?))|(?:\sZ(?<Z>\d+(?:\.\d+)?))|(?:\sE(?<E>\d+(?:\.\d+)?))|(?:\sF(?<F>\d+(?:\.\d+)?)))*$)">
class G1 : public Entry<R"(G1((?:\sX(?<X>-?\d+(?:\.\d+)?))|(?:\sY(?<Y>-?\d+(?:\.\d+)?))|(?:\sZ(?<Z>-?\d+(?:\.\d+)?))|(?:\sE(?<E>-?\d+(?:\.\d+)?))|(?:\sF(?<F>-?\d+(?:\.\d+)?)))*$)">
{
public:
G1() = delete;
G1(size_t index, std::string line)
: Entry{ index, std::move(line) } {};

void operator()() override final;
G1(size_t index, std::string line);
std::optional<double> X;
std::optional<double> Y;
std::optional<double> Z;
std::optional<double> E;
std::optional<double> F;
};

} // namespace dulcificum::gcode::ast
Expand Down
Loading

0 comments on commit 8d36970

Please sign in to comment.