MotionLib  1.0.0
SamBuCa motion library
MGrblMainTrigger.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:19:38
21 ** Author: Sylvain Fargier <sylvain.fargier@cern.ch>
22 */
23 
24 #include "MGrblMainTrigger.hpp"
25 
26 #include <ccut/async.hpp>
27 
28 #include "MGrblPlatform.hpp"
29 
30 using namespace logger;
31 using namespace smc;
32 using namespace smc::internal;
33 using namespace smc::units;
35 
36 MGrblMainTrigger::MGrblMainTrigger(const std::string &uid,
37  const std::shared_ptr<MGrblPlatform> &mgrbl) :
38  Trigger{uid},
39  m_mgrbl(mgrbl)
40 {}
41 
42 std::future<void> MGrblMainTrigger::arm() const
43 {
44  try
45  {
46  std::shared_ptr<MGrblPlatform> mgrbl(m_mgrbl.lock());
47  if (!mgrbl)
48  throw Exception(ErrorCode::Canceled, "platform stopped (deleted)");
49 
50  return mgrbl->run<void>(
51  [](ImmediateCmd &cmd, MGrblPlatform::Context &ctx) {
52  grbl::ErrorCode rc = ctx.device->send(
53  mgrbl::ControlSymbol + mgrbl::gen::Control::TrigArm + "=1");
54  ctx.checkReply(rc);
55  cmd.prom->get<void>().set_value();
56  });
57  }
58  catch (const Exception &ex)
59  {
60  return ccut::make_future_error(ex);
61  }
62 }
63 
64 std::future<void> MGrblMainTrigger::disarm() const
65 {
66  try
67  {
68  std::shared_ptr<MGrblPlatform> mgrbl(m_mgrbl.lock());
69  if (!mgrbl)
70  throw Exception(ErrorCode::Canceled, "platform stopped (deleted)");
71 
72  return mgrbl->run<void>(
73  [](ImmediateCmd &cmd, MGrblPlatform::Context &ctx) {
74  grbl::ErrorCode rc = ctx.device->send(
75  mgrbl::ControlSymbol + mgrbl::gen::Control::TrigArm + "=0");
76  ctx.checkReply(rc);
77  cmd.prom->get<void>().set_value();
78  });
79  }
80  catch (const Exception &ex)
81  {
82  return ccut::make_future_error(ex);
83  }
84 }
85 
86 std::future<bool> MGrblMainTrigger::isArmed() const
87 {
88  try
89  {
90  std::shared_ptr<MGrblPlatform> mgrbl(m_mgrbl.lock());
91  if (!mgrbl)
92  throw Exception(ErrorCode::Canceled, "platform stopped (deleted)");
93 
94  return mgrbl->run<bool>([](ImmediateCmd &cmd,
96  bool ret{false};
97  grbl::ErrorCode rc = ctx.device->send(
98  (mgrbl::ControlSymbol + mgrbl::gen::Control::TrigArm),
99  [&ret](const std::string &line) {
100  return mgrbl::MGrblParser::parseIsArmed(line.substr(1), ret);
101  });
102  ctx.checkReply(rc);
103  cmd.prom->get<bool>().set_value(ret);
104  });
105  }
106  catch (const Exception &ex)
107  {
108  return ccut::make_future_error<bool>(ex);
109  }
110 }
111 
112 std::future<void> MGrblMainTrigger::trig() const
113 {
114  try
115  {
116  std::shared_ptr<MGrblPlatform> mgrbl(m_mgrbl.lock());
117  if (!mgrbl)
118  throw Exception(ErrorCode::Canceled, "platform stopped (deleted)");
119 
120  return mgrbl->run<void>(
121  [](ImmediateCmd &cmd, MGrblPlatform::Context &ctx) {
122  grbl::ErrorCode rc = ctx.device->send(
123  mgrbl::ControlSymbol + mgrbl::gen::Control::TrigSoft);
124  ctx.checkReply(rc);
125  cmd.prom->get<void>().set_value();
126  });
127  }
128  catch (const Exception &ex)
129  {
130  return ccut::make_future_error(ex);
131  }
132 }
static bool parseIsArmed(const std::string &line, bool &isArmed)
parse isArmed reply
Definition: MGrblParser.cpp:65
Exception thrown by MotionController in case of issues with command.
Definition: Exception.hpp:61
std::future< void > disarm() const override
disarm the trigger
std::future< void > trig() const override
soft-trigger the trigger
std::future< bool > isArmed() const override
check if trigger is armed
main motion-lib namespace
Definition: Client.cpp:30