MotionLib  1.0.0
SamBuCa motion library
GrblGenerator.hpp
1 /*
2 ** Copyright (C) 2020 Sylvain Fargier
3 **
4 ** This software is provided 'as-is', without any express or implied
5 ** warranty. In no event will the authors be held liable for any damages
6 ** arising from the use of this software.
7 **
8 ** Permission is granted to anyone to use this software for any purpose,
9 ** including commercial applications, and to alter it and redistribute it
10 ** freely, subject to the following restrictions:
11 **
12 ** 1. The origin of this software must not be misrepresented; you must not
13 ** claim that you wrote the original software. If you use this software
14 ** in a product, an acknowledgment in the product documentation would be
15 ** appreciated but is not required.
16 ** 2. Altered source versions must be plainly marked as such, and must not be
17 ** misrepresented as being the original software.
18 ** 3. This notice may not be removed or altered from any source distribution.
19 **
20 ** Created on: 2022-02-16T09:29:28+01:00
21 ** Author: Sylvain Fargier <fargie_s> <sylvain.fargier@cern.ch>
22 **
23 */
24 
25 #ifndef GRBLGENERATOR_HPP__
26 #define GRBLGENERATOR_HPP__
27 
28 #include <cstdint>
29 #include <map>
30 #include <memory>
31 
32 #include <ccut/BitMask.hpp>
33 
34 #include "GrblTypes.hpp"
35 
36 /*
37  * groups and structure taken from grblHAL core gcode.h
38  */
39 
40 namespace logger {
41 class Logger;
42 }
43 
44 namespace grbl {
45 namespace gen {
46 
56 enum class ModalGroup
57 {
58  NonModal = 0, // G0 {G4,G10,G28,G28.1,G30,G30.1,G53,G92,G92.1}
59  MotionMode = 1, // G1 {G0,G1,G2,G3,G38.2,G80}
60  PlaneSelect = 2, // G2 {G17,G18,G19}
61  DistanceMode = 3, // G3 {G90,G91}
62  ArcIJKDistanceMode = 4, // G4 {G91.1}
63  FeedRateMode = 5, // G5 {G93,G94,G95}
64  Units = 6, // G6 {G20,G21}
65  CutterRadiusCompensation = 7, // G7 {G40}
66  ToolLengthOffset = 8, // G8 {G43,G43.1,G49}
67  CCRetractMode = 10, // G10 {G98,G99}
68  Scaling = 11, // G11 {G50,G51}
69  CoordinateSystem = 12, // G12 {G54,G55,G56,G57,G58,G59}
70  ControlMode = 13, // G13 {G61}
71  SpindleSpeedMode = 14, // G14 {G96,G97}
72  LatheDiameterMode = 15, // G16 {G7,G8}
73 
74  ProgramFlow = 104, // M4 {M0,M1,M2,M30}
75  ToolChange = 106, // M6
76  SpindleTurning = 107, // M7 {M3,M4,M5}
77  CoolantControl = 108, // M8 {M7,M8,M9}
78  Override = 109, // M9 {M49,M50,M51,M53,M56}
79  DigitalOut = 110, // M10 {M62, M63, M64, M65}
80 };
81 std::string to_string(ModalGroup group);
82 
87 struct Modal
88 {
89  typedef uint8_t GroupId;
90 
91  ModalGroup group;
92  std::string value;
93 };
94 const logger::Logger &operator<<(const logger::Logger &, const Modal &);
95 
97 struct NonModal
98 {
99  static constexpr ModalGroup modalGroup = ModalGroup::NonModal;
100 
101  static const Modal Dwell;
102 
109  static Modal SetCoordinate(int8_t system = -1, bool current = false);
110 
116  static Modal GoHome(uint8_t index = 0);
117 
123  static Modal SetHome(uint8_t index = 0);
124  static const Modal AbsoluteOverride;
125  static const Modal SetCoordinateOffset;
126  static const Modal ResetCoordinateOffset;
127  static const Modal ClearCoordinateOffset;
128  static const Modal RestoreCoordinateOffset;
129 };
130 
133 {
134  static constexpr ModalGroup modalGroup = ModalGroup::DistanceMode;
135  static const Modal Relative;
136  static const Modal Absolute;
137 };
138 
141 {
142  static constexpr ModalGroup modalGroup = ModalGroup::MotionMode;
143  static const Modal Rapid;
144  static const Modal Linear;
145 };
146 
149 {
150  static constexpr ModalGroup modalGroup = ModalGroup::DigitalOut;
151  static Modal on(uint8_t port, bool immediate = false);
152  static Modal off(uint8_t port, bool immediate = false);
153 };
154 
157 {
158  static constexpr ModalGroup modalGroup = ModalGroup::CoordinateSystem;
164  static Modal set(uint8_t system);
165 };
166 
170 struct Position
171 {
172  grbl::Axis axis;
173  grbl_float_t value;
174 };
175 const logger::Logger &operator<<(const logger::Logger &, const Position &);
176 
178 struct System
179 {
180  static const std::string ParserState; // $G
181  static const std::string Jog; // $J
182  static const std::string NGCParams; // $#
183  static const std::string Settings; // $$
184  static const std::string AllSettings; // $+
185  static const std::string ToggleBlockDelete; // $B
186  static const std::string ToggleSingleBlock; // $S
187  static const std::string ToggleOptionalStop; // $O
188  static const std::string CheckMode; // $C
189  static const std::string DisableLock; // $X
190  static const std::string Home; // $H $HX $HY $HZ $HA $HB $HC
191  static const std::string Help; // $HELP
192  static const std::string Sleep; // $SLP
193  static const std::string SetToolReference; // $TLR
194  static const std::string ToolProbeWorkpiece; // $TLR
195  static const std::string BuildInfo; // $I
196  static const std::string AllBuildInfo; // $I+
197  static const std::string SettingsReset; // $RST
198  static const std::string StartupLines; // $N $N0 $N1
199  static const std::string EnumerateAlarms; // $EQ
200  static const std::string EnumerateErrors; // $EE
201  static const std::string EnumerateGroups; // $EG
202  static const std::string EnumerateSettings; // $ES
203  static const std::string EnumerateAll; // $E*
204  static const std::string EnumeratePins; // $PINS
205  static const std::string ReportLastSignalsEvent; // $LEV
206  static const std::string ReportCurrentLimitState; // $LIM
207  static const std::string ReportSpindleData; // $SD
208  static const std::string SpindleResetData; // $SR
209 
210  static inline std::string getSetting(SettingId settingId)
211  {
212  return "$" + std::to_string(settingId);
213  }
214 
215  static std::string setSetting(SettingId settingId, const std::string &value);
216 };
217 
218 struct Realtime
219 {
220  static const std::string Reset; // 0x18 (ctrl-X)
221  static const std::string CycleStart; // 0x81 (ctrl-F)
222  static const std::string FeedHold; // 0x82 (ctrl-Y)
223  static const std::string StatusReport; // ?
224 };
225 
226 struct Setting
227 {
228  enum class HomingCycleBit : uint8_t
229  {
230  Enable = 0x01,
231  SingleAxisCommands = 0x02,
232  InitLock = 0x04,
233  ForceSetOrigin = 0x08,
234 
235  Manual = 0x20,
236  OverrideLocks = 0x40,
237  KeepOnReset = 0x80
238  };
239 
240  enum class StatusReportBit : uint16_t
241  {
242  MachinePosition = 0x01,
243  BufferState = 0x02,
244  LineNumbers = 0x04,
245  FeedSpeed = 0x08,
246  PinState = 0x10,
247  WorkCoordOffset = 0x20,
248  Overrides = 0x40,
249  ProbeCoordinates = 0x80,
250  SyncOnWCOChange = 0x100,
251  ParserState = 0x200,
252  AlarmSubState = 0x400,
253  RunSubState = 0x800
254  };
255 };
256 
257 } // namespace gen
258 
259 struct GeneratorData;
260 
262 {
263 public:
264  Generator();
265  Generator(const Generator &) = default;
266 
267  std::string str() const;
268  bool fail() const;
269 
270  const std::string &errorString() const;
271 
272  const Generator &operator<<(const gen::Modal &modal) const;
273  const Generator &operator<<(const gen::Position &position) const;
274 
275 protected:
276  std::shared_ptr<GeneratorData> m_data;
277 };
278 
281 } // namespace grbl
282 
283 template<>
284 struct ccut_enable_bitmask<grbl::gen::Setting::StatusReportBit> : std::true_type
285 {};
286 
287 #endif
const std::string & to_string(Axis::State state)
convert State to string
Definition: Axis.cpp:78
static Modal set(uint8_t system)
construct modal to set coordinate system
static Modal SetHome(uint8_t index=0)
Set home coordinate.
static Modal SetCoordinate(int8_t system=-1, bool current=false)
Set the Coordinate command.
static Modal GoHome(uint8_t index=0)
Go to home coordinate.
ModalGroup
Modal Groups of commands.
CoordinateSystem control commands.
Digital output control commands.
distance-mode selector
MotionMode selector.
non-modal special modal group
set given axis to position
System immediate commands.