akash a s
Published © GPL3+

Arduino line follower controlled by ROS1

You will learn how to establish serial communication between ROS and Arduino by implementing a simple publisher and subscriber setup.

IntermediateFull instructions provided5 hours211
Arduino line follower controlled by ROS1

Things used in this project

Hardware components

Arduino Nano R3
Arduino Nano R3
×1
6v N20 Motor 100rpm
×2
44mm N20 wheel
×2
N20 Mounting bracket
×2
N20 castor
×1
MX1508 motor driver
×1
TCRT5000L IR Array
×1
3.7v LIPO battery
×2

Software apps and online services

Arduino IDE
Arduino IDE
Robot Operating System
ROS Robot Operating System

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)
Mastech MS8217 Autorange Digital Multimeter
Digilent Mastech MS8217 Autorange Digital Multimeter

Story

Read more

Schematics

circuit diagram

Code

Script file.txt

Python
#!/usr/bin/env python

import rospy
from std_msgs.msg import String

def ir_state_callback(msg):
    if msg.data == "00100":
        led_pub.publish("forward")
    elif msg.data == "00010":
        led_pub.publish("right")
    elif msg.data == "01000":
        led_pub.publish("left")
    elif msg.data == "00001":
        led_pub.publish("cright")
    elif msg.data == "10000":
        led_pub.publish("ceft")
    elif msg.data == "00011":
        led_pub.publish("sright")
    elif msg.data == "11000":
        led_pub.publish("sleft")
    elif msg.data == "00110":
        led_pub.publish("mright")
    elif msg.data == "01100":
        led_pub.publish("mleft")
    elif msg.data == "00000":
        led_pub.publish("stop")

rospy.init_node('arduino_led_control')

rospy.Subscriber('/ir_state', String, ir_state_callback)
led_pub = rospy.Publisher('/mov_state', String, queue_size=10)

rospy.spin()

Arduino ino file.txt

C/C++
#include <ros.h>
#include <std_msgs/String.h>

ros::NodeHandle nh;

const int irPin1 = 2;
const int irPin2 = 3;
const int irPin3 = 4;
const int irPin4 = 5;
const int irPin5 = 6;

const int ledPin = 13;     // LED pin

std_msgs::String str_msg;
ros::Publisher ir_pub("/ir_state", &str_msg);

void movCallback(const std_msgs::String& cmd_msg) {
  String command = cmd_msg.data;

  if (command == "forward") {
    digitalWrite(7, 0);
    digitalWrite(8, 1);
    digitalWrite(9, 0);
    digitalWrite(10, 1);
  } else if (command == "right") {
    digitalWrite(7, 0);
    digitalWrite(8, 0);
    digitalWrite(9, 0);
    digitalWrite(10, 1);
  } else if (command == "left") {
    digitalWrite(7, 0);
    digitalWrite(8, 1);
    digitalWrite(9, 0);
    digitalWrite(10, 0);
  } else if (command == "cright") {
    digitalWrite(7, 0);
    digitalWrite(8, 0);
    digitalWrite(9, 0);
    digitalWrite(10, 1);
  } else if (command == "cleft") {
    digitalWrite(7, 0);
    digitalWrite(8, 1);
    digitalWrite(9, 0);
    digitalWrite(10, 0);
  } else if (command == "sright") {
    digitalWrite(7, 0);
    digitalWrite(8, 0);
    digitalWrite(9, 0);
    digitalWrite(10, 1);
  } else if (command == "sleft") {
    digitalWrite(7, 0);
    digitalWrite(8, 1);
    digitalWrite(9, 0);
    digitalWrite(10, 0);
  } else if (command == "mright") {
    digitalWrite(7, 0);
    digitalWrite(8, 0);
    digitalWrite(9, 0);
    digitalWrite(10, 1);
  } else if (command == "mleft") {
    digitalWrite(7, 0);
    digitalWrite(8, 1);
    digitalWrite(9, 0);
    digitalWrite(10, 0);
  } else if (command == "stop") {
    digitalWrite(7, 0);
    digitalWrite(8, 0);
    digitalWrite(9, 0);
    digitalWrite(10, 0);
  } else {
    digitalWrite(7, 0);
    digitalWrite(8, 0);
    digitalWrite(9, 0);
    digitalWrite(10, 0);
    digitalWrite(11, 0);

  }
}

ros::Subscriber<std_msgs::String> sub("mov_state", &movCallback);

void setup() {
  pinMode(irPin1, INPUT);
  pinMode(irPin2, INPUT);
  pinMode(irPin3, INPUT);
  pinMode(irPin4, INPUT);
  pinMode(irPin5, INPUT);

  pinMode(ledPin, OUTPUT);

  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);

  nh.initNode();
  nh.advertise(ir_pub);
  nh.subscribe(sub);
}

void loop() {
  int irState1 = digitalRead(irPin1);
  int irState2 = digitalRead(irPin2);
  int irState3 = digitalRead(irPin3);
  int irState4 = digitalRead(irPin4);
  int irState5 = digitalRead(irPin5);

  if (irState1 && irState2 && !irState3 && irState4 && irState5 ) {
    str_msg.data = "00100";
  } else if (irState1 && irState2 && irState3 && !irState4 && irState5 ) {
    str_msg.data = "00010";
  } else if (irState1 && !irState2 && irState3 && irState4 && irState5 ) {
    str_msg.data = "01000";
  } else if (!irState1 && !irState2 && !irState3 && !irState4 && !irState5 ) {
    str_msg.data = "11111";
  } else if (!irState1 && irState2 && irState3 && irState4 && irState5 ) {
    str_msg.data = "10000";
  } else if (irState1 && irState2 && irState3 && irState4 && !irState5 ) {
    str_msg.data = "00001";
  } else if (!irState1 && !irState2 && irState3 && irState4 && irState5 ) {
    str_msg.data = "11000";
  } else if (irState1 && irState2 && irState3 && !irState4 && !irState5 ) {
    str_msg.data = "00011";
  } else if (irState1 && !irState2 && !irState3 && irState4 && irState5 ) {
    str_msg.data = "01100";
  } else if (irState1 && irState2 && !irState3 && !irState4 && irState5 ) {
    str_msg.data = "00110";
  } else {
    str_msg.data = "00000";
  }

  ir_pub.publish(&str_msg);
  nh.spinOnce();

  // Wait for incoming messages from ROS
  delay(100);
}

Credits

akash a s

akash a s

4 projects • 5 followers
Electronics student who loves making projects

Comments