MotionLib  1.0.0
SamBuCa motion library
MEdgeGrblDevice.cpp
1 /*
2 ** Copyright (C) 2022 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: 2022-09-22T19:00:04
21 ** Author: Sylvain Fargier <sylvain.fargier@cern.ch>
22 */
23 
24 #include "MEdgeGrblDevice.hpp"
25 
26 #include <chrono>
27 #include <cstdlib>
28 
29 #include <ccut/Regex.hpp>
30 #include <ccut/utils.hpp>
31 #include <logger/Logger.hpp>
32 
33 #include "Exception.hpp"
34 #include "util/grbl/MGrblParser.hpp"
35 
36 using namespace logger;
37 using namespace std::chrono;
38 
39 static const ccut::Regex s_grblInit("^CMGrbl.*$");
40 
41 namespace edge {
42 
43 MEdgeGrblDevice::MEdgeGrblDevice(int lun, const std::string &driverPath)
44 {
45  setenv("GRBL_SIM_INSTANCES", "8", 1);
46  init(lun, driverPath);
47  const std::string cmd = mgrbl::ControlSymbol +
48  mgrbl::gen::Control::GrblCount;
49  if (send(
50  cmd,
51  [&cmd](const std::string &msg) {
52  notice(loggerCat) << "<--(init) " << msg;
53  if (s_grblInit.search(msg))
54  throw grbl::ErrorCode{0};
55  else if (ccut::startsWith(msg, cmd))
56  return true;
57  return true;
58  },
59  milliseconds{5000}) != grbl::ErrorCode{0})
60  {
61  error(loggerCat) << "MGrbl initialization failed";
62  throw smc::Exception{smc::ErrorCode::InternalError,
63  std::string{"Failed to get Grbl banner"}};
64  }
65 }
66 
67 } // namespace edge
Exception thrown by MotionController in case of issues with command.
Definition: Exception.hpp:61