ROS with V-rep (2) (ROS学习笔记五)

        昨晚完成了将ROS与V-rep的桥接,虽然没有读出有效消息,但已经从rostopic中看到了相应的ropic出现,今天经过简单的调试,果然可以实现对V-rep中的消息在ROS中的接收与发送。


一、在V-rep中创建ROS话题

接着昨天的工作,仔细研究LUA中的函数参数后修改昨天的LUA代码中的那三行如下:

sensorPub = simExtROS_enablePublisher('/frontUS',1000,simros_strmcmd_read_proximity_sensor,hFrontUS,-1,'std_msgs/Float64')

rightPub = simExtROS_enableSubscriber('/right_cmd',1000,simros_strmcmd_set_joint_target_velocity,hRightMotor,-1,'std_msgs/Float64')

leftPub = simExtROS_enableSubscriber('/left_cmd',1000,simros_strmcmd_set_joint_target_velocity,hRightMotor,-1,'std_msgs/Float64')


以上三个函数其实就是整个工程搭桥的核心部分了,这三个函数不论是从函数名还是参数上都比较难搞,经过一番研究我理解的如下:

sensorPub等号前面的其实只是一个在Vrep中的变量,随便命名不影响在ROS中的调用。

三个函数的第一个参数即为需要发送/接收的话题名称,如果没有这个话题,进程便会在ROS中建立一个话题,并向其中发送/接收消息,

第二个参数为消息队列,在这里如果消息频率没有问题则问题不大。

第三个参数最为重要,意味着V-rep以什么消息类型去接收/发送消息,这里其实需要的是一个number型参数,但V-rep中已经定义好了一些变量,如“simros_strmcmd_set_joint_target_velocity”就是一个stdmsg::Float64 型参数,于是他创建的话题中需要去接收这样一个消息,来控制轮子的转动。

第四个参数决定了将接收到的消息传送给V-rep中的哪个部件,这个参数就是之前从V-rep中get到的Handle,这样就可以将目标handle 与ROS话题链接起来

第五个参数参考上篇中的最后一个参考资料,如果不需要则为-1。

第六个参数目前我还没有搞得很清楚,似乎是一个字符串就可以正常运行,不知道具体用在哪里,但不影响运行。


二、在ROS中读取消息

有了(一)中的工作,我们其实topic中已经有传感器的输出与等待输入的topic了。

我们固然可以直接rostopic pub来控制两轮转速,但对proximity 的读取则无法直接echo出来,因为我们并没有定义这个消息类型,这是需要找到Vrep_common/Pro...Data.msg,将其复制到我们自己工作区中创建的msg文件夹中,然后修改CMakeList与package文件,具体可以参考以下内容:


为了更好的控制,我写了一个myrobot_control.cpp来控制小车转动,代码如下:

#include<ros/ros.h>
#include<string>
#include "std_msgs/Float64.h"
#include "geometry_msgs/Twist.h"

using namespace std;

int main(int argc, char** argv){
	ros::init(argc, argv, "controller");
	ros::NodeHandle n;
	
	ros::Publisher leftwheel_pub = n.advertise<std_msgs::Float64>("left_vel",1000);
	ros::Publisher rightwheel_pub = n.advertise<std_msgs::Float64>("right_vel",1000);

	ros::Rate loop_rate(1);

	int count = 0;
	std_msgs::Float64 left_velocity,right_velocity;	
	left_velocity.data = right_velocity.data = 0;
	
	while(ros::ok()){
	   	count++;
		if(count >= 5) count = 0;
		left_velocity.data = count;
		right_velocity.data = -count;
		
		leftwheel_pub.publish(left_velocity);
		rightwheel_pub.publish(right_velocity);

		ros::spinOnce();
		loop_rate.sleep();
	}
	return 0;
}

同时配置CMakeList如下:

cmake_minimum_required(VERSION 2.8.3)
project(vrep_common)

## Add support for C++11, supported in ROS Kinetic and newer
# add_definitions(-std=c++11)

## Find catkin macros and libraries
## if COMPONENTS list like find_package(catkin REQUIRED COMPONENTS xyz)
## is used, also find other catkin packages
find_package(catkin REQUIRED COMPONENTS roscpp rospy std_msgs geometry_msgs message_generation)

## System dependencies are found with CMake's conventions
# find_package(Boost REQUIRED COMPONENTS system)


## Uncomment this if the package has a setup.py. This macro ensures
## modules and global scripts declared therein get installed
## See http://ros.org/doc/api/catkin/html/user_guide/setup_dot_py.html
# catkin_python_setup()

