参考https://www.jianshu.com/p/682e80efbe5a
首先创建工作空间,创建工作空间
mkdir -p ~/test_ws/src
cd ~/test_ws
catkin_make
source devel/setup.bash
在~t/test_ws/src下创建功能包,创建功能包
catkin_create_pkg beginner_tutorials std_msgs roscpp rospy
在~/test_ws/src/beginner_tutorials中拷贝从/opt/ros/kinetic/share/turtlebot_stage中复制的maps文件夹
在~/test_ws/src/beginner_tutorials/src中创建sharemem.h文件,其中定义共享空间中的数据结构。文件内容为
#ifndef SHAREMEM_H
#define SHAREMEM_H
struct ShareMem
{
double x; // x方向的速度(前后)
double y; // y方向的速度(左右)
double z; // 旋转
};
#endif
新建talker节点。创建共享内存、监听共享内存区域的速度变化和发布速度话题。
/****************************************
*文件名: talker.cpp *
*简介: 链接非ROS进程和ROS节点的ROS节点 *
*BY: CR (qq: 414481619) *
*创建时间: 2018.10.09 *
****************************************/
#include "ros/ros.h"
#include
#include
#include
#include
#include
#include
#include
#include
#include "sharemem.h"
using namespace std;
int main(int argc, char **argv)
{
ros::init(argc, argv, "talker");
ros::NodeHandle n;
ros::Publisher chatter_pub = n.advertise("cmd_vel", 1000);
ros::Rate loop_rate(100); // 100Hz的定时器
ShareMem _ShareMem;
// 创建共享内存 ,相当于打开文件,文件不存在则创建
int shmid = shmget((key_t)2333, sizeof(_ShareMem), 0666|IPC_CREAT);
if(shmid==-1)
{
perror("shmget err");
return errno;
}
cout<x = 0;
shareMem->y = 0;
shareMem->z = 0;
pthread_mutex_t mutex;
pthread_mutex_init(&mutex, NULL);
while (ros::ok())
{
geometry_msgs::Twist cmdvel_msg;
pthread_mutex_lock(&mutex);
cmdvel_msg.linear.x = shareMem->x;
cmdvel_msg.linear.y = shareMem->y;
cmdvel_msg.angular.z = shareMem->z;
pthread_mutex_unlock(&mutex);
chatter_pub.publish(cmdvel_msg);
ros::spinOnce();
loop_rate.sleep();
}
pthread_mutex_destroy(&mutex);
shmdt(shareMem); //将共享内存段与当前进程脱离
shmctl(shmid, IPC_RMID, NULL); //IPC_RMID为删除内存段
return 0;
}
在beginner_tutorials 的 CMakeLists.txt 中添加:
include_directories(include ${catkin_INCLUDE_DIRS})
add_executable(talker src/talker.cpp)
target_link_libraries(talker ${catkin_LIBRARIES})
在终端中:
cd ~/test_ws
catkin_make
创建launch文件新建launch文件夹,创建stage_tast.launch文件。
创建速度控制父类,便于其他速度控制类的编写,其他速度控制类只需要继承父类的框架,完成自己的mainloop()就好。
/****************************************
*文件名: VelControl.h *
*简介: 速度控制父类 *
*BY: CR (qq: 414481619) *
*创建时间: 2018.10.09 *
****************************************/
// this code ism't in ros system
#include
#include
#include
#include
#include
#include
#include
#include
#include "sharemem.h"
using namespace std;
class VelControl
{
public:
VelControl();
~VelControl();
void mainloop();
protected:
ShareMem *shareMem;
pthread_mutex_t mutex;
void writeVel(double x, double y, double z);
};
VelControl::VelControl()
{
pthread_mutex_init(&mutex, NULL);
// 绑定共享内存
int shmid = shmget((key_t)2333, 0, 0);
if(shmid != -1)
{
shareMem =(ShareMem *)shmat(shmid, NULL, 0);
cout<x = x;
shareMem->y = y;
shareMem->z = z;
pthread_mutex_unlock(&mutex);
}
键盘控制例程
/****************************************
*文件名: keyboard_not_ros.cpp *
*简介: 速度控制例程 *
*BY: CR (qq: 414481619) *
*创建时间: 2018.10.09 *
****************************************/
// this code isn't in ros system
#include "VelControl.h"
using namespace std;
class keyboard_not_ros:VelControl
{
public:
void mainloop();
};
void keyboard_not_ros::mainloop()
{
char c = 'a';
while(c!='q')
{
cout<<"please input a letter"<
编译程序,在 ~/test_ws/src/beginner_tutorials/src/ 目录下,终端输入:
g++ -o keyboard_not_ros keyboard_not_ros.cpp
启动launch文件
roslaunch beginner_tutorials stage_test.launch
另一个终端中,~/test_ws/src/beginner_tutorials/src下输入
./keyboard_not_ros