6#ifndef _USE_MATH_DEFINES
7#define _USE_MATH_DEFINES
21#include <rcutils/logging_macros.h>
23using namespace std::chrono_literals;
82bool ControlBoard_nws_ros2::messageVectorsCheck(
const std::string &
valueName,
const std::vector<std::string> &names,
const std::vector<double> &
ref_values,
const std::vector<double> &
derivative){
83 if(!namesCheck(names)) {
87 bool allJoints = names.size() == 0 || names.size() == m_subdevice_joints;
97 RCLCPP_ERROR(m_node->get_logger(),
"The %s vector and the names one are not the same size",
valueName.c_str());
105 RCLCPP_ERROR(m_node->get_logger(),
"The %s vector and the secondary one are not the same size",
valueName.c_str());
113 RCLCPP_ERROR(m_node->get_logger(),
"All joints where selected bt the vector sizes do not match");
122bool ControlBoard_nws_ros2::messageVectorsCheck(
const std::string &
valueName,
const std::vector<std::string> &names,
const std::vector<double> &
ref_values){
123 if(!namesCheck(names)) {
127 bool allJoints = names.size() == 0 || names.size() == m_subdevice_joints;
144bool ControlBoard_nws_ros2::namesCheck(
const std::vector<std::string> &names){
145 if(names.size() > m_subdevice_joints){
147 RCLCPP_ERROR(m_node->get_logger(),
"The specified joint names vector is longer than expected");
151 for (
const auto& name : names){
152 if(m_quickJointRef.count(name) == 0){
154 RCLCPP_ERROR(m_node->get_logger(),
"%s is not a valid joint name",name.c_str());
164bool ControlBoard_nws_ros2::messageVectorsCheck(
const std::string &
valueName,
const std::vector<std::string> &names,
const std::vector<std::string> &
ref_values){
165 if(!namesCheck(names)) {
169 bool allJoints = names.size() == 0 || names.size() == m_subdevice_joints;
186void ControlBoard_nws_ros2::positionTopic_callback(
const yarp_control_msgs::msg::Position::SharedPtr msg) {
188 std::lock_guard <std::mutex>
lg(m_cmdMutex);
190 bool noJoints = msg->names.size() == 0;
191 bool noSpeed = msg->ref_velocities.size() == 0;
199 if(!messageVectorsCheck(
"Position",msg->names,msg->positions,msg->ref_velocities)){
211 for(
size_t i=0;
i<(
noJoints ? m_subdevice_joints : msg->positions.size());
i++){
212 size_t index =
noJoints ?
i : m_quickJointRef[msg->names[
i]];
248void ControlBoard_nws_ros2::positionDirectTopic_callback(
const yarp_control_msgs::msg::PositionDirect::SharedPtr msg) {
249 std::lock_guard <std::mutex>
lg(m_cmdMutex);
251 bool noJoints = msg->names.size() == 0;
260 if(!messageVectorsCheck(
"Position",msg->names,msg->positions)){
270 for(
size_t i=0;
i<(
noJoints ? m_subdevice_joints : msg->positions.size());
i++){
271 size_t index =
noJoints ?
i : m_quickJointRef[msg->names[
i]];
292void ControlBoard_nws_ros2::velocityTopic_callback(
const yarp_control_msgs::msg::Velocity::SharedPtr msg) {
294 std::lock_guard <std::mutex>
lg(m_cmdMutex);
296 bool noJoints = msg->names.size() == 0;
297 bool noAccel = msg->ref_accelerations.size() == 0;
305 if(!messageVectorsCheck(
"Velocities",msg->names,msg->velocities,msg->ref_accelerations)){
317 for(
size_t i=0;
i<(
noJoints ? m_subdevice_joints : msg->velocities.size());
i++){
318 size_t index =
noJoints ?
i : m_quickJointRef[msg->names[
i]];
353void ControlBoard_nws_ros2::getJointsNamesCallback(
const std::shared_ptr<rmw_request_id_t>
request_header,
354 const std::shared_ptr<yarp_control_msgs::srv::GetJointsNames::Request> request,
355 std::shared_ptr<yarp_control_msgs::srv::GetJointsNames::Response> response){
356 std::lock_guard <std::mutex>
lg(m_cmdMutex);
361 response->response =
"INVALID";
366 bool noIndexes = request->joint_indexes.size() == 0;
368 if(!
noIndexes && request->joint_indexes.size() > m_subdevice_joints){
369 yCError(
CONTROLBOARD_ROS2) <<
"request->joint_indexes vector cannot be longer than the actual number of joints:"<< request->joint_indexes.size() <<
"instead of" << m_subdevice_joints;
370 RCLCPP_ERROR(m_node->get_logger(),
"request->joint_indexes vector cannot be longer than the actual number of joints: %ld instead of %ld", request->joint_indexes.size(), m_subdevice_joints);
372 response->response =
"SIZE_ERROR";
379 for(
size_t i=0;
i<m_subdevice_joints;
i++){
382 RCLCPP_ERROR(m_node->get_logger(),
"Name retrieval failed for joint number %ld",
i);
384 response->response =
"GET_NAME_ERROR";
388 response->names.push_back(
tempName);
395 RCLCPP_ERROR(m_node->get_logger(),
"Name retrieval failed for joint number %d",
i);
397 response->response =
"GET_NAME_ERROR";
401 response->names.push_back(
tempName);
405 response->response =
"OK";
409void ControlBoard_nws_ros2::getAvailableModesCallback(
const std::shared_ptr<rmw_request_id_t>
request_header,
410 const std::shared_ptr<yarp_control_msgs::srv::GetAvailableControlModes::Request> request,
411 std::shared_ptr<yarp_control_msgs::srv::GetAvailableControlModes::Response> response){
412 std::lock_guard <std::mutex>
lg(m_cmdMutex);
417 response->response =
"INVALID";
422 if(request->only_implemented){
427 response->modes.push_back(x.first);
431 response->response =
"OK";
435void ControlBoard_nws_ros2::getControlModesCallback(
const std::shared_ptr<rmw_request_id_t>
request_header,
436 const std::shared_ptr<yarp_control_msgs::srv::GetControlModes::Request> request,
437 std::shared_ptr<yarp_control_msgs::srv::GetControlModes::Response> response){
438 std::lock_guard <std::mutex>
lg(m_cmdMutex);
440 bool noJoints = request->names.size() == 0;
446 response->response =
"INVALID";
452 if(!namesCheck(request->names)){
453 response->response =
"NAMES_ERROR";
467 RCLCPP_ERROR(m_node->get_logger(),
"Error while retrieving the control mode for joint %s",request->names[
i].c_str());
468 response->response =
"RETRIEVE_ERROR";
476 response->response =
"OK";
482void ControlBoard_nws_ros2::getPositionCallback(
const std::shared_ptr<rmw_request_id_t>
request_header,
483 const std::shared_ptr<yarp_control_msgs::srv::GetPosition::Request> request,
484 std::shared_ptr<yarp_control_msgs::srv::GetPosition::Response> response) {
485 std::lock_guard <std::mutex>
lg(m_cmdMutex);
487 bool noJoints = request->names.size() == 0;
493 response->response =
"INVALID";
499 if(!namesCheck(request->names)){
500 response->response =
"NAMES_ERROR";
507 double *
tempPos =
new double[m_jointNames.size()];
512 RCLCPP_ERROR(m_node->get_logger(),
"Error while retrieving joints positions");
513 response->response =
"RETRIEVE_ERROR";
522 size_t index =
noJoints ?
i : m_quickJointRef[request->names[
i]];
535 response->response =
"OK";
541void ControlBoard_nws_ros2::getVelocityCallback(
const std::shared_ptr<rmw_request_id_t>
request_header,
542 const std::shared_ptr<yarp_control_msgs::srv::GetVelocity::Request> request,
543 std::shared_ptr<yarp_control_msgs::srv::GetVelocity::Response> response) {
544 std::lock_guard <std::mutex>
lg(m_cmdMutex);
546 bool noJoints = request->names.size() == 0;
552 response->response =
"INVALID";
558 if(!namesCheck(request->names)){
559 response->response =
"NAMES_ERROR";
566 double *
tempVel =
new double[m_jointNames.size()];
571 RCLCPP_ERROR(m_node->get_logger(),
"Error while retrieving joints speeds");
572 response->response =
"RETRIEVE_ERROR";
581 size_t index =
noJoints ?
i : m_quickJointRef[request->names[
i]];
594 response->response =
"OK";
600void ControlBoard_nws_ros2::setControlModesCallback(
const std::shared_ptr<rmw_request_id_t>
request_header,
601 const std::shared_ptr<yarp_control_msgs::srv::SetControlModes::Request> request,
602 std::shared_ptr<yarp_control_msgs::srv::SetControlModes::Response> response){
603 std::lock_guard <std::mutex>
lg(m_cmdMutex);
605 bool noJoints = request->names.size() == 0;
611 response->response =
"INVALID";
616 if(!messageVectorsCheck(
"Control mode",request->names,request->modes)){
618 response->response =
"INVALID";
628 RCLCPP_ERROR(m_node->get_logger(),
"Cannot set mode to %s",request->modes[
i].c_str());
634 RCLCPP_ERROR(m_node->get_logger(),
"Error while setting the control mode for joint %s to %s",request->names[
i].c_str(),request->modes[
i].c_str());
635 response->response =
"SET_ERROR";
641 response->response =
"OK";
constexpr yarp::conf::vocab32_t VOCAB_CM_MIXED
constexpr yarp::conf::vocab32_t VOCAB_CM_TORQUE
constexpr yarp::conf::vocab32_t VOCAB_CM_PWM
constexpr yarp::conf::vocab32_t VOCAB_CM_HW_FAULT
constexpr yarp::conf::vocab32_t VOCAB_CM_IMPEDANCE_VEL
constexpr yarp::conf::vocab32_t VOCAB_CM_IDLE
constexpr yarp::conf::vocab32_t VOCAB_CM_CALIB_DONE
constexpr yarp::conf::vocab32_t VOCAB_CM_NOT_CONFIGURED
constexpr yarp::conf::vocab32_t VOCAB_CM_POSITION
constexpr yarp::conf::vocab32_t VOCAB_CM_CONFIGURED
constexpr yarp::conf::vocab32_t VOCAB_CM_CURRENT
constexpr yarp::conf::vocab32_t VOCAB_CM_IMPEDANCE_POS
constexpr yarp::conf::vocab32_t VOCAB_CM_POSITION_DIRECT
constexpr yarp::conf::vocab32_t VOCAB_CM_VELOCITY
constexpr yarp::conf::vocab32_t VOCAB_CM_CALIBRATING
double convertDegreesToRadians(double degrees)
virtual bool getAxisName(int axis, std::string &name)=0
virtual bool getJointType(int axis, yarp::dev::JointTypeEnum &type)
virtual bool setControlMode(const int j, const int mode)=0
Set the current control mode.
virtual bool getControlMode(int j, int *mode)=0
Get the current control mode.
virtual bool getEncoders(double *encs)=0
Read the position of all axes.
virtual bool getEncoderSpeeds(double *spds)=0
Read the instantaneous speed of all axes.
virtual bool setRefSpeeds(const double *spds)=0
Set reference speed on all joints.
virtual bool positionMove(int j, double ref)=0
Set new reference point for a single axis.
virtual bool setPositions(const int n_joint, const int *joints, const double *refs)=0
Set new reference point for all axes.
virtual bool setRefAccelerations(const double *accs)=0
Set reference acceleration on all joints.
virtual bool velocityMove(int j, double sp)=0
Start motion at a given speed, single joint.
A mini-server for performing network communication in the background.
#define yCError(component,...)
#define YARP_LOG_COMPONENT(name,...)
For streams capable of holding different kinds of content, check what they actually have.
@ VOCAB_JOINTTYPE_REVOLUTE
An interface to the operating system, including Port based communication.