24 #include <ccut/utils.hpp>
25 #include <logger/Logger.hpp>
27 #include "GrblPlatform.hpp"
28 #include "util/grbl/GrblDeviceBase.hpp"
29 #include "util/grbl/GrblGenerator.hpp"
32 using namespace smc::internal;
33 using namespace logger;
37 GrblPlatform::Context::Context(
const std::string &prefix,
38 const std::string &
id) :
43 std::string GrblPlatform::Context::getErrorMessage(grbl::ErrorCode code)
const
45 decltype(errors)::const_iterator it = errors.find(code);
46 if (it == errors.end())
53 std::string GrblPlatform::Context::getAlarmMessage(grbl::AlarmCode alarm)
const
55 decltype(alarms)::const_iterator it = alarms.find(alarm);
56 if (it == alarms.end())
68 "command failed: " + getErrorMessage(code));
74 std::set<std::string> ret;
75 std::transform(setting.begin(), setting.end(),
76 std::inserter(ret, ret.begin()),
77 [](
const SettingMap::value_type &t) { return t.first; });
79 std::transform(localSetting.begin(), localSetting.end(),
80 std::inserter(ret, ret.begin()),
81 [](
const LocalSettingMap::value_type &t) { return t.first; });
88 std::set<std::string> ret;
89 std::string groupName =
to_string(grblAxis) +
"-axis";
90 auto group = settingGroup.find(groupName);
91 if (group != settingGroup.end())
94 int groupId = group->second;
95 for (
const auto &s : setting)
97 if (s.second.groupId == groupId)
99 if (ccut::startsWith(s.first, groupName))
100 ret.insert(s.first.substr(groupName.size()));
110 const std::string &name,
111 grbl::Axis axis)
const
113 std::string pfx =
to_string(axis) +
"-axis ";
114 SettingMap::const_iterator it = setting.find(pfx + name);
115 if (it == setting.end())
116 it = setting.find(name);
118 if (it == setting.end())
120 throw Exception(ErrorCode::InvalidArguments,
"no such setting: " + name);
126 const std::string &name)
const
128 SettingMap::const_iterator it = setting.find(name);
130 if (it == setting.end())
132 throw Exception(ErrorCode::InvalidArguments,
"no such setting: " + name);
137 void GrblPlatform::Context::setSetting(
const std::string &name,
138 const std::string &value)
140 LocalSettingMap::const_iterator itls = localSetting.find(name);
141 if (itls != localSetting.end())
143 itls->second.set(*
this, value);
147 _sendSetSetting(findSetting(name).
id, value);
154 grbl::ErrorCode rc = device->send(
155 grbl::gen::System::getSetting(
id),
156 [
this,
id, &ret](
const std::string &line) {
157 if (GrblParser::getLineType(line) == grbl::LineType::Setting)
159 GrblParser::Setting setting;
160 if ((GrblParser::parseSetting(line, setting)) &&
161 (setting.first ==
id))
163 ret = setting.second;
174 const std::string &value)
176 grbl::ErrorCode rc = device->send(grbl::gen::System::setSetting(
id, value));
180 bool GrblPlatform::Context::isStepperMessage(
const std::string &line)
182 return ccut::startsWith(line,
"[MSG:Stepper");
187 grbl::ErrorCode ret{-1};
188 for (
size_t i = 0; (ret != grbl::ErrorCode{0}) && (i < 3); ++i)
190 ret = this->device->sendRealtime(
191 grbl::gen::Realtime::Reset, [](
const std::string &line) {
192 if (ccut::startsWith(line,
"GrblHAL"))
193 throw grbl::ErrorCode{0};
196 if (ret != grbl::ErrorCode{0})
198 warning(s_loggerCat) <<
"failed to reset";
199 std::this_thread::sleep_for(std::chrono::milliseconds{1000});
202 if (ret != grbl::ErrorCode{0})
203 crit(s_loggerCat) <<
"failed to reset";
210 for (
size_t i = 0; (ret != grbl::ErrorCode{0}) && (i < 3); ++i)
212 std::this_thread::sleep_for(std::chrono::milliseconds{100});
213 ret = this->device->send(grbl::gen::System::DisableLock);
215 if (ret != grbl::ErrorCode{0})
216 crit(s_loggerCat) <<
"failed to unlock";
219 for (
size_t i = 0; (ret != grbl::ErrorCode{0}) && (i < 3); ++i)
220 ret = this->device->sendRealtime(
221 grbl::gen::Realtime::Reset, [](
const std::string &line) {
222 if (ccut::startsWith(line,
"GrblHAL"))
223 throw grbl::ErrorCode{0};
226 if (ret != grbl::ErrorCode{0})
227 crit(s_loggerCat) <<
"failed to reset";
232 for (
size_t i = 0; (ret != grbl::ErrorCode{0}) && (i < 3); ++i)
234 ret = this->device->sendRealtime(
235 grbl::gen::Realtime::StatusReport,
236 [
this](
const std::string &message) {
238 grbl::LineType::State)
240 this->device->dataSignal(message);
241 throw grbl::ErrorCode{0};
245 if (ret != grbl::ErrorCode{0})
247 warning(s_loggerCat) <<
"failed to retrieve state";
248 std::this_thread::sleep_for(std::chrono::milliseconds{1000});
251 if (ret != grbl::ErrorCode{0})
252 crit(s_loggerCat) <<
"failed to retrieve state";
253 info(s_loggerCat) <<
"instance reset, state: " << this->state.runState;
static LineType getLineType(const std::string &line)
detect line type
Exception thrown by MotionController in case of issues with command.
const std::string & to_string(Axis::State state)
convert State to string
main motion-lib namespace
void _sendSetSetting(grbl::SettingId settingId, const std::string &value)
set a setting on Grbl
std::set< std::string > listConfig()
list config values
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
void sendReset()
reset grbl instance
std::string _sendGetSetting(grbl::SettingId settingId)
retrieve a setting from Grbl, eventually update internal cache