29 void ControlBoard_nws_yarp::closePorts()
35 inputStreamingPort.interrupt();
36 inputStreamingPort.close();
39 outputPositionStatePort.
close();
42 extendedOutputStatePort.
close();
58 bool 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) {
180 bool 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(iPidControl);
213 yCWarning(
CONTROLBOARD,
"Part <%s>: IPidControl interface was not found in subdevice.", partName.c_str());
217 subdevice_ptr->
view(iPositionControl);
218 if (!iPositionControl) {
219 yCError(
CONTROLBOARD,
"Part <%s>: IPositionControl interface was not found in subdevice. Quitting", partName.c_str());
224 subdevice_ptr->
view(iPositionDirect);
225 if (!iPositionDirect) {
226 yCWarning(
CONTROLBOARD,
"Part <%s>: IPositionDirect interface was not found in subdevice.", partName.c_str());
230 subdevice_ptr->
view(iVelocityControl);
231 if (!iVelocityControl) {
232 yCError(
CONTROLBOARD,
"Part <%s>: IVelocityControl interface was not found in subdevice. Quitting", partName.c_str());
237 subdevice_ptr->
view(iEncodersTimed);
238 if (!iEncodersTimed) {
239 yCError(
CONTROLBOARD,
"Part <%s>: IEncodersTimed interface was not found in subdevice. Quitting", partName.c_str());
244 subdevice_ptr->
view(iMotor);
246 yCWarning(
CONTROLBOARD,
"Part <%s>: IMotor interface was not found in subdevice.", partName.c_str());
250 subdevice_ptr->
view(iMotorEncoders);
251 if (!iMotorEncoders) {
252 yCWarning(
CONTROLBOARD,
"Part <%s>: IMotorEncoders interface was not found in subdevice.", partName.c_str());
256 subdevice_ptr->
view(iAmplifierControl);
257 if (!iAmplifierControl) {
258 yCWarning(
CONTROLBOARD,
"Part <%s>: IAmplifierControl interface was not found in subdevice.", partName.c_str());
262 subdevice_ptr->
view(iControlLimits);
263 if (!iControlLimits) {
264 yCWarning(
CONTROLBOARD,
"Part <%s>: IControlLimits interface was not found in subdevice.", partName.c_str());
268 subdevice_ptr->
view(iControlCalibration);
269 if (!iControlCalibration) {
270 yCWarning(
CONTROLBOARD,
"Part <%s>: IControlCalibration interface was not found in subdevice.", partName.c_str());
274 subdevice_ptr->
view(iTorqueControl);
275 if (!iTorqueControl) {
276 yCWarning(
CONTROLBOARD,
"Part <%s>: ITorqueControl interface was not found in subdevice.", partName.c_str());
280 subdevice_ptr->
view(iImpedanceControl);
281 if (!iImpedanceControl) {
282 yCWarning(
CONTROLBOARD,
"Part <%s>: IImpedanceControl interface was not found in subdevice.", partName.c_str());
286 subdevice_ptr->
view(iControlMode);
288 yCWarning(
CONTROLBOARD,
"Part <%s>: IControlMode interface was not found in subdevice.", partName.c_str());
292 subdevice_ptr->
view(iAxisInfo);
294 yCWarning(
CONTROLBOARD,
"Part <%s>: IAxisInfo interface was not found in subdevice.", partName.c_str());
298 subdevice_ptr->
view(iPreciselyTimed);
299 if (!iPreciselyTimed) {
300 yCWarning(
CONTROLBOARD,
"Part <%s>: IPreciselyTimed interface was not found in subdevice.", partName.c_str());
304 subdevice_ptr->
view(iInteractionMode);
305 if (!iInteractionMode) {
306 yCWarning(
CONTROLBOARD,
"Part <%s>: IInteractionMode interface was not found in subdevice.", partName.c_str());
310 subdevice_ptr->
view(iRemoteVariables);
311 if (!iRemoteVariables) {
312 yCWarning(
CONTROLBOARD,
"Part <%s>: IRemoteVariables interface was not found in subdevice.", partName.c_str());
316 subdevice_ptr->
view(iPWMControl);
318 yCWarning(
CONTROLBOARD,
"Part <%s>: IPWMControl interface was not found in subdevice.", partName.c_str());
322 subdevice_ptr->
view(iCurrentControl);
323 if (!iCurrentControl) {
324 yCWarning(
CONTROLBOARD,
"Part <%s>: ICurrentControl interface was not found in subdevice.", partName.c_str());
330 if (!iPositionControl->
getAxes(&tmp_axes)) {
331 yCError(
CONTROLBOARD) <<
"Part <%s>: Failed to get axes number for subdevice " << partName.c_str();
335 yCError(
CONTROLBOARD,
"Part <%s>: attached device has an invalid number of joints (%d)", partName.c_str(), tmp_axes);
338 subdevice_joints =
static_cast<size_t>(tmp_axes);
339 times.
resize(subdevice_joints);
342 streaming_parser.
init(subdevice_ptr);
345 RPC_parser.
init(subdevice_ptr);
351 void ControlBoard_nws_yarp::closeDevice()
354 streaming_parser.
reset();
358 if (subdevice_owned) {
359 subdevice_ptr->
close();
360 delete subdevice_ptr;
362 subdevice_ptr =
nullptr;
363 subdevice_owned =
false;
364 subdevice_joints = 0;
365 subdevice_ready =
false;
370 iPidControl =
nullptr;
371 iPositionControl =
nullptr;
372 iPositionDirect =
nullptr;
373 iVelocityControl =
nullptr;
374 iEncodersTimed =
nullptr;
376 iMotorEncoders =
nullptr;
377 iAmplifierControl =
nullptr;
378 iControlLimits =
nullptr;
379 iControlCalibration =
nullptr;
380 iTorqueControl =
nullptr;
381 iImpedanceControl =
nullptr;
382 iControlMode =
nullptr;
384 iPreciselyTimed =
nullptr;
385 iInteractionMode =
nullptr;
386 iRemoteVariables =
nullptr;
387 iPWMControl =
nullptr;
388 iCurrentControl =
nullptr;
394 if (subdevice_ready) {
398 if (!setDevice(poly,
false)) {
414 if (subdevice_owned) {
431 constexpr
int reads_for_warning = 20;
432 if (inputStreamingPort.getPendingReads() >= reads_for_warning) {
433 yCWarning(
CONTROLBOARD) <<
"Number of streaming input messages to be read is " << inputStreamingPort.getPendingReads() <<
" and can overflow";
437 time.
update(std::accumulate(times.
begin(), times.
end(), 0.0) / subdevice_joints);
456 if (iEncodersTimed) {
466 if (iMotorEncoders) {
476 if (iTorqueControl) {
488 if (iCurrentControl) {
490 }
else if (iAmplifierControl) {
502 if (iInteractionMode) {
509 extendedOutputState_buffer.
write();
513 v.
resize(subdevice_joints);
517 outputPositionStatePort.
write();
const yarp::os::LogComponent & CONTROLBOARD()
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.
void write(bool forceStrict=false)
Try to write the last buffer returned by PortWriterBuffer::get.
T & get()
A synonym of PortWriterBuffer::prepare.
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 Value & find(const std::string &key) const =0
Gets a value corresponding to a given keyword.
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.
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,...)
An interface for the device drivers.
An interface to the operating system, including Port based communication.
The main, catch-all namespace for YARP.