MeshLib
 
Loading...
Searching...
No Matches
MRGcodeProcessor.h
Go to the documentation of this file.
1#pragma once
2#include "MRMeshFwd.h"
3#include "MRVector3.h"
4#include "MRMatrix3.h"
6#include <vector>
7#include <string>
8#include <optional>
9#include <functional>
10
11
12namespace MR
13{
14
15// class to process g-code source and generate toolpath
17{
18public:
19
20 template<typename Vec>
22 {
23 // tool movement parsed from gcode
24 std::vector<Vec> path;
25 // parser warning
26 std::string warning;
27 };
30 // structure that stores information about the movement of the tool, specified by some string of commands
32 {
34 std::vector<Vector3f> toolDirection; // tool direction for each point from action.path
35 bool idle = true;
36 float feedrate = 100.f;
37 // return true if operation was parsed without warnings
38 bool valid() const { return action.warning.empty(); }
39 operator bool() const { return valid(); }
40 };
41
42 // reset internal states
44
45 // set g-code source
46 MRMESH_API void setGcodeSource( const GcodeSource& gcodeSource );
47
48 // process all lines g-code source and generate corresponding move actions
49 MRMESH_API std::vector<MoveAction> processSource();
50
51 struct Command
52 {
53 char key; // in lowercase
54 float value;
55 };
56
57 // process all commands from one line g-code source and generate corresponding move action;
58 // \param externalStorage to avoid memory allocation on each line
59 MRMESH_API MoveAction processLine( const std::string_view& line, std::vector<Command> & externalStorage );
60
61 // settings
63 const CNCMachineSettings& getCNCMachineSettings() { return cncSettings_; }
64
65private:
66 enum class WorkPlane
67 {
68 xy,
69 zx,
70 yz
71 };
72
73 // parse program methods
74 static void parseFrame_( const std::string_view& frame, std::vector<Command> & outCommands );
75 void applyCommand_( const Command& command );
76 void applyCommandG_( const Command& command );
77 MoveAction generateMoveAction_();
78 MoveAction generateReturnToHomeAction_();
79 void resetTemporaryStates_();
80
81 // g-command actions
82
83 // g0, g1
84 MoveAction moveLine_( const Vector3f& newPoint, const Vector3f& newAngles );
85 // g2, g3
86 MoveAction moveArc_( const Vector3f& newPoint, const Vector3f& newAngles, bool clockwise );
87
88 // g17, g18, g19
89 void updateWorkPlane_( WorkPlane wp );
90
91 // g51
92 void updateScaling_();
93
94 // g-command helper methods
95
96 // sample arc points in 2d by begin point, end point and center in (0, 0)
97 BaseAction2f getArcPoints2_( const Vector2f& beginPoint, const Vector2f& endPoint, bool clockwise );
98 // sample arc points in 3d by begin point, end point and center
99 BaseAction3f getArcPoints3_( const Vector3f& center, const Vector3f& beginPoint, const Vector3f& endPoint, bool clockwise );
100 // sample arc points in 3d by begin point, end point and radius
101 // r > 0 : angle - [0, 180]
102 // r < 0 : angle - (0, 360)
103 BaseAction3f getArcPoints3_( float r, const Vector3f& beginPoint, const Vector3f& endPoint, bool clockwise );
104
105 // sample arc points of tool movement during rotation
106 MoveAction getToolRotationPoints_( const Vector3f& newRotationAngles );
107
108 Vector3f calcNewTranslationPos_();
109 Vector3f calcNewRotationAngles_();
110 Vector3f calcRealCoord_( const Vector3f& translationPos, const Vector3f& rotationAngles );
111 void updateRotationAngleAndMatrix_( const Vector3f& rotationAngles );
112 Vector3f calcRealCoordCached_( const Vector3f& translationPos, const Vector3f& rotationAngles );
113 Vector3f calcRealCoordCached_( const Vector3f& translationPos );
114
115 // mode of instrument movement
116 enum class MoveMode
117 {
118 Idle, // fast idle movement (linear)
119 Line, // working linear movement
120 Clockwise, // working movement in an arc clockwise
121 Counterclockwise // working movement in an arc counterclockwise
122 };
123 // type of coordinates to be entered.
124 // movement - repeatable.
125 // other - after execution, it will return to movement
126 enum class CoordType
127 {
128 Movement,
129 ReturnToHome,
130 Scaling
131 };
132
133 // internal states (such as current position and different work modes)
134 CoordType coordType_ = CoordType::Movement;
135 MoveMode moveMode_ = MoveMode::Idle;
136 WorkPlane workPlane_ = WorkPlane::xy;
137 Matrix3f toWorkPlaneXf_; // work plane for calculation arc movement
138 Vector3f translationPos_; // last values of x, y, z (translation. positions of linear movement motors)
139 Vector3f rotationAngles_; // last values of a, b, c (rotation. angles of circular movement motors)
140 bool absoluteCoordinates_ = true; //absolute coordinates or relative coordinates
141 Vector3f scaling_ = Vector3f::diagonal( 1.f );
142 bool inches_ = false;
143 float feedrate_ = 100.f;
144 float feedrateMax_ = 0.f;
145
146 // cached data
147 std::array<Matrix3f, 3> cacheRotationMatrix_; // cached rotation matrices. to avoid calculating for each line (without rotation)
148
149 // input data (data entered in last line)
150 Vector3f inputCoords_; // x, y, z
151 Vector3b inputCoordsReaded_; // x, y, z was read
152 std::optional<float> radius_; // r
153 std::optional<Vector3f> arcCenter_; // i, j, k
154 Vector3f inputRotation_; // a, b, c
155 Vector3b inputRotationReaded_; // a, b, c was read
156
157 std::vector<std::string_view> gcodeSource_; // string list with sets of command (frames)
158
159 // internal / machine settings
160 float accuracy_ = 1.e-3f;
161 CNCMachineSettings cncSettings_;
162 std::vector<int> rotationAxesOrderMap_ = {0, 1, 2}; // mapping axis sequence number to axis number in storage
163
164};
165
166}
#define MRMESH_API
Definition MRMesh/MRMeshFwd.h:46
#define MRMESH_CLASS
Definition MRMesh/MRMeshFwd.h:50
class with CNC machine emulation settings
Definition MRCNCMachineSettings.h:14
Definition MRGcodeProcessor.h:17
MRMESH_API MoveAction processLine(const std::string_view &line, std::vector< Command > &externalStorage)
const CNCMachineSettings & getCNCMachineSettings()
Definition MRGcodeProcessor.h:63
MRMESH_API void setCNCMachineSettings(const CNCMachineSettings &settings)
MRMESH_API void reset()
MRMESH_API std::vector< MoveAction > processSource()
MRMESH_API void setGcodeSource(const GcodeSource &gcodeSource)
arbitrary 3x3 matrix
Definition MRDotNet/MRMatrix3.h:8
represents a 3-dimentional float-typed vector
Definition MRDotNet/MRVector3.h:8
Definition MRCameraOrientationPlugin.h:7
std::vector< std::string > GcodeSource
Definition MRMesh/MRMeshFwd.h:508
MRMESH_CLASS Vector3b
Definition MRMesh/MRMeshFwd.h:137
Definition MRGcodeProcessor.h:22
std::vector< Vec > path
Definition MRGcodeProcessor.h:24
std::string warning
Definition MRGcodeProcessor.h:26
Definition MRGcodeProcessor.h:52
float value
Definition MRGcodeProcessor.h:54
char key
Definition MRGcodeProcessor.h:53
Definition MRGcodeProcessor.h:32
std::vector< Vector3f > toolDirection
Definition MRGcodeProcessor.h:34
bool valid() const
Definition MRGcodeProcessor.h:38
BaseAction3f action
Definition MRGcodeProcessor.h:33