Creating a ROS package for the blink demo

We are done with installing wiringpi; let's create a ROS package for the LED blink demo. I hope you have already created a ROS workspace on the board. For this demo, we are connecting the LED anode to the twelfth pin of the board (first pin in wiringpi). The LED cathode is connected to GND.

The following figure shows the circuit of the demo. It is applicable to RPi and Odroid.

Figure 24: Board connected to an LED

Okay! Let's make a ROS package for creating a blinking ROS node. Here is the command to create a ROS package for this demo:

    $ catkin_create_pkg ros_wiring_example roscpp std_msgs

You will also get the complete package from chapter_4_codes/ros_wiring_example.

Create an src folder inside the new package, and copy the blink.cpp file from the existing code. The blink code is and is as follows:

    #include "ros/ros.h" 
    #include "std_msgs/Bool.h" 
 
    #include <iostream> 
    #include "wiringPi.h" 
 
    //Wiring PI 1 
    #define LED 1 
 
    void blink_callback(const std_msgs::Bool::ConstPtr& msg) 
    { 
 
 
     if(msg->data == 1){ 
 
      digitalWrite (LED, HIGH) ; 
      ROS_INFO("LED ON"); 
 
 
      } 
 
     if(msg->data == 0){ 
 
      digitalWrite (LED, LOW) ; 
      ROS_INFO("LED OFF"); 
 
      } 
    } 
 
    int main(int argc, char** argv) 
    { 
 
      ros::init(argc, argv,"blink_led"); 
      ROS_INFO("Started Odroid-C1 Blink Node"); 
      wiringPiSetup (); 
      pinMode(LED, OUTPUT); 
 
 
      ros::NodeHandle n; 
      ros::Subscriber sub =   
       n.subscribe("led_blink",10,blink_callback); 
      ros::spin(); 
 
    } 

The preceding code will subscribe to a topic called /led_blink, which is a Boolean type. If the value is true, the LED will turn on, otherwise it'll be off.

The following is the CMakeLists.txt file for compiling the code:

    cmake_minimum_required(VERSION 2.8.3) 
    project(ros_wiring_examples) 
 
    find_package(catkin REQUIRED COMPONENTS 
      roscpp 
      std_msgs 
    ) 
 
    find_package(Boost REQUIRED COMPONENTS system) 
 
 
    set(wiringPi_include "/usr/local/include") 
 
 
    include_directories( 
      ${catkin_INCLUDE_DIRS} 
      ${wiringPi_include} 
    ) 
 
    LINK_DIRECTORIES("/usr/local/lib") 
 
 
    add_executable(blink_led src/blink.cpp) 
 
    target_link_libraries(blink_led 
       ${catkin_LIBRARIES} wiringPi 
     ) 

After changing CMakeLists.txt, we can perform a catkin_make to build the ROS node.

If everything builds successfully, we can run the demo using the following procedure.

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

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