sr_ronex_flash_LED_with_PWM.cpp
Go to the documentation of this file.
00001 /*
00002 * Copyright (c) 2013, Shadow Robot Company, All rights reserved.
00003 * 
00004 * This library is free software; you can redistribute it and/or
00005 * modify it under the terms of the GNU Lesser General Public
00006 * License as published by the Free Software Foundation; either
00007 * version 3.0 of the License, or (at your option) any later version.
00008 * 
00009 * This library is distributed in the hope that it will be useful,
00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00012 * Lesser General Public License for more details.
00013 * 
00014 * You should have received a copy of the GNU Lesser General Public
00015 * License along with this library.
00016 */
00017 
00024 //-------------------------------------------------------------------------------
00025 
00026 #include <string>
00027 #include <ros/ros.h>
00028 #include <ros/console.h>
00029 #include <boost/lexical_cast.hpp>
00030 
00031 #include "sr_ronex_msgs/PWM.h"
00032 #include "sr_ronex_utilities/sr_ronex_utilities.hpp"
00033 
00034 //-------------------------------------------------------------------------------
00035 
00039 class SrRonexFindGeneralIOModule
00040 {
00041 public:
00042   SrRonexFindGeneralIOModule() {}
00043   ~SrRonexFindGeneralIOModule() {}
00044 
00045 public:
00053   bool get_path_(const unsigned int& ronex_id_as_int, std::string& path)
00054   {
00055     std::string ronex_id = this->to_string_(ronex_id_as_int);
00056 
00057     // Wait until there's at least one General I/O module.
00058     ros::Rate loop_rate(10);
00059     std::string param;
00060     while ( ros::param::get("/ronex/devices/0/ronex_id", param ) == false )
00061     {
00062       ROS_INFO_STREAM("Waiting for General I/O module to be loaded properly.\n");
00063       loop_rate.sleep();
00064     }
00065 
00066     // When -1 is returned, the module with the given id is not present on the parameter server.
00067     // Note that ronex parameter id starts from zero.
00068     int ronex_parameter_id = ronex::get_ronex_param_id(ronex_id);
00069     if ( ronex_parameter_id == -1 )
00070     {
00071       ROS_ERROR_STREAM("Did not find the General I/O module with ronex_id " << ronex_id << ".\n");
00072       return false;  // Failed to set path.
00073     }
00074 
00075     // The module is present on the parameter server and ronex_parameter_id
00076     // contains the id on which the module is stored on the parameter server.
00077 
00078     std::string path_key = ronex::get_ronex_devices_string(ronex_parameter_id, std::string("path"));
00079     ros::param::get(path_key, path);
00080 
00081     return true;  // Path is set.
00082   }
00083 
00084 private:
00091   std::string to_string_(unsigned int d)
00092   {
00093     return boost::lexical_cast<std::string>(d);
00094   }
00095 };
00096 
00097 //-------------------------------------------------------------------------------
00098 
00105 void flash_LED(ros::NodeHandle& n, const std::string& topic)
00106 {
00107   ros::Publisher pub = n.advertise<sr_ronex_msgs::PWM>(topic, 1000);
00108 
00109   uint16_t pwm_period = 320;
00110   // Start with a 100% duty cycle.
00111   uint16_t pwm_on_time_0 = pwm_period;
00112   // The second output is not used.
00113   uint16_t pwm_on_time_1 = 0;
00114 
00115   ros::Rate loop_rate(100);
00116   while ( ros::ok() )
00117   {
00118     // Flash the light...
00119     pwm_on_time_0 -= 10;
00120     if (pwm_on_time_0 == 0 || pwm_on_time_0 > pwm_period)
00121       pwm_on_time_0 = pwm_period;
00122 
00123     sr_ronex_msgs::PWM msg;
00124     msg.pwm_period    = pwm_period;
00125     msg.pwm_on_time_0 = pwm_on_time_0;
00126     msg.pwm_on_time_1 = pwm_on_time_1;
00127 
00128     // The publish() function sends the message.
00129     // The parameter is the message object.
00130     pub.publish(msg);
00131 
00132     ros::spinOnce();
00133     loop_rate.sleep();
00134   }
00135 }
00136 
00137 //-------------------------------------------------------------------------------
00138 
00144 int main(int argc, char **argv)
00145 {
00146   // Initialize ROS with a unique node name.
00147   ros::init(argc, argv, "sr_ronex_flash_LED_with_PWM");
00148 
00149   // Create a handle to this process' node.
00150   ros::NodeHandle n;
00151 
00152   std::cout <<
00153           "Please configure D0 pin of your ronex module as output (you can use plugin Dynamic Reconfigure in rqt): \n";
00154 
00155   // Get the path of the General I/O module with the given ronex id.
00156   // Note that you may have to set the value of ronex_id,
00157   // depending on which General I/O board the LED is connected to.
00158   std::string input = "";
00159   unsigned int ronex_id;
00160   std::cout << "Please enter the ronex id: ";
00161   std::getline(std::cin, input);
00162   ronex_id = boost::lexical_cast<unsigned int>(input);
00163   std::string path;
00164   SrRonexFindGeneralIOModule findModule;
00165   if ( findModule.get_path_( ronex_id, path ) )
00166   {
00167     // Always use the first digital I/O channel to flash the LED light.
00168     // For example "/ronex/general_io/1" + "/command/pwm/0".
00169     std::string topic = path + "/command/pwm/0";
00170     ROS_INFO_STREAM("Topic = " << topic << "\n");
00171     flash_LED(n, topic);
00172   }
00173 
00174   return 0;
00175 }
00176 
00177 //-------------------------------------------------------------------------------


sr_ronex_examples
Author(s): Ugo Cupcic, Toni Oliver, Mark Pitchless, Yi Li
autogenerated on Thu Jun 6 2019 21:22:11