http://qiita.com/Yggdra070202/items/51cbc9bb594a5ab6a8c2 を参考にインストール
環境
ubuntu 14.04LTS
ROS jade
Arduino NANO/w328
Install
$ sudo apt-get install arduino
$ sudo apt-get install ros-jade-rosserial ros-jade-rosserial-arduino
$ arduino
フォルダ作成のための起動なので、起動したら終了する。
$ cd ~/catkin_ws/src
$ git clone https://github.com/ros-drivers/rosserial.git
$ cd ..
$ catkin_make
$ catkin_make install
$ source install/setup.bash
Arduinoのライブラリインストール
$ cd ~/sketchbook/libraries
$ rosrun rosserial_arduino make_libraries.py .
ros_libというフォルダができる。
テスト
$ arduino
ファイル->スケッチの例->ros_lib->HelloWorld
マイコンとか諸々を設定して書き込み
※マイコンが違うとrosrun rosserial_python serial_node.py /dev/ttyUSB0
の時点で
Unable to sync with device; possible link problem or link software version mismatch such as hydro rosserial_python with groovy Arduino
というエラーが出た。。
$ roscore
(別端末で)$ rosrun rosserial_python serial_node.py /dev/ttyUSB0
(別端末で)$ rostopic echo /chater
Adafruit NeoPixel
D2,D3,D4,D5にNeoPixelを3個づつ付けたハードを考えます。
以下のスケッチで
/led_command トピックに文字列(RED,GREEN,BLUE,YELLOW,CYAN,PURPLE,WHITE)を送ることでLEDを表示させます。それ以外では消えるはずですが、OFFや0と言った文字列を送ると エラーInbound TCP/IP connection failed: field data must be of type str
が発生します。XXXとかなら大丈夫でした。
例
$ rostopic pub /led_command std_msgs/String "RED"
Arduino に https://github.com/adafruit/Adafruit_NeoPixel からライブラリを入れる必要があります。
# define max_led 128
# include <Adafruit_NeoPixel.h>
/*
* rosserial PubSub Example
* Prints "hello world!" and toggles led
*/
# include <ros.h>
# include <std_msgs/String.h>
# include <std_msgs/Empty.h>
ros::NodeHandle nh;
Adafruit_NeoPixel led1 = Adafruit_NeoPixel(3, 2, NEO_GRB + NEO_KHZ800);
Adafruit_NeoPixel led2 = Adafruit_NeoPixel(3, 3, NEO_GRB + NEO_KHZ800);
Adafruit_NeoPixel led3 = Adafruit_NeoPixel(3, 4, NEO_GRB + NEO_KHZ800);
Adafruit_NeoPixel led4 = Adafruit_NeoPixel(3, 5, NEO_GRB + NEO_KHZ800);
void messageCb( const std_msgs::String& msg){
const char RED[]="RED";
const char GREEN[]="GREEN";
const char BLUE[]="BLUE";
const char YELLOW[]="YELLOW";
const char CYAN[]="CYAN";
const char PURPLE[]="PURPLE";
const char WHITE[]="WHITE";
// digitalWrite(13, HIGH-digitalRead(13)); // blink the led
if(strcmp(msg.data,RED)==0){
for(int i=0;i<3;i++){
led1.setPixelColor(i,led1.Color(max_led,0,0));
led2.setPixelColor(i,led2.Color(max_led,0,0));
led3.setPixelColor(i,led3.Color(max_led,0,0));
led4.setPixelColor(i,led4.Color(max_led,0,0));
}
led1.show();
led2.show();
led3.show();
led4.show();
}
else if(strcmp(msg.data,GREEN)==0){
for(int i=0;i<3;i++){
led1.setPixelColor(i,led1.Color(0,max_led,0));
led2.setPixelColor(i,led2.Color(0,max_led,0));
led3.setPixelColor(i,led3.Color(0,max_led,0));
led4.setPixelColor(i,led4.Color(0,max_led,0));
}
led1.show();
led2.show();
led3.show();
led4.show();
}
else if(strcmp(msg.data,BLUE)==0){
for(int i=0;i<3;i++){
led1.setPixelColor(i,led1.Color(0,0,max_led));
led2.setPixelColor(i,led2.Color(0,0,max_led));
led3.setPixelColor(i,led3.Color(0,0,max_led));
led4.setPixelColor(i,led4.Color(0,0,max_led));
}
led1.show();
led2.show();
led3.show();
led4.show();
}
else if(strcmp(msg.data,YELLOW)==0){
for(int i=0;i<3;i++){
led1.setPixelColor(i,led1.Color(max_led,max_led,0));
led2.setPixelColor(i,led2.Color(max_led,max_led,0));
led3.setPixelColor(i,led3.Color(max_led,max_led,0));
led4.setPixelColor(i,led4.Color(max_led,max_led,0));
}
led1.show();
led2.show();
led3.show();
led4.show();
}
else if(strcmp(msg.data,CYAN)==0){
for(int i=0;i<3;i++){
led1.setPixelColor(i,led1.Color(0,max_led,max_led));
led2.setPixelColor(i,led2.Color(0,max_led,max_led));
led3.setPixelColor(i,led3.Color(0,max_led,max_led));
led4.setPixelColor(i,led4.Color(0,max_led,max_led));
}
led1.show();
led2.show();
led3.show();
led4.show();
}
else if(strcmp(msg.data,PURPLE)==0){
for(int i=0;i<3;i++){
led1.setPixelColor(i,led1.Color(max_led,0,max_led));
led2.setPixelColor(i,led2.Color(max_led,0,max_led));
led3.setPixelColor(i,led3.Color(max_led,0,max_led));
led4.setPixelColor(i,led4.Color(max_led,0,max_led));
}
led1.show();
led2.show();
led3.show();
led4.show();
}
else if(strcmp(msg.data,WHITE)==0){
for(int i=0;i<3;i++){
led1.setPixelColor(i,led1.Color(max_led,max_led,max_led));
led2.setPixelColor(i,led2.Color(max_led,max_led,max_led));
led3.setPixelColor(i,led3.Color(max_led,max_led,max_led));
led4.setPixelColor(i,led4.Color(max_led,max_led,max_led));
}
led1.show();
led2.show();
led3.show();
led4.show();
}
else {
for(int i=0;i<3;i++){
led1.setPixelColor(i,led1.Color(0,0,0));
led2.setPixelColor(i,led2.Color(0,0,0));
led3.setPixelColor(i,led3.Color(0,0,0));
led4.setPixelColor(i,led4.Color(0,0,0));
}
led1.show();
led2.show();
led3.show();
led4.show();
}
}
ros::Subscriber<std_msgs::String> sub("led_command", messageCb );
std_msgs::String str_msg;
ros::Publisher chatter("chatter", &str_msg);
char hello[13] = "hello world!";
void setup()
{
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(13, OUTPUT);
led1.begin();
led2.begin();
led3.begin();
led4.begin();
nh.initNode();
nh.advertise(chatter);
nh.subscribe(sub);
}
void loop()
{
str_msg.data = hello;
chatter.publish( &str_msg );
nh.spinOnce();
delay(500);
}