################################################
## Declare ROS messages, services and actions ##
################################################

## To declare and build messages, services or actions from within this
## package, follow these steps:
## * Let MSG_DEP_SET be the set of packages whose message types you use in
##   your messages/services/actions (e.g. std_msgs, actionlib_msgs, ...).
## * In the file package.xml:
##   * add a build_depend tag for "message_generation"
##   * add a build_depend and a run_depend tag for each package in MSG_DEP_SET
##   * If MSG_DEP_SET isn't empty the following dependency has been pulled in
##     but can be declared for certainty nonetheless:
##     * add a run_depend tag for "message_runtime"
## * In this file (CMakeLists.txt):
##   * add "message_generation" and every package in MSG_DEP_SET to
##     find_package(catkin REQUIRED COMPONENTS ...)
##   * add "message_runtime" and every package in MSG_DEP_SET to
##     catkin_package(CATKIN_DEPENDS ...)
##   * uncomment the add_*_files sections below as needed
##     and list every .msg/.srv/.action file to be processed
##   * uncomment the generate_messages entry below
##   * add every package in MSG_DEP_SET to generate_messages(DEPENDENCIES ...)

## Generate messages in the 'msg' folder
 add_message_files(
   FILES
   ProximitySensorData.msg
#   Message1.msg
#   Message2.msg
 )

## Generate services in the 'srv' folder
# add_service_files(
#   FILES
#   Service1.srv
#   Service2.srv
# )

## Generate actions in the 'action' folder
# add_action_files(
#   FILES
#   Action1.action
#   Action2.action
# )

## Generate added messages and services with any dependencies listed here
 generate_messages(
   DEPENDENCIES
   std_msgs  # Or other packages containing msgs
   geometry_msgs
 )

################################################
## Declare ROS dynamic reconfigure parameters ##
################################################

## To declare and build dynamic reconfigure parameters within this
## package, follow these steps:
## * In the file package.xml:
##   * add a build_depend and a run_depend tag for "dynamic_reconfigure"
## * In this file (CMakeLists.txt):
##   * add "dynamic_reconfigure" to
##     find_package(catkin REQUIRED COMPONENTS ...)
##   * uncomment the "generate_dynamic_reconfigure_options" section below
##     and list every .cfg file to be processed

## Generate dynamic reconfigure parameters in the 'cfg' folder
# generate_dynamic_reconfigure_options(
#   cfg/DynReconf1.cfg
#   cfg/DynReconf2.cfg
# )

###################################
## catkin specific configuration ##
###################################
## The catkin_package macro generates cmake config files for your package
## Declare things to be passed to dependent projects
## INCLUDE_DIRS: uncomment this if you package contains header files
## LIBRARIES: libraries you create in this project that dependent projects also need
## CATKIN_DEPENDS: catkin_packages dependent projects also need
## DEPENDS: system dependencies of this project that dependent projects also need
#catkin_package(
#  INCLUDE_DIRS include
#  LIBRARIES vrep_common
#  CATKIN_DEPENDS other_catkin_pkg
#  DEPENDS system_lib
#)
catkin_package(
  LIBRARIES vrep_common
  CATKIN_DEPENDS message_runtime roscpp rospy std_msgs geometry_msgs 
  DEPENDS system_lib
  )
###########
## Build ##
###########

## Specify additional locations of header files
## Your package locations should be listed before other locations
# include_directories(include)

## Declare a C++ library
# add_library(${PROJECT_NAME}
#   src/${PROJECT_NAME}/vrep_common.cpp
# )

## Add cmake target dependencies of the library
## as an example, code may need to be generated before libraries
## either from message generation or dynamic reconfigure
# add_dependencies(${PROJECT_NAME} ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})

## Declare a C++ executable
## With catkin_make all packages are built within a single CMake context
## The recommended prefix ensures that target names across packages don't collide
# add_executable(${PROJECT_NAME}_node src/vrep_common_node.cpp)

## Rename C++ executable without prefix
## The above recommended prefix causes long target names, the following renames the
## target back to the shorter version for ease of user use
## e.g. "rosrun someones_pkg node" instead of "rosrun someones_pkg someones_pkg_node"
# set_target_properties(${PROJECT_NAME}_node PROPERTIES OUTPUT_NAME node PREFIX "")

## Add cmake target dependencies of the executable
## same as for the library above
# add_dependencies(${PROJECT_NAME}_node ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})

## Specify libraries to link a library or executable target against
# target_link_libraries(${PROJECT_NAME}_node
#   ${catkin_LIBRARIES}
# )

