ROS arduino write examples 2-6
ROS arduino write examples 2-6
Digital write:
Here’s an example Arduino sketch that controls a digital output pin based on ROS messages:
#include <ros.h>
#include <std_msgs/Bool.h>
ros::NodeHandle nh;
void setup() {
pinMode(pin, OUTPUT); // Set the digital pin as output
nh.initNode();
nh.subscribe(sub); // Subscribe to the topic
}
void loop() {
nh.spinOnce(); // Process incoming ROS messages
delay(10); // Add a small delay to avoid overloading the loop
}
This code listens to the digital_out topic and sets the state of pin 13 based on the received
message (True for HIGH, False for LOW).
In a new terminal, launch the rosserial node to establish communication with the Arduino.
This command opens a serial connection to the Arduino and allows it to communicate with ROS.