ROS interface with Arduino Sensor Actuator 2-4
ROS interface with Arduino Sensor Actuator 2-4
1. Hardware Connections:
Connected the VCC pin of the DHT11 to the 5V pin on the Arduino.
Connected the GND pin of the DHT11 to the GND pin on the Arduino.
Connected the DATA pin of the DHT11 to a digital pin 2 on the Arduino.
Launch the necessary ROS nodes, including the subscriber node and any other nodes required.
Arduino is connected to laptop, and the ROS serial node is running to establish communication
between the Arduino and ROS.
3. Data Exchange:
Once the Arduino and ROS are successfully connected, the sensor data will be published by the
Arduino and received by the subscriber node in ROS. Now, temperature and humidity data in
ROS can be accessed and used for further processing, visualization, or control of our robot
system.
4) To subscribe to the topic “Temperature”, use the command “rostopic echo temperature”
a rospackage can also be created to predefine the movement of rover. Firstly a ros package is created in
~/home/catkin_ws/src by the following command using catkin workspace:
catkin_create_pkg car_control rospy geometry_msgs
Now create a python file in the src directory of car_control. This file contains the necessary instructions
to ensure that the rover moves in a rectangular path each time the file is executed.
Source devel/setup.bash
#include "DHT.h"
#include <ros.h>
#include <sensor_msgs/Temperature.h>
#include <sensor_msgs/RelativeHumidity.h>
#include <SoftwareSerial.h>
#include <geometry_msgs/Twist.h>
DHT dht(DHTPIN, DHTTYPE); //// Initialize DHT sensor for normal 16mhz Arduino
#define IN1 9
#define IN2 8
#define IN3 7
#define IN4 6
ros::NodeHandle node;
sensor_msgs::Temperature temp_msg;
sensor_msgs::RelativeHumidity humidity_msg;
geometry_msgs::Twist msg;
void forward()
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
void stop()
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
forward(); //i
analogWrite(test, 255);
else
stop(); //k
analogWrite(test, 0);
void setup()
node.initNode();
node.advertise(pub_temp);
node.advertise(pub_humidity);
dht.begin();
node.initNode();
node.subscribe(sub);
pinMode(test, OUTPUT);
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(IN3, OUTPUT);
pinMode(IN4, OUTPUT);
}
void loop()
begn:delay(1000);
humidity_msg.relative_humidity = dht.readHumidity();
temp_msg.temperature = dht.readTemperature();
temp_msg.header.stamp = node.now();
humidity_msg.header.stamp = node.now();
pub_temp.publish( &temp_msg );
pub_humidity.publish( &humidity_msg );
digitalWrite(test, LOW);
node.spinOnce();
#!/usr/bin/env python3
import rospy
def move_rectangular():
rospy.init_node('car_control_node', anonymous=True)
# Move forward
twist_msg = Twist()
pub.publish(twist_msg)
# Stop
twist_msg.linear.x = 0.0
pub.publish(twist_msg)
rospy.sleep(2.0)
if _name_ == '_main_':
try:
move_rectangular()
except rospy.ROSInterruptException:
pass