Ask Your Question

Subscribing to message and re-publishing only specific part, real time?

asked 2018-04-16 03:47:44 -0600

nyxaria gravatar image

Hi everyone,

I am currently working on sending the information from /joint_states to an Arduino, however the information in my message is too large to be sent, even after increasing the buffer.

My plan is to make a program that subscribes to joint_states, scrapes the position array and re-publishes on a different topic. I am new to ROS so I have no idea where to start.

Could someone give me a helping hand? Thank you so much in advance :).

edit retag flag offensive close merge delete

3 Answers

Sort by ยป oldest newest most voted

answered 2018-04-17 06:30:51 -0600

mohsen gravatar image

updated 2018-04-17 06:44:35 -0600

It might seem a bit redundant, but that's how I would do it.
(I'm assuming you want to keep everything except the position array)
1- Define a new message. I'm defining it in package myPkg and calling it newMsg.

Header header  
string[] name
float64[] velocity
float64[] effort

2- Write a node that on receiving a message (callback), publishes the data you want.

import rospy  
from myPkg.msg import newMsg  
from sensor_msgs.msg import JointState  

def callback(msg):  
       out.header = msg.header  
       out.velocity = msg.velocity  
       out.effort = msg.effort  

sub = rospy.Subscriber('in', JointState, callback)  
pub = rospy.Publisher('out', newMsg)  

Note: If concerned about performance, implement this in the node you're subscribing to.

edit flag offensive delete link more


+1 for the example. That is actually exactly what the transform node does / allows you, but without writing an actual node.

gvdhoorn gravatar imagegvdhoorn ( 2018-04-17 07:03:57 -0600 )edit

Thank you, I ended up using your solution as the basis for mine :). Have a great day!

nyxaria gravatar imagenyxaria ( 2018-04-19 09:18:46 -0600 )edit

answered 2018-04-16 04:02:59 -0600

gvdhoorn gravatar image

This is something you can use the transform node for in topic_tools.

See #q261935 for a recent example.

edit flag offensive delete link more


Hi, thank you for your response :). I have tried to do it using this way but only realised what I did wrong once I completed a working solution using @mohsen 's answer. (lack of understand how to construct MultiArrays :p.) Have a great day!

nyxaria gravatar imagenyxaria ( 2018-04-19 09:17:49 -0600 )edit

answered 2018-04-19 09:25:56 -0600

nyxaria gravatar image

I ended up basing my solution around @mohsen 's answer:

Node file:

#!/usr/bin/env python
import rospy
import importlib
import math

from sensor_msgs.msg import JointState  
from inmoov_urdf.msg import JointAngles
from std_msgs.msg import Int16MultiArray
from std_msgs.msg import MultiArrayDimension

global pub;

def main():
    global pub;
    sub = rospy.Subscriber('/joint_states', JointState, callback)

    pub = rospy.Publisher('/joint_angles', JointAngles)  

def callback(msg):  
    global pub;

    out = JointAngles();
    out.angles = normalise(msg.position);

def normalise(pos):
    to_return = []
    for i, val in enumerate(pos):
        if(i == 0 or i == 3 or i == 4 or i == 22 or i == 23): # shift by +pi/2
            to_return += [math.degrees(val+1.5707)];
        elif(i == 1): # shift by +pi/4
            to_return += [math.degrees(val+0.7854)];
        elif(i == 2 or i == 5 or i == 24 or i == 25): # abs
            to_return += [abs(math.degrees(val))];
            to_return += [math.degrees(val)];
    return to_return;   

if __name__ == "__main__":

with JointAngles.msg being:

int16[] angles

Thank you @gvdhoorn and @mohsen for your input :)

edit flag offensive delete link more



This appears to do quite a bit more than just 'republishing'.

gvdhoorn gravatar imagegvdhoorn ( 2018-04-19 09:47:33 -0600 )edit

@gvdhoorn yes you are right ^^. I ended up adding that functionality after I got the re-publishing working, but you are right it is more than just what I asked for :)

nyxaria gravatar imagenyxaria ( 2018-04-19 12:07:09 -0600 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2018-04-16 03:47:44 -0600

Seen: 81 times

Last updated: Apr 19 '18