29void ControlBoard_nws_yarp::closePorts()
35 inputStreamingPort.interrupt();
36 inputStreamingPort.close();
39 outputPositionStatePort.
close();
42 extendedOutputStatePort.
close();
58bool ControlBoard_nws_yarp::checkPortName(
Searchable& params)
61 if (!params.
check(
"name")) {
63 "*************************************************************************************\n"
64 "* controlBoard_nws_yarp missing mandatory parameter 'name' for port name, usage is: *\n"
65 "* name: full port prefix name with leading '/', e.g. /robotName/part/ *\n"
66 "*************************************************************************************";
71 if (partName[0] !=
'/') {
73 "*************************************************************************************\n"
74 "* controlBoard_nws_yarp 'name' parameter for port name does not follow convention, *\n"
75 "* it MUST start with a leading '/' since it is used as the full prefix port name *\n"
76 "* name: full port prefix name with leading '/', e.g. /robotName/part/ *\n"
77 "* A temporary automatic fix will be done for you, but please fix your config file *\n"
78 "*************************************************************************************";
79 rootName =
"/" + partName;
93 if (!checkPortName(config)) {
94 yCError(
CONTROLBOARD) <<
"'portName' was not correctly set, check you r configuration file";
99 if (prop.
check(
"period")) {
110 yCDebug(
CONTROLBOARD) <<
"'period' parameter missing, using default thread period = 0.02s";
111 period = default_period;
116 if (prop.
check(
"subdevice")) {
117 prop.setMonitor(config.getMonitor());
118 if (!openAndAttachSubDevice(prop)) {
122 subdevice_ready =
true;
125 rootName = prop.
check(
"rootName",
Value(
"/"),
"starting '/' if needed.").asString();
126 partName = prop.
check(
"name",
Value(
"controlboard"),
"prefix for port names").asString();
127 rootName += (partName);
128 if (rootName.find(
"//") != std::string::npos) {
129 rootName.replace(rootName.find(
"//"), 2,
"/");
133 if (!inputRPCPort.
open((rootName +
"/rpc:i"))) {
139 inputRPC_buffer.
attach(inputRPCPort);
140 RPC_parser.
attach(inputRPC_buffer);
142 if (!inputStreamingPort.open(rootName +
"/command:i")) {
147 inputStreamingPort.setStrict();
148 inputStreamingPort.useCallback(streaming_parser);
150 if (!outputPositionStatePort.
open(rootName +
"/state:o")) {
157 if (!extendedOutputStatePort.
open(rootName +
"/stateExt:o")) {
162 extendedOutputState_buffer.
attach(extendedOutputStatePort);
166 if (subdevice_ready) {
180bool ControlBoard_nws_yarp::openAndAttachSubDevice(
Property& prop)
186 std::string subdevice = prop.
find(
"subdevice").
asString();
187 p.setMonitor(prop.getMonitor(), subdevice.c_str());
189 p.
put(
"device", subdevice);
193 subDeviceOwned->open(p);
195 if (!subDeviceOwned->isValid()) {
200 return setDevice(subDeviceOwned,
true);
207 subdevice_ptr = driver;
208 subdevice_owned = owned;
211 subdevice_ptr->
view(iJointFault);
213 yCWarning(
CONTROLBOARD,
"Part <%s>: iJointFault interface was not found in subdevice.", partName.c_str());
217 subdevice_ptr->
view(iPidControl);
219 yCWarning(
CONTROLBOARD,
"Part <%s>: IPidControl interface was not found in subdevice.", partName.c_str());
223 subdevice_ptr->
view(iPositionControl);
224 if (!iPositionControl) {
225 yCError(
CONTROLBOARD,
"Part <%s>: IPositionControl interface was not found in subdevice. Quitting", partName.c_str());
230 subdevice_ptr->
view(iPositionDirect);
231 if (!iPositionDirect) {
232 yCWarning(
CONTROLBOARD,
"Part <%s>: IPositionDirect interface was not found in subdevice.", partName.c_str());
236 subdevice_ptr->
view(iVelocityControl);
237 if (!iVelocityControl) {
238 yCError(
CONTROLBOARD,
"Part <%s>: IVelocityControl interface was not found in subdevice. Quitting", partName.c_str());
243 subdevice_ptr->
view(iEncodersTimed);
244 if (!iEncodersTimed) {
245 yCError(
CONTROLBOARD,
"Part <%s>: IEncodersTimed interface was not found in subdevice. Quitting", partName.c_str());
250 subdevice_ptr->
view(iMotor);
252 yCWarning(
CONTROLBOARD,
"Part <%s>: IMotor interface was not found in subdevice.", partName.c_str());
256 subdevice_ptr->
view(iMotorEncoders);
257 if (!iMotorEncoders) {
258 yCWarning(
CONTROLBOARD,
"Part <%s>: IMotorEncoders interface was not found in subdevice.", partName.c_str());
262 subdevice_ptr->
view(iAmplifierControl);
263 if (!iAmplifierControl) {
264 yCWarning(
CONTROLBOARD,
"Part <%s>: IAmplifierControl interface was not found in subdevice.", partName.c_str());
268 subdevice_ptr->
view(iControlLimits);
269 if (!iControlLimits) {
270 yCWarning(
CONTROLBOARD,
"Part <%s>: IControlLimits interface was not found in subdevice.", partName.c_str());
274 subdevice_ptr->
view(iControlCalibration);
275 if (!iControlCalibration) {
276 yCWarning(
CONTROLBOARD,
"Part <%s>: IControlCalibration interface was not found in subdevice.", partName.c_str());
280 subdevice_ptr->
view(iTorqueControl);
281 if (!iTorqueControl) {
282 yCWarning(
CONTROLBOARD,
"Part <%s>: ITorqueControl interface was not found in subdevice.", partName.c_str());
286 subdevice_ptr->
view(iImpedanceControl);
287 if (!iImpedanceControl) {
288 yCWarning(
CONTROLBOARD,
"Part <%s>: IImpedanceControl interface was not found in subdevice.", partName.c_str());
292 subdevice_ptr->
view(iControlMode);
294 yCWarning(
CONTROLBOARD,
"Part <%s>: IControlMode interface was not found in subdevice.", partName.c_str());
298 subdevice_ptr->
view(iAxisInfo);
300 yCWarning(
CONTROLBOARD,
"Part <%s>: IAxisInfo interface was not found in subdevice.", partName.c_str());
304 subdevice_ptr->
view(iPreciselyTimed);
305 if (!iPreciselyTimed) {
306 yCWarning(
CONTROLBOARD,
"Part <%s>: IPreciselyTimed interface was not found in subdevice.", partName.c_str());
310 subdevice_ptr->
view(iInteractionMode);
311 if (!iInteractionMode) {
312 yCWarning(
CONTROLBOARD,
"Part <%s>: IInteractionMode interface was not found in subdevice.", partName.c_str());
316 subdevice_ptr->
view(iRemoteVariables);
317 if (!iRemoteVariables) {
318 yCWarning(
CONTROLBOARD,
"Part <%s>: IRemoteVariables interface was not found in subdevice.", partName.c_str());
322 subdevice_ptr->
view(iPWMControl);
324 yCWarning(
CONTROLBOARD,
"Part <%s>: IPWMControl interface was not found in subdevice.", partName.c_str());
328 subdevice_ptr->
view(iCurrentControl);
329 if (!iCurrentControl) {
330 yCWarning(
CONTROLBOARD,
"Part <%s>: ICurrentControl interface was not found in subdevice.", partName.c_str());
336 if (!iPositionControl->
getAxes(&tmp_axes)) {
337 yCError(
CONTROLBOARD) <<
"Part <%s>: Failed to get axes number for subdevice " << partName.c_str();
341 yCError(
CONTROLBOARD,
"Part <%s>: attached device has an invalid number of joints (%d)", partName.c_str(), tmp_axes);
344 subdevice_joints =
static_cast<size_t>(tmp_axes);
345 times.
resize(subdevice_joints);
348 streaming_parser.
init(subdevice_ptr);
351 RPC_parser.
init(subdevice_ptr);
357void ControlBoard_nws_yarp::closeDevice()
360 streaming_parser.
reset();
364 if (subdevice_owned) {
365 subdevice_ptr->
close();
366 delete subdevice_ptr;
368 subdevice_ptr =
nullptr;
369 subdevice_owned =
false;
370 subdevice_joints = 0;
371 subdevice_ready =
false;
376 iPidControl =
nullptr;
377 iPositionControl =
nullptr;
378 iPositionDirect =
nullptr;
379 iVelocityControl =
nullptr;
380 iEncodersTimed =
nullptr;
382 iMotorEncoders =
nullptr;
383 iAmplifierControl =
nullptr;
384 iControlLimits =
nullptr;
385 iControlCalibration =
nullptr;
386 iTorqueControl =
nullptr;
387 iImpedanceControl =
nullptr;
388 iControlMode =
nullptr;
390 iPreciselyTimed =
nullptr;
391 iInteractionMode =
nullptr;
392 iRemoteVariables =
nullptr;
393 iPWMControl =
nullptr;
394 iCurrentControl =
nullptr;
395 iJointFault =
nullptr;
401 if (subdevice_ready) {
405 if (!setDevice(poly,
false)) {
421 if (subdevice_owned) {
438 constexpr int reads_for_warning = 20;
439 if (inputStreamingPort.getPendingReads() >= reads_for_warning) {
440 yCWarning(
CONTROLBOARD) <<
"Number of streaming input messages to be read is " << inputStreamingPort.getPendingReads() <<
" and can overflow";
459 if (iEncodersTimed) {
469 if (iMotorEncoders) {
479 if (iTorqueControl) {
491 if (iCurrentControl) {
493 }
else if (iAmplifierControl) {
505 if (iInteractionMode) {
512 for (
double tt : times)
514 if (std::abs(times[0] - tt) > 1.0)
516 yCError(
CONTROLBOARD,
"Encoder Timestamps are not consistent! Data will not be published.");
523 time.
update(std::accumulate(times.begin(), times.end(), 0.0) / subdevice_joints);
527 extendedOutputState_buffer.
write();
531 v.
resize(subdevice_joints);
535 outputPositionStatePort.
write();
const yarp::os::LogComponent & CONTROLBOARD()
constexpr double default_period
void run() override
Loop function.
bool open(yarp::os::Searchable &prop) override
Open the DeviceDriver.
bool close() override
Close the DeviceDriver.
bool attach(yarp::dev::PolyDriver *poly) override
Attach to another object.
bool detach() override
Detach the object (you must have first called attach).
void init(yarp::dev::DeviceDriver *x)
Initialization.
virtual bool initialize()
Initialize the internal data.
void init(yarp::dev::DeviceDriver *x)
Initialization.
Interface implemented by all device drivers.
bool view(T *&x)
Get an interface to the device driver.
bool close() override
Close the DeviceDriver.
void attach(yarp::os::TypedReader< yarp::os::Bottle > &source)
Attach this object to a source of messages.
virtual bool getCurrents(double *vals)=0
virtual bool getControlModes(int *modes)=0
Get the current control mode (multiple joints).
virtual bool getCurrents(double *currs)=0
Get the instantaneous current measurement for all motors.
virtual bool getEncodersTimed(double *encs, double *time)=0
Read the instantaneous acceleration of all axes.
virtual bool getEncoderAccelerations(double *accs)=0
Read the instantaneous acceleration of all axes.
virtual bool getEncoderSpeeds(double *spds)=0
Read the instantaneous speed of all axes.
virtual bool getInteractionModes(int n_joints, int *joints, yarp::dev::InteractionModeEnum *modes)=0
Get the current interaction mode of the robot for a set of joints, values can be stiff or compliant.
virtual bool getMotorEncoderSpeeds(double *spds)=0
Read the instantaneous speed of all motor encoders.
virtual bool getMotorEncoderAccelerations(double *accs)=0
Read the instantaneous acceleration of all motor encoders.
virtual bool getMotorEncoders(double *encs)=0
Read the position of all motor encoders.
virtual bool getDutyCycles(double *vals)=0
Gets the current dutycycle of the output of the amplifier (i.e.
virtual bool getAxes(int *ax)=0
Get the number of controlled axes.
virtual bool getTorques(double *t)=0
Get the value of the torque for all joints (this is the feedback if you have torque sensors).
A container for a device driver.
yarp::sig::VectorOf< double > motorVelocity
yarp::sig::VectorOf< double > jointAcceleration
yarp::sig::VectorOf< int > controlMode
bool pwmDutycycle_isValid
yarp::sig::VectorOf< int > interactionMode
yarp::sig::VectorOf< double > current
bool jointVelocity_isValid
yarp::sig::VectorOf< double > motorAcceleration
bool motorVelocity_isValid
yarp::sig::VectorOf< double > motorPosition
yarp::sig::VectorOf< double > pwmDutycycle
bool jointAcceleration_isValid
yarp::sig::VectorOf< double > torque
bool motorAcceleration_isValid
bool interactionMode_isValid
yarp::sig::VectorOf< double > jointPosition
bool motorPosition_isValid
bool jointPosition_isValid
yarp::sig::VectorOf< double > jointVelocity
void close() override
Stop port activity.
bool setEnvelope(PortWriter &envelope) override
Set an envelope (e.g., a timestamp) to the next message which will be sent.
bool open(const std::string &name) override
Start port operation, with a specific name, with automatically-chosen network parameters.
void interrupt() override
Interrupt any current reads or writes attached to the port.
void write(bool forceStrict=false)
Write the current object being returned by BufferedPort::prepare.
T & prepare()
Access the object which will be transmitted by the next call to yarp::os::BufferedPort::write.
An abstraction for a periodic thread.
bool setPeriod(double period)
Set the (new) period of the thread.
bool isRunning() const
Returns true when the thread is started, false otherwise.
bool start()
Call this to start the thread.
void stop()
Call this to stop the thread, this call blocks until the thread is terminated (and releaseThread() ca...
void attach(Port &port)
Attach this buffer to a particular port.
T & get()
A synonym of PortWriterBuffer::prepare.
void write(bool forceStrict=false)
Try to write the last buffer returned by PortWriterBuffer::get.
void attach(Port &port)
Set the Port to which objects will be written.
void setReader(PortReader &reader) override
Set an external reader for port data.
bool removeCallbackLock() override
Remove a lock on callbacks added with setCallbackLock()
void interrupt() override
Interrupt any current reads or writes attached to the port.
bool setEnvelope(PortWriter &envelope) override
Set an envelope (e.g., a timestamp) to the next message which will be sent.
void close() override
Stop port activity.
bool open(const std::string &name) override
Start port operation, with a specific name, with automatically-chosen network parameters.
A class for storing options and configuration information.
Value & find(const std::string &key) const override
Gets a value corresponding to a given keyword.
std::string toString() const override
Return a standard text representation of the content of the object.
void fromString(const std::string &txt, bool wipe=true)
Interprets a string as a list of properties.
void put(const std::string &key, const std::string &value)
Associate the given key with the given string.
bool check(const std::string &key) const override
Check if there exists a property of the given name.
void unput(const std::string &key)
Remove the association from the given key to a value, if present.
A base class for nested structures that can be searched.
virtual bool check(const std::string &key) const =0
Check if there exists a property of the given name.
virtual std::string toString() const =0
Return a standard text representation of the content of the object.
virtual Value & find(const std::string &key) const =0
Gets a value corresponding to a given keyword.
An abstraction for a time stamp and/or sequence number.
void update()
Set the timestamp to the current time, and increment the sequence number (wrapping to 0 if the sequen...
A single value (typically within a Bottle).
virtual yarp::conf::float64_t asFloat64() const
Get 64-bit floating point value.
virtual bool isFloat64() const
Checks if value is a 64-bit floating point number.
virtual std::string asString() const
Get string value.
void resize(size_t size) override
Resize the vector.
iterator begin() noexcept
Returns an iterator to the beginning of the VectorOf.
T * data()
Return a pointer to the first element of the vector.
iterator end() noexcept
Returns an iterator to the end of the VectorOf.
#define yCError(component,...)
#define yCWarning(component,...)
#define yCDebug(component,...)
For streams capable of holding different kinds of content, check what they actually have.
An interface to the operating system, including Port based communication.
The main, catch-all namespace for YARP.