c++和python实现ros中的helloworld

该文介绍了使用ROS(机器人操作系统)创建C++和PythonHelloWorld程序的步骤,包括创建工作空间、功能包、编写代码、修改CMakeLists.txt以及编译和运行。同时,还提到了使用terminator终端工具进行多命令管理。

一、cpp

1.新建文件

mkdir -p catkin_ws/src
cd catkin_ws
catkin_make -DPYTHON_EXECUTABLE=usr/bin/python3

2.建立功能包

cd src
catkin_create_pkg helloworld roscpp rospy std_msgs
cd helloworld/src

3.源码与编译

sudo gedit hello.cpp

写入:

#include"ros/ros.h"
int main(int argc ,char *argv[ ] )
{  ros::init(argc,argv,"haha");
   ROS_INFO("hello world")
   return o;
}

编译前修改:CMakelist

包的映射:用helloworld映射hello.cpp
lib映射也是一样

add_executable(helloworld src/hello.cpp)
target_link_libraries(helloworld
   ${catkin_LIBRARIES}
 )

返回去编译:
catkin_make

4.结果:
roscore
rosrun helloworld helloworld
在这里插入图片描述

二、py

py版本基本操作相差不大,只是细节部分的处理:

mkdir -p catkin_ws/src
cd catkin_ws
catkin_make -DPYTHON_EXECUTABLE=usr/bin/python3
cd src
catkin_create_pkg helloworld roscpp rospy std_msgs

此时不再是进入,而是创建和src同级的scripts

mkdir -p helloworls/scripts

然后写hello.py:

import rospy

if __name__ == "__main__":
    rospy.init_node("Hello")
    rospy.loginfo("Hello World from python!")

再去修改CMakelists
c++的后面注释:

catkin_install_python(PROGRAMS
   scripts/hello.py
   DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
 )

以上的只是改了scripts/hello.py
然后正常编译执行
结果:
在这里插入图片描述
写到这里,我发现一个很不错的终端方案:

sudo apt-get install terminator

安装好后就能进行分屏了,十分适合运行多命令
再修改一下终端配置:

 sudo gedit ~/.config/terminator/config
 [global_config]
  geometry_hinting = False
  handle_size = 1
  inactive_color_offset = 1.0
  title_font = mry_KacstQurn Bold 11
  title_hide_sizetext = True
[keybindings]
[layouts]
  [[default]]
    [[[child1]]]
      parent = window0
      profile = default
      type = Terminal
    [[[window0]]]
      parent = ""
      type = Window
[plugins]
[profiles]
  [[default]]
    background_darkness = 0.76
    background_image = None
    background_type = transparent
    cursor_color = "#3036ec"
    custom_command = tmux
    font = Ubuntu Mono 13
    foreground_color = "#ffffff"
    login_shell = True
    show_titlebar = False
    use_system_font = False
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

白云千载尽

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值