evarobot_bumper.cpp
Go to the documentation of this file.
00001 #include "evarobot_bumper/evarobot_bumper.h"
00002 
00003 #include <dynamic_reconfigure/server.h>
00004 #include <evarobot_bumper/ParamsConfig.h>
00005 
00006 
00007 using namespace std;
00008 
00009 int i_error_code = 0;
00010 bool b_always_on;
00011 bool b_is_received_params = false;
00012 bool b_collision[3] = {true, true, true};
00013 
00014 void ProduceDiagnostics(diagnostic_updater::DiagnosticStatusWrapper &stat)
00015 {
00016     stringstream ss;
00017     bool b_any_coll = false;
00018 
00019     ss << "Collision: ";
00020 
00021     for(int i = 0; i < MAX_BUMPER; i++)
00022     {
00023         if(!b_collision[i])
00024         {
00025             ss << "Bumper[" << i << "] ";
00026             b_any_coll = true;
00027         }
00028     }
00029 
00030     if(i_error_code<0)
00031     {
00032         stat.summaryf(diagnostic_msgs::DiagnosticStatus::ERROR, "%s", GetErrorDescription(i_error_code).c_str());
00033         i_error_code = 0;
00034     }
00035     else if(b_any_coll)
00036     {
00037         stat.summaryf(diagnostic_msgs::DiagnosticStatus::WARN, "%s", ss.str().c_str());
00038     }
00039     else
00040     {
00041         stat.summary(diagnostic_msgs::DiagnosticStatus::OK, "No collision!");
00042     }
00043 }
00044 
00045 void CallbackReconfigure(evarobot_bumper::ParamsConfig &config, uint32_t level)
00046 {
00047     ROS_DEBUG("EvarobotBumper: Bumper Param Callback Function...");
00048 
00049     b_is_received_params = true;
00050 
00051     b_always_on = config.alwaysOn;
00052 }
00053 
00054 int main(int argc, char **argv)
00055 {
00056     key_t key;
00057     sem_t *mutex;
00058     FILE * fd;
00059 
00060     key = 1005;
00061 
00062     mutex = sem_open(SEM_NAME,O_CREAT,0644,1);
00063     if(mutex == SEM_FAILED)
00064     {
00065         ROS_INFO(GetErrorDescription(-44).c_str());
00066         i_error_code = -44;
00067 
00068         sem_unlink(SEM_NAME);
00069 
00070         return(-1);
00071     }
00072 
00073 
00074     // ROS PARAMS
00075     double d_frequency;
00076     double d_min_freq, d_max_freq;
00077     string str_i2c_device_address;
00078     string str_i2c_path;
00079     // rosparams end
00080 
00081     ros::Publisher pub_bumper;
00082     im_msgs::Bumper bumpers;
00083 
00084     ros::init(argc, argv, "/evarobot_bumper");
00085     ros::NodeHandle n;
00086 
00087     n.param<string>("evarobot_bumper/i2c_path", str_i2c_path, "/dev/i2c-1");
00088     n.param("evarobot_bumper/alwaysOn", b_always_on, false);
00089     n.param("evarobot_bumper/minFreq", d_min_freq, 0.2);
00090     n.param("evarobot_bumper/maxFreq", d_max_freq, 10.0);
00091     if(!n.getParam("evarobot_bumper/frequency", d_frequency))
00092     {
00093         ROS_INFO(GetErrorDescription(-27).c_str());
00094         i_error_code = -27;
00095     }
00096 
00097     // Set publisher
00098     pub_bumper = n.advertise<im_msgs::Bumper>("bumper", 10);
00099 
00100     // Dynamic Reconfigure
00101     dynamic_reconfigure::Server<evarobot_bumper::ParamsConfig> srv;
00102     dynamic_reconfigure::Server<evarobot_bumper::ParamsConfig>::CallbackType f;
00103     f = boost::bind(&CallbackReconfigure, _1, _2);
00104     srv.setCallback(f);
00106 
00107     // Define frequency
00108     ros::Rate loop_rate(d_frequency);
00109     IMEIO * eio;
00110     try {
00111         eio = new IMEIO(0b00100000, string("/dev/i2c-1"),  mutex);
00112                 eio->SetPinADirection(IMEIO::BUMPER0, IMEIO::INPUT);
00113         eio->SetPinADirection(IMEIO::BUMPER1, IMEIO::INPUT);
00114         eio->SetPinADirection(IMEIO::BUMPER2, IMEIO::INPUT);
00115     } catch(int i) {
00116         ROS_INFO(GetErrorDescription(i).c_str());
00117         i_error_code = i;
00118     }
00119 
00120     vector<int> T_i_bumper_no;
00121 
00122     T_i_bumper_no.resize(MAX_BUMPER);
00123 
00124     T_i_bumper_no[0] = IMEIO::BUMPER0;
00125     T_i_bumper_no[1] = IMEIO::BUMPER1;
00126     T_i_bumper_no[2] = IMEIO::BUMPER2;
00127 
00128 
00129     // Diagnostics
00130     diagnostic_updater::Updater updater;
00131     updater.setHardwareID("IM-BMP10");
00132     updater.add("bumper", &ProduceDiagnostics);
00133 
00134     diagnostic_updater::HeaderlessTopicDiagnostic pub_freq("bumper", updater,
00135             diagnostic_updater::FrequencyStatusParam(&d_min_freq, &d_max_freq, 0.1, 10));
00136 
00137     while(ros::ok())
00138     {
00139         if(b_is_received_params)
00140         {
00141             ROS_DEBUG("EvarobotBumper: Updating Bumper Params...");
00142 
00143             b_is_received_params = false;
00144         }
00145 
00146         for(uint i = 0; i < T_i_bumper_no.size(); i++)
00147         {
00148             im_msgs::BumperState bumper_state;
00149 
00150             bool b_data;
00151             try{
00152                 eio->GetPinAValue(T_i_bumper_no[i], b_data);
00153             } catch(int e) {
00154                 ROS_INFO(GetErrorDescription(e).c_str());
00155                 i_error_code = e;
00156             }
00157 
00158             b_collision[i] = b_data;
00159             bumper_state.bumper_state = b_data;
00160             bumpers.state.push_back(bumper_state);
00161         }
00162 
00163         bumpers.header.stamp = ros::Time::now();
00164 
00165         // Publish Data
00166         if(pub_bumper.getNumSubscribers() > 0 || b_always_on)
00167         {
00168             pub_bumper.publish(bumpers);
00169             pub_freq.tick();
00170         }
00171 
00172         bumpers.state.clear();
00173         updater.update();
00174         ros::spinOnce();
00175         loop_rate.sleep();
00176 
00177     }
00178     return 0;
00179 }


evarobot_bumper
Author(s): Mehmet Akcakoca
autogenerated on Fri Feb 12 2016 01:15:15