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

send int16 via std_msg/string msg

asked 2011-12-28 08:35:31 -0500

jayson ding gravatar image

Dear All

I am trying to send out int16 from host pc to arduino target. I firstly converted the int16 to two chars and then packaged them into one std_msg/string and send it to the target. My problem is that when I send data which is < 256 and > 0. the high bit will be zero. In this case, the string length will not be two char anymore which cause the com failed. Do anyone know how to fix this problem? thanks a lot.

I understand that my approach is a little awkward. I can use std_msg/int16 do that job directly. I am doing this is aiming to use the same string to carry multiple commands and data.

edit retag flag offensive close merge delete

3 Answers

Sort by ยป oldest newest most voted
4

answered 2011-12-29 00:55:20 -0500

joq gravatar image

Encode the data as either a hex or decimal character string, then parse it on the other end. That is a lot more effort than just sending std_msgs/int16, but seems necessary for multiple commands using the same string message type.

Maybe there is a better way to approach your design problem. Could you instead use multiple message types? Multiple topics?

edit flag offensive delete link more

Comments

Agreed; using multiple topics with the appropriate message types is the right way to do this. And you get demultiplexing for free rather than having to do it yourself.
ahendrix gravatar image ahendrix  ( 2011-12-30 14:26:10 -0500 )edit
0

answered 2011-12-29 12:11:01 -0500

jayson ding gravatar image

Hi Jack Thanks for your answer. I tried to encode my data to decimal character string. My python code is "D %d %d %d" % data1, data2, data3. Unfortunately, the string length is changing among 13, 16, and 19. It depended on value of the data*. I feel this is not the right way. So I took your second suggestion and published two msgs std_msgs/String msg and geometry_msgs/Vector3 to deal with cmd and data respectively. I just tested it out. It works well. thanks. A side notes that I chose Vector3 vs std_msgs/Int16MultiArray just because I could not figure out how to publish Int16MultiArray in python. It is headache. Do you have any thing about this? thanks a lot. Jayson

edit flag offensive delete link more

Comments

Proper messages are still better, but as a remark: You can use print width modifiers in python, e.g. "D %05d %05d %05d"
dornhege gravatar image dornhege  ( 2011-12-29 12:31:12 -0500 )edit
Please use comments or edit your original question with new information. ROS Answers is not a forum or mailing list, and answers may be reordered later by votes/age, so replies may not make sense later. Thanks for helping to keep ROS Answers easy to read in the future!
Eric Perko gravatar image Eric Perko  ( 2011-12-30 15:57:33 -0500 )edit
0

answered 2011-12-30 00:35:52 -0500

jayson ding gravatar image

Hi Christian, You are right, I can print it with fixed width. Thanks for reminding me. Best

edit flag offensive delete link more

Comments

Please use comments or edit your original question with new information. ROS Answers is not a forum or mailing list, and answers may be reordered later by votes/age, so replies may not make sense later. Thanks for helping to keep ROS Answers easy to read in the future!
Eric Perko gravatar image Eric Perko  ( 2011-12-30 15:57:27 -0500 )edit

Question Tools

Stats

Asked: 2011-12-28 08:35:31 -0500

Seen: 1,475 times

Last updated: Dec 30 '11