Home | Tutorials | Wiki | Issues
Ask Your Question
0

Read IMU sensor from a plugin

asked 2018-08-20 10:01:27 -0500

this post is marked as community wiki

This post is a wiki. Anyone with karma >75 is welcome to improve it.

Hello,

I made a model of a robot and I added and IMU sensor to it in the sdf file as follow:

   <sensor name="imu_sensor" type="imu">
    <pose>0 0 0 0 0 0</pose>
    <always_on>1</always_on>
    <update_rate>100.0</update_rate>
  </sensor>

I'm controlling my robot using a plugin I wrote and everything is working fine, but I would like to read the IMU sensor data inside my plugin's code. I know that I can subscribe to the topic of the IMU ("~/gazebo/default/imu_sensor/imu") and read from there, but I'm wondering how can I get from code an instance of the IMU sensor object and read the IMU like I read the pose of the model.

Basically my question is: How can I read the IMU data using an instance of an IMU object in the model? I guess should be something like below, but I don't know how to glue everything together yet.

ImuSensor imu;
double acc = imu.GetLinearAcceleration();
edit retag flag offensive close merge delete

Comments

I'm wondering why do you want to do that? Why not just subscribe to the topic?

arifrahman gravatar imagearifrahman ( 2018-08-21 02:48:32 -0500 )edit

The reason is that in my plugin I'm collecting some data ( including imu ) to send out in another message and I don't want to synchronize the data from the imu topic with the other data. I rather prefer read all the elements/data I need at the "same time".

maikelo gravatar imagemaikelo ( 2018-08-22 09:08:36 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
0

answered 2018-08-22 09:27:15 -0500

maikelo gravatar image

Answering my own question,

You can have an instance of the IMU sensor by using the SensorManager class and read the IMU data by using the ImuSensor class as follow:

sensors::SensorManager *pMgr = sensors::SensorManager::Instance() ;
if(pMgr == nullptr)
{
   /*ERROR*/
}
else
{
    sensors::SensorPtr pSensor = pMgr->GetSensor("imu_sensor");
    if(pSensor == nullptr)
    {
       /*ERROR*/
    } 
    else
    {
       sensors::ImuSensorPtr pImuSensor = dynamic_pointer_cast<sensors::ImuSensor, sensors::Sensor>(pSensor);
       if(pImuSensor == nullptr)
       {
           /*ERROR*/
       }
       else
       {
            /*Do something ... for example to read the linear accel from the IMU:*/
            ignition::math::Vector3d linearAcceleration = m_pImuSensor->LinearAcceleration();
       }
    }
}
edit flag offensive delete link more
Login/Signup to Answer

Question Tools

Stats

Asked: 2018-08-20 10:01:27 -0500

Seen: 473 times

Last updated: Aug 22 '18