Monitoring light using Arduino and ROS

We can start coding a basic Arduino-ROS node that can sense the amount of light using a light-dependent resistor (LDR). You can use any Arduino for this demo; here, we are going to use the Arduino Mega 2560. Given in the following figure is the circuit of an LDR with the Arduino. The characteristic of an LDR is that it is basically a resistor in which the resistance across it changes when light falls on it. The maximum resistance is when there is no light and minimum when light falls on it.

Figure 9: Arduino-LDR interfacing circuit

We have to connect one pin to 5V from the Arduino board and the next terminal to the Arduino's A0 pin. That terminal is connected to the GND pin through a 10 KΩ resistor. It is basically a voltage divider circuit. The equation for finding the voltage at A0 is as follows:

V_a0 = 5 * (R2 / (R1 + R2))

From the equation, it is clear that when there is no light, we will get the minimum voltage, and when there is light, we'll get the maximum. This value can be read out using an Arduino program.

Here is the ROS code to read from an LDR:

    #include <Arduino.h> 
    #include <ros.h> 
    #include <rosserial_arduino/Adc.h> 
 
    ros::NodeHandle nh; 
 
    rosserial_arduino::Adc adc_msg; 
    ros::Publisher p("adc", &adc_msg); 
 
    void setup() 
    { 
     nh.initNode(); 
     nh.advertise(p); 
    } 
 
    //We average the analog reading to elminate some of the noise 
    int averageAnalog(int pin){ 
      int v=0; 
      for(int i=0; i<4; i++) v+= analogRead(pin); 
      return v/4; 
    } 
 
    long adc_timer; 
 
    void loop() 
    { 
      adc_msg.adc0 = averageAnalog(0); 
      p.publish(&adc_msg); 
      nh.spinOnce(); 
      delay(50); 
    } 

Here is the explanation of the code:

    #include <Arduino.h> 
    #include <ros.h> 
    #include <rosserial_arduino/Adc.h> 

The <Arduino.h> library contains definitions of Arduino-specific functions. The <ros.h> library contains Arduino-to-ROS client functionalities. The <rosserial_arduino/Adc.h> header contains message definitions for carrying several ADC values in a single message.

    ros::NodeHandle nh; 

This create a ROS node handle. Like other ROS nodes, we are using this handle to publish and subscribe to Arduino.

    rosserial_arduino::Adc adc_msg; 
    ros::Publisher p("adc", &adc_msg); 

This code will create an adc_msg instance and create a publisher object.

    void setup() 
    { 
     nh.initNode(); 
     nh.advertise(p); 
    } 

This will initialize the node and bind the publisher object to start publishing the topic called /adc.

    void loop() 
    { 
      adc_msg.adc0 = averageAnalog(0); 
      p.publish(&adc_msg); 
      nh.spinOnce(); 
      delay(50); 
    } 

In the loop, the Analog value from pin A0 is read and the average is computed. The average value will be published to the /adc topic.

After compiling the code, you can select the board from Tools | Board and Serial Port from the list. You can now burn the code into the Arduino board.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.146.221.149