{"id":90,"date":"2017-04-28T14:25:40","date_gmt":"2017-04-28T18:25:40","guid":{"rendered":"http:\/\/commons.trincoll.edu\/trinityrobotics\/?p=90"},"modified":"2017-04-28T14:25:40","modified_gmt":"2017-04-28T18:25:40","slug":"ros-arduino-serial","status":"publish","type":"post","link":"https:\/\/commons.trincoll.edu\/trinityrobotics\/ros-arduino-serial\/","title":{"rendered":"ROS Arduino Serial"},"content":{"rendered":"<p>Please refer to this <a href=\"http:\/\/howtomechatronics.com\/tutorials\/arduino\/ultrasonic-sensor-hc-sr04\/\">tutorial<\/a> of the ultrasound sensor HC-SR04.<\/p>\n<p>Ultrasound sensors measures distance by sending sound waves against objects in front of it and receiving the echos (so it doesn&#8217;t work well against objects that absorb sound, e.g. furs).<\/p>\n<p>This code runs in the Arduino, and communicates the message serially through the USB cable to the roscore on the laptop.<\/p>\n<pre>#define trigPin 12\r\n#define echoPin 13\r\n\r\n<strong>\/\/ 12 and 13 refers to the pin numbers on Arduino\r\n\/\/ You can choose to use any pins on the Arduino for digital \r\n\/\/ signal read and write (which sensor uses) <\/strong>\r\n\r\n#include &lt;ros.h&gt;\r\n#include &lt;std_msgs\/Int16.h&gt;\r\n\r\nros::NodeHandle nh;\r\n\r\nstd_msgs::Int16 int_msg;\r\nros::Publisher arduino(\"distance\", &amp;int_msg);\r\n\r\n<strong>\/\/ Standard ROS node intialization<\/strong>\r\n\r\nvoid setup() { <strong>\/\/ Setup code runs only once<\/strong>\r\n\u00a0 \/\/Serial.begin (9600);  <strong>\r\n  \/\/ You can choose to see output on a serial monitor\r\n  \/\/ on the computer \r\n  \/\/ But from what I found, Arduino can't seem to maintain\r\n  \/\/ both <\/strong><strong>serial communication to ROS and serial monitor<\/strong>\r\n\u00a0 pinMode(trigPin, OUTPUT); <strong>\/\/ marking pins to be read and write<\/strong> \r\n\u00a0 pinMode(echoPin, INPUT); \r\n\u00a0 nh.initNode();\r\n\u00a0 nh.advertise(arduino); \r\n}<\/pre>\n<pre>void loop() { <strong>\/\/ This part of the code runs repeatedly<\/strong>\r\n\u00a0 long duration;\r\n\u00a0 int distance;\r\n\r\n\u00a0 digitalWrite(trigPin, LOW); <strong>\/\/<\/strong> <strong>This is just setup code <\/strong>\r\n\u00a0 delayMicroseconds(2);       <strong>\/\/ So that ultrasound sensor would<\/strong>\r\n\u00a0 digitalWrite(trigPin, HIGH);<strong>\/\/ start reading<\/strong>\r\n\u00a0 delayMicroseconds(10);\r\n\u00a0 digitalWrite(trigPin, LOW); \r\n\r\n\u00a0 duration = pulseIn(echoPin, HIGH); <strong>\r\n  \/\/ The data coming in from ultrasound sensor is just \r\n  \/\/ digital data (0, 1), but it signifies distance data \r\n  \/\/ (0 ~ 200) by sending \"pulses\" of certain length \r\n<\/strong>\r\n\u00a0 distance = (duration\/2) \/ 29.1;\r\n\u00a0 int_msg.data = distance;\r\n\r\n\u00a0 if (distance &gt;= 2000 ) {\r\n\u00a0\u00a0\u00a0 \/\/Serial.println(\"Out of range\");\r\n\u00a0 }\r\n\u00a0 else {\r\n\u00a0\u00a0\u00a0 \/\/Serial.print(distance);\r\n\u00a0\u00a0\u00a0 \/\/Serial.println(\" cm\");\r\n\u00a0\u00a0\u00a0 arduino.publish(&amp;int_msg);\r\n\u00a0\u00a0\u00a0 nh.spinOnce();\r\n    <strong>\/\/ Standard publish routine for ROS<\/strong>\r\n\u00a0 }\r\n\u00a0 delay(500); <strong>\/\/ Wait 500 ms before running again<\/strong>\r\n}\r\n~<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Please refer to this tutorial of the ultrasound sensor HC-SR04. Ultrasound sensors measures distance by sending sound waves against objects in front of it and receiving the echos (so it doesn&#8217;t work well against objects that absorb sound, e.g. furs). This code runs in the Arduino, and communicates the message serially through the USB cable &hellip; <a href=\"https:\/\/commons.trincoll.edu\/trinityrobotics\/ros-arduino-serial\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;ROS Arduino Serial&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1933,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/commons.trincoll.edu\/trinityrobotics\/wp-json\/wp\/v2\/posts\/90"}],"collection":[{"href":"https:\/\/commons.trincoll.edu\/trinityrobotics\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/commons.trincoll.edu\/trinityrobotics\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/commons.trincoll.edu\/trinityrobotics\/wp-json\/wp\/v2\/users\/1933"}],"replies":[{"embeddable":true,"href":"https:\/\/commons.trincoll.edu\/trinityrobotics\/wp-json\/wp\/v2\/comments?post=90"}],"version-history":[{"count":1,"href":"https:\/\/commons.trincoll.edu\/trinityrobotics\/wp-json\/wp\/v2\/posts\/90\/revisions"}],"predecessor-version":[{"id":91,"href":"https:\/\/commons.trincoll.edu\/trinityrobotics\/wp-json\/wp\/v2\/posts\/90\/revisions\/91"}],"wp:attachment":[{"href":"https:\/\/commons.trincoll.edu\/trinityrobotics\/wp-json\/wp\/v2\/media?parent=90"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/commons.trincoll.edu\/trinityrobotics\/wp-json\/wp\/v2\/categories?post=90"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/commons.trincoll.edu\/trinityrobotics\/wp-json\/wp\/v2\/tags?post=90"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}