【V-REP学习记录03】Interacting with V-REP using ROS services

【V-REP学习记录01】Setting up V-REP with ROS

【V-REP学习记录02】Understanding the vrep_plugin

作为第一个例子,我们将使用ROS服务来启动和停止模拟场景。要做到这一点,我们必须呼吁

/vrep/simRosStartSimulation和/vrep/simRosStopSimulation服务。我们将

讨论vrep_demo_pkg/src目录下的start_stop_scene.cpp文件的源代码:(本人已添加注释)

//这段代码的逻辑非常简单,它使用ROS服务通信机制来调用Vrep仿真器提供的启动和停止仿真服务。首先,它创建了用于启动和停止仿真的服务客户端,并初始化了对应的服务请求消息。然后,它调用启动仿真的服务,并检查返回结果是否为成功。如果启动服务成功,它会打印一条消息,然后使用sleep(5)函数休眠5秒钟。之后,它调用停止仿真的服务,并再次检查返回结果。如果停止服务也成功,它会打印一条停止仿真的消息。如果启动或停止服务失败,它会打印相应的错误消息。

//请注意,为了使这段代码能正常运行,需要包含所使用的ROS消息类型的头文件,并且需要在CMakeLists.txt文件中链接相关的库。此外,还需要安装Vrep仿真器并启动它,确保仿真器提供了相应的服务。

#include "ros/ros.h"
#include "vrep_common/simRosStartSimulation.h"
#include "vrep_common/simRosStopSimulation.h"

int main( int argc, char** argv ) {

	// 初始化ROS节点
	ros::init( argc, argv, "start_stop_vrep_node");
	ros::NodeHandle n;

	// 创建用于启动Vrep仿真的服务客户端
  	ros::ServiceClient start_vrep_client = n.serviceClient<vrep_common::simRosStartSimulation>("/vrep/simRosStartSimulation");
	vrep_common::simRosStartSimulation start_srv;

	// 创建用于停止Vrep仿真的服务客户端
  	ros::ServiceClient stop_vrep_client = n.serviceClient<vrep_common::simRosStopSimulation>("/vrep/simRosStopSimulation");
	vrep_common::simRosStopSimulation stop_srv;

	ROS_INFO("Starting Vrep simulation...");

	// 调用启动Vrep仿真的服务
	if( start_vrep_client.call( start_srv ) ) {

		// 如果启动服务成功
		if( start_srv.response.result != -1 ) {
				ROS_INFO("Simulation started, wait 5 seconds before stop it!");
				sleep(5);// 休眠5秒

				// 调用停止Vrep仿真的服务
				if( stop_vrep_client.call( stop_srv ) ) {

					// 如果停止服务成功
					if( stop_srv.response.result != -1 ) {
						ROS_INFO("Simulation stopped");	
					}
				}
				else
					ROS_ERROR("Failed to call /vrep/simRosStopSimulation service");
		}

	}
	// 如果启动服务失败
	else
		ROS_ERROR("Failed to call /vrep/simRosStartSimulation service");

	
}

讨论vrep_demo_pkg/src目录下的start_stop_scene_with_msg.cpp文件的源代码:(本人已添加注释)

//这段代码的主要目的是启动Vrep仿真并在启动后等待5秒钟后停止仿真。它使用ROS服务通信机制来调用Vrep仿真器提供的启动和停止仿真服务,以及在Vrep仿真器的状态栏中显示消息的服务。

//请注意,为了使这段代码能正常运行,需要包含所使用的ROS消息类型的头文件,并且需要在CMakeLists.txt文件中链接相关的库。此外,还需要安装Vrep仿真器并启动它,确保仿真器提供了相应的服务。

#include "ros/ros.h"
#include "vrep_common/simRosStartSimulation.h"
#include "vrep_common/simRosStopSimulation.h"
#include "vrep_common/simRosAddStatusbarMessage.h"

int main( int argc, char** argv ) {

	// 初始化ROS节点
	ros::init( argc, argv, "start_stop_vrep_node");
	ros::NodeHandle n;

        // 创建用于启动Vrep仿真的服务客户端
  	ros::ServiceClient start_vrep_client = n.serviceClient<vrep_common::simRosStartSimulation>("/vrep/simRosStartSimulation");
	vrep_common::simRosStartSimulation start_srv;

 	// 创建用于停止Vrep仿真的服务客户端
  	ros::ServiceClient stop_vrep_client = n.serviceClient<vrep_common::simRosStopSimulation>("/vrep/simRosStopSimulation");
	vrep_common::simRosStopSimulation stop_srv;

	// 创建用于在状态栏中显示消息的服务客户端
	ros::ServiceClient msg_client = n.serviceClient<vrep_common::simRosAddStatusbarMessage>("/vrep/simRosAddStatusbarMessage");
	vrep_common::simRosAddStatusbarMessage msg_srv;

	ROS_INFO("Starting Vrep simulation...");

 	// 调用启动Vrep仿真的服务
	if( start_vrep_client.call( start_srv ) ) {
	// 如果启动服务成功
		if( start_srv.response.result != -1 ) {
				ROS_INFO("Simulation started, wait 5 seconds before stop it!");

				int cnt = 0;
				while( cnt++ < 5 ) {
				// 构建显示在状态栏中的消息
					//std::cout << "Simulation while stop in " << 6-cnt << " seconds" << std::endl;
					std::stringstream ss;
					ss << "Simulation while stop in " << 6-cnt << " seconds";
					msg_srv.request.message = ss.str();

					// 调用在状态栏中显示消息的服务
					if( !msg_client.call( msg_srv ) ) {
						ROS_WARN("Failed to call /vrep/simRosAddStatusbarMessage service"); 					
					}
					
					sleep(1);	// 休眠1秒
				}

 				// 调用停止Vrep仿真的服务
				if( stop_vrep_client.call( stop_srv ) ) {
					// 如果停止服务成功
					if( stop_srv.response.result != -1 ) {
						ROS_INFO("Simulation stopped");	
					}
				}
				else
					ROS_ERROR("Failed to call /vrep/simRosStopSimulation service");
		}

	}
	// 如果启动服务失败
	else
		ROS_ERROR("Failed to call /vrep/simRosStartSimulation service");

	
}

加入功能包后重新编译,运行: 

rosrun vrep_demo_pkg start_stop_scene_with_msg

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值