Robotics StackExchange | Archived questions

Read IMU sensor from a plugin

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();

Asked by maikelo on 2018-08-20 10:01:27 UTC

Comments

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

Asked by arifrahman on 2018-08-21 02:48:32 UTC

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".

Asked by maikelo on 2018-08-22 09:08:36 UTC

Answers

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();
       }
    }
}

Asked by maikelo on 2018-08-22 09:27:15 UTC

Comments