Course Project Get Sensor Feedback

Hi

Because I’m new to python, I’m not quite sure how the whole python class works.
If we want to get the IMU sensor data, do I have to use one more function called get_imudata(),
Or I can only use a function in this way

    def callback(self, msg):
        self._imudata = msg
        print msg

if __name__ == '__main__':
    rospy.init_node('IMU_subscriber_node')
    IMU_read_obj = IMUSubscriberClass()
    while True:
        IMU_read_obj.callback()

But this code only run 4 -5 times than shows error massage, how could I correct this code?

Thanks

Hi @youngyangcs,

Your callback function can be named anything you like, as long as it’s a valid function name and you specify the same name in the subscriber definition.

Please post the error you’re seeing along with the Exercise or Example you’re trying to complete so we can help further.

Then I suggest you take our free Python course to bring yourself up to speed!

I changed the function to this

def topic_callback(self, msg):
        self._imudata = msg
        rospy.logdebug(self._imudata)
        return self._imudata

and

while not ctrl_c:
        data = imu_reader_object.topic_callback(Imu())
        rospy.loginfo(data)
        rate.sleep()

I use the Imu() as the agreement for the function topic_callback, but I only get all-zero value from the output. Could you please point out where I’m wrong?


Cheers

Hi @youngyangcs,

Which Exercise or Example are you trying to complete? I need to understand the full context of what you’re trying to do in order to figure out what the problem might be. Please also download and send your complete package to baderinola@theconstructsim.com so I can have a look.

Hi I’m tring to finish the Course Project of ROS Basic in 5 days. This is the first section to establish a IMU subscriber.

In the given solution, you use two functions to get the data


image

What I’m tring to do is combining these two functions into one, and the upon code are what I’ve modified based on your solution.

If this is not clear, I’ll send you the package.

Cheers
Yanjun Yang

Hi @youngyangcs,

I see…

These all-zero values are coming because you’re passing in an empty object, Imu(), to topic_callback. topic_callback originally receives real data from the Subscriber based created in def __init__, but now you’re also passing your own empty Imu() to it!

You really can’t combine these two functions effectively because they are performing different and complementary (opposing, sort of) functions:

  • In the original code, topic_callback gets updated Imu data from the Subscriber and updates self._imu_data.
  • get_imudata fetches the most current Imu data from the variable self._imudata.

I hope this clarifies.