24 #include "MFEAxisPositionMonitor.hpp"
26 #include <ccut/async.hpp>
27 #include <ccut/utils.hpp>
29 #include "MFEPlatform.hpp"
30 #include "device/AxisPositionMonitor.hpp"
31 #include "platform/specialized/MFEPlatform/MFEAxis.hpp"
34 using namespace smc::internal;
38 MFEAxisPositionMonitor::MFEAxisPositionMonitor(
39 const std::string &uid,
40 const std::shared_ptr<Axis> &axis,
41 const std::shared_ptr<PositionSensor> &position,
42 const std::shared_ptr<MFEPlatform> &mfe) :
47 static inline std::future<units::value_t> getReg(MFEPlatform::Shared &mfe,
48 const std::string ®Name)
57 throw Exception{ErrorCode::InvalidArguments,
58 "edge driver not available"};
59 else if (!ctx.getRegInfo(regName, info))
60 throw Exception{ErrorCode::InvalidArguments,
61 "failed to get reg info: " + regName};
64 ctx.edge->read(info.reg, info.shift, value);
67 units::value_t::value_type(
reinterpret_cast<int32_t &
>(value))};
72 std::future<units::value_t> MFEAxisPositionMonitor::getDifference(
73 units::unit_t unit)
const
77 if (unit != units::steps)
78 throw Exception{ErrorCode::IncompatibleUnits,
79 "failed to convert to " +
to_string(unit)};
81 MFEPlatform::Shared mfe(m_mfe.lock());
83 throw Exception(ErrorCode::Canceled,
"platform stopped (deleted)");
85 const std::string regName{
uid() +
"/diff"};
86 return getReg(mfe, regName);
90 return ccut::make_future_error<units::value_t>(ex);
95 units::unit_t unit)
const
99 if (unit != units::steps)
100 throw Exception{ErrorCode::IncompatibleUnits,
101 "failed to convert to " +
to_string(unit)};
103 MFEPlatform::Shared mfe(m_mfe.lock());
105 throw Exception(ErrorCode::Canceled,
"platform stopped (deleted)");
107 const std::string regName{
uid() +
"/diff/max"};
108 return getReg(mfe, regName);
112 return ccut::make_future_error<units::value_t>(ex);
120 MFEPlatform::Shared mfe(m_mfe.lock());
121 std::shared_ptr<const MFEAxisPositionMonitor>
self{
122 std::static_pointer_cast<const MFEAxisPositionMonitor>(
123 shared_from_this())};
126 throw Exception(ErrorCode::Canceled,
"platform stopped (deleted)");
128 return mfe->run<State>(
130 mfe->processUpdate(ctx);
132 cmd.prom->get<State>().set_value(self->lastState());
137 return ccut::make_future_error<State>(ex);
145 MFEPlatform::Shared mfe(m_mfe.lock());
147 throw Exception(ErrorCode::Canceled,
"platform stopped (deleted)");
149 const std::string regName{
uid() +
"/slost/gcr"};
150 return mfe->run<
void>(
158 throw Exception{ErrorCode::InvalidArguments,
159 "edge driver not available"};
161 throw Exception{ErrorCode::InvalidArguments,
162 "failed to get reg info: " + regName};
164 uint32_t mask{info.mask << info.shift};
165 ctx.edge->read(info.reg, gcr);
168 if (!enabled || !(gcr & mask))
169 ctx.edge->write(info.reg, gcr | mask);
171 ctx.edge->write(info.reg, gcr & ~mask);
173 logger::info(MFEPlatform::s_loggerCat)
174 <<
"step-lost detection "
175 << (enabled ?
"enabled" :
"disabled") <<
" on " << regName;
176 cmd.prom->get<
void>().set_value();
181 return ccut::make_future_error<void>(ex);
187 MFEPlatform::Shared mfe(m_mfe.lock());
189 throw Exception(ErrorCode::Canceled,
"platform stopped (deleted)");
190 else if (unit != units::steps)
191 throw Exception(ErrorCode::IncompatibleUnits,
192 "failed to convert position");
194 const std::string &
uid{this->
uid()};
196 mfe->getResolverMonitorState(
198 0,
uid.length() - MFEPlatform::s_resolverMonitorSuffix.length()),
201 double(*
reinterpret_cast<const int32_t *
>(&diff))};
206 MFEPlatform::Shared mfe(m_mfe.lock());
208 throw Exception(ErrorCode::Canceled,
"platform stopped (deleted)");
210 const std::string &
uid{this->
uid()};
212 return mfe->getResolverMonitorState(
214 0,
uid.length() - MFEPlatform::s_resolverMonitorSuffix.length()),
222 std::shared_ptr<MFEPlatform> mfe(m_mfe.lock());
224 throw Exception(ErrorCode::Canceled,
"platform stopped (deleted)");
226 grbl::Axis grblAxis = mfe->getResolverMonitorAxis(
uid());
228 return mfe->run<std::set<std::string>>(
230 std::set<std::string> ret;
233 [&ret, grblAxis](GrblPlatform::Shared &,
235 for (
const std::string &name : ctx.
listConfig(grblAxis))
237 if (ccut::startsWith(
238 name, MFEPlatform::s_resolverSettingPrefix))
240 const std::string setting{name.substr(
241 MFEPlatform::s_resolverSettingPrefix.size())};
242 if (!MFEPlatform::s_monitorConfigHidden.count(
249 cmd.prom->get<std::set<std::string>>().set_value(ret);
254 return ccut::make_future_error<std::set<std::string>>(ex);
259 const std::string &name)
const
263 PositionSensor::Shared position{positionSensorDevice()};
267 ErrorCode::InvalidArguments,
268 "Failed to find positionSensor for device: " +
uid());
270 return position->getConfig(name);
274 return ccut::make_future_error<std::string>(ex);
279 const std::string &value)
const
283 PositionSensor::Shared position{positionSensorDevice()};
287 ErrorCode::InvalidArguments,
288 "Failed to find positionSensor for device: " +
uid());
290 return position->setConfig(name, value);
294 return ccut::make_future_error(ex);
This object represent a link between an axis and a position sensor.
DeviceId uid() const
Get the address of device.
Exception thrown by MotionController in case of issues with command.
units::value_t lastDifference(units::unit_t unit) const override
Get last known difference.
std::future< void > setEnabled(bool enabled) override
Enable/disable the monitor.
std::future< State > getState() const override
Get the current device state.
std::future< std::set< std::string > > listConfig() const override
list device configuration options
std::future< void > setConfig(const std::string &name, const std::string &value) const override
set device configuration value
std::future< units::value_t > getMaxDifference(units::unit_t unit) const override
Get max difference since monitor was enabled.
std::future< std::string > getConfig(const std::string &name) const override
get device configuration value
State lastState() const override
Get last known state.
const std::string & to_string(Axis::State state)
convert State to string
main motion-lib namespace
std::set< std::string > listConfig()
list config values
bool getRegInfo(const std::string &uid, RegInfo &info) const
Get the RegInfo object.