0% found this document useful (0 votes)
2 views

ROS arduino read examples 2-5

The document provides examples of Arduino code for reading digital and analog inputs using ROS integration through rosserial_arduino. It includes setup instructions and explanations for each code snippet, detailing how to publish the input states to ROS topics. The digital read example uses a boolean message type, while the analog read example publishes voltage values as floats.

Uploaded by

Mehwish Aleem
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

ROS arduino read examples 2-5

The document provides examples of Arduino code for reading digital and analog inputs using ROS integration through rosserial_arduino. It includes setup instructions and explanations for each code snippet, detailing how to publish the input states to ROS topics. The digital read example uses a boolean message type, while the analog read example publishes voltage values as floats.

Uploaded by

Mehwish Aleem
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

ROS Arduino input read examples

Digital read:
Below is a simple example of an Arduino code that reads a digital input using ROS (Robot
Operating System) integration through rosserial_arduino. The code will read a digital pin and
publish the state to a ROS topic.

### *Arduino Code Example*


#include <ros.h>
#include <std_msgs/Bool.h>
ros::NodeHandle nh;
const int inputPin = 2; // Change this to the digital pin you are using
std_msgs::Bool inputStateMsg; // Message type to publish the input state
ros::Publisher inputPublisher("digital_input", &inputStateMsg); // Topic name: "digital_input"

void setup() {
pinMode(inputPin, INPUT); // Set the input pin mode
nh.initNode(); // Initialize ROS node
nh.advertise(inputPublisher); // Advertise the topic
}
void loop() {
bool pinState = digitalRead(inputPin); // Read the digital pin
inputStateMsg.data = pinState; // Assign the state to the message
inputPublisher.publish(&inputStateMsg); // Publish the message
nh.spinOnce(); // Handle ROS communication
delay(100); // Adjust the delay as needed
}
### *Explanation*
1. ros.h and std_msgs/Bool.h: ROS libraries included for communication and message types.
2. inputPin: Defines which digital pin to read from.
3. std_msgs::Bool inputStateMsg: Stores the pin state.
4. ros::Publisher inputPublisher: Publishes the input state to the digital_input topic.
5. setup(): Initializes the pin mode and sets up the ROS node.
6. loop(): Reads the pin, updates the message, and publishes it to ROS.

### *Setup Instructions*


1. Install the rosserial_arduino package on your ROS system.
2. Load this code onto the Arduino using the Arduino IDE.
3. Run roscore on your ROS machine.
4. Connect the Arduino and start the ROS node with rosrun rosserial_python serial_node.py
/dev/ttyUSB0 (adjust the port if needed).
5. Use rostopic echo / digital_input to view the published analog values.

Analog read:
### *Arduino Code Example for Analog Read with ROS*

cpp
#include <ros.h>
#include <std_msgs/Float32.h>

ros::NodeHandle nh;

const int analogPin = A0; // Change to your analog input pin


std_msgs::Float32 analogValueMsg; // Message type to publish analog value
ros::Publisher analogPublisher("analog_input", &analogValueMsg); // Topic name:
"analog_input"

void setup() {
nh.initNode(); // Initialize the ROS node
nh.advertise(analogPublisher); // Advertise the topic
}

void loop() {
int rawValue = analogRead(analogPin); // Read the analog input
float voltage = (rawValue / 1023.0) * 5.0; // Convert to voltage (assuming 5V reference)

analogValueMsg.data = voltage; // Assign voltage to message


analogPublisher.publish(&analogValueMsg); // Publish the message
nh.spinOnce(); // Handle ROS communication
delay(100); // Adjust delay for your application
}

### *Explanation*
1. *Library Imports*: ros.h and std_msgs/Float32.h for ROS communication and message type.
2. *analogPin*: Defines the analog input pin to read from (e.g., A0).
3. *analogValueMsg*: Stores the voltage reading as a float.
4. *analogPublisher*: Publishes to the analog_input topic.
5. *loop()*: Reads the analog input, converts it to voltage, updates the message, and publishes it
to ROS.

### *Setup Instructions*


1. Install rosserial on your ROS system.
2. Connect your Arduino, load the code using the Arduino IDE.
3. Run roscore on your computer.
4. Use rosrun rosserial_python serial_node.py /dev/ttyUSB0 (or your port) to connect the
Arduino to ROS.
5. Use rostopic echo /analog_input to view the published analog values.

You might also like