How to write a callback subscriber for sensor_state (turtlebot3_msgs/SensorState)

asked 2021-09-13 08:58:29 -0500

Mordred gravatar image

updated 2021-09-13 13:41:20 -0500

I was trying to make a python script. In the subscriber callback, I am stuck on how do I create and get the values of the wheel encoders. I ask for help about this subject and someone said that the encoders are Uint32 which doesn't give much help. btw it's python

Version: Ubuntu (64-bit)

`#!/usr/bin/env python

import rospy

from turtlebot3_msgs.msg import SensorState

def callback(msg):

  print ("left encoder = " + {left_encoder.msg})
  print ("right encoder = " + {right_encoder.msg})

def talker():

  rospy.Subscriber("/sensor_state", SensorState, callback)

if __name__ == '__main__':

except rospy.ROSInterruptException:
edit retag flag offensive close merge delete


Could you pls show code snippets, the errors you are getting and ROS version you are working please.

osilva gravatar image osilva  ( 2021-09-13 13:33:36 -0500 )edit

I have updated the question

Mordred gravatar image Mordred  ( 2021-09-13 13:42:08 -0500 )edit

Thank you for sharing the code. Could you pls share your outputs.


Header header

uint8 bumper

float32 cliff

float32 sonar

float32 illumination

uint8 led

uint8 button

bool torque

int32 left_encoder # (-2,147,483,648 ~ 2,147,483,647)

int32 right_encoder # (-2,147,483,648 ~ 2,147,483,647)

float32 battery

I understand why the suggestion for int32 as the format of the msg for left_encoder and right_encoder are in this type.

osilva gravatar image osilva  ( 2021-09-13 13:56:15 -0500 )edit

I was investigating a little and it seems it may have to do with OpenCR firmware not updated and not necessarily your code. Check this thread on no battery similar issue. I’ll keep digging.

osilva gravatar image osilva  ( 2021-09-13 15:59:04 -0500 )edit

Yes I believe this is to opencr setup.

Hope this helps to check if it’s a hardware issue. Let me know and I’ll turn this into an answer

osilva gravatar image osilva  ( 2021-09-13 16:16:30 -0500 )edit

I tried to replicate in simulation, but this is only available with turtlebot3 hardware.

osilva gravatar image osilva  ( 2021-09-13 17:43:17 -0500 )edit