fingertip_monitor.cpp
Go to the documentation of this file.
00001 #include <numeric>
00002 #include <math.h>
00003 #include <boost/lambda/lambda.hpp>
00004 
00005 #include <ros/ros.h>
00006 #include <pr2_msgs/PressureState.h>
00007 #include <std_msgs/Bool.h>
00008 #include <std_msgs/Float32.h>
00009 #include <pr2_collision_monitor/FingerState.h>
00010 #include <pr2_collision_monitor/GetFingerState.h>
00011 #include <pr2_collision_monitor/fir_diff_filter.h>
00012 
00013 #define bl boost::lambda
00014 
00015 using namespace std;
00016 using namespace boost;
00017 
00018 namespace pr2_collision_monitor {
00019 
00020     class FingertipMonitor {
00021         public:
00022             ros::NodeHandle nh;
00023             std::vector<boost::shared_ptr<FIRDiffFilter> > r_finger_filters;
00024             std::vector<boost::shared_ptr<FIRDiffFilter> > l_finger_filters;
00025             std::string arm;
00026             ros::Subscriber pressure_sub;
00027             ros::Publisher coll_state_pub, coll_bool_pub, mag_sig_pub;
00028             ros::ServiceServer coll_state_srv;
00029             std::vector<std::vector<double> > r_fing_vals, l_fing_vals;
00030             FingerState::Ptr cur_state;
00031             bool training_mode;
00032             double threshold;
00033             
00034             FingertipMonitor();
00035             void onInit();
00036             void pressureCallback(const pr2_msgs::PressureState& msg); 
00037             void computeStats();
00038             bool srvCallback(GetFingerState::Request& req, GetFingerState::Response& resp);
00039     };
00040 
00041     FingertipMonitor::FingertipMonitor() : nh("~"), r_fing_vals(21), l_fing_vals(21) {
00042     }
00043 
00044     void FingertipMonitor::onInit() {
00045         nh.param<std::string>("arm", arm, std::string("r"));
00046         nh.param<double>("sensor_threshold", threshold, 2.0);
00047         double time_constant;
00048         int history;
00049         nh.param<double>("time_constant", time_constant, 0.01);
00050         nh.param<int>("history", history, 30);
00051 
00052         // initialize the filters
00053         for(uint32_t i=0;i<21;i++) {
00054             r_finger_filters.push_back(
00055                     shared_ptr<FIRDiffFilter>(new FIRDiffFilter(history, time_constant)));
00056             l_finger_filters.push_back(
00057                     shared_ptr<FIRDiffFilter>(new FIRDiffFilter(history, time_constant)));
00058         }
00059 
00060         // subscriber to the pressure topic
00061         pressure_sub = nh.subscribe("/pressure/" + arm + "_gripper_motor", 2, 
00062                                     &FingertipMonitor::pressureCallback, this);
00063         // publish a FingerState message which details the collision state of all sensors
00064         // in that arm
00065         // TODO KEEP/REMOVE FINGERSTATE?
00066         //coll_state_pub = nh.advertise<FingerState>("collision_state", 2);
00067         // publish a simple bool summarizing whether any sensor has collided
00068         coll_bool_pub = nh.advertise<std_msgs::Bool>("collision_detected", 2);
00069         mag_sig_pub = nh.advertise<std_msgs::Float32>("finger_signal", 2);
00070         // accessor for the current collision state of the sensor
00071         //coll_state_srv = nh.advertiseService("state_request",
00072                                              //&FingertipMonitor::srvCallback, this);
00073     }
00074 
00078     void FingertipMonitor::pressureCallback(const pr2_msgs::PressureState& msg) {
00079         // update the filters and set the state of the collision detection
00080         //FingerState::Ptr state(new FingerState()); 
00081         //state->r_finger_tip.resize(21); state->l_finger_tip.resize(21);
00082         double max_sig = 0;
00083         for(uint32_t i=0;i<21;i++) {
00084             double r_sig = r_finger_filters[i]->updateState(msg.r_finger_tip[i]);
00085             max_sig = max(r_sig*r_sig, max_sig);
00086             double l_sig = l_finger_filters[i]->updateState(msg.l_finger_tip[i]);
00087             max_sig = max(l_sig*l_sig, max_sig);
00088         }
00089         //coll_state_pub.publish(state);
00090         //cur_state = state;
00091         double signal = max_sig;
00092         std_msgs::Float32 signal_msg;
00093         signal_msg.data = signal;
00094         mag_sig_pub.publish(signal_msg);
00095 
00096         // publish a simple bool if the signal has exceeded the set threshold
00097         std_msgs::Bool coll_bool_msg;
00098         coll_bool_msg.data = signal > threshold;
00099         coll_bool_pub.publish(coll_bool_msg);
00100     }
00101 
00102     bool FingertipMonitor::srvCallback(GetFingerState::Request& req, 
00103                                        GetFingerState::Response& resp) {
00104         resp.state = *cur_state;
00105         return true;
00106     }
00107 
00108 };
00109 
00110 using namespace pr2_collision_monitor;
00111 
00112 int main(int argc, char **argv)
00113 {
00114     ros::init(argc, argv, "fingertip_monitor", ros::init_options::AnonymousName);
00115     FingertipMonitor fm;
00116     fm.onInit();
00117     ros::spin();
00118     return 0;
00119 }
00120 
00121 


pr2_collision_monitor
Author(s): Kelsey Hawkins, Advisor: Prof. Charlie Kemp (Healthcare Robotics Lab at Georgia Tech)
autogenerated on Wed Nov 27 2013 11:40:10