【V-REP Learning Record 03】Interacting with V-REP using ROS services

【V-REP Learning Record 01】Setting up V-REP with ROS

【V-REP Learning Record 02】Understanding the vrep_plugin

As a first example, we will use a ROS service to start and stop simulation scenarios. To do this we must call

/vrep/simRosStartSimulation and /vrep/simRosStopSimulation services. we will

Discuss the source code of the start_stop_scene.cpp file in the vrep_demo_pkg/src directory: (I have added comments)

//这段代码的逻辑非常简单,它使用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");

	
}

Discuss the source code of the start_stop_scene_with_msg.cpp file in the vrep_demo_pkg/src directory: (I have added comments)

//这段代码的主要目的是启动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");

	
}

Recompile after adding the function package and run: 

rosrun vrep_demo_pkg start_stop_scene_with_msg

 

Guess you like

Origin blog.csdn.net/cz_include/article/details/131386812