ros开发增加clion常用模板
clion 初始化开发配置笔记针对ros
1. clion模板配置
模板数据在下面!
2.大小写匹配提示
3. 打开clion后不会进入上一次项目
2. c++ class 模板配置
#parse("C File Header.h")
#[[#ifndef]]# ${INCLUDE_GUARD}
#[[#define]]# ${INCLUDE_GUARD}
${NAMESPACES_OPEN}
class ${NAME} {
public:
${NAME}();
~${NAME}();
};
${NAMESPACES_CLOSE}
#[[#endif]]# //${INCLUDE_GUARD}
#parse("C File Header.h")
#[[#include]]# "${HEADER_FILENAME}"
${NAME}::${NAME}(){
}
${NAME}::~${NAME}(){
}
3.python ros环境配置
Clion的Python环境¶
为了演示第一个程序,首先我们按照前面的例子,在workspace中创建一个demo_py
的package,我们基于这个demo_py
进行讲解开发。
使用clion打开demo_py
。
1. 创建scripts目录¶
在demo_py
的目录中创建scripts
目录,用于写python代码。
2.配置Python环境¶
打开clion的setting
,来到Build,Execution,Deployment
的Python Interpreter下
,点击设置按键,点击添加。
打开python环境设置按钮,添加python环境
Tip
特别要注意**的是,目前ROS Melodic版本还**不支持python3,要等到ROS N版才会支持。
因此,我们选择环境的时候**选择python2.x版本**。
注意: 新建的python文件需要改权限才能运行 chmoe 777 *
-----------------------------------------------------------------------------------------------
模板数据
python 模板:
py_main
#!/usr/bin/env python
# coding:utf-8
import rospy
# 导入数据
from std_msgs.msg import String
if __name__ == '__main__':
# 节点名
nodeName = 'py_publisher'
# 初始化节点 定义匿名节点加参数 anonymous=True
rospy.init_node(nodeName,anonymous=True)
py_node
import rospy
# 节点名
nodeName = 'py_publisher'
# 初始化节点 定义匿名节点加参数 anonymous=True
rospy.init_node(nodeName,anonymous=True)
1.py_class_PyQt5
#!/usr/bin/env python
# coding:utf-8
from PyQt5.QtWidgets import QWidget,QFormLayout,QLineEdit,QPushButton,QLabel
from geometry_msgs.msg import Twist
from turtlesim.msg import Pose
import rospy
class MainWindow(QWidget):
def __init__(self,publisher):
super(MainWindow, self).__init__()
pass
2.py_main_PyQt5
#!/usr/bin/env python
# coding:utf-8
from PyQt5.QtWidgets import QApplication
from MainWindow import MainWindow
import sys
import rospy
if __name__ == '__main__':
# 创建节点
rospy.init_node("py_control")
# 创建Qt
app = QApplication(sys.argv)
# 创建窗口
w = MainWindow()
# 展示窗口
w.show()
# 等待程序结束
sys.exit(app.exec_())
3.1server
py_client_main
#!/usr/bin/env python
# coding:utf-8
import rospy
from roscpp_tutorials.srv import TwoInts,TwoIntsRequest
if __name__ == '__main__':
# 创建节点
rospy.init_node("py_client")
# service名字
serviceName = 'py_service'
# 创建客户端
proxy = rospy.ServiceProxy(serviceName, TwoInts)
# 等待service上线
proxy.wait_for_service()
# 请求数据
req = TwoIntsRequest()
req.a = 12
req.b = 13
# 调用service
result = proxy.call(req)
print result
py_client
import rospy
from roscpp_tutorials.srv import TwoInts,TwoIntsRequest
# service名字
serviceName = 'py_service'
# 创建客户端
proxy = rospy.ServiceProxy(serviceName, TwoInts)
# 等待service上线
proxy.wait_for_service()
# 请求数据
req = TwoIntsRequest()
req.a = 12
req.b = 13
# 调用service
result = proxy.call(req)
print result
3.1 py_server_main
#!/usr/bin/env python
# coding:utf-8
import rospy
from roscpp_tutorials.srv import TwoInts
def callBack(req):
return req.a+req.b
if __name__ == '__main__':
# 创建节点
rospy.init_node("py_server")
# service名字
serviceName = 'py_service'
# 创建server端
service = rospy.Service(serviceName, TwoInts, callBack)
# 阻塞
rospy.spin()
py_server
import rospy
from roscpp_tutorials.srv import TwoInts
def callBack(req):
return req.a+req.b
# 创建节点
rospy.init_node("py_server")
# service名字
serviceName = 'py_service'
# 创建server端
service = rospy.Service(serviceName, TwoInts, callBack)
# 阻塞
rospy.spin()
4. topic
py_publisher_main
#!/usr/bin/env python
# coding:utf-8
import rospy
# 导入数据
from std_msgs.msg import String
if __name__ == '__main__':
# 节点名
nodeName = 'py_publisher'
# 初始化节点
rospy.init_node(nodeName,anonymous=True)
# 话题名
topicName = 'py_topic'
# 创建发布者
publisher = rospy.Publisher(topicName, String, queue_size=5)
# 每一秒钟发送一条消息
rate = rospy.Rate(1)
i = 0
# 循环
while not rospy.is_shutdown():
# 消息内容
data = String()
data.data = 'hello {}'.format(i)
# 发送消息
publisher.publish(data)
# 增加数据
i += 1
rate.sleep()
py_publisher
import rospy
# 导入数据
from std_msgs.msg import String
# 话题名
topicName = 'py_topic'
# 创建发布者
publisher = rospy.Publisher(topicName, String, queue_size=5)
# 每一秒钟发送一条消息
rate = rospy.Rate(1)
i = 0
# 循环
while not rospy.is_shutdown():
# 消息内容
data = String()
data.data = 'hello {}'.format(i)
# 发送消息
publisher.publish(data)
# 增加数据
i += 1
rate.sleep()
py_subscriber_main
#!/usr/bin/env python
# coding:utf-8
import rospy
# from std_msgs.msg import String
from geometry_msgs.msg import Twist
import threading
# python订阅者回调在子线程中
def callBack(data):
print '接收的线程id:{}'.format(threading.current_thread().name)
print '收到数据:{}'.format(data.linear.x)
if __name__ == '__main__':
print '主线程线程id:{}'.format(threading.current_thread().name)
# 节点名
nodeName = 'py_subscriber'
# 初始化节点
rospy.init_node(nodeName,anonymous=True)
# topic名字
topicName = 'py_topic'
# 创建订阅者
# 参数1:topic名字
# 参数2:topic数据类型
# 参数3:回调函数
subscriber = rospy.Subscriber(topicName, Twist, callBack)
# 事件处理
rospy.spin()
py_subscriber
import rospy
# from std_msgs.msg import String
from geometry_msgs.msg import Twist
def callBack(data):
data.linear.x
print '收到数据:{}'.format(data.linear.x)
# topic名字
topicName = 'py_topic'
# 创建订阅者
# 参数1:topic名字
# 参数2:topic数据类型
# 参数3:回调函数
subscriber = rospy.Subscriber(topicName, Twist, callBack)
# 事件处理
rospy.spin()
c++ 模板:
cpp_main
#include <iostream>
#include <ros/ros.h>
using namespace std;
int main(int argc,char *argv[]){
//节点名
string nodeName = "$args1$";
//初始化节点
ros::init(argc,argv,nodeName);
//创建节点
ros::NodeHandle node;
$args2$
//事件轮询
ros::spin();
return 0;
}
cpp_node
//创建节点
ros::NodeHandle node;
1. cpp_class_source_Qt5
#include <QWidget>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QPushButton>
#include <QLineEdit>
#include <QCheckBox>
#include <ros/ros.h>
#include <std_srvs/Empty.h>
#include <turtlesim/Spawn.h>
#include <turtlesim/Kill.h>
#include <turtlesim/SetPen.h>
#include <turtlesim/TeleportAbsolute.h>
#include <turtlesim/TeleportRelative.h>
#include <iostream>
#include "MainWindow.h"
MainWindow::MainWindow(ros::NodeHandle node, QWidget *parent) : QWidget(parent), node(node)
{
}
MainWindow::~MainWindow() {
}
2. cpp_class_head_Qt5
#include <QWidget>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QPushButton>
#include <QLineEdit>
#include <QCheckBox>
#include <ros/ros.h>
#include <std_srvs/Empty.h>
#include <turtlesim/Spawn.h>
#include <turtlesim/Kill.h>
#include <turtlesim/SetPen.h>
#include <turtlesim/TeleportAbsolute.h>
#include <turtlesim/TeleportRelative.h>
#include <iostream>
using namespace std;
class MainWindow: public QWidget {
public:
explicit MainWindow(ros::NodeHandle node,QWidget* parent = Q_NULLPTR);
~MainWindow() override;
};
3.server
cpp_client_main
#include <iostream>
#include <ros/ros.h>
#include <roscpp_tutorials/TwoInts.h>
using namespace std;
int main(int argc,char *argv[]){
//节点名
string nodeName = "cpp_client";
//初始化节点
ros::init(argc,argv,nodeName,ros::init_options::AnonymousName);
//创建节点
ros::NodeHandle node;
//service名字
string serviceName = "cpp_service";
//创建client
ros::ServiceClient client = node.serviceClient<roscpp_tutorials::TwoInts>(serviceName);
//等待server端上线
client.waitForExistence();
//定义数据
roscpp_tutorials::TwoInts service;
service.request.a = 12;
service.request.b = 13;
//调用service
client.call(service);
ROS_INFO_STREAM("result:"<<service.response.sum);
//事件轮询
ros::spin();
return 0;
}
cpp_client
#include <iostream>
#include <ros/ros.h>
#include <roscpp_tutorials/TwoInts.h>
using namespace std;
//service名字
string serviceName = "cpp_service";
//创建client
ros::ServiceClient client = node.serviceClient<roscpp_tutorials::TwoInts>(serviceName);
//等待server端上线
client.waitForExistence();
//定义数据
roscpp_tutorials::TwoInts service;
service.request.a = 12;
service.request.b = 13;
//调用service
client.call(service);
ROS_INFO_STREAM("result:"<<service.response.sum);
//事件轮询
ros::spin();
3.1
cpp_server_main
#include <iostream>
#include <ros/ros.h>
#include <roscpp_tutorials/TwoInts.h>
using namespace std;
bool callBack(roscpp_tutorials::TwoIntsRequest& req,roscpp_tutorials::TwoIntsResponse& res){
//从请求中获取两个参数
//把和通过res返回出去
res.sum = req.a+req.b;
return true;
}
int main(int argc,char *argv[]){
//节点名
string nodeName = "cpp_server";
//初始化节点
ros::init(argc,argv,nodeName);
//创建节点
ros::NodeHandle node;
//service名字
string serviceName = "cpp_service";
//创建server端
const ros::ServiceServer &server = node.advertiseService(serviceName, callBack);
//事件轮询
ros::spin();
return 0;
}
cpp_server
#include <iostream>
#include <ros/ros.h>
#include <roscpp_tutorials/TwoInts.h>
using namespace std;
bool callBack(roscpp_tutorials::TwoIntsRequest& req,roscpp_tutorials::TwoIntsResponse& res){
//从请求中获取两个参数
//把和通过res返回出去
res.sum = req.a+req.b;
return true;
}
//service名字
string serviceName = "cpp_service";
//创建server端
const ros::ServiceServer &server = node.advertiseService(serviceName, callBack);
//事件轮询
ros::spin();
4 topic
cpp_publisher_main
#include <iostream>
#include <ros/ros.h>
#include <std_msgs/String.h>
using namespace std;
int main(int argc,char *argv[]){
//节点名
string nodeName = "cpp_publisher";
//初始化节点 匿名节点
ros::init(argc,argv,nodeName,ros::init_options::AnonymousName);
//创建节点
ros::NodeHandle node;
//节点名
string topicName = "cpp_topic";
//创建topic发送者
//参数1:节点名
//参数2:消息队列容量
const ros::Publisher &publisher = node.advertise<std_msgs::String>(topicName, 5);
//每隔一秒钟发送一条消息
ros::Rate rate(1);
int i = 0;
while (ros::ok()){
std_msgs::String data;
data.data = "hello "+to_string(i);
//发送一条消息
publisher.publish(data);
//增加i
i+=1;
//睡眠
rate.sleep();
}
return 0;
}
cpp_publishe
#include <iostream>
#include <ros/ros.h>
#include <std_msgs/String.h>
using namespace std;
//节点名
string topicName = "cpp_topic";
//创建topic发送者
//参数1:节点名
//参数2:消息队列容量
const ros::Publisher &publisher = node.advertise<std_msgs::String>(topicName, 5);
//每隔一秒钟发送一条消息
ros::Rate rate(1);
int i = 0;
while (ros::ok()){
std_msgs::String data;
data.data = "hello "+to_string(i);
//发送一条消息
publisher.publish(data);
//增加i
i+=1;
//睡眠
rate.sleep();
}
cpp_subscriber_main
#include <iostream>
#include <std_msgs/String.h>
#include <ros/ros.h>
#include <thread>
using namespace std;
//c++ 订阅者的回调在主线程中
void callBack(std_msgs::String::ConstPtr data){
cout<<"回调:"<<this_thread::get_id()<<endl;
cout<<"收到消息:"<<data->data<<endl;
}
int main(int argc, char *argv[]) {
cout<<"主线程:"<<this_thread::get_id()<<endl;
//节点名
string nodeName = "cpp_subscriber";
// string nodeName = "cpp_publisher";
//初始化节点
ros::init(argc, argv, nodeName,ros::init_options::AnonymousName);
//创建节点
ros::NodeHandle node;
//topic名字
string topicName = "cpp_topic";
//订阅者
//参数1:topic名字
//参数2:队列长度
//参数3:回调函数
//注意:返回的Subscriber必须要接收,如果不接受 可能收不到消息
const ros::Subscriber &subscriber = node.subscribe<std_msgs::String>(topicName, 5, callBack);
//ros::spin 处理事件
ros::spin();
return 0;
}
cpp_subscriber
#include <iostream>
#include <std_msgs/String.h>
#include <ros/ros.h>
#include <thread>
using namespace std;
//c++ 订阅者的回调在主线程中
void callBack(std_msgs::String::ConstPtr data){
cout<<"回调:"<<this_thread::get_id()<<endl;
cout<<"收到消息:"<<data->data<<endl;
}
//topic名字
string topicName = "cpp_topic";
//订阅者
//参数1:topic名字
//参数2:队列长度
//参数3:回调函数
//注意:返回的Subscriber必须要接收,如果不接受 可能收不到消息
const ros::Subscriber &subscriber = node.subscribe<std_msgs::String>(topicName, 5, callBack);
ros::spin();
上一篇: IOS自动化-appium环境搭建
下一篇: KONG网关 — 插件开发