3 #ifndef __TECHNOSOFT_IPOS_BASE_HPP__
4 #define __TECHNOSOFT_IPOS_BASE_HPP__
13 #include <yarp/conf/numeric.h>
15 #include <yarp/os/Timer.h>
17 #include <yarp/dev/DeviceDriver.h>
18 #include <yarp/dev/IAxisInfo.h>
19 #include <yarp/dev/IControlLimits.h>
20 #include <yarp/dev/IControlMode.h>
21 #include <yarp/dev/ICurrentControl.h>
22 #include <yarp/dev/IEncodersTimed.h>
23 #include <yarp/dev/IImpedanceControl.h>
24 #include <yarp/dev/IInteractionMode.h>
25 #include <yarp/dev/IJointFault.h>
26 #include <yarp/dev/IMotor.h>
27 #include <yarp/dev/IMotorEncoders.h>
28 #include <yarp/dev/IPidControl.h>
29 #include <yarp/dev/IPositionControl.h>
30 #include <yarp/dev/IPositionDirect.h>
31 #include <yarp/dev/IRemoteVariables.h>
32 #include <yarp/dev/ITorqueControl.h>
33 #include <yarp/dev/IVelocityControl.h>
34 #include <yarp/dev/PolyDriver.h>
36 #include "CanOpenNode.hpp"
37 #include "CommandBuffer.hpp"
38 #include "EncoderRead.hpp"
39 #include "ICanBusSharer.hpp"
40 #include "PdoProtocol.hpp"
41 #include "StateObserver.hpp"
43 #define CHECK_JOINT(j) do { if (int ax; getAxes(&ax), (j) != ax - 1) return false; } while (0)
45 #define CHECK_MODE(mode) do { if ((mode) != actualControlMode) return false; } while (0)
55 public yarp::dev::IAxisInfoRaw,
56 public yarp::dev::IControlLimitsRaw,
57 public yarp::dev::IControlModeRaw,
58 public yarp::dev::ICurrentControlRaw,
59 public yarp::dev::IEncodersTimedRaw,
60 public yarp::dev::IImpedanceControlRaw,
61 public yarp::dev::IInteractionModeRaw,
62 public yarp::dev::IJointFaultRaw,
63 public yarp::dev::IMotorRaw,
64 public yarp::dev::IMotorEncodersRaw,
65 public yarp::dev::IPidControlRaw,
66 public yarp::dev::IPositionControlRaw,
67 public yarp::dev::IPositionDirectRaw,
68 public yarp::dev::IRemoteVariablesRaw,
69 public yarp::dev::ITorqueControlRaw,
70 public yarp::dev::IVelocityControlRaw,
77 iEncodersTimedRawExternal(
nullptr),
78 iExternalEncoderCanBusSharer(
nullptr),
79 monitorThread(
nullptr)
84 bool open(yarp::os::Searchable & config)
override;
85 bool close()
override;
89 unsigned int getId()
override;
98 bool getAxisNameRaw(
int axis, std::string & name)
override;
99 bool getJointTypeRaw(
int axis, yarp::dev::JointTypeEnum & type)
override;
103 bool setLimitsRaw(
int axis,
double min,
double max)
override;
104 bool getLimitsRaw(
int axis,
double * min,
double * max)
override;
105 bool setVelLimitsRaw(
int axis,
double min,
double max)
override;
106 bool getVelLimitsRaw(
int axis,
double * min,
double * max)
override;
110 bool getControlModesRaw(
int * modes)
override
111 {
return getControlModeRaw(0, &modes[0]); }
113 bool getControlModesRaw(
int n_joint,
const int * joints,
int * modes)
override
114 {
return getControlModeRaw(joints[0], &modes[0]); }
116 bool setControlModesRaw(
int * modes)
override
117 {
return setControlModeRaw(0, modes[0]); }
119 bool setControlModesRaw(
int n_joint,
const int * joints,
int * modes)
override
120 {
return setControlModeRaw(joints[0], modes[0]); }
124 bool getCurrentRaw(
int m,
double * curr)
override;
126 bool getCurrentsRaw(
double * currs)
override
127 {
return getCurrentRaw(0, &currs[0]); }
129 bool getCurrentRangeRaw(
int m,
double * min,
double * max)
override;
131 bool getCurrentRangesRaw(
double * min,
double * max)
override
132 {
return getCurrentRangeRaw(0, min, max); }
134 bool setRefCurrentRaw(
int m,
double curr)
override;
136 bool setRefCurrentsRaw(
const double * currs)
override
137 {
return setRefCurrentRaw(0, currs[0]); }
139 bool setRefCurrentsRaw(
int n_motor,
const int * motors,
const double * currs)
override
140 {
return setRefCurrentRaw(motors[0], currs[0]); }
142 bool getRefCurrentRaw(
int m,
double * curr)
override;
144 bool getRefCurrentsRaw(
double * currs)
override
145 {
return getRefCurrentRaw(0, &currs[0]); }
149 bool getAxes(
int * ax)
override;
150 bool resetEncoderRaw(
int j)
override;
152 bool resetEncodersRaw()
override
153 {
return resetEncoderRaw(0); }
155 bool setEncoderRaw(
int j,
double val)
override;
157 bool setEncodersRaw(
const double * vals)
override
158 {
return setEncoderRaw(0, vals[0]); }
160 bool getEncoderRaw(
int j,
double * v)
override;
162 bool getEncodersRaw(
double * encs)
override
163 {
return getEncoderRaw(0, &encs[0]); }
165 bool getEncoderSpeedRaw(
int j,
double * sp)
override;
167 bool getEncoderSpeedsRaw(
double * spds)
override
168 {
return getEncoderSpeedRaw(0, &spds[0]); }
170 bool getEncoderAccelerationRaw(
int j,
double * spds)
override;
172 bool getEncoderAccelerationsRaw(
double * accs)
override
173 {
return getEncoderAccelerationRaw(0, &accs[0]); }
177 bool getEncoderTimedRaw(
int j,
double * encs,
double * time)
override;
179 bool getEncodersTimedRaw(
double * encs,
double * times)
override
180 {
return getEncoderTimedRaw(0, &encs[0], ×[0]); }
184 bool getImpedanceRaw(
int j,
double * stiffness,
double * damping)
override
187 bool setImpedanceRaw(
int j,
double stiffness,
double damping)
override
190 bool setImpedanceOffsetRaw(
int j,
double offset)
override
193 bool getImpedanceOffsetRaw(
int j,
double * offset)
override
196 bool getCurrentImpedanceLimitRaw(
int j,
double * min_stiff,
double * max_stiff,
double * min_damp,
double * max_damp)
override
201 bool getInteractionModeRaw(
int axis, yarp::dev::InteractionModeEnum * mode)
override
204 bool getInteractionModesRaw(
int n_joints,
int * joints, yarp::dev::InteractionModeEnum * modes)
override
205 {
return getInteractionModeRaw(joints[0], &modes[0]); }
207 bool getInteractionModesRaw(yarp::dev::InteractionModeEnum * modes)
override
208 {
return getInteractionModeRaw(0, &modes[0]); }
210 bool setInteractionModeRaw(
int axis, yarp::dev::InteractionModeEnum mode)
override
213 bool setInteractionModesRaw(
int n_joints,
int * joints, yarp::dev::InteractionModeEnum * modes)
override
214 {
return setInteractionModeRaw(joints[0], modes[0]); }
216 bool setInteractionModesRaw(yarp::dev::InteractionModeEnum * modes)
override
217 {
return setInteractionModeRaw(0, modes[0]); }
221 bool getLastJointFaultRaw(
int j,
int & fault, std::string & message)
override;
225 bool getNumberOfMotorsRaw(
int * num)
override;
226 bool getTemperatureRaw(
int m,
double * val)
override;
227 bool getTemperaturesRaw(
double * vals)
override;
228 bool getTemperatureLimitRaw(
int m,
double * temp)
override;
229 bool setTemperatureLimitRaw(
int m,
double temp)
override;
230 bool getGearboxRatioRaw(
int m,
double * val)
override;
231 bool setGearboxRatioRaw(
int m,
double val)
override;
235 bool getNumberOfMotorEncodersRaw(
int * num)
override;
236 bool resetMotorEncoderRaw(
int m)
override;
238 bool resetMotorEncodersRaw()
override
239 {
return resetMotorEncoderRaw(0); }
241 bool setMotorEncoderCountsPerRevolutionRaw(
int m,
double cpr)
override;
242 bool getMotorEncoderCountsPerRevolutionRaw(
int m,
double * cpr)
override;
243 bool setMotorEncoderRaw(
int m,
double val)
override;
245 bool setMotorEncodersRaw(
const double * vals)
override
246 {
return setMotorEncoderRaw(0, vals[0]); }
248 bool getMotorEncoderRaw(
int m,
double * v)
override;
250 bool getMotorEncodersRaw(
double * encs)
override
251 {
return getMotorEncoderSpeedRaw(0, &encs[0]); }
253 bool getMotorEncoderTimedRaw(
int m,
double * encs,
double * stamp)
override;
255 bool getMotorEncodersTimedRaw(
double * encs,
double * stamps)
override
256 {
return getMotorEncoderTimedRaw(0, &encs[0], &stamps[0]); }
258 bool getMotorEncoderSpeedRaw(
int m,
double * sp)
override;
260 bool getMotorEncoderSpeedsRaw(
double * spds)
override
261 {
return getMotorEncoderSpeedRaw(0, &spds[0]); }
263 bool getMotorEncoderAccelerationRaw(
int m,
double * spds)
override;
265 bool getMotorEncoderAccelerationsRaw(
double * accs)
override
266 {
return getMotorEncoderAccelerationRaw(0, &accs[0]); }
270 bool setPidRaw(
const yarp::dev::PidControlTypeEnum & pidtype,
int j,
const yarp::dev::Pid & pid)
override
273 bool setPidsRaw(
const yarp::dev::PidControlTypeEnum & pidtype,
const yarp::dev::Pid * pids)
override
274 {
return setPidRaw(pidtype, 0, pids[0]); }
276 bool setPidReferenceRaw(
const yarp::dev::PidControlTypeEnum & pidtype,
int j,
double ref)
override
279 bool setPidReferencesRaw(
const yarp::dev::PidControlTypeEnum & pidtype,
const double * refs)
override
280 {
return setPidReferenceRaw(pidtype, 0, refs[0]); }
282 bool setPidErrorLimitRaw(
const yarp::dev::PidControlTypeEnum & pidtype,
int j,
double limit)
override
285 bool setPidErrorLimitsRaw(
const yarp::dev::PidControlTypeEnum & pidtype,
const double * limits)
override
286 {
return setPidErrorLimitRaw(pidtype, 0, limits[0]); }
288 bool getPidErrorRaw(
const yarp::dev::PidControlTypeEnum & pidtype,
int j,
double * err)
override
291 bool getPidErrorsRaw(
const yarp::dev::PidControlTypeEnum & pidtype,
double * errs)
override
292 {
return getPidErrorRaw(pidtype, 0, &errs[0]); }
294 bool getPidOutputRaw(
const yarp::dev::PidControlTypeEnum & pidtype,
int j,
double * out)
override
297 bool getPidOutputsRaw(
const yarp::dev::PidControlTypeEnum & pidtype,
double * outs)
override
298 {
return getPidOutputRaw(pidtype, 0, &outs[0]); }
300 bool getPidRaw(
const yarp::dev::PidControlTypeEnum & pidtype,
int j, yarp::dev::Pid * pid)
override
303 bool getPidsRaw(
const yarp::dev::PidControlTypeEnum & pidtype, yarp::dev::Pid * pids)
override
304 {
return getPidRaw(pidtype, 0, &pids[0]); }
306 bool getPidReferenceRaw(
const yarp::dev::PidControlTypeEnum & pidtype,
int j,
double * ref)
override
309 bool getPidReferencesRaw(
const yarp::dev::PidControlTypeEnum & pidtype,
double * refs)
override
310 {
return getPidReferenceRaw(pidtype, 0, &refs[0]); }
312 bool getPidErrorLimitRaw(
const yarp::dev::PidControlTypeEnum & pidtype,
int j,
double * limit)
override
315 bool getPidErrorLimitsRaw(
const yarp::dev::PidControlTypeEnum & pidtype,
double * limits)
override
316 {
return getPidErrorLimitRaw(pidtype, 0, &limits[0]); }
318 bool resetPidRaw(
const yarp::dev::PidControlTypeEnum & pidtype,
int j)
override
321 bool disablePidRaw(
const yarp::dev::PidControlTypeEnum & pidtype,
int j)
override
324 bool enablePidRaw(
const yarp::dev::PidControlTypeEnum & pidtype,
int j)
override
327 bool setPidOffsetRaw(
const yarp::dev::PidControlTypeEnum & pidtype,
int j,
double v)
override
330 bool isPidEnabledRaw(
const yarp::dev::PidControlTypeEnum & pidtype,
int j,
bool * enabled)
override
335 using yarp::dev::IPositionControlRaw::positionMoveRaw;
337 bool positionMoveRaw(
const double * refs)
override
338 {
return positionMoveRaw(0, refs[0]); }
340 bool positionMoveRaw(
int n_joint,
const int * joints,
const double * refs)
override
341 {
return positionMoveRaw(joints[0], refs[0]); }
343 using yarp::dev::IPositionControlRaw::relativeMoveRaw;
345 bool relativeMoveRaw(
const double * deltas)
override
346 {
return relativeMoveRaw(0, deltas[0]); }
348 bool relativeMoveRaw(
int n_joint,
const int * joints,
const double * deltas)
override
349 {
return relativeMoveRaw(joints[0], deltas[0]); }
351 using yarp::dev::IPositionControlRaw::checkMotionDoneRaw;
353 bool checkMotionDoneRaw(
bool * flag)
override
354 {
return checkMotionDoneRaw(0, flag); }
356 bool checkMotionDoneRaw(
int n_joint,
const int * joints,
bool * flag)
override
357 {
return checkMotionDoneRaw(joints[0], flag); }
359 bool setRefSpeedsRaw(
const double * spds)
override
360 {
return setRefSpeedRaw(0, spds[0]); }
362 bool setRefSpeedsRaw(
int n_joint,
const int * joints,
const double * spds)
override
363 {
return setRefSpeedRaw(joints[0], spds[0]); }
365 using yarp::dev::IPositionControlRaw::setRefAccelerationRaw;
367 bool setRefAccelerationsRaw(
const double * accs)
override
368 {
return setRefAccelerationRaw(0, accs[0]); }
370 bool setRefAccelerationsRaw(
int n_joint,
const int * joints,
const double * accs)
override
371 {
return setRefAccelerationRaw(joints[0], accs[0]); }
373 bool getRefSpeedsRaw(
double * spds)
override
374 {
return getRefSpeedRaw(0, &spds[0]); }
376 bool getRefSpeedsRaw(
int n_joint,
const int * joints,
double * spds)
override
377 {
return getRefSpeedRaw(joints[0], &spds[0]); }
379 using yarp::dev::IPositionControlRaw::getRefAccelerationRaw;
381 bool getRefAccelerationsRaw(
double * accs)
override
382 {
return getRefAccelerationRaw(0, &accs[0]); }
384 bool getRefAccelerationsRaw(
int n_joint,
const int * joints,
double * accs)
override
385 {
return getRefAccelerationRaw(joints[0], &accs[0]); }
387 using yarp::dev::IPositionControlRaw::stopRaw;
389 bool stopRaw()
override
390 {
return stopRaw(0); }
392 bool stopRaw(
int n_joint,
const int * joints)
override
393 {
return stopRaw(joints[0]); }
395 bool getTargetPositionsRaw(
double * refs)
override
396 {
return getTargetPositionRaw(0, &refs[0]); }
398 bool getTargetPositionsRaw(
int n_joint,
const int * joints,
double * refs)
override
399 {
return getTargetPositionRaw(joints[0], &refs[0]); }
403 bool setPositionsRaw(
const double * refs)
override
404 {
return setPositionRaw(0, refs[0]); }
406 bool setPositionsRaw(
int n_joint,
const int * joints,
const double * refs)
override
407 {
return setPositionRaw(joints[0], refs[0]); }
409 bool getRefPositionsRaw(
double * refs)
override
410 {
return getRefPositionRaw(0, &refs[0]); }
412 bool getRefPositionsRaw(
int n_joint,
const int * joints,
double * refs)
override
413 {
return getRefPositionRaw(joints[0], &refs[0]); }
417 bool getRefTorqueRaw(
int j,
double * t)
override;
419 bool getRefTorquesRaw(
double * t)
override
420 {
return getRefTorqueRaw(0, &t[0]); }
422 bool setRefTorqueRaw(
int j,
double t)
override;
424 bool setRefTorquesRaw(
int n_joint,
const int * joints,
const double * t)
override
425 {
return setRefTorqueRaw(joints[0], t[0]); }
427 bool setRefTorquesRaw(
const double * t)
override
428 {
return setRefTorqueRaw(0, t[0]); }
430 bool getTorqueRaw(
int j,
double * t)
override;
432 bool getTorquesRaw(
double * t)
override
433 {
return getTorqueRaw(0, &t[0]); }
435 bool getTorqueRangeRaw(
int j,
double * min,
double * max)
override;
437 bool getTorqueRangesRaw(
double * min,
double * max)
override
438 {
return getTorqueRangeRaw(0, &min[0], &max[0]); }
440 bool getMotorTorqueParamsRaw(
int j, yarp::dev::MotorTorqueParameters * params)
override;
441 bool setMotorTorqueParamsRaw(
int j,
const yarp::dev::MotorTorqueParameters params)
override;
445 using yarp::dev::IVelocityControlRaw::velocityMoveRaw;
447 bool velocityMoveRaw(
const double * sp)
override
448 {
return velocityMoveRaw(0, sp[0]); }
450 bool velocityMoveRaw(
int n_joint,
const int * joints,
const double * spds)
override
451 {
return velocityMoveRaw(joints[0], spds[0]); }
453 bool getRefVelocitiesRaw(
double * vels)
override
454 {
return getRefVelocityRaw(0, &vels[0]); }
456 bool getRefVelocitiesRaw(
int n_joint,
const int * joints,
double * vels)
override
457 {
return getRefVelocityRaw(joints[0], &vels[0]); }
461 enum report_level { NONE, INFO, WARN, FAULT };
462 enum limit_switch { POSITIVE, NEGATIVE, INACTIVE };
467 const std::bitset<16> & actual;
468 const std::bitset<16> & stored;
471 bool reportBitToggle(
report_storage report,
int level, std::size_t pos,
const char * msgSet,
const char * msgReset =
nullptr);
473 virtual void interpretModesOfOperation(std::int8_t modesOfOperation) = 0;
474 virtual void interpretIpStatus(std::uint16_t ipStatus) {}
475 virtual void onPositionLimitTriggered() = 0;
476 virtual void reset() = 0;
505 std::unique_ptr<StateObserver> controlModeObserverPtr {
new StateObserver(1.0)};
506 std::unique_ptr<EncoderRead> lastEncoderRead {
nullptr};
513 std::bitset<16> der2;
516 std::int8_t modesOfOperation {0};
520 bool configuredOnce {
false};
524 std::atomic<std::int16_t> lastCurrentRead {0};
526 std::atomic<yarp::conf::vocab32_t> actualControlMode {0};
527 std::atomic<yarp::conf::vocab32_t> requestedcontrolMode {0};
529 std::atomic<double> tr {0.0};
530 std::atomic<double> k {0.0};
531 std::atomic<int> encoderPulses {0};
533 std::atomic<double> maxVel {0.0};
534 std::atomic<double> min {0.0};
535 std::atomic<double> max {0.0};
536 std::atomic<double> refSpeed {0.0};
537 std::atomic<double> refAcceleration {0.0};
539 std::atomic<double> lastHeartbeat {0.0};
540 std::atomic<std::uint8_t> lastNmtState {0};
541 std::atomic<std::uint16_t> lastFaultCode {0};
542 std::atomic<const char *> lastFaultMessage;
544 std::atomic<limit_switch> limitSwitchState {INACTIVE};
548 yarp::conf::vocab32_t initialControlMode {0};
550 double drivePeakCurrent {0.0};
551 double samplingPeriod {0.0};
553 std::string axisName;
554 yarp::conf::vocab32_t jointType {0};
556 bool reverse {
false};
558 PdoConfiguration tpdo1Conf;
559 PdoConfiguration tpdo2Conf;
560 PdoConfiguration tpdo3Conf;
562 double heartbeatPeriod {0.0};
563 double syncPeriod {0.0};
565 unsigned int canId {0};
572 void interpretMsr(std::uint16_t msr);
573 void interpretMer(std::uint16_t mer);
574 void interpretDer(std::uint16_t der);
575 void interpretDer2(std::uint16_t der2);
576 void interpretCer(std::uint16_t cer);
577 void interpretStatusword(std::uint16_t statusword);
579 void handleTpdo1(std::uint16_t statusword, std::uint16_t msr, std::int8_t modesOfOperation);
580 void handleTpdo2(std::uint16_t mer, std::uint16_t der);
581 void handleTpdo3(std::int32_t position, std::int16_t current);
585 bool monitorWorker(
const yarp::os::YarpTimerEvent & event);
587 bool setLimitRaw(
double limit,
bool isMin);
588 bool getLimitRaw(
double * limit,
bool isMin);
590 yarp::dev::PolyDriver externalEncoderDevice;
591 yarp::dev::IEncodersTimedRaw * iEncodersTimedRawExternal;
594 yarp::os::Timer * monitorThread;
Wrapper for CAN protocol handles with handy accessors.
Definition: CanOpenNode.hpp:35
A buffer for periodic commands implementing linear interpolation.
Definition: CommandBuffer.hpp:16
std::pair< std::uint16_t, std::string > code_t
Emergency error code.
Definition: EmcyConsumer.hpp:35
Abstract base for a CAN bus sharer.
Definition: ICanBusSharer.hpp:25
Implementation-agnostic consumer for TX CAN transfers.
Definition: ICanSenderDelegate.hpp:22
Data-free state observer.
Definition: StateObserver.hpp:75
Base class for all proxied TechnosoftIpos implementations.
Definition: TechnosoftIposBase.hpp:72
unsigned int getId() override
Retrieve CAN node ID.
Definition: ICanBusSharerImpl.cpp:39
bool validateInitialState()
Make sure stored variables actually make sense.
Definition: TechnosoftIposBase.cpp:505
double internalUnitsToDegrees(double value, int derivativeOrder=0) const
Convert position, speed or acceleration to degrees.
Definition: TechnosoftIposBase.cpp:656
std::int16_t currentToInternalUnits(double value) const
Convert current to internal units.
Definition: TechnosoftIposBase.cpp:663
bool initialize() override
Perform CAN node initialization.
Definition: ICanBusSharerImpl.cpp:67
std::vector< unsigned int > getAdditionalIds() override
Retrieve more associated CAN node IDs, if any.
Definition: ICanBusSharerImpl.cpp:46
double currentToTorque(double current) const
Convert current (amperes) to torque (Nm).
Definition: TechnosoftIposBase.cpp:684
double internalUnitsToPeakCurrent(std::int16_t value) const
Apply internal iPOS conversion to express drive peak current in amperes.
Definition: TechnosoftIposBase.cpp:677
bool notifyMessage(const can_message &message) override
Notify observers that a new CAN message has arrived.
Definition: ICanBusSharerImpl.cpp:176
bool registerSender(ICanSenderDelegate *sender) override
Pass a handle to a CAN sender delegate instance.
Definition: ICanBusSharerImpl.cpp:58
double internalUnitsToCurrent(std::int16_t value) const
Convert current to amperes.
Definition: TechnosoftIposBase.cpp:670
bool finalize() override
Finalize CAN node communications.
Definition: ICanBusSharerImpl.cpp:134
double torqueToCurrent(double torque) const
Convert torque (Nm) to current (amperes).
Definition: TechnosoftIposBase.cpp:691
double degreesToInternalUnits(double value, int derivativeOrder=0) const
Convert position, speed or acceleration to internal units.
Definition: TechnosoftIposBase.cpp:649
bool awaitControlMode(yarp::conf::vocab32_t mode)
Wait with timeout for requested control mode change.
Definition: TechnosoftIposBase.cpp:642
NmtState
NMT state machine.
Definition: NmtProtocol.hpp:33
DriveState
CiA 402 drive states (associated to statusword).
Definition: DriveStatusMachine.hpp:22
@ NOT_READY_TO_SWITCH_ON
Not ready to switch on state.
The main, catch-all namespace for Robotics Lab UC3M.
Definition: groups.dox:6
Definition: TechnosoftIposBase.hpp:465
Proxy CAN message structure.
Definition: CanMessage.hpp:20