Ask Your Question
1

Is there a way to extract specific info or lines from a ROS topic?

asked 2021-03-09 15:13:47 -0600

etorobot gravatar image

updated 2021-03-09 21:33:40 -0600

echo /joint_states has the following output (shortened).

I made a node (using the rospy tutorial) to subscribe to /joint_states and I want the.py script to print() just the position list for use outside ROS, not the whole topic.

How can I extract only that one line from the topic?

Any help appreciated!

---
header: 
  seq: 1734
  stamp: 
    secs: 26
    nsecs:  47000000
  frame_id: ''
name: 
  - L_CROTCH_Y
  - L_CROTCH_R
  - L_CROTCH_P
  - L_KNEE_P
  - L_ANKLE_R
  - L_ANKLE_P
  - CHEST_Y
  - LC-AI-linear-joint
  - LC-AO-linear-joint
  - RC-W-linear-joint
  - LC-W-linear-joint
  - RC-E-linear-joint
  - LC-E-linear-joint
position: [0.00015912794687559303, -0.00013126947362762615, -0.5109341293645083, 1.1760409850349758, 0.000248513719677129, -0.621929533407773, -0.000435712016344022, -0.25723620888891585, 0.0001570613057798813, -0.00010196824444583892, -0.5109459927618113, 1.176033708862523, 0.00026405600006946736, -0.6219324270315635, 2.4978990728770677e-06, -0.0017088975996239417, -2.073140234539537, 0.1084099023783044, -0.058023574829095245, -1.0747267840396235, 0.07317868560597149, 0.0032100904000482025, 1.6205784723865542, 0.5235246181475844, -2.073396637452086, -0.10915019469469348, 0.05786516725301176, -1.0742435456501649, -0.07401658373418843, 0.003328953066405395, -1.6208511322518637, 0.5236036706122303, 0.2857, 0.2857, 0.2229, 0.294777, 0.2977, 0.2857, 0.2857, 0.2229, 0.2977, 0.2977, 0.2641, 0.2641, 0.1878, 0.1878]
velocity: [-1.3228298182769372e-05, 0.003776438677399348, -0.0004475842306390639, 0.0005277640174040183, 0.00017784552316915054, -1.3651984069540118e-05, 0.0005193152271762515, -1.614980274114962e-05, -2.00906204147508e-05, 0.003781257562926932, 0.0003987885595227354, -0.0001448261644036203, 0.0001767970472550341, -6.29682030487069e-06, 1.0756488468918085e-05, 8.071757825288712e-06, 0.006530372894968822, -0.0022088213690768304, -0.0016988138511383664, -0.008485019320501625, 0.0011095488166591757, -0.0024041403239194944, 0.0010985414608061655, -0.004589661263847082, -0.006443145635355184, -0.0024326191176086316, -0.0015454480678420079, 0.007643253393764543, 0.0002669792459987253, 0.002038055197724077, 0.0002605698826494082, 0.003338718115727315, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
effort: [-0.003956578081039798, -0.103970838003065, -10.348208900103906, 12.521092591095684, 0.031017161031442676, -0.12736535358104373, 0.024752505226941078, 22.123612918291016, 0.0034862249945595775, -0.2969554758624179, -10.330423298916536, 12.466804055037972, -0.021633696134477715, -0.1281664874752675, -0.00023514434759137692, 1.1822407190049664, 28.710767380445425, 4.002011405040487, 1.9539374192954484, -55.435105205047684, -0.08386314941827289, -17.16595565303094, -0.08274324761003982, -0.0006322724365340204, 29.273691838522797, -3.495521534240986, -1.4834178286291244, -55.63014000109101, -0.09213406138346147, -16.939107019010283, -0.09077836527859873, -0.00042287233021992554, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
edit retag flag offensive close merge delete

Comments

1

What are you looking to extract?

jayess gravatar image jayess  ( 2021-03-09 15:18:18 -0600 )edit

@jayess just this part:

position: [0.00015912794687559303, -0.00013126947362762615, -0.5109341293645083, 1.1760409850349758, 0.000248513719677129, -0.621929533407773, -0.000435712016344022, -0.25723620888891585, 0.0001570613057798813, -0.00010196824444583892, -0.5109459927618113, 1.176033708862523, 0.00026405600006946736, -0.6219324270315635, 2.4978990728770677e-06, -0.0017088975996239417, -2.073140234539537, 0.1084099023783044, -0.058023574829095245, -1.0747267840396235, 0.07317868560597149, 0.0032100904000482025, 1.6205784723865542, 0.5235246181475844, -2.073396637452086, -0.10915019469469348, 0.05786516725301176, -1.0742435456501649, -0.07401658373418843, 0.003328953066405395, -1.6208511322518637, 0.5236036706122303, 0.2857, 0.2857, 0.2229, 0.294777, 0.2977, 0.2857, 0.2857, 0.2229, 0.2977, 0.2977, 0.2641, 0.2641, 0.1878, 0.1878]
etorobot gravatar image etorobot  ( 2021-03-09 15:52:04 -0600 )edit

2 Answers

Sort by ยป oldest newest most voted
1

answered 2021-03-09 23:00:09 -0600

Via commandline

rostopic echo /joint_states | grep position

Via code make a subscriber and in the callback, take the message object and print(msg.position)

edit flag offensive delete link more

Comments

thank you! they both work!

print(joint_states.position) in callback gives:

 >>= python simplesub.py          
(0.00015512971358007838, -0.00015967116068381712, -0.5109057242141067, 1.1760468735261653, 0.00024737518151100293, -0.6219273124713371, -0.00043378914150490606, -0.25728330147008904, 0.00015295854040879757, -0.00013035336442333807, -0.5109116400316044, 1.176043473406131, 0.00026297911536165907, -0.6219294475672827, 2.3686681173184236e-06, -0.0017019379157545052, -2.0731745346858754, 0.10844604105347311, -0.057998265002420835, -1.0747005312985296, 0.07318421198008158, 0.0032205623245452246, 1.6205839810166822, 0.5235180193010072, -2.0735647804400736, -0.10926420886570778, 0.0577272133804709, -1.0744295533357717, -0.07392402403748027, 0.003115224999999684, -1.6207593400342353, 0.5235428314880762, 0.2857, 0.2857, 0.2229, 0.294777, 0.2977, 0.2857, 0.2857, 0.2229, 0.2977, 0.2977, 0.2641, 0.2641, 0.1878, 0.1878)
etorobot gravatar image etorobot  ( 2021-03-09 23:14:02 -0600 )edit
1

Can you mark the answer as accepted?

stevemacenski gravatar image stevemacenski  ( 2021-03-10 01:01:17 -0600 )edit
3

answered 2021-03-10 03:08:16 -0600

gvdhoorn gravatar image

updated 2021-03-10 03:15:04 -0600

I made a node (using the rospy tutorial) to subscribe to /joint_states and I want the.py script to print() just the position list for use outside ROS, not the whole topic.

rostopic echo can actually print individual fields. The grep should not be needed, nor a custom node.

See the rostopic echo documentation:

echo <topic-name/field>

Display specific fields in a message.

$ rostopic echo /my_topic/field_name

And see #q54895 for a previous question about this.

For JointState:

rostopic echo /joint_states/position

should work

edit flag offensive delete link more

Comments

rostopic echo /joint_states/position works in console

>>= rostopic echo /joint_states/position
[0.00015846172468917373, -0.00013917082967925174, -0.5109520651984194, 1.176033061817138, 0.0002480363156142424, -0.6219319452419783, -0.00043551693502062657, -0.25721493268895024, 0.00015645224814834745, -0.00010988429784202527, -0.5109659467739442, 1.1760253582176348, 0.0002635422334038843, -0.621934983228341, 2.8759981429691814e-06, -0.001725037542044635, -2.0730975836732286, 0.10848981362952725, -0.057972814677291484, -1.0746624245112641, 0.07318469499950343, 0.00323110174674267, 1.6205845054169492, 0.5236412224584802, -2.073346746902119, -0.1092237021374939, 0.057807670438532716, -1.074215977780912, -0.07399643209973757, 0.0033379011578772684, -1.620831187381248, 0.5237314724596493, 0.2857, 0.2857, 0.2229, 0.294777, 0.2977, 0.2857, 0.2857, 0.2229, 0.2977, 0.2977, 0.2641, 0.2641, 0.1878, 0.1878]
---
etorobot gravatar image etorobot  ( 2021-03-10 06:22:13 -0600 )edit
1

Yes, that's what you asked about, isn't it?

Creating a separate node just to print the value of a single field in messages on a topic shouldn't be necessary.

gvdhoorn gravatar image gvdhoorn  ( 2021-03-10 06:23:34 -0600 )edit

Is there an equivalent of this that can work in code (.py)? This is what I'm trying to achieve:

  • Node subscribes to /joint states to get positions
  • Script outputs (outside ROS) the list of positions from /joint_state/position
  • External program gives new list of positions
  • Node publishes this as JointTrajectoryPoint
etorobot gravatar image etorobot  ( 2021-03-10 06:35:58 -0600 )edit
2

This goes much further than simply printing a single field from an incoming message.

You should post a separate question.

gvdhoorn gravatar image gvdhoorn  ( 2021-03-10 06:45:59 -0600 )edit

Oh I didn't know that @gvdhoorn, that's really good to know!

stevemacenski gravatar image stevemacenski  ( 2021-03-11 14:17: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

Stats

Asked: 2021-03-09 15:13:47 -0600

Seen: 111 times

Last updated: Mar 10