c++实现ros by example volume1 例子timed_out_and_back功能

之前没有查找相关资料,过后发现有相似功能例程,网址:http://blog.csdn.net/scliu12345/article/details/44538927

之前以为ros::Rate 时间片用完后,会重新从while(ros::ok())开始,后来证明该想法是不对的,应该是从断点处继续执行。上文的网址博文有源码中关键部分的注释,我的只是一个不同思路,但建议用上文博文的方法。

cpp源文件放在beginer_turtorials/src下。

编辑功能包目录中的CMakeList,txt文件,加入下面两行:

add_executable(move_my_turtlebot src/move_my_turtlebot.cpp)

target_link_libraries(move_my_turtlebot ${catkin_LIBRARIES})

回到工作环境目录

$ cd ~/catkin_ws

编译

$ catkin_make

运行是先launch主节点,我的是roslaunch turtlebot_bringup minimal.launch

然后,rosrun beginner_turtorials move_my_turtlebot



<pre name="code" class="cpp">// move_my_turtlebot.cpp : 定义控制台应用程序的入口点。
//

#include "ros/ros.h"
#include "geometry_msgs/Twist.h"

#define pi 3.1415926

int main(int argc, char **argv)
{
	ros::init(argc,argv,"move_my_turtlebot");
	ros::NodeHandle n;
	ros::Publisher vel_pub = n.advertise<geometry_msgs::Twist>("/cmd_vel_mux/input/safety_controller",1);

	unsigned char rate;
	rate = 50;
	ros::Rate loop_rate(rate);
	float linear_speed, goal_distance, linear_duration, angular_speed, goal_angle, angular_duration;
	
	linear_speed = 0.2;
	goal_distance = 1.0;
	linear_duration = goal_distance / linear_speed;

	angular_speed = 1.0;
	goal_angle = pi;
	angular_duration = goal_angle / angular_speed;

	int linear_ticks,angular_ticks;
	linear_ticks = angular_ticks = 0;
	linear_ticks =int(linear_duration*rate);
	angular_ticks =int(angular_duration*rate);

	geometry_msgs::Twist msg;

	while (ros::ok())
	{		
		if(linear_ticks != 0)//先前行
		{			
			msg.linear.x = linear_speed;
			vel_pub.publish(msg);
			msg.linear.x = 0;
			ros::spinOnce();
			linear_ticks--;
			loop_rate.sleep();
		}
		else if (angular_ticks != 0)//后旋转
		{
			msg.angular.z = angular_speed;
			vel_pub.publish(msg);
			msg.angular.z = 0;
			ros::spinOnce();
			angular_ticks--;
			loop_rate.sleep();
		}
		else//退出
		{	msg.linear.x = 0;
			msg.angular.z = 0;
			vel_pub.publish(msg);
			ros::spinOnce();
			loop_rate.sleep();
			ROS_INFO("%s", "Stopping the robot ...");
			break;
		}
	}	
	return 0;
}

好的实现应该是如下的,完成走出及回来的过程。

// move_my_turtlebot2.cpp
//

#include "ros/ros.h"
#include "geometry_msgs/Twist.h"

#define pi 3.1415926

int main(int argc, char **argv)
{
	ros::init(argc,argv,"move_my_turtlebot2");
	ros::NodeHandle n;
	ros::Publisher vel_pub = n.advertise<geometry_msgs::Twist>("/cmd_vel_mux/input/safety_controller",1);

	unsigned char rate;
	rate = 50;
	ros::Rate loop_rate(rate);
	float linear_speed, goal_distance, linear_duration, angular_speed, goal_angle, angular_duration;
	
	linear_speed = 0.2;
	goal_distance = 1.0;
	linear_duration = goal_distance / linear_speed;

	angular_speed = 1.0;
	goal_angle = pi;
	angular_duration = goal_angle / angular_speed;

	int ticks;
	

	geometry_msgs::Twist msg;

	while (ros::ok())
	{		
		for(char re=0;re<2;re++)
		{
			msg.linear.x = linear_speed;
			int ticks=int(linear_duration*rate);
			for(int i=0;i<ticks;i++)
			{
				vel_pub.publish(msg);
				loop_rate.sleep();	
			}

			msg.linear.x = 0;
			vel_pub.publish(msg);
			ros::Duration(1).sleep();

			msg.angular.z = angular_speed;
			ticks=int(angular_duration*rate);
			for(int i=0;i<ticks;i++)
			{
				vel_pub.publish(msg);
				loop_rate.sleep();	
			}

			msg.angular.z = 0;
			vel_pub.publish(msg);
			ros::Duration(1).sleep();

		}
		
		msg.linear.x = 0;
		msg.angular.z = 0;
		vel_pub.publish(msg);
		ROS_INFO("%s", "Stopping the robot ...");
		break;
		
	}	
	return 0;
}




                
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Every day, more and more people learn and use the C++ programming Language. I have taught C to thousands of students in my life. I see many of those students now moving to C++ in their school work or career. The C++ language is becoming an industry-accepted standard programming language, using the solid foundation of C to gain a foothold. C++ is simply a better C than C. C++ By Example is one of several books in Que抯 new line of By Example series. The philosophy of these books is simple: The best way to teach computer programming concepts is with multiple examples. Command descriptions, format syntax, and language references are not enough to teach a newcomer a programming language. Only by looking at numerous examples and by running sample programs can programming students get more than just a 揻eel?for the language. Table of Contents Overview I Introduction to C++ 1 Welcome to C++ 2 What Is a Program? 3 Your First C++ Program 4 Variables and Literals 5 Character Arrays and Strings 6 Preprocessor Directives 7 Simple Input/Output II Using C++ Operators 8 Using C++ Math Operators and Precedence 9 Relational Operators 10 Logical Operators 11 Additional C++ Operators III C++ Constructs 12 The while Loop 13 The for Loop 14 Other Loop Options 15 The switch and goto Statements 16 Writing C++ Functions IV Variable Scope and Modular Programming 17 Variable Scope 18 Passing Value 19 Function Return Values and Prototyping 20 Default Arguments and Function Overloading V Character Input/Output and String Functions 21 Device and Character Input/Output 22 Character, String, and Numeric Functions Contents x VI Arrays and Pointers 23 Introducing Arrays 24 Array Processing 25 Multidimensional Arrays 26 Pointers 27 Pointers and Arrays VII Structures and File Input/Output 28 Structures 29 Arrays of Structures 30 Sequential Files 31 Random-Access Files 32 Introduction to Object-Oriented Programming VIII References A Memory Addressing, Binary, and Hexadecimal Review B Answers to Review Questions C ASCII Table D C++ Precedence Table E Keyword and Function Reference F The Mailing List Application Glossary Index
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值