|
@@ -4,6 +4,7 @@
|
|
Author: jally, corvin
|
|
Author: jally, corvin
|
|
History:
|
|
History:
|
|
20200404:增加可以直接转90度,掉头的功能 by corvin.
|
|
20200404:增加可以直接转90度,掉头的功能 by corvin.
|
|
|
|
+ 20200412:增加使用服务方式来获取红外测距值-corvin.
|
|
**************************************************************/
|
|
**************************************************************/
|
|
#include <ros/ros.h>
|
|
#include <ros/ros.h>
|
|
#include <std_msgs/Float32.h>
|
|
#include <std_msgs/Float32.h>
|
|
@@ -12,6 +13,7 @@
|
|
#include <tf/transform_listener.h>
|
|
#include <tf/transform_listener.h>
|
|
#include "rasp_imu_hat_6dof/GetYawData.h"
|
|
#include "rasp_imu_hat_6dof/GetYawData.h"
|
|
#include "ros_arduino_msgs/InfraredSensors.h"
|
|
#include "ros_arduino_msgs/InfraredSensors.h"
|
|
|
|
+#include "ros_arduino_msgs/GetInfraredDistance.h"
|
|
|
|
|
|
|
|
|
|
#define GO_FORWARD 0
|
|
#define GO_FORWARD 0
|
|
@@ -22,11 +24,15 @@
|
|
|
|
|
|
//global variable
|
|
//global variable
|
|
ros::Publisher twist_pub;
|
|
ros::Publisher twist_pub;
|
|
-ros::ServiceClient srvClient;
|
|
|
|
-rasp_imu_hat_6dof::GetYawData srv;
|
|
|
|
|
|
+ros::ServiceClient yawSrvClient;
|
|
|
|
+rasp_imu_hat_6dof::GetYawData yawSrv;
|
|
|
|
+
|
|
|
|
+//红外测距相关的服务
|
|
|
|
+ros::ServiceClient distSrvClient;
|
|
|
|
+ros_arduino_msgs::GetInfraredDistance distSrv;
|
|
|
|
|
|
float warn_dist = 0.15; //warn check distance
|
|
float warn_dist = 0.15; //warn check distance
|
|
-float tolerance_dist = 0.04; //对警告距离的容忍值
|
|
|
|
|
|
+float tolerance_dist = 0.05; //对警告距离的容忍值
|
|
|
|
|
|
float front_dist;
|
|
float front_dist;
|
|
float left_dist;
|
|
float left_dist;
|
|
@@ -65,10 +71,10 @@ void publishTwistCmd(float linear_x, float linear_y, float angular_z)
|
|
* Description:控制小车移动在过道中间,比较左右两边的距离后
|
|
* Description:控制小车移动在过道中间,比较左右两边的距离后
|
|
* 然后进行水平左右移动.
|
|
* 然后进行水平左右移动.
|
|
**********************************************************/
|
|
**********************************************************/
|
|
-void horizMoveMiddle(float left,float right,float tolerance)
|
|
|
|
|
|
+void horizMoveMiddle(float left, float right, float tolerance)
|
|
{
|
|
{
|
|
float diff = left - right;
|
|
float diff = left - right;
|
|
- ROS_INFO("diff:%f,left:%f,right:%f",diff,left,right);
|
|
|
|
|
|
+ ROS_INFO("diff:%f,left:%f,right:%f",diff, left, right);
|
|
if((fabs(diff) > tolerance)&&(left!=(float)0.3)&&(right!=(float)0.3))
|
|
if((fabs(diff) > tolerance)&&(left!=(float)0.3)&&(right!=(float)0.3))
|
|
{
|
|
{
|
|
if(diff < 0) //move right
|
|
if(diff < 0) //move right
|
|
@@ -98,6 +104,7 @@ void goForward()
|
|
tf::StampedTransform tf_Transform;
|
|
tf::StampedTransform tf_Transform;
|
|
float dist = 0.0;
|
|
float dist = 0.0;
|
|
float check_dist = 0.50;
|
|
float check_dist = 0.50;
|
|
|
|
+ float front_dist = 0.0;
|
|
|
|
|
|
try
|
|
try
|
|
{
|
|
{
|
|
@@ -106,14 +113,18 @@ void goForward()
|
|
catch(tf::TransformException &ex)
|
|
catch(tf::TransformException &ex)
|
|
{
|
|
{
|
|
ROS_ERROR("tf_Listener.waitForTransform timeout error !");
|
|
ROS_ERROR("tf_Listener.waitForTransform timeout error !");
|
|
- ROS_ERROR("%s",ex.what());
|
|
|
|
|
|
+ ROS_ERROR("%s", ex.what());
|
|
ros::shutdown();
|
|
ros::shutdown();
|
|
}
|
|
}
|
|
|
|
|
|
tf_Listener.lookupTransform(odomFrame, baseFrame, ros::Time(0), tf_Transform);
|
|
tf_Listener.lookupTransform(odomFrame, baseFrame, ros::Time(0), tf_Transform);
|
|
float x_start = tf_Transform.getOrigin().x();
|
|
float x_start = tf_Transform.getOrigin().x();
|
|
float y_start = tf_Transform.getOrigin().y();
|
|
float y_start = tf_Transform.getOrigin().y();
|
|
- while((dist < check_dist) && (ros::ok()))
|
|
|
|
|
|
+
|
|
|
|
+ distSrvClient.call(distSrv); //通过服务调用来获取测距值
|
|
|
|
+ front_dist = distSrv.response.front_dist; //记录当前测距值,防止撞墙
|
|
|
|
+ ROS_WARN("Front dist:%f", front_dist);
|
|
|
|
+ while((dist<check_dist)&&(front_dist>tolerance_dist*3.0)&&(ros::ok()))
|
|
{
|
|
{
|
|
ROS_INFO("Go forward, dist:%f", dist);
|
|
ROS_INFO("Go forward, dist:%f", dist);
|
|
publishTwistCmd(linear_x_speed, 0, 0);
|
|
publishTwistCmd(linear_x_speed, 0, 0);
|
|
@@ -123,6 +134,10 @@ void goForward()
|
|
float x = tf_Transform.getOrigin().x();
|
|
float x = tf_Transform.getOrigin().x();
|
|
float y = tf_Transform.getOrigin().y();
|
|
float y = tf_Transform.getOrigin().y();
|
|
dist = sqrt(pow((x - x_start), 2) + pow((y - y_start), 2));
|
|
dist = sqrt(pow((x - x_start), 2) + pow((y - y_start), 2));
|
|
|
|
+
|
|
|
|
+ distSrvClient.call(distSrv); //通过服务调用来获取测距值
|
|
|
|
+ front_dist = distSrv.response.front_dist; //记录当前测距值,防止撞墙
|
|
|
|
+ ROS_WARN("Front dist:%f", front_dist);
|
|
}
|
|
}
|
|
publishTwistCmd(0, 0, 0); //stop move
|
|
publishTwistCmd(0, 0, 0); //stop move
|
|
ROS_INFO("Go forward finish !!!");
|
|
ROS_INFO("Go forward finish !!!");
|
|
@@ -186,7 +201,7 @@ int controlTurn(int flag)
|
|
tf_Listener.lookupTransform(odomFrame, baseFrame, ros::Time(0), tf_Transform);
|
|
tf_Listener.lookupTransform(odomFrame, baseFrame, ros::Time(0), tf_Transform);
|
|
float last_angle = fabs(tf::getYaw(tf_Transform.getRotation()));
|
|
float last_angle = fabs(tf::getYaw(tf_Transform.getRotation()));
|
|
float turn_angle = 0.0;
|
|
float turn_angle = 0.0;
|
|
- ROS_INFO("first_angle:%f",last_angle);
|
|
|
|
|
|
+ ROS_INFO("first_angle:%f", last_angle);
|
|
while((fabs(turn_angle) < check_angle)&&(ros::ok()))
|
|
while((fabs(turn_angle) < check_angle)&&(ros::ok()))
|
|
{
|
|
{
|
|
publishTwistCmd(0, 0, angular); //原地转弯,不需要linear_x,y的控制
|
|
publishTwistCmd(0, 0, angular); //原地转弯,不需要linear_x,y的控制
|
|
@@ -226,8 +241,8 @@ void checkInfraredDist(float infrared_f,float infrared_l,float infrared_r,
|
|
{
|
|
{
|
|
static int flag = 0;
|
|
static int flag = 0;
|
|
|
|
|
|
- srvClient.call(srv); //通过服务调用来获取yaw值
|
|
|
|
- start_yaw_data = srv.response.yaw; //记录当前yaw值,用于后面修正
|
|
|
|
|
|
+ yawSrvClient.call(yawSrv); //通过服务调用来获取yaw值
|
|
|
|
+ start_yaw_data = yawSrv.response.yaw; //记录当前yaw值,用于后面修正
|
|
|
|
|
|
//判断能否左转,并且上一次动作不能是转弯
|
|
//判断能否左转,并且上一次动作不能是转弯
|
|
if((infrared_l == (float)0.3)&&(flag != 1))
|
|
if((infrared_l == (float)0.3)&&(flag != 1))
|
|
@@ -267,7 +282,7 @@ void checkInfraredDist(float infrared_f,float infrared_l,float infrared_r,
|
|
* 0 -0
|
|
* 0 -0
|
|
* -----
|
|
* -----
|
|
* / \
|
|
* / \
|
|
- * 1.57 | | -1.57
|
|
|
|
|
|
+ * 1.57| | -1.57
|
|
* \ /
|
|
* \ /
|
|
* ------
|
|
* ------
|
|
* 3.14 -3.14
|
|
* 3.14 -3.14
|
|
@@ -312,8 +327,8 @@ void yawUpdate(const float start_yaw)
|
|
//开始不断的计算偏差,调整角度
|
|
//开始不断的计算偏差,调整角度
|
|
while(fabs(diff) > 0.01)
|
|
while(fabs(diff) > 0.01)
|
|
{
|
|
{
|
|
- srvClient.call(srv);
|
|
|
|
- now_yaw = srv.response.yaw;
|
|
|
|
|
|
+ yawSrvClient.call(yawSrv);
|
|
|
|
+ now_yaw = yawSrv.response.yaw;
|
|
ROS_INFO("Now Yaw data:%f, Target Yaw:%f", now_yaw, target);
|
|
ROS_INFO("Now Yaw data:%f, Target Yaw:%f", now_yaw, target);
|
|
|
|
|
|
diff = target - now_yaw;
|
|
diff = target - now_yaw;
|
|
@@ -360,6 +375,7 @@ int main(int argc, char **argv)
|
|
std::string infrared_topic;
|
|
std::string infrared_topic;
|
|
std::string cmd_topic;
|
|
std::string cmd_topic;
|
|
std::string yaw_service;
|
|
std::string yaw_service;
|
|
|
|
+ std::string dist_service;
|
|
|
|
|
|
ros::init(argc, argv, "infrared_maze_node");
|
|
ros::init(argc, argv, "infrared_maze_node");
|
|
ros::NodeHandle handle;
|
|
ros::NodeHandle handle;
|
|
@@ -367,6 +383,7 @@ int main(int argc, char **argv)
|
|
ros::param::get("~infrared_topic", infrared_topic);
|
|
ros::param::get("~infrared_topic", infrared_topic);
|
|
ros::param::get("~cmd_topic", cmd_topic);
|
|
ros::param::get("~cmd_topic", cmd_topic);
|
|
ros::param::get("~yaw_service", yaw_service);
|
|
ros::param::get("~yaw_service", yaw_service);
|
|
|
|
+ ros::param::get("~dist_service", dist_service);
|
|
ros::param::get("~linear_x", linear_x_speed);
|
|
ros::param::get("~linear_x", linear_x_speed);
|
|
ros::param::get("~linear_y", linear_y_speed);
|
|
ros::param::get("~linear_y", linear_y_speed);
|
|
ros::param::get("~angular_speed", angular_speed);
|
|
ros::param::get("~angular_speed", angular_speed);
|
|
@@ -377,7 +394,8 @@ int main(int argc, char **argv)
|
|
|
|
|
|
ros::Subscriber sub_infrared = handle.subscribe(infrared_topic, 1, infrared_callback);
|
|
ros::Subscriber sub_infrared = handle.subscribe(infrared_topic, 1, infrared_callback);
|
|
twist_pub = handle.advertise<geometry_msgs::Twist>(cmd_topic, 1);
|
|
twist_pub = handle.advertise<geometry_msgs::Twist>(cmd_topic, 1);
|
|
- srvClient = handle.serviceClient<rasp_imu_hat_6dof::GetYawData>(yaw_service);
|
|
|
|
|
|
+ yawSrvClient = handle.serviceClient<rasp_imu_hat_6dof::GetYawData>(yaw_service);
|
|
|
|
+ distSrvClient = handle.serviceClient<ros_arduino_msgs::GetInfraredDistance>(dist_service);
|
|
|
|
|
|
ros::spin();
|
|
ros::spin();
|
|
return 0;
|
|
return 0;
|