|
@@ -7,6 +7,7 @@
|
|
|
History:
|
|
|
20200404:增加可以直接转90度,掉头的功能 by corvin.
|
|
|
20200412:增加使用服务方式来获取红外测距值-corvin.
|
|
|
+ 20200424:增加每次前进时移动距离的参数,可以配置.-corvin
|
|
|
**************************************************************/
|
|
|
#include <ros/ros.h>
|
|
|
#include <std_msgs/Float32.h>
|
|
@@ -35,8 +36,9 @@ 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.25; //warn check distance
|
|
|
float tolerance_dist = 0.05; //对警告距离的容忍值
|
|
|
+float forward_dist = 0.50; //每次前进移动时走的距离
|
|
|
|
|
|
float front_dist;
|
|
|
float left_dist;
|
|
@@ -148,12 +150,11 @@ void yawUpdate(const float start_yaw)
|
|
|
/****************************************************
|
|
|
* Description:控制小车前进移动0.5m
|
|
|
***************************************************/
|
|
|
-void goForward()
|
|
|
+void goForward(const float check_dist)
|
|
|
{
|
|
|
tf::TransformListener tf_Listener;
|
|
|
tf::StampedTransform tf_Transform;
|
|
|
- float dist = 0.0;
|
|
|
- float check_dist = 0.50;
|
|
|
+ float dist = 0.0; //每次前进移动的距离
|
|
|
float front_dist = 0.0;
|
|
|
|
|
|
try
|
|
@@ -191,7 +192,6 @@ void goForward()
|
|
|
}
|
|
|
publishTwistCmd(0, 0, 0); //stop move
|
|
|
ROS_INFO("Go forward finish !!!");
|
|
|
- ros::Duration(0.1).sleep(); //100 ms
|
|
|
}
|
|
|
|
|
|
/*************************************************************
|
|
@@ -226,7 +226,7 @@ int controlTurn(int flag)
|
|
|
break;
|
|
|
|
|
|
case GO_FORWARD: //小车直行50cm
|
|
|
- goForward();
|
|
|
+ goForward(forward_dist);
|
|
|
return 1;
|
|
|
|
|
|
default:
|
|
@@ -282,13 +282,13 @@ int controlTurn(int flag)
|
|
|
|
|
|
/**********************************************************
|
|
|
* Description:控制小车移动在过道中间,比较左右两边的距离后
|
|
|
- * 然后进行水平左右移动.
|
|
|
+ * 然后进行水平左右移动.使小车保持在过道中间位置.
|
|
|
**********************************************************/
|
|
|
void horizMoveMiddle(float tolerance)
|
|
|
{
|
|
|
- float diff = 0.0;
|
|
|
+ float diff = 0.0; //计算左右两边距离差
|
|
|
float begin_yaw = 0.0;
|
|
|
- float now_left = 0.0;
|
|
|
+ float now_left = 0.0;
|
|
|
float now_right = 0.0;
|
|
|
|
|
|
turnFlag = HORIZ_MOVE;
|
|
@@ -296,13 +296,13 @@ void horizMoveMiddle(float tolerance)
|
|
|
begin_yaw = yawSrv.response.yaw;
|
|
|
|
|
|
distSrvClient.call(distSrv);
|
|
|
- now_left = distSrv.response.left_dist;
|
|
|
- ros::Duration(0.02).sleep();//在获取两个传感器值之间需要加延迟
|
|
|
+ now_left = distSrv.response.left_dist; //获取左边距离
|
|
|
+ ros::Duration(0.02).sleep();//在获取两次传感器值之间需要加延迟
|
|
|
distSrvClient.call(distSrv);
|
|
|
- now_right = distSrv.response.right_dist;
|
|
|
+ now_right = distSrv.response.right_dist;//获取右边距离
|
|
|
|
|
|
diff = now_left - now_right;
|
|
|
- ROS_INFO("diff:%f,left:%f,right:%f",diff,now_left,now_right);
|
|
|
+ ROS_INFO("diff:%f,left:%f,right:%f", diff, now_left, now_right);
|
|
|
while((fabs(diff)>tolerance)&&(now_left!=(float)0.3)&&(now_right!=(float)0.3))
|
|
|
{
|
|
|
if(diff < 0) //move right
|
|
@@ -320,11 +320,11 @@ void horizMoveMiddle(float tolerance)
|
|
|
|
|
|
distSrvClient.call(distSrv);
|
|
|
now_left = distSrv.response.left_dist;
|
|
|
- ros::Duration(0.02).sleep();//在获取两个传感器值之间需要加延迟
|
|
|
+ ros::Duration(0.02).sleep();//在获取两次传感器值之间需要加延迟
|
|
|
distSrvClient.call(distSrv);
|
|
|
now_right = distSrv.response.right_dist;
|
|
|
|
|
|
- diff = now_left - now_right;
|
|
|
+ diff = now_left - now_right; //计算左右距离差值
|
|
|
}
|
|
|
//当平移结束后立刻停止移动
|
|
|
publishTwistCmd(0, 0, 0); //stop move
|
|
@@ -335,11 +335,12 @@ void horizMoveMiddle(float tolerance)
|
|
|
* 进行连续两次旋转,在每次转弯后必须有个直行.这里需要注意的是当小车在转弯
|
|
|
* 后一定要有一个可以修正转弯角度的功能,否则转弯后角度不准确小车前进方向
|
|
|
* 会错.这里的走迷宫策略是左转优先,中间是直行,最后是右转.
|
|
|
+ * 当前方,左边,右边都无法移动时,进行掉头动作,然后直行.
|
|
|
**************************************************************************/
|
|
|
void checkInfraredDist(float infrared_f,float infrared_l,float infrared_r,
|
|
|
float tolerance, float warn_distance)
|
|
|
{
|
|
|
- static int flag = 0;
|
|
|
+ static int flag = 0; //记录是否转过弯的标志
|
|
|
|
|
|
yawSrvClient.call(yawSrv); //通过服务调用来获取yaw值
|
|
|
start_yaw_data = yawSrv.response.yaw; //记录当前yaw值,用于后面修正
|
|
@@ -349,21 +350,21 @@ void checkInfraredDist(float infrared_f,float infrared_l,float infrared_r,
|
|
|
{
|
|
|
ROS_WARN("turn Left! yaw_data:%f", start_yaw_data);
|
|
|
turnFlag = TURN_LEFT;
|
|
|
- flag = 1;
|
|
|
+ flag = 1; //设置转弯标志
|
|
|
}
|
|
|
- else if(infrared_f > tolerance*5.0)//判断能否直行
|
|
|
+ else if(infrared_f > warn_distance)//判断能否直行
|
|
|
{
|
|
|
//始终控制小车保持在过道的中间位置
|
|
|
ROS_WARN("go Forward ! yaw_data: %f", start_yaw_data);
|
|
|
horizMoveMiddle(tolerance);
|
|
|
turnFlag = GO_FORWARD;
|
|
|
- flag = 0;
|
|
|
+ flag = 0; //设置直行标志
|
|
|
}
|
|
|
else if((infrared_r == (float)0.3)&&(flag != 1))//判断能否右转
|
|
|
{
|
|
|
ROS_WARN("turn Right ! yaw_data: %f", start_yaw_data);
|
|
|
turnFlag = TURN_RIGHT;
|
|
|
- flag = 1;
|
|
|
+ flag = 1; //设置转弯标志
|
|
|
}
|
|
|
else //小车掉头
|
|
|
{
|
|
@@ -401,7 +402,7 @@ int main(int argc, char **argv)
|
|
|
std::string infrared_topic;
|
|
|
std::string cmd_topic;
|
|
|
std::string yaw_service;
|
|
|
- std::string dist_service;
|
|
|
+ std::string infrared_service;
|
|
|
|
|
|
ros::init(argc, argv, "infrared_maze_node");
|
|
|
ros::NodeHandle handle;
|
|
@@ -409,7 +410,7 @@ int main(int argc, char **argv)
|
|
|
ros::param::get("~infrared_topic", infrared_topic);
|
|
|
ros::param::get("~cmd_topic", cmd_topic);
|
|
|
ros::param::get("~yaw_service", yaw_service);
|
|
|
- ros::param::get("~dist_service", dist_service);
|
|
|
+ ros::param::get("~infrared_service", infrared_service);
|
|
|
ros::param::get("~linear_x", linear_x_speed);
|
|
|
ros::param::get("~linear_y", linear_y_speed);
|
|
|
ros::param::get("~angular_speed", angular_speed);
|
|
@@ -417,11 +418,12 @@ int main(int argc, char **argv)
|
|
|
ros::param::get("~tolerance_dist", tolerance_dist);
|
|
|
ros::param::get("~odom_frame", odomFrame);
|
|
|
ros::param::get("~base_frame", baseFrame);
|
|
|
+ ros::param::get("~forward_dist", forward_dist);
|
|
|
|
|
|
ros::Subscriber sub_infrared = handle.subscribe(infrared_topic, 1, infrared_callback);
|
|
|
twist_pub = handle.advertise<geometry_msgs::Twist>(cmd_topic, 1);
|
|
|
yawSrvClient = handle.serviceClient<rasp_imu_hat_6dof::GetYawData>(yaw_service);
|
|
|
- distSrvClient = handle.serviceClient<ros_arduino_msgs::GetInfraredDistance>(dist_service);
|
|
|
+ distSrvClient = handle.serviceClient<ros_arduino_msgs::GetInfraredDistance>(infrared_service);
|
|
|
|
|
|
ros::spin();
|
|
|
return 0;
|