MotionLib  1.0.0
SamBuCa motion library
MFEAxis.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-17T15:48:58
21 ** Author: Sylvain Fargier <sylvain.fargier@cern.ch>
22 */
23 
24 #include "MFEAxis.hpp"
25 
26 #include <ccut/async.hpp>
27 #include <ccut/utils.hpp>
28 
29 #include "MFEPlatform.hpp"
30 
31 using namespace smc;
32 using namespace smc::internal;
34 
35 MFEAxis::MFEAxis(const std::string &uid,
36  const std::shared_ptr<MFEPlatform> &mfe,
37  const std::shared_ptr<GrblPlatform> &grbl) :
38  MGrblAxis(uid, mfe, grbl)
39 {}
40 
41 std::future<void> MFEAxis::setActualPosition(
42  const units::Value &userPosition) const
43 {
44  try
45  {
46  units::Value pos{userPosition};
47 
48  if (!convert(pos, units::unit_t::steps))
49  throw Exception(ErrorCode::IncompatibleUnits,
50  "failed to convert in steps");
51  /* this will update both `motion_core.drv_step` register and grbl
52  * internal position */
53  return setConfig("driver position", std::to_string(pos.value));
54  }
55  catch (const Exception &ex)
56  {
57  return ccut::make_future_error(ex);
58  }
59 }
60 std::future<std::set<std::string>> MFEAxis::listConfig() const
61 {
62  try
63  {
64  updateInstance();
65  std::shared_ptr<GrblPlatform> grbl(m_grbl.lock());
66  if (!grbl)
67  throw Exception(ErrorCode::Canceled, "platform stopped (deleted)");
68 
69  grbl::Axis grblAxis = grbl->getAxis(uid());
70 
71  return grbl->run<std::set<std::string>>(
72  [grblAxis](ImmediateCmd &cmd, GrblPlatform::Context &ctx) {
73  std::set<std::string> ret;
74  for (const std::string &name : ctx.listConfig(grblAxis))
75  {
76  if (!ccut::startsWith(name,
77  MFEPlatform::s_resolverSettingPrefix))
78  ret.insert(name);
79  }
80  cmd.prom->get<std::set<std::string>>().set_value(ret);
81  });
82  }
83  catch (const Exception &ex)
84  {
85  return ccut::make_future_error<std::set<std::string>>(ex);
86  }
87 }
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< std::set< std::string > > listConfig() const override
list device configuration options
Definition: MFEAxis.cpp:60
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 > setConfig(const std::string &name, const std::string &value) const override
set device configuration value
Definition: MGrblAxis.cpp:204
const std::string & to_string(Axis::State state)
convert State to string
Definition: Axis.cpp:78
main motion-lib namespace
Definition: Client.cpp:30