joy callback access to data

asked 2022-12-30 01:50:04 -0500

PointCloud gravatar image

Hi all, I have created a customized version of the joystick publisher, basically adding support for the old Logitech F310. This is a python node publishing on the topic. Everything works great.

Now in my main project (on the Bot), I can see the topic being published and also see the values change as expected, but I am struggling to understand how to access the actual readings of the axes.

The MicroROS development for Teensy 4.1 is done on Ubuntu 22.04, ROS2 Humble in VS Code and Platformio. Below is what the function looks like. Very simplistic for now.

void subscription_callback_autobot_joy(const void * msgin) {  
  const sensor_msgs__msg__Joy * msg = (const sensor_msgs__msg__Joy *)msgin;
  vel_demand_x = msg->axes[1];

  if (autobot_joy_Debug == 1)
    debugln("autobot_joy received");
    debug("msg: "); debugln(msg->;
    debugln(" ");

Looking at the


I can see the data is contained inside as axes and buttons. From further looking around the web and the ROS wiki, I found the website below, indicating, I have to possibly convert the message datatype from ROS realm into c/c++. Further in the documentation it mentions variable-length array type. From looking through


and also issuing command

ros2 interface show sensor_msgs/msg/Joy
# Reports the state of a joystick's axes and buttons.

# The timestamp is the time at which data is received from the joystick.
std_msgs/Header header
    builtin_interfaces/Time stamp
        int32 sec
        uint32 nanosec
    string frame_id

# The axes measurements from a joystick.
float32[] axes

# The buttons measurements from a joystick.
int32[] buttons

I can see it's a float32[] for axes -> in the documentation that suggests a float in c, which my variable is.

Website I found mentioning conversion

Now the question:

how would I read the value of let's say first axis [0] in the callback?

Please advise, I'm not sure what I am missing here!

Thanks and happy new year to y'all!

edit retag flag offensive close merge delete


I'm not sure I understand your question. You already show this:

vel_demand_x = msg->axes[1];

wouldn't that be how you access elements of the axes array of the msg structure instance?


Website I found mentioning conversion

while many things will be similar, please note micro-ROS targets ROS 2, while the ROS wiki you link documents ROS 1.

gvdhoorn gravatar image gvdhoorn  ( 2023-01-02 05:50:22 -0500 )edit

Note btw I would not recommend using joystick data directly for tele-op purposes.

Using a geometry_msgs/Twist as input would be much more according to best practices, as it abstracts the input to your platform in an appropriate way (it's very likely two different joysticks would have different axis mapping fi, which, if you were to use axes values directly, would mean you'd have to update your code).

gvdhoorn gravatar image gvdhoorn  ( 2023-01-05 06:49:52 -0500 )edit