LoginSignup
5
2

More than 5 years have passed since last update.

openFrameWorksでROSを使う

Last updated at Posted at 2017-05-23

目的

openFrameWorksで作成したアプリケーションからROS通信を行う方法。
PCL_ROSをMakeFileでコンパイルするのやり方に等しい。

openframeworksとは

"「創造的なコーディング」のためのC++のオープンソースツールキット"
http://openframeworks.cc/ja/

強力なアドオンを組み込むことができる。
http://ofxaddons.com/popular

openframeworksのプロジェクト作成

projectGeneratorでプロジェクト作成

Screenshot from 2017-05-20 10-06-55.png

Makefileの編集

Makefileとconfig.makeを編集する。

Makefile

Makefileにopenframeworksのルートディレクトリを追記する。
絶対パスを指定するとどのフォルダでもコンパイルすることができるようになる。

ifneq ($(wildcard config.make),)
    include config.make
endif

ifndef OF_ROOT
    OF_ROOT=$(realpath /home/nnn112358/Documents/of_v0.9.8_linux64/)
endif

include $(OF_ROOT)/libs/openFrameworksCompiled/project/makefileCommon/compile.project.mk

config.make

config.makeにROSとのリンクを記載する。

PROJECT_LDFLAGS=-Wl,-rpath=./libs
PROJECT_LDFLAGS+=$(SUBLIBS) $(ros_libs_nocolon) 
ros_libs = $(shell pkg-config --libs roscpp nav_msgs tf)
ros_libs_nocolon = $(subst -l:,,$(ros_libs))
PROJECT_OPTIMIZATION_CFLAGS_DEBUG = `pkg-config --cflags roscpp nav_msgs tf` -w -O2 
PROJECT_OPTIMIZATION_CFLAGS_RELEASE = `pkg-config --cflags roscpp nav_msgs tf` -w -O2 

ソースの編集

ソースにROSとの通信処理を書く。
ros::initはmainの引数が必要なので、main.cppにros::initを記載し、
初期化処理はofApp::setup()
逐次処理はofApp::update()
に記載する。

main.cpp

#include "ofMain.h"
#include "ofApp.h"
#include <ros/ros.h>

int main(int argc, char *argv[]){
    ros::init(argc, argv, "ros_tutorial_msg_publisher");  // ROSノード名の初期化
   ofSetupOpenGL(1024,768,OF_WINDOW);           
    ofApp *app = new ofApp();
    ofRunApp(app);
}

ofApp.cpp

void ofApp::setup(){
    chatter_pub = n.advertise<std_msgs::String>("chatter", 1000);
}
void ofApp::update(){
    std_msgs::String msg;
    std::stringstream ss;
    ss << "hello world " << endl;
    msg.data = ss.str();

    ROS_INFO("%s", msg.data.c_str());
    chatter_pub.publish(msg);
    ros::spinOnce();
}

5
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
2