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

Unpacking ROS messages

asked 2020-12-09 05:20:51 -0500

Tahir M. gravatar image

updated 2020-12-09 06:04:47 -0500

mgruhler gravatar image

Any suggestions to reduce this repetitive code:

    SendLowROS.motorCmd[FR_0].Kp = msg.Kp[0];
    SendLowROS.motorCmd[FR_1].Kp = msg.Kp[1];
    SendLowROS.motorCmd[FR_2].Kp = msg.Kp[2];

    SendLowROS.motorCmd[FL_0].Kp = msg.Kp[0];
    SendLowROS.motorCmd[FL_1].Kp = msg.Kp[1];
    SendLowROS.motorCmd[FL_2].Kp = msg.Kp[2];

    SendLowROS.motorCmd[RR_0].Kp = msg.Kp[0];
    SendLowROS.motorCmd[RR_1].Kp = msg.Kp[1];
    SendLowROS.motorCmd[RR_2].Kp = msg.Kp[2];

    SendLowROS.motorCmd[RL_0].Kp = msg.Kp[0];
    SendLowROS.motorCmd[RL_1].Kp = msg.Kp[1];
    SendLowROS.motorCmd[RL_2].Kp = msg.Kp[2];

I am trying to use strucutred binding/similar unpacking concepts like std::tie and std::tuple but unfortunatley no success so far.

auto [SendLowROS.motorCmd[FR_0].Kp, SendLowROS.motorCmd[FR_1].Kp, SendLowROS.motorCmd[FR_2].Kp, 
        SendLowROS.motorCmd[FL_0].Kp, SendLowROS.motorCmd[FL_1].Kp, SendLowROS.motorCmd[FL_2].Kp, 
        SendLowROS.motorCmd[RR_0].Kp, SendLowROS.motorCmd[RR_1].Kp, SendLowROS.motorCmd[RR_2].Kp, 
        SendLowROS.motorCmd[RL_0].Kp, SendLowROS.motorCmd[RL_1].Kp, SendLowROS.motorCmd[RL_2].Kp] = msg.Kd;

and another approach

std::tie(SendLowROS.motorCmd[FR_0].Kp, SendLowROS.motorCmd[FR_1].Kp, SendLowROS.motorCmd[FR_2].Kp, 
              SendLowROS.motorCmd[FL_0].Kp, SendLowROS.motorCmd[FL_1].Kp, SendLowROS.motorCmd[FL_2].Kp, 
              SendLowROS.motorCmd[RR_0].Kp, SendLowROS.motorCmd[RR_1].Kp, SendLowROS.motorCmd[RR_2].Kp, 
              SendLowROS.motorCmd[RL_0].Kp, SendLowROS.motorCmd[RL_1].Kp, SendLowROS.motorCmd[RL_2].Kp) = std::tuple(msg.Kd);
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2020-12-09 08:57:11 -0500

SalahSoliman gravatar image

Assuming FR_0 = 0, FR_1 = 1, FR_2 = 2, FL_0 = 3, FL_1 = 4, and so on

int j=0;
    for(int i=0; i<4;i++)
    {    
        j = 3*i;    
        SendLowROS.motorCmd[j].Kp = msg.Kp[0];
        SendLowROS.motorCmd[j+1].Kp = msg.Kp[1];
        SendLowROS.motorCmd[j+2].Kp = msg.Kp[2];
    }

Note: for higher optimization you can remove the j and modify the loop's condition.

If you can supply the values for the: FR_0, FR_1, FR_2, FL_0, FL_1, ..., a more reasonable solution can be found.

edit flag offensive delete link more

Question Tools

2 followers

Stats

Asked: 2020-12-09 05:20:51 -0500

Seen: 183 times

Last updated: Dec 09 '20