MotionLib  1.0.0
SamBuCa motion library
MGrblAxis.cpp
1 /*
2 ** Copyright (C) 2025 CERN
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: 2025-04-17T11:31:20
21 ** Author: Sylvain Fargier <sylvain.fargier@cern.ch>
22 */
23 
24 #include "MGrblAxis.hpp"
25 
26 #include <ccut/async.hpp>
27 
28 #include "MGrblPlatform.hpp"
29 #include "util/grbl/GrblDeviceBase.hpp"
30 #include "util/grbl/GrblGenerator.hpp"
31 
32 using namespace logger;
33 using namespace smc;
34 using namespace smc::internal;
35 using namespace smc::units;
37 
38 MGrblAxis::MGrblAxis(const std::string &uid,
39  const std::shared_ptr<MGrblPlatform> &mgrbl,
40  const std::shared_ptr<GrblPlatform> &grbl) :
41  GrblAxis(uid, grbl),
42  m_mgrbl{mgrbl}
43 {}
44 
45 void MGrblAxis::updateInstance() const
46 {
47  std::shared_ptr<MGrblPlatform> mgrbl(m_mgrbl.lock());
48  if (!mgrbl)
49  throw Exception(ErrorCode::Canceled, "platform stopped (deleted)");
50 
51  mgrbl->updateInstance(const_cast<MGrblAxis &>(*this));
52 }
53 
54 std::future<void> MGrblAxis::moveTo(const units::Value &position) const
55 {
56  try
57  {
58  updateInstance();
59  return GrblAxis::moveTo(position);
60  }
61  catch (const Exception &ex)
62  {
63  return ccut::make_future_error(ex);
64  }
65 }
66 
67 std::future<void> MGrblAxis::moveBy(const units::Value &distance) const
68 {
69  try
70  {
71  updateInstance();
72  return GrblAxis::moveBy(distance);
73  }
74  catch (const Exception &ex)
75  {
76  return ccut::make_future_error(ex);
77  }
78 }
79 
80 std::future<void> MGrblAxis::stop() const
81 {
82  try
83  {
84  updateInstance();
85  return GrblAxis::stop();
86  }
87  catch (const Exception &ex)
88  {
89  return ccut::make_future_error(ex);
90  }
91 }
92 
93 std::future<void> MGrblAxis::pause() const
94 {
95  try
96  {
97  updateInstance();
98  return GrblAxis::pause();
99  }
100  catch (const Exception &ex)
101  {
102  return ccut::make_future_error(ex);
103  }
104 }
105 
106 std::future<void> MGrblAxis::resume() const
107 {
108  try
109  {
110  updateInstance();
111  return GrblAxis::resume();
112  }
113  catch (const Exception &ex)
114  {
115  return ccut::make_future_error(ex);
116  }
117 }
118 
119 std::future<units::value_t> MGrblAxis::getPosition(units::unit_t unit) const
120 {
121  try
122  {
123  updateInstance();
124  return GrblAxis::getPosition(unit);
125  }
126  catch (const Exception &ex)
127  {
128  return ccut::make_future_error<units::value_t>(ex);
129  }
130 }
131 
132 units::value_t MGrblAxis::lastPosition(units::unit_t unit) const
133 {
134  updateInstance();
135  return GrblAxis::lastPosition(unit);
136 }
137 
138 std::future<void> MGrblAxis::setActualPosition(const units::Value &position) const
139 {
140  try
141  {
142  updateInstance();
143  return GrblAxis::setActualPosition(position);
144  }
145  catch (const Exception &ex)
146  {
147  return ccut::make_future_error(ex);
148  }
149 }
150 
151 std::future<void> MGrblAxis::takeReference(
152  const std::chrono::milliseconds &timeout) const
153 {
154  try
155  {
156  updateInstance();
157  return GrblAxis::takeReference(timeout);
158  }
159  catch (const Exception &ex)
160  {
161  return ccut::make_future_error(ex);
162  }
163 }
164 
165 std::future<Axis::State> MGrblAxis::getState() const
166 {
167  try
168  {
169  updateInstance();
170  return GrblAxis::getState();
171  }
172  catch (const Exception &ex)
173  {
174  return ccut::make_future_error<Axis::State>(ex);
175  }
176 }
177 
178 std::future<std::set<std::string>> MGrblAxis::listConfig() const
179 {
180  try
181  {
182  updateInstance();
183  return GrblAxis::listConfig();
184  }
185  catch (const Exception &ex)
186  {
187  return ccut::make_future_error<std::set<std::string>>(ex);
188  }
189 }
190 
191 std::future<std::string> MGrblAxis::getConfig(const std::string &name) const
192 {
193  try
194  {
195  updateInstance();
196  return GrblAxis::getConfig(name);
197  }
198  catch (const Exception &ex)
199  {
200  return ccut::make_future_error<std::string>(ex);
201  }
202 }
203 
204 std::future<void> MGrblAxis::setConfig(const std::string &name,
205  const std::string &value) const
206 {
207  try
208  {
209  std::shared_ptr<MGrblPlatform> mgrbl(m_mgrbl.lock());
210  if (!mgrbl)
211  throw Exception(ErrorCode::Canceled, "platform stopped (deleted)");
212 
213  const std::string uid{this->uid()};
214  std::shared_ptr<GrblPlatform> grbl{mgrbl->getFirstInstance()};
215  grbl::Axis grblAxis = grbl->getAxis(uid);
216 
217  return mgrbl->run<void>([name, value, grblAxis, uid,
218  mgrbl](ImmediateCmd &cmd,
219  MGrblPlatform::Context &ctx) {
220  grbl::SettingId id;
221  mgrbl->runOnInstance(
222  ctx,
223  [&name, &grblAxis, &id](GrblPlatform::Shared &,
224  GrblPlatform::Context &ctx) {
225  id = ctx.findSetting(name, grblAxis).id;
226  },
227  true);
228 
229  const std::string settingCmd = grbl::gen::System::setSetting(id,
230  value);
231  mgrbl->runOnInstance(
232  ctx,
233  [&settingCmd, &uid, &id, &value](GrblPlatform::Shared &grbl,
234  GrblPlatform::Context &ctx) {
235  grbl::ErrorCode rc = ctx.device->send(settingCmd);
236  ctx.checkReply(rc);
237  grbl->updateAxisSetting(uid, id, value);
238  });
239  cmd.prom->get<void>().set_value();
240  });
241  }
242  catch (const Exception &ex)
243  {
244  return ccut::make_future_error<void>(ex);
245  }
246 }
247 
248 bool MGrblAxis::canConvert(const units::Value &value, units::unit_t unit) const
249 {
250  updateInstance();
251  return GrblAxis::canConvert(value, unit);
252 }
253 
254 bool MGrblAxis::convert(units::Value &value, units::unit_t unit) const
255 {
256  updateInstance();
257  return GrblAxis::convert(value, unit);
258 }
DeviceId uid() const
Get the address of device.
Definition: DeviceBase.cpp:76
Exception thrown by MotionController in case of issues with command.
Definition: Exception.hpp:61
std::future< State > getState() const override
Get the current axis state.
Definition: GrblAxis.cpp:391
bool canConvert(const units::Value &value, units::unit_t unit) const override
Check if unit conversion is possible in a manner specific to this device.
Definition: GrblAxis.cpp:506
std::future< void > resume() const override
Resume paused motion.
Definition: GrblAxis.cpp:216
std::future< void > pause() const override
Pause ongoing motion, prevent queued motions from running.
Definition: GrblAxis.cpp:195
std::future< void > takeReference(const std::chrono::milliseconds &timeout=std::chrono::seconds{ 120}) const override
Start a homing sequence.
Definition: GrblAxis.cpp:329
std::future< std::set< std::string > > listConfig() const override
list device configuration options
Definition: GrblAxis.cpp:413
std::future< units::value_t > getPosition(units::unit_t unit) const override
Get current position of this axis.
Definition: GrblAxis.cpp:238
units::value_t lastPosition(units::unit_t unit) const override
Get last known position of the axis.
Definition: GrblAxis.cpp:308
std::future< void > setActualPosition(const units::Value &position) const override
Set current axis position.
Definition: GrblAxis.cpp:273
std::future< void > stop() const override
Stop any ongoing motion, flush the motion-queue.
Definition: GrblAxis.cpp:172
std::future< std::string > getConfig(const std::string &name) const override
get device configuration value
Definition: GrblAxis.cpp:434
bool convert(units::Value &value, units::unit_t unit) const override
Convert value in a manner specific to this device.
Definition: GrblAxis.cpp:512
std::future< void > moveBy(const units::Value &distance) const override
Move this axis by given distance.
Definition: GrblAxis.cpp:92
std::future< void > moveTo(const units::Value &position) const override
Move this axis to given position.
Definition: GrblAxis.cpp:48
bool canConvert(const units::Value &value, units::unit_t unit) const override
Check if unit conversion is possible in a manner specific to this device.
Definition: MGrblAxis.cpp:248
std::future< std::set< std::string > > listConfig() const override
list device configuration options
Definition: MGrblAxis.cpp:178
std::future< void > moveBy(const units::Value &distance) const override
Move this axis by given distance.
Definition: MGrblAxis.cpp:67
bool convert(units::Value &value, units::unit_t unit) const override
Convert value in a manner specific to this device.
Definition: MGrblAxis.cpp:254
std::future< void > moveTo(const units::Value &position) const override
Move this axis to given position.
Definition: MGrblAxis.cpp:54
std::future< void > takeReference(const std::chrono::milliseconds &timeout=std::chrono::seconds{ 120}) const override
Start a homing sequence.
Definition: MGrblAxis.cpp:151
std::future< void > stop() const override
Stop any ongoing motion, flush the motion-queue.
Definition: MGrblAxis.cpp:80
std::future< State > getState() const override
Get the current axis state.
Definition: MGrblAxis.cpp:165
units::value_t lastPosition(units::unit_t unit) const override
Get last known position of the axis.
Definition: MGrblAxis.cpp:132
std::future< std::string > getConfig(const std::string &name) const override
get device configuration value
Definition: MGrblAxis.cpp:191
std::future< void > setConfig(const std::string &name, const std::string &value) const override
set device configuration value
Definition: MGrblAxis.cpp:204
std::future< void > pause() const override
Pause ongoing motion, prevent queued motions from running.
Definition: MGrblAxis.cpp:93
std::future< void > resume() const override
Resume paused motion.
Definition: MGrblAxis.cpp:106
std::future< void > setActualPosition(const units::Value &position) const override
Set current axis position.
Definition: MGrblAxis.cpp:138
std::future< units::value_t > getPosition(units::unit_t unit) const override
Get current position of this axis.
Definition: MGrblAxis.cpp:119
main motion-lib namespace
Definition: Client.cpp:30
void checkReply(grbl::ErrorCode code)
check command result code
const grbl::GrblParser::SettingDesc & findSetting(const std::string &name, grbl::Axis axis) const
find setting id for axis