/*
* rosserial Publisher Example
* Prints "hello world!"
*/
// Use the following line if you have a Leonardo or MKR1000
//#define USE_USBCON
#include <ros.h>
#include <std_msgs/String.h>
ros::NodeHandle nh;
std_msgs::String str_msg;
ros::Publisher chatter("chatter", &str_msg);
char hello[13] = "hello world!";
void setup()
{
nh.initNode();
nh.advertise(chatter);
}
void loop()
{
str_msg.data = hello;
chatter.publish( &str_msg );
nh.spinOnce();
delay(1000);
}
อัพโหลดโค้ดเข้าสู่ Arduino (ผมใช้ arduino uno r3)
2.นำสาย USB Arduino ไปเสียบที่ port USB ของ Workstation(Raspberry pi 3)
3.จากนั้น เปิด Terminal ใหม่แล้วใช้คำสั่ง
roscore
เปิด Terminal ใหม่แล้วใช้คำสั่ง
rosrun rosserial_python serial_node.py /dev/ttyACM0*** หากเกิด Error -> could not open port /dev/ttyACM0: [Errno 13] Permission denied: '/dev/ttyACM0'
แก้ไขด้วยการเพิ่ม username เข้าไปใน dailout group
sudo adduser <username> dialout*** <username> ของผมคือ Pi
จากนั้นให้ Restart Workstation(Raspberry pi 3) แล้วเรียกคำสั้่ง
rosrun rosserial_python serial_node.py /dev/ttyACM0อีกครั้ง จากนั้นเปิด Terminal ใหม่แล้วใช้คำสั่ง
rostopic echo chatter

No comments:
Post a Comment