Infrared Communication Suitable Option for Swarm Robotics?

Hi Everyone!

This topic has been prevalent for the past months, and I can tell how many members are absolutely sick of me speaking of this topic. Well, if you are new to this topic, I did swarm robotics for a middle school science project, with the guidance of only the arduino forums, i’ve learned quite a lot about electronics (and the infamous XYZ situation…). I have asked several questions in the past, all very newbie. Some were “Phototransistor Problem: Cannot properly detect light”, “How does TCS230 color sensor program work?”, and the most infamous and shamed in my mind, “How NOT to do a science project”.

The swarm robots are small in numbers. Only two in fact, but enough to show swarm intelligence. The robots are assigned to find a piece of blue tape from an arbitrary distance from it using Particle swarm Intelligence with the other robot. The main method of communication between the robots are infrared emitters and phototransistors. Other sensors include a color sensor. The main method of movement is by servos and wheels.

The infrared communication system uses emitters, eight all lined around the top of the robot like some sort of crown for the robot. These emitters would keep infrared light on to tell the other robots to avoid that robot with the emitter that is on. Right next to these emitters are phototransistors to detect the infrared light and how intense the light is in analog values. These values are not based on specific units of measurement, but just a general way to know the difference of values to tell if the infrared light source is near or far away. These two components working together creates two tools for the robot to use, object detection and communication. Communication between robots is, as I said before, simple “if on” and “if distance is (number), then avoid by (respective turning function)” This same comparison statement can be used for the object detection aspect of my robots.

I also want to share the code that I used for this project.

/* By BrokenAvocado:
   This is the Arduino Swarm robot project. The goal of the whole
   swarm of robots is to find the colored tape at the end 
   utilizing the distance of their surroundings and each other. 
   How these robots will find the distance of each other is
   using infrared emitters and phototransistors to measure the led
   intensity for every robot. As soon as the intensity reaches a 
   certain point, the robot will turn in the opposite direction 
   from that robot. This will effectively create a swarm that is 
   flocking and therefore a swarm of partical swarm optimization. 
 */
 
//Libraries
#include <FreqCount.h>
#include <MD_TCS230.h>
#include <Servo.h>

//Color Sensor Pins
#define s2 2
#define s3 4
#define out 5
#define BLACK_CAL 0
#define WHITE_CAL 1
#define READ_VAL  2

//Infrared Leds and Phototransistors Pins
#define flr A4 //Front Left
#define frr A2 //Front Right
#define fr A3 //Front 
#define slr A5 //Side Left
#define srr A2 //Side Right
#define blr A6 //Back Left
#define brr A1 //Back Right
#define emit 6 //Infrared Led

//Servo Pins
Servo ls; //Left Servo
Servo rs; //Right Servo

//Color values
colorData  rgb; //Custom variable to store rgb

//Servo values
int lf = 150; //Left Forward
int rf = 30; //Right Forward
int lr = 30; //Left Reverse
int rr = 150; //Right Reverse
int stall = 90; //Stop

MD_TCS230 CS(s2, s3);

void setup() {
  pinMode(fr, INPUT);
  pinMode(flr, INPUT);
  pinMode(frr, INPUT);
  pinMode(srr, INPUT);
  pinMode(slr, INPUT);
  pinMode(blr, INPUT);
  pinMode(brr, INPUT);
  pinMode(emit, OUTPUT);
  ls.attach(9);
  rs.attach(10);
  CS.begin();
}

void loop() {
  digitalWrite(emit, HIGH);
  ls.write(lf);
  rs.write(rf); 
  irobjectdetect();
  readSensor();
  execute();
}

void irobjectdetect() {
  if(analogRead(flr) <= 550) // front left ir receiver then turn right
  {
    turnr();
  }
  else if(analogRead(frr) <= 550) // front right ir receiver then turn left
  {
    turnl();
  }
  else if(analogRead(fr) <= 550) // front receiver then turn left
  {
    turnl();
  }
  else if(analogRead(blr) <= 550) // back left ir receiver then turn right
  {
    turnr();
  }
  else if(analogRead(brr) <= 550) // back right ir receiver then turn left
  {
    turnl();
  }
  else if(analogRead(srr) <= 550) // side right ir receiver then turn left
  {
    turnl();
  }
  else if(analogRead(slr) <= 550) // side left ir receiver then turn right
  {
    turnr();
  }
  else
  {
    ls.write(lf);
    rs.write(rf);
  }
}

