简单测试ROS里面C++ 和 python 文件获取参数格式
生活随笔
收集整理的這篇文章主要介紹了
简单测试ROS里面C++ 和 python 文件获取参数格式
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
測試的是從參數文件獲取參數
先看python 文件,test.py文件 從 test.yaml 參數文件獲取參數,
測試的變量為?base_frame? 和?base_frame1,分別獲取的同一個參數。代碼如下:
#test.pyimport rospy from geometry_msgs.msg import Twistclass ArduinoROS():def __init__(self):# 唯一的節點名 日志級別為DEBUG 等級排序為 DEBUG INFO WARN ERROR FATALrospy.init_node('Arduino', log_level=rospy.DEBUG)# self. 變量, 從 .yaml 獲取參數self.base_frame = rospy.get_param("~base_frame", 'base_link')self.base_frame1 = rospy.get_param("base_frame", 'base_link')rospy.on_shutdown(self.shutdown)while not rospy.is_shutdown():print "param : " + self.base_frame + " " + self.base_frame1rospy.sleep(1)def shutdown():print "shut down node..."if __name__ == '__main__':myArduino = ArduinoROS() # test.yamlbase_frame: base_footprint測試結果在最后,
然后再看C++文件,test1.cpp 文件從 test1.yaml 參數文件獲取參數。
測試變量是?port 和?cfg_baud_rate_ ,獲取不同的參數,代碼如下:
// test1.cppusing namespace std;int main(int argc, char **argv) {ros::init(argc, argv, "test1");ros::NodeHandle nh;serial::Serial ser;std::string port;int cfg_baud_rate_ = 48500;std::string imu_frame_id;nh.param<std::string>("/test1/port", port, "/dev/tty123");nh.param<int>("/test1/imu_baudrate", cfg_baud_rate_, 9600);nh.param<std::string>("/test1/imu_frame_id", imu_frame_id, "imu_base");ros::Rate r(1);while (ros::ok()){std::cout << "port : " << port << " ,rate : " << cfg_baud_rate_ << " ,frame : " << imu_frame_id << std::endl;r.sleep();}return 0; } # test1.yamlport: /dev/ttyIMU imu_baudrate: 115200 imu_freq: 100 angle_offset: 0.99測試結果如下截圖:
結論:
python 文件中,
rospy.get_param("~base_frame", "base_link") 會先獲取 test.yaml參數文件里面的參數
rospy.get_param("base_frame", "base_link")? 則會獲取當前代碼中的參數
?
C++文件中
nh.param<std::string>("/test1/port",?port,?"/dev/tty123"); 先獲取test1.yaml文件里面的參數
nh.param<int>("/test1/imu_baudrate",?cfg_baud_rate_,?9600); 先定義了參數,也會獲取test1.yaml文件里面的參數。
?
后續有更多測試,再持續更新,先這樣。。。
總結
以上是生活随笔為你收集整理的简单测试ROS里面C++ 和 python 文件获取参数格式的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: js实现图片移动
- 下一篇: 解决可以上QQ,但是无法上网的问题