#############
## Install ##
#############

# all install targets should use catkin DESTINATION variables
# See http://ros.org/doc/api/catkin/html/adv_user_guide/variables.html

## Mark executable scripts (Python etc.) for installation
## in contrast to setup.py, you can choose the destination
# install(PROGRAMS
#   scripts/my_python_script
#   DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
# )

## Mark executables and/or libraries for installation
# install(TARGETS ${PROJECT_NAME} ${PROJECT_NAME}_node
#   ARCHIVE DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
#   LIBRARY DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
#   RUNTIME DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
# )

## Mark cpp header files for installation
# install(DIRECTORY include/${PROJECT_NAME}/
#   DESTINATION ${CATKIN_PACKAGE_INCLUDE_DESTINATION}
#   FILES_MATCHING PATTERN "*.h"
#   PATTERN ".svn" EXCLUDE
# )

## Mark other files for installation (e.g. launch and bag files, etc.)
# install(FILES
#   # myfile1
#   # myfile2
#   DESTINATION ${CATKIN_PACKAGE_SHARE_DESTINATION}
# )

#############
## Testing ##
#############

## Add gtest based cpp test target and link libraries
# catkin_add_gtest(${PROJECT_NAME}-test test/test_vrep_common.cpp)
# if(TARGET ${PROJECT_NAME}-test)
#   target_link_libraries(${PROJECT_NAME}-test ${PROJECT_NAME})
# endif()

## Add folders to be run by python nosetests
# catkin_add_nosetests(test)
include_directories (include ${catkin_INCLUDE_DIRS})
add_executable(vrep_common myrobot_control.cpp)
target_link_libraries(vrep_common ${catkin_LIBRARIES})

package如下:

<?xml version="1.0"?>
<package>
  <name>vrep_common</name>
  <version>0.0.0</version>
  <description>The vrep_common package</description>

  <!-- One maintainer tag required, multiple allowed, one person per tag --> 
  <!-- Example:  -->
  <!-- <maintainer email="jane.doe@example.com">Jane Doe</maintainer> -->
  <maintainer email="jkwang@todo.todo">jkwang</maintainer>


  <!-- One license tag required, multiple allowed, one license per tag -->
  <!-- Commonly used license strings: -->
  <!--   BSD, MIT, Boost Software License, GPLv2, GPLv3, LGPLv2.1, LGPLv3 -->
  <license>TODO</license>


  <!-- Url tags are optional, but multiple are allowed, one per tag -->
  <!-- Optional attribute type can be: website, bugtracker, or repository -->
  <!-- Example: -->
  <!-- <url type="website">http://wiki.ros.org/vrep_common</url> -->


  <!-- Author tags are optional, multiple are allowed, one per tag -->
  <!-- Authors do not have to be maintainers, but could be -->
  <!-- Example: -->
  <!-- <author email="jane.doe@example.com">Jane Doe</author> -->


  <!-- The *_depend tags are used to specify dependencies -->
  <!-- Dependencies can be catkin packages or system dependencies -->
  <!-- Examples: -->
  <!-- Use build_depend for packages you need at compile time: -->
  <!--   <build_depend>message_generation</build_depend> -->
  <!-- Use buildtool_depend for build tool packages: -->
  <!--   <buildtool_depend>catkin</buildtool_depend> -->
  <!-- Use run_depend for packages you need at runtime: -->
  <!--   <run_depend>message_runtime</run_depend> -->
  <!-- Use test_depend for packages you need only for testing: -->
  <!--   <test_depend>gtest</test_depend> -->
  <buildtool_depend>catkin</buildtool_depend>
  <build_depend>message_generation</build_depend>
  <build_depend>roscpp</build_depend>
  <build_depend>rospy</build_depend>
  <build_depend>std_msgs</build_depend>
  <build_depend>geometry_msgs</build_depend>

  <run_depend>message_runtime</run_depend>
  <run_depend>roscpp</run_depend>
  <run_depend>rospy</run_depend>
  <run_depend>std_msgs</run_depend>
  <run_depend>geometry_msgs</run_depend>  
	<!-- The export tag contains other, unspecified, tags -->
  <export>
    <!-- Other tools can request additional information be placed here -->

  </export>
</package>

至此应该可以echo出传感器的信息,并且实现控制小车逐渐变快的转动了。

注:为了保证V-rep对消息类型的要求得到满足,建议将rospkg的名字改为vrep_common ,省去了在V-rep中进一步修改其他地方的烦恼。


  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值