void turnr() {
  ls.write(lf);
  rs.write(stall);
  delay(1000);
  rs.write(rf);
}

void turnl() {
  ls.write(stall);
  rs.write(rf);
  delay(1000);
  ls.write(lf);
}

uint8_t fsmReadValue2(uint8_t state2, uint8_t valType2, uint8_t maxReads2)
//FSM = Finite State Machine
{
  static  uint8_t selChannel2;
  static  uint8_t readCount2;
  static  sensorData  sd;

  switch(state2)
  {
  case 0: // Wait for delay
    delay(3000);
    state2++;
    break;

  case 1: // start the reading process
    CS.read();
    state2++;
    break;

  case 2: // wait for a read to complete
    if (CS.available()) 
    {
      sensorData  sd;
      colorData rgb;

      switch(valType2)
      {
        case BLACK_CAL: 
        CS.getRaw(&sd); 
        CS.setDarkCal(&sd);   
        break;

        case WHITE_CAL: 
        CS.getRaw(&sd); 
        CS.setWhiteCal(&sd);  
        
        break;
      }
      state2++;
    }
    break;

  default:  // reset fsm
    state2 = 0;
    break;
    
}
return(state2);

}

void caliSensor() 
{
  static uint8_t  runState2 = 0;   
  static uint8_t  readState2 = 0;

  switch(runState2)
  {
  case 0: // calibrate black
    readState2 = fsmReadValue2(readState2, BLACK_CAL, 2);
    if (readState2 == 0) runState2++;
    break;

  case 1: // calibrate white
    readState2 = fsmReadValue2(readState2, WHITE_CAL, 2);
    if (readState2 == 0) runState2++;
    break;
  default:
    runState2 = 0; // start again if we get here as something is wrong
  }
}


void execute() {
   if(rgb.value[TCS230_RGB_R] == 86 && rgb.value[TCS230_RGB_G] == 87 && rgb.value[TCS230_RGB_B] == 150)
    {
      ls.write(stall);
      rs.write(stall);
    }
    else
    {
      ls.write(lf);
      rs.write(rf);
    }
}

uint8_t fsmReadValue(uint8_t state, uint8_t valType, uint8_t maxReads)
//FSM = Finite State Machine
{
  static  uint8_t selChannel;
  static  uint8_t readCount;
  static  sensorData  sd;

  switch(state)
  {
  case 0: // Wait for delay
    delay(200);
    state++;
    break;

  case 1: // start the reading process
    CS.read();
    state++;
    break;

  case 2: // wait for a read to complete
    if (CS.available()) 
    {
      sensorData  sd;
      colorData rgb;

      switch(valType)
      {
      case READ_VAL:  
      CS.getRGB(&rgb);
        
        break;
      }
      state++;
    }
    break;

  default:  // reset fsm
    state = 0;
    break;
  }

  return(state);
}

void readSensor() 
{
  static uint8_t  runState = 0;   
  static uint8_t  readState = 0;

  switch(runState)
  {
  case 0: // read color
    readState = fsmReadValue(readState, READ_VAL, 1);
    break;

  default:
    runState = 0; // start again if we get here as something is wrong
  }
}

But today, I want to speak about the actual usability of infrared communication for such a simple swarm robotics project. It has been harder to implement and perfect these sensors than to use them. Tuning them work efficiently is incredibly hard. Often times, I wish I used different alternatives to this simple swarm robotics project. Now, i’m finished with the project and in the aftermath, I want to simply think about the approach I took this project, even revising the robots in the future.

So the discussion begins with this question, for the context of this project, Was Infrared communication a suitable option, even if I used the correct sensors for the situation? If infrared wasn’t, then what other alternatives could I have done for this project? Don’t take me as an idiot though, I have other alternatives at mind, but its only me :). I’m always curious what others have to say about this project’s approach.

Again, this may look like spam to those who are well aware of my situation before. My apologies.

Was Infrared communication a suitable option

For two nearby devices to exchange a small amount of data in ideal lighting when in line of sight? Yes.

For more than two devices? No. If line of sight isn't possible? No. In all light levels? No. For large amounts of data? No. Over long distances? No.

Hi brokenAvocado,

I am just starting something similar to this and came across your golden question. I was hoping you could shed some light on the topic because unlike you I feel pretty lost here. I was thinking of using IRs for the same purpose i.e. detecting if there was a robot in a particular direction and approximately how far. I am trying to find out what's the approximate range of detection with this approach? Also, could you maybe point me into a direction of possible alternatives?

Any help would be much appreciated!