ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

To assign float numbers to double arrays

asked 2020-04-14 05:34:04 -0600

azerila gravatar image

updated 2020-04-14 08:29:48 -0600

In the hardware interface of ros_control, I have

double joint_position_[7]

and in the read() function I want to assign values to it which I have got from a ros service call that is defined by type "sensor_msgs/JointState", i.e.,

$ rosmsg show sensor_msgs/JointState 
std_msgs/Header header
  uint32 seq
  time stamp
  string frame_id
string[] name
float64[] position
float64[] velocity
float64[] effort

What I tried was:

void ROBOTHardwareInterface::read() {
    bool success = client.call(j_state_srv);
    joint_position_ = j_state_srv.response.j_state.position
   ... 
   }

which by no surprise gives this error:

error: incompatible types in assignment of ‘sensor_msgs::JointState_<std::allocator<void> >::_position_type {aka std::vector<double>}’ to ‘double [7]’
  joint_position_ = j_state_srv.response.j_state.position

Is there a good workaround for this?

edit retag flag offensive close merge delete

Comments

As @bob-ROS mentioned, is there a reason you need to use a double[7]? If you instead used another std::vector<double> this would be a trivial operation.

jarvisschultz gravatar image jarvisschultz  ( 2020-04-14 10:12:44 -0600 )edit

@jarvisschultz the problem is I am rather new to c++ and don't know what options I have. On the other hand , I'm also new to ros_control to know what's possible. Can you please also note how you would write it? especially how I should declare it in the header file.

azerila gravatar image azerila  ( 2020-04-14 15:00:25 -0600 )edit

Maybe something like this

#include <vector>
std::vector<double> joint_position_;
// then you should be able to do something like this:
joint_position_ = j_state_srv.response.j_state.position
jarvisschultz gravatar image jarvisschultz  ( 2020-04-14 16:14:35 -0600 )edit

@jarvisschultz It gave another error. In some other lines I also use them like the below. I'm not sure if ros_control is compatible with it.

for (int i = 0; i < num_joints_; ++i) {
     hardware_interface::JointStateHandle    jointStateHandle (
                                                            joint_names_    [i],
                                            &joint_position_ [i],
                                &joint_velocity_ [i],
                                    &joint_effort_     [i]);
        // Create position joint interface
        hardware_interface::JointHandle    jointPositionHandle (jointStateHandle, &joint_position_command_[i]);
        hardware_interface::JointHandle    jointVelocityHandle (jointStateHandle, &joint_velocity_command_[i]);
        hardware_interface::JointHandle    jointEffortHandle   (jointStateHandle, &joint_effort_command_  [i]);}
azerila gravatar image azerila  ( 2020-04-14 17:03:45 -0600 )edit

1 Answer

Sort by » oldest newest most voted
0

answered 2020-04-14 08:35:04 -0600

bob-ROS gravatar image

updated 2020-04-14 09:01:41 -0600

I would not recommend using a double array over a vector but if you wanna convert it I would try.

std::copy(j_state_srv.response.j_state.position.begin(), j_state_srv.response.j_state.position.end(), joint_position_);

You should make sure that the size of your double array is the same as the position vector.

edit flag offensive delete link more

Comments

I am rather new to c++ and am also not sure what works with ros_control. Can you please mention an alternative better way to do it?

azerila gravatar image azerila  ( 2020-04-14 15:01:04 -0600 )edit

Question Tools

2 followers

Stats

Asked: 2020-04-14 05:34:04 -0600

Seen: 830 times

Last updated: Apr 14 '20