|
@@ -1,4 +1,4 @@
|
|
|
-/****************************************************************
|
|
|
+/*****************************************************************
|
|
|
* Copyright:2016-2021 www.corvin.cn ROS小课堂
|
|
|
* Description:使用串口来读取IMU模块的数据,并通过ROS话题topic将
|
|
|
* 数据发布出来.芯片中默认读取出来的加速度数据单位是g,需要将其
|
|
@@ -16,8 +16,8 @@
|
|
|
* 20210319:增加使用service方式可以修改imu模块串口通信波特率.
|
|
|
* 注意修改了波特率后,需要将IMU模块短开使用新波特率连接.
|
|
|
* 20210321:增加可以设置D0,D1,D2,D3共4个IO引脚输出数字高低电平
|
|
|
- * 的服务,这里的高电平为3.3V.
|
|
|
-****************************************************************/
|
|
|
+ * 的服务,这里的高电平为3.3V.增加通过服务方式获取yaw数据.
|
|
|
+*****************************************************************/
|
|
|
#include <ros/ros.h>
|
|
|
#include <tf/tf.h>
|
|
|
#include <imu_data.h>
|
|
@@ -27,8 +27,11 @@
|
|
|
#include "serial_imu_hat_6dof/setYawZero.h"
|
|
|
#include "serial_imu_hat_6dof/setBaudRate.h"
|
|
|
#include "serial_imu_hat_6dof/setPinOutHL.h"
|
|
|
+#include "serial_imu_hat_6dof/getYawData.h"
|
|
|
|
|
|
|
|
|
+static float g_yawData; //全局变量,存储当前yaw值,可以通过服务来得到该值
|
|
|
+
|
|
|
/**********************************************************
|
|
|
* Description:将yaw角度归零的话题回调函数,只要往话题中
|
|
|
* 发布一条Empty消息,即可将yaw角度归零.
|
|
@@ -80,6 +83,13 @@ bool pinOutHLService(serial_imu_hat_6dof::setPinOutHL::Request &req,
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
+bool getYawDataService(serial_imu_hat_6dof::getYawData::Request &req,
|
|
|
+ serial_imu_hat_6dof::getYawData::Response &res)
|
|
|
+{
|
|
|
+ res.yaw = g_yawData;
|
|
|
+ return true;
|
|
|
+}
|
|
|
+
|
|
|
int main(int argc, char **argv)
|
|
|
{
|
|
|
float yaw, pitch, roll;
|
|
@@ -94,6 +104,7 @@ int main(int argc, char **argv)
|
|
|
std::string yaw_zero_service;
|
|
|
std::string baud_update_service;
|
|
|
std::string pin_outHL_service;
|
|
|
+ std::string yaw_data_service;
|
|
|
|
|
|
int pub_hz = 0;
|
|
|
float degree2Rad = 3.1415926/180.0;
|
|
@@ -107,13 +118,14 @@ int main(int argc, char **argv)
|
|
|
ros::param::get("~baud_rate", baud);
|
|
|
ros::param::get("~pub_hz", pub_hz);
|
|
|
ros::param::get("~link_name", link_name);
|
|
|
- ros::param::get("~pub_data_topic", imu_topic);
|
|
|
- ros::param::get("~pub_temp_topic", temp_topic);
|
|
|
- ros::param::get("~yaw_zero_topic", yaw_zero_topic);
|
|
|
+ ros::param::get("~pub_data_topic", imu_topic);
|
|
|
+ ros::param::get("~pub_temp_topic", temp_topic);
|
|
|
+ ros::param::get("~yaw_zero_topic", yaw_zero_topic);
|
|
|
ros::param::get("~yaw_zero_service", yaw_zero_service);
|
|
|
ros::param::get("~yaw_pub_topic", yaw_pub_topic);
|
|
|
ros::param::get("~baud_update_srv", baud_update_service);
|
|
|
ros::param::get("~pin_outHL_srv", pin_outHL_service);
|
|
|
+ ros::param::get("~yaw_data_srv", yaw_data_service);
|
|
|
|
|
|
//初始化imu模块串口,根据设备号和波特率进行连接
|
|
|
int ret = initSerialPort(imu_dev.c_str(), baud);
|
|
@@ -125,12 +137,13 @@ int main(int argc, char **argv)
|
|
|
}
|
|
|
ROS_INFO("IMU module is working...");
|
|
|
|
|
|
- //定义三个服务,分别是更新波特率,yaw角度归零和控制引脚输出高低电平
|
|
|
- ros::ServiceServer baudSrv = handle.advertiseService(baud_update_service, setBaudService);
|
|
|
- ros::ServiceServer yawSrv = handle.advertiseService(yaw_zero_service, yawZeroService);
|
|
|
- ros::ServiceServer pinSrv = handle.advertiseService(pin_outHL_service, pinOutHLService);
|
|
|
+ //定义四个服务,分别是更新波特率,yaw角度归零,控制引脚输出高低电平和获取yaw当前角度
|
|
|
+ ros::ServiceServer setBaudSrv= handle.advertiseService(baud_update_service, setBaudService);
|
|
|
+ ros::ServiceServer setyawSrv = handle.advertiseService(yaw_zero_service, yawZeroService);
|
|
|
+ ros::ServiceServer pinOutSrv = handle.advertiseService(pin_outHL_service, pinOutHLService);
|
|
|
+ ros::ServiceServer getYawSrv = handle.advertiseService(yaw_data_service, getYawDataService);
|
|
|
|
|
|
- ros::Subscriber yaw_zero_sub = handle.subscribe(yaw_zero_topic, 1, yawZeroCallback);
|
|
|
+ ros::Subscriber yawZeroSub = handle.subscribe(yaw_zero_topic, 1, yawZeroCallback);
|
|
|
ros::Publisher imu_pub = handle.advertise<sensor_msgs::Imu>(imu_topic, 1);
|
|
|
ros::Publisher yaw_pub = handle.advertise<std_msgs::Float32>(yaw_pub_topic, 1);
|
|
|
ros::Rate loop_rate(pub_hz);
|
|
@@ -150,6 +163,7 @@ int main(int argc, char **argv)
|
|
|
if(yaw >= 3.1415926)
|
|
|
yaw -= 6.2831852;
|
|
|
|
|
|
+ g_yawData = yaw; //获取yaw值,可以通过服务来得到该值
|
|
|
//publish yaw data to topic
|
|
|
yaw_msg.data = yaw;
|
|
|
yaw_pub.publish(yaw_msg);
|