imu.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2015 Aldebaran
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16 */
17 
18 /*
19 * LOCAL includes
20 */
21 #include "imu.hpp"
22 #include "../tools/from_any_value.hpp"
23 
24 /*
25 * ROS includes
26 */
27 //#include <tf/transform_datatypes.h>
29 
30 /*
31 * BOOST includes
32 */
33 #include <boost/foreach.hpp>
34 #define for_each BOOST_FOREACH
35 
36 namespace naoqi
37 {
38 namespace converter {
39 
40 
41  ImuConverter::ImuConverter(const std::string& name, const IMU::Location& location, const float& frequency, const qi::SessionPtr& session):
42  BaseConverter(name, frequency, session),
43  p_memory_(session->service("ALMemory"))
44  {
45  if(location == IMU::TORSO){
46  msg_imu_.header.frame_id = "base_link";
47  data_names_list_.push_back("DCM/Time");
48  data_names_list_.push_back("Device/SubDeviceList/InertialSensor/AngleX/Sensor/Value");
49  data_names_list_.push_back("Device/SubDeviceList/InertialSensor/AngleY/Sensor/Value");
50  data_names_list_.push_back("Device/SubDeviceList/InertialSensor/AngleZ/Sensor/Value");
51  data_names_list_.push_back("Device/SubDeviceList/InertialSensor/GyroscopeX/Sensor/Value");
52  data_names_list_.push_back("Device/SubDeviceList/InertialSensor/GyroscopeY/Sensor/Value");
53  data_names_list_.push_back("Device/SubDeviceList/InertialSensor/GyroscopeZ/Sensor/Value");
54  data_names_list_.push_back("Device/SubDeviceList/InertialSensor/AccelerometerX/Sensor/Value");
55  data_names_list_.push_back("Device/SubDeviceList/InertialSensor/AccelerometerY/Sensor/Value");
56  data_names_list_.push_back("Device/SubDeviceList/InertialSensor/AccelerometerZ/Sensor/Value");
57  }
58  else if(location == IMU::BASE){
59  msg_imu_.header.frame_id = "base_footprint";
60  data_names_list_.push_back("DCM/Time");
61  data_names_list_.push_back("Device/SubDeviceList/InertialSensorBase/AngleX/Sensor/Value");
62  data_names_list_.push_back("Device/SubDeviceList/InertialSensorBase/AngleY/Sensor/Value");
63  data_names_list_.push_back("Device/SubDeviceList/InertialSensorBase/AngleZ/Sensor/Value");
64  data_names_list_.push_back("Device/SubDeviceList/InertialSensorBase/GyroscopeX/Sensor/Value");
65  data_names_list_.push_back("Device/SubDeviceList/InertialSensorBase/GyroscopeY/Sensor/Value");
66  data_names_list_.push_back("Device/SubDeviceList/InertialSensorBase/GyroscopeZ/Sensor/Value");
67  data_names_list_.push_back("Device/SubDeviceList/InertialSensorBase/AccelerometerX/Sensor/Value");
68  data_names_list_.push_back("Device/SubDeviceList/InertialSensorBase/AccelerometerY/Sensor/Value");
69  data_names_list_.push_back("Device/SubDeviceList/InertialSensorBase/AccelerometerZ/Sensor/Value");
70  }
71  }
72 
74  {
75 
76  }
77 
79  {
80 
81  }
82 
84  {
85  callbacks_[action] = cb;
86  }
87 
88  void ImuConverter::callAll(const std::vector<message_actions::MessageAction>& actions)
89  {
90  // Get inertial data
91  std::vector<float> memData;
92  try {
93  qi::AnyValue anyvalues = p_memory_.call<qi::AnyValue>("getListData", data_names_list_);
94  tools::fromAnyValueToFloatVector(anyvalues, memData);
95  } catch (const std::exception& e) {
96  std::cerr << "Exception caught in ImuConverter: " << e.what() << std::endl;
97  return;
98  }
99  // angle (X,Y,Z) = memData(1,2,3);
100  // gyro (X,Y,Z) = memData(4,5,6);
101  // acc (X,Y,Z) = memData(7,8,9);
102 
103  const ros::Time& stamp = ros::Time::now();
104  msg_imu_.header.stamp = stamp;
105 
106  tf2::Quaternion tf_quat;
107  tf_quat.setRPY( memData[1], memData[2], memData[3] );
108  msg_imu_.orientation = tf2::toMsg( tf_quat );
109 
110  //msg_imu_.orientation = tf::createQuaternionMsgFromRollPitchYaw(
111  // memData[1],
112  // memData[2],
113  // memData[3]);
114 
115  msg_imu_.angular_velocity.x = memData[4];
116  msg_imu_.angular_velocity.y = memData[5];
117  msg_imu_.angular_velocity.z = memData[6];
118 
119  msg_imu_.linear_acceleration.x = memData[7];
120  msg_imu_.linear_acceleration.y = memData[8];
121  msg_imu_.linear_acceleration.z = memData[9];
122 
123  // Covariances unknown
124  msg_imu_.orientation_covariance[0] = -1;
125  msg_imu_.angular_velocity_covariance[0] = -1;
126  msg_imu_.linear_acceleration_covariance[0] = -1;
127 
128 
130  {
131  callbacks_[action]( msg_imu_ );
132  }
133  }
134 
135 }
136 
137 }
void setRPY(const tf2Scalar &roll, const tf2Scalar &pitch, const tf2Scalar &yaw)
boost::function< void(sensor_msgs::Imu &) > Callback_t
Definition: imu.hpp:48
sensor_msgs::Imu msg_imu_
Definition: imu.hpp:62
virtual void callAll(const std::vector< message_actions::MessageAction > &actions)
Definition: imu.cpp:88
qi::AnyObject p_memory_
Definition: imu.hpp:63
ImuConverter(const std::string &name, const IMU::Location &location, const float &frequency, const qi::SessionPtr &session)
Definition: imu.cpp:41
action
void registerCallback(const message_actions::MessageAction action, Callback_t cb)
Definition: imu.cpp:83
B toMsg(const A &a)
virtual void reset()
Definition: imu.cpp:78
std::vector< float > fromAnyValueToFloatVector(qi::AnyValue &value, std::vector< float > &result)
static Time now()
std::map< message_actions::MessageAction, Callback_t > callbacks_
Definition: imu.hpp:67
#define for_each
Definition: imu.cpp:34
std::vector< std::string > data_names_list_
Definition: imu.hpp:64


naoqi_driver
Author(s): Karsten Knese
autogenerated on Sat Feb 15 2020 03:24:26