Bye Bye Moore

PoCソルジャーな零細事業主が作業メモを残すブログ

ROSからM5stackにふれる その3:HelloWorldもといpublisher

実際のところ

#include <ros.h>
#include <std_msgs/String.h>
#include <M5Stack.h>


ros::NodeHandle  nh;

std_msgs::String str_msg;
ros::Publisher chatter("chatter", &str_msg);

char hello[10] = "hello ROS";

void setup()
{ 
  // initialize the M5Stack object
  M5.begin();
  M5.Power.begin();
  
  M5.Lcd.setCursor(10, 10);
  M5.Lcd.setTextColor(RED);
  M5.Lcd.setTextSize(1);
  M5.Lcd.printf("ROS Test!");

  nh.initNode();
  nh.advertise(chatter);
}

void loop()
{  
  str_msg.data = hello;
  chatter.publish( &str_msg );
  nh.spinOnce();
  delay(1000);
}

動作確認

まずはroscore

$ roscore

COM6に接続するので

$ rosrun rosserial_python serial_node.py /dev/ttyS6

実際購読すると……一秒ごとにメッセージがきます

$ rostopic echo chatter
data: "hello ROS"
---
data: "hello ROS"
---
...