Ask Your Question
0

setting each bit in std_msg::Int8 rather the whole byte?

asked 2016-11-11 07:22:51 -0500

215 gravatar image

I at the moment trying to come up with a solution such that I can set my std_msgs::Int8 bitwise instead of setting the whole byte.

The reason why i want to do that is because, i am currently using the message type as a register, in which the 4 last bits is read by a node, that performs certain actions based on that value.

is it possible to do something like std_msg::Int8 msg.data[0] = True and thereby only set one bit?

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
2

answered 2016-11-11 08:12:15 -0500

Your exact syntax is incorrect, but you can definitely set individual bits and decode the number in a subscriber. For example, something like msg.data = 8 would set the data field to have 0b00001000 in binary. You can set values using hex/binary/decimal (see integer literals), and you can also use C++ bitwise operators to set and access individual bits.

edit flag offensive delete link more
2

answered 2016-11-11 08:12:24 -0500

gvdhoorn gravatar image

No, that is not possible, as the semantics of an std_msgs/Int8 are such that it is considered a single, 8 bit integer value, not a set of individual bits.

You have two options:

  1. use a message that contains a (fixed size) set of the bool primitive type
  2. use bit manipulation to set individual bits in an Int8

Note that option 1 will not make ROS automagically consider the (fixed size) set of bools an integer, but it is the only way to (re)set individual boolean values in a ROS message.

edit flag offensive delete link more

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: 2016-11-11 07:22:51 -0500

Seen: 44 times

Last updated: Nov 11 '16