phidgets_getdata_synchronous.cpp
Go to the documentation of this file.
00001 /**************************************************************************************************
00002  Software License Agreement (BSD License)
00003 
00004  Copyright (c) 2011-2014, LAR toolkit developers - University of Aveiro - http://lars.mec.ua.pt
00005  All rights reserved.
00006 
00007  Redistribution and use in source and binary forms, with or without modification, are permitted
00008  provided that the following conditions are met:
00009 
00010   *Redistributions of source code must retain the above copyright notice, this list of
00011    conditions and the following disclaimer.
00012   *Redistributions in binary form must reproduce the above copyright notice, this list of
00013    conditions and the following disclaimer in the documentation and/or other materials provided
00014    with the distribution.
00015   *Neither the name of the University of Aveiro nor the names of its contributors may be used to
00016    endorse or promote products derived from this software without specific prior written permission.
00017  
00018  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
00019  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
00020  FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
00021  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00022  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00023  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
00024  IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
00025  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00026 ***************************************************************************************************/
00027 
00035 #include "phidgets_getdata_synchronous.h"
00036 
00037 using namespace std;
00038 
00039 
00040 #include <boost/property_tree/ptree.hpp>
00041 #include <boost/property_tree/xml_parser.hpp>
00042 #include <boost/foreach.hpp>
00043 #include <boost/shared_ptr.hpp>
00044 
00045 using boost::property_tree::ptree;
00046 
00047 
00048 /* Calibration and raw values */
00049 double p1_calib_value_LC1, p2_calib_value_LC1, p3_calib_value_LC1;
00050 double p1_calib_value_FSR1, p2_calib_value_FSR1, p3_calib_value_FSR1;
00051 double p1_calib_value_IR1, p2_calib_value_IR1, p3_calib_value_IR1;
00052 
00053 double p1_value_LC1, p2_value_LC1, p3_value_LC1;
00054 double p1_value_FSR1, p2_value_FSR1, p3_value_FSR1;
00055 double p1_value_IR1, p2_value_IR1, p3_value_IR1; 
00056 
00057 // XML FILES
00058 class Calibration
00059 {
00060     public:
00061         
00062         double max;
00063         double min;
00064 };
00065 
00066 class Sensor
00067 {
00068     public:
00069         
00070         typedef boost::shared_ptr<Sensor> Ptr;
00071         
00072         string name;
00073         string type;
00074         Calibration calibration;
00075 
00076 };
00077 
00078 class Pedal
00079 {
00080     public:
00081         
00082         typedef boost::shared_ptr<Pedal> Ptr;
00083         
00084         vector<Sensor::Ptr> sensors;
00085         string name;
00086 };
00087 
00088 ostream& operator<<(ostream& o, Sensor::Ptr& sensor)
00089 {
00090     o<<"Sensor: "<<sensor->name<<" ";
00091     o<<"Type: "<<sensor->type<<" ";
00092     o<<"Min: "<<sensor->calibration.min<<" ";
00093     o<<"Max: "<<sensor->calibration.max<<" ";
00094     return o;
00095 }
00096 
00097 const ptree& empty_ptree()
00098 {
00099     static ptree t;
00100     return t;
00101 }
00102 
00103 
00107 vector<Pedal::Ptr> readPedalCalibration(string file_name,string& user_name, ptree& pt)
00108 {
00109     vector<Pedal::Ptr> pedals;
00110     
00111     // Try to read the file, if can't files doesn't exist!
00112     try
00113     {
00114         read_xml(file_name, pt, boost::property_tree::xml_parser::trim_whitespace);
00115     }catch(boost::property_tree::xml_parser::xml_parser_error& error)
00116     {
00117         cout<<"Cannot read XML file:\n\t"<<error.what()<<endl;
00118         return pedals;
00119     }
00120     
00121     user_name = pt.get<std::string>("calibration.<xmlattr>.user_name");
00122     
00123     cout<<"Load calibration for user: "<<user_name<<endl;
00124     
00125     BOOST_FOREACH(ptree::value_type const& v, pt.get_child("calibration"))
00126     {    
00127         if(v.first == "pedal")
00128         {
00129             Pedal::Ptr pedal(new Pedal);
00130             
00131             pedal->name = v.second.get<string>("name");
00132             
00133             for(auto it=v.second.begin();it!=v.second.end();it++)
00134             {
00135                 if(it->first == "sensor")
00136                 {
00137                     Sensor::Ptr sensor(new Sensor);
00138                     
00139                     sensor->name = it->second.get<string>("name");
00140                     sensor->type = it->second.get<string>("type");
00141                     sensor->calibration.max = it->second.get<double>("max");
00142                     sensor->calibration.min = it->second.get<double>("min");
00143                     
00144                     pedal->sensors.push_back(sensor);
00145                 }
00146             }
00147             
00148             pedals.push_back(pedal);
00149         }
00150     }
00151     
00152     return pedals;
00153 }
00154 
00158 void writeExistingCalibrationFile(string file_name, ptree& pt,const vector<Pedal::Ptr>& pedals)
00159 {
00160     boost::property_tree::ptree root_node;
00161 
00162     for(auto it = pt.begin(); it != pt.end();it++)
00163     {
00164 //         cout<<it->first<<endl; // debug
00165         if(it->first == "calibration")
00166         {
00167             for(auto cc_it = it->second.begin();cc_it!=it->second.end();cc_it++)//cc_it - calibration child iterator
00168             {
00169 //                 cout<<"  "<<cc_it->first<<endl; // debug
00170                 if(cc_it->first=="pedal")
00171                 {
00172                     //get the pedal name
00173                     string pedal_name = cc_it->second.get<string>("name");
00174                     
00175                     Pedal::Ptr pedal;
00176                     find_if(pedals.begin(),pedals.end(),[&](const Pedal::Ptr& p){if(p->name == pedal_name){pedal = p; return true;}});
00177                                         
00178                     for(auto pc_it = cc_it->second.begin(); pc_it!=cc_it->second.end();pc_it++)//pc_it - pedal child iterator
00179                     {
00180 //                         cout<<"    "<<pc_it->first<<endl; // debug
00181                         if(pc_it->first == "sensor")
00182                         {
00183                             string sensor_name = pc_it->second.get<string>("name");
00184                             
00185                             Sensor::Ptr sensor;
00186                             find_if(pedal->sensors.begin(),pedal->sensors.end(),[&](const Sensor::Ptr& s){if(s->name == sensor_name){sensor = s; return true;}});
00187                             
00188                             for(auto sc_it = pc_it->second.begin(); sc_it!=pc_it->second.end();sc_it++)
00189                             {
00190                                 if(sc_it->first=="type")
00191                                     sc_it->second.put_value(sensor->type);
00192                                 
00193                                 if(sc_it->first=="min")
00194                                     sc_it->second.put_value(sensor->calibration.min);
00195                                 
00196                                 if(sc_it->first=="max")
00197                                     sc_it->second.put_value(sensor->calibration.max);
00198                             }
00199                         }
00200                     }
00201                 }
00202             }
00203         }
00204     }
00205     
00206     boost::property_tree::xml_writer_settings<char> settings('\t', 1);
00207     
00208     // Write the property tree to the XML file.
00209     write_xml(file_name, pt,std::locale(), settings);
00210 }
00211 
00215 void writeCalibrationFile(string file_name,const vector<Pedal::Ptr>& pedals, const string& user)
00216 {
00217     ptree pt;
00218     
00219     boost::property_tree::ptree root_node;
00220    
00221     root_node.add("<xmlattr>.user_name",user);
00222     for(uint i=0;i<pedals.size();i++)
00223     {
00224         boost::property_tree::ptree pedal;
00225         
00226         pedal.add("name",pedals[i]->name);
00227         
00228         for(uint h=0;h<pedals[i]->sensors.size();h++)
00229         {
00230             boost::property_tree::ptree sensor;
00231 
00232             sensor.add("name", pedals[i]->sensors[h]->name);
00233             sensor.add("type", pedals[i]->sensors[h]->type);
00234             sensor.add("min", pedals[i]->sensors[h]->calibration.min);
00235             sensor.add("max", pedals[i]->sensors[h]->calibration.max);
00236             
00237             pedal.add_child("sensor",sensor);
00238         }
00239         
00240         
00241         root_node.add_child("pedal",pedal);
00242 //         root_node.add_child("<xmlattr>.user_name",pedal);
00243     }
00244     
00245     pt.add_child("calibration",root_node);
00246     
00247     boost::property_tree::xml_writer_settings<char> settings('\t', 1);
00248     
00249     // Write the property tree to the XML file.
00250     write_xml(file_name, pt,std::locale(), settings);
00251 }
00252 
00253 
00257 
00258 
00259 int CCONV CAttachHandler(CPhidgetHandle IFK, void *userptr)
00260 {
00261     ((PhidgetClass *)userptr)->AttachHandler(IFK);
00262     return 0;
00263 }
00264 int CCONV CDetachHandler(CPhidgetHandle IFK, void *userptr)
00265 {
00266     ((PhidgetClass *)userptr)->DetachHandler(IFK);
00267     return 0;
00268 }
00269 int CCONV CErrorHandler(CPhidgetHandle IFK, void *userptr, int ErrorCode, const char *unknown)
00270 {
00271     ((PhidgetClass *)userptr)->ErrorHandler(IFK,ErrorCode,unknown); 
00272     return 0;
00273 }
00274 
00275 PhidgetClass::PhidgetClass(boost::asio::io_service& io_service,ros::NodeHandle& nh_):
00276 ir_timer(io_service),
00277 high_rate_timer(io_service),
00278 nh(nh_)
00279 {
00280     ir1_index = 3; //set the index of the infrared sensor 1
00281     ir2_index = 7; //set the index of the infrared sensor 2
00282     
00283     fsr1_index = 1; //set the index of the infrared sensor 1
00284     fsr2_index = 2; //set the index of the infrared sensor 2
00285     fsr3_index = 5; //set the index of the infrared sensor 3
00286     fsr4_index = 6; //set the index of the infrared sensor 4
00287     
00288     lc12_index = 0; //set the index of the infrared sensor 1
00289     lc34_index = 4; //set the index of the infrared sensor 2
00290     
00291     // define low rate period
00292     ir_period = 25; //milliseconds
00293            
00294     // now schedule the low rate timer.
00295     ir_timer.expires_from_now(boost::posix_time::milliseconds(ir_period));
00296     ir_timer.async_wait(boost::bind(&PhidgetClass::irHandler,this,boost::asio::placeholders::error));
00297     
00298     
00299     // define high rate period
00300     high_rate_period = 10; //milliseconds
00301         
00302     // now schedule the high rate timer.
00303     high_rate_timer.expires_from_now(boost::posix_time::milliseconds(high_rate_period));
00304     high_rate_timer.async_wait(boost::bind(&PhidgetClass::highRateHandler,this,boost::asio::placeholders::error));
00305     
00306   
00307     ir1_publisher = nh_.advertise<sensor_msgs::Range>("IR_1", 1);
00308     ir2_publisher = nh_.advertise<sensor_msgs::Range>("IR_2", 1);
00309     ForceVal_publisher = nh_.advertise<pressure_cells::SenVal>("ForceVal", 1);
00310     LC_marker_pub = nh_.advertise<visualization_msgs::Marker>( "LC_marker_data", 1); 
00311     FSR_marker_pub = nh_.advertise<visualization_msgs::Marker>( "FSR_marker_data", 1); 
00312     
00313     double LC1_data=0.01;
00314     double FSR1_data=0.01;
00315     
00316     LC_pub();
00317     FSR_pub();
00318     
00319     //Init message fields
00320     ir1.radiation_type = sensor_msgs::Range::INFRARED;
00321     ir1.field_of_view = 0.1745;
00322     ir1.min_range = 0; // meters
00323     ir1.max_range = 0.3; // meters
00324     ir2.radiation_type = sensor_msgs::Range::INFRARED;
00325     ir2.field_of_view = 0.1745;
00326     ir2.min_range = 0;
00327     ir2.max_range = 0.3;
00328     ir2.range = 0;
00329 
00330     ForceVal.sen1=0; // sen1 = LC1
00331     ForceVal.sen2=0; // sen2 = LC2
00332     ForceVal.sen3=0; // sen3 = FSR1
00333     ForceVal.sen4=0; // sen4 = FSR2
00334     
00335 }
00336 
00340 void PhidgetClass::LC_pub()
00341 {
00342     LC_marker.header.frame_id = "base_link";
00343     LC_marker.header.stamp = ros::Time();
00344     
00345     LC_marker.ns = "LC_data";
00346     LC_marker.id = 0;
00347     LC_marker.type = visualization_msgs::Marker::CUBE;
00348     LC_marker.action = visualization_msgs::Marker::ADD;
00349     LC_marker.scale.x = 1;
00350     LC_marker.scale.y = 0.1;
00351     LC_marker.scale.z = LC1_data*4;
00352     LC_marker.pose.position.x = -3;
00353     LC_marker.pose.position.y = 0;
00354     LC_marker.pose.position.z = LC_marker.scale.z*0.5;
00355     LC_marker.pose.orientation.x = 0.0;
00356     LC_marker.pose.orientation.y = 0.0;
00357     LC_marker.pose.orientation.z = 0.0;
00358     LC_marker.pose.orientation.w = 0.0;
00359     LC_marker.color.a = 1.0;
00360     LC_marker.color.r = 0.0;
00361     LC_marker.color.g = 1.0;
00362     LC_marker.color.b = 0.0;
00363     
00364     LC_marker_pub.publish(LC_marker);
00365 }
00366 
00367 void PhidgetClass::FSR_pub()
00368 {
00369     FSR_marker.header.frame_id = "base_link";
00370     FSR_marker.header.stamp = ros::Time();
00371     
00372     FSR_marker.ns = "FSR_data";
00373     FSR_marker.id = 0;
00374     FSR_marker.type = visualization_msgs::Marker::CUBE;
00375     FSR_marker.action = visualization_msgs::Marker::ADD;
00376     FSR_marker.scale.x = 1;
00377     FSR_marker.scale.y = 0.1;
00378     FSR_marker.scale.z = FSR1_data*4;
00379     FSR_marker.pose.position.x = -1.5;
00380     FSR_marker.pose.position.y = 0;
00381     FSR_marker.pose.position.z = FSR_marker.scale.z*0.5;
00382     FSR_marker.pose.orientation.x = 0.0;
00383     FSR_marker.pose.orientation.y = 0.0;
00384     FSR_marker.pose.orientation.z = 0.0;
00385     FSR_marker.pose.orientation.w = 0.0;
00386     FSR_marker.color.a = 1.0;
00387     FSR_marker.color.r = 0.0;
00388     FSR_marker.color.g = 0.0;
00389     FSR_marker.color.b = 1.0;
00390     
00391     FSR_marker_pub.publish(FSR_marker);
00392 }
00393 
00394 
00398 void PhidgetClass::irHandler(boost::system::error_code const& cError)
00399 {
00400     if (cError.value() == boost::asio::error::operation_aborted)
00401         return;
00402 
00403     if (cError && cError.value() != boost::asio::error::operation_aborted)
00404         return; // throw an exception?
00405 
00406 //     cout << "Read IR sensor: "<< 1000./ir_period << "hz" << endl;
00407     
00408     readIRSensors();
00409     calibrateIRSensors();
00410     publishIRValues();
00411 
00412     // Schedule the timer again...
00413     ir_timer.expires_from_now(boost::posix_time::milliseconds(ir_period));
00414     ir_timer.async_wait(boost::bind(&PhidgetClass::irHandler, this, boost::asio::placeholders::error));
00415 }
00416         
00417 
00421 void PhidgetClass::highRateHandler(boost::system::error_code const& cError)
00422 {
00423     if (cError.value() == boost::asio::error::operation_aborted)
00424         return;
00425 
00426     if (cError && cError.value() != boost::asio::error::operation_aborted)
00427         return; // throw an exception?
00428     
00429 //     cout << "High rate sensor: "<< 1000./high_rate_period << "hz" << endl;
00430 
00431     readFSRSensors();
00432     calibrateFSRSensors();
00433     publishFSRValues();
00434     
00435     readLCSensors();
00436     calibrateLCSensors();
00437     publishLCValues();
00438         
00439     // Schedule the timer again...
00440     high_rate_timer.expires_from_now(boost::posix_time::milliseconds(high_rate_period));
00441     high_rate_timer.async_wait(boost::bind(&PhidgetClass::highRateHandler, this, boost::asio::placeholders::error));
00442 }
00443 
00444 
00445 void PhidgetClass::start()
00446 {
00447     CPhidgetInterfaceKit_create(&ifKit);
00448     CPhidget_set_OnAttach_Handler((CPhidgetHandle)ifKit, CAttachHandler, this);
00449     CPhidget_set_OnDetach_Handler((CPhidgetHandle)ifKit, CDetachHandler, this);
00450     CPhidget_set_OnError_Handler((CPhidgetHandle)ifKit, CErrorHandler, this);
00451     CPhidget_open((CPhidgetHandle)ifKit, -1);
00452 }
00453 
00454 int PhidgetClass::AttachHandler(CPhidgetHandle IFK)
00455 {
00456     int serialNo;
00457     const char *name;
00458 
00459     CPhidget_getDeviceName(IFK, &name);
00460     CPhidget_getSerialNumber(IFK, &serialNo);
00461 
00462     printf("%s %10d attached!\n", name, serialNo);
00463 
00464     return 0;
00465 }
00466 
00467 int PhidgetClass::DetachHandler(CPhidgetHandle IFK)
00468 {
00469     int serialNo;
00470     const char *name;
00471 
00472     CPhidget_getDeviceName (IFK, &name);
00473     CPhidget_getSerialNumber(IFK, &serialNo);
00474 
00475     printf("%s %10d detached!\n", name, serialNo);
00476 
00477     return 0;
00478 }
00479 
00480 int PhidgetClass::ErrorHandler(CPhidgetHandle IFK, int ErrorCode, const char *unknown)
00481 {
00482     printf("Error handled. %d - %s", ErrorCode, unknown);
00483 
00484     return 0;
00485 }
00486 
00487 
00491 int PhidgetClass::readIRSensors()
00492 {
00493     CPhidgetInterfaceKit_getSensorValue(ifKit,ir1_index,&ir1_value);
00494     CPhidgetInterfaceKit_getSensorValue(ifKit,ir2_index,&ir2_value);
00495 
00496 //     cout<<"IR1: "<<ir1_value<<endl;
00497     // cout<<"sensor: "<<ir2_index<<" value: "<<ir2_value<<endl;
00498 }
00499 
00500 
00501 int PhidgetClass::calibrateIRSensors()
00502 {
00503     // Pedal 1:
00504     if(ir1_value<80)
00505     {
00506         ir1_v = 0.3;     
00507     }
00508     else if(ir1_value>530)
00509     {
00510         ir1_v = 0.04;
00511     }
00512     else
00513     {
00514         ir1_v = 20.76/(ir1_value-11);
00515     }
00516             
00517     p1_value_IR1=(ir1_v-0.04)/(0.3-0.04); 
00518                    
00519 //     p1_calib_value_IR1=(p1_value_IR1-ir1_min)/(ir1_max-ir1_min);
00520     
00521     
00522     // Pedal 2:
00523     if(ir2_value<80)
00524     {
00525         ir2_v = 0.3;     
00526     }
00527     else if(ir2_value>530)
00528     {
00529         ir2_v = 0.04;
00530     }
00531     else
00532     {
00533         ir2_v = 20.76/(ir2_value-11);
00534     }
00535                     
00536     p2_value_IR1=(ir2_v-0.04)/(0.3-0.04); 
00537     
00538 //     p2_calib_value_IR1=(p2_value_IR1-ir2_min)/(ir2_max-ir2_min);
00539     
00540     
00541     ir1.range = p1_value_IR1;
00542     ir2.range = p2_value_IR1;
00543     
00544     p3_value_IR1=p1_value_IR1;
00545 }   
00546         
00547 
00548 void PhidgetClass::publishIRValues()
00549 {
00550     //Publish readings in meters
00551     ir1.header.frame_id = "base_link";
00552     ir1.header.stamp = ros::Time::now();
00553     ir1_publisher.publish(ir1);
00554     
00555     ir2.header.frame_id = "base_link";
00556     ir2.header.stamp = ros::Time::now();
00557     ir2_publisher.publish(ir2);
00558 }
00559 
00560 
00564 int PhidgetClass::readFSRSensors()
00565 {
00566     CPhidgetInterfaceKit_getSensorValue(ifKit,fsr1_index,&fsr1_value);
00567     CPhidgetInterfaceKit_getSensorValue(ifKit,fsr2_index,&fsr2_value);
00568     CPhidgetInterfaceKit_getSensorValue(ifKit,fsr3_index,&fsr3_value);
00569     CPhidgetInterfaceKit_getSensorValue(ifKit,fsr4_index,&fsr4_value);
00570     
00571 //    cout<<"FSR_1: "<<fsr1_value<<endl;
00572 //    cout<<"sensor: "<<fsr2_index<<" value: "<<fsr2_value<<endl;
00573 }
00574 
00575 
00576 int PhidgetClass::calibrateFSRSensors()
00577 {   
00578     int fsr_value1;
00579     fsr_value1=(fsr1_value+fsr2_value)/2;
00580     
00581     int fsr_value2;
00582     fsr_value2=(fsr3_value+fsr4_value)/2;
00583     
00584     
00585     //Calibration
00586         // Pedal 1:
00587         fsr1_v=fsr_value1/fsr1_max;
00588         fsr1_vmin=fsr1_min/fsr1_max;
00589         fsr1_vmax=fsr1_max/fsr1_max;
00590         
00591         FSR1_data=(fsr1_v-fsr1_vmin)/(fsr1_vmax-fsr1_vmin);
00592 
00593         if (FSR1_data<=fsr1_vmin)
00594         {
00595             FSR1_data=0.;
00596             
00597         }
00598         else if (FSR1_data>=fsr1_vmax)
00599         {
00600             FSR1_data=1.;
00601             
00602         }
00603         
00604         p1_calib_value_FSR1=FSR1_data; 
00605         p1_value_FSR1=0.;
00606         
00607                 
00608         // Pedal 2:
00609         fsr2_v=fsr_value2/fsr2_max;
00610         fsr2_vmin=fsr2_min/fsr2_max;
00611         fsr2_vmax=fsr2_max/fsr2_max;
00612         
00613         FSR2_data=(fsr2_v-fsr2_vmin)/(fsr2_vmax-fsr2_vmin);
00614 
00615         if (FSR2_data<=fsr2_vmin)
00616         {
00617             FSR2_data=0.;
00618             
00619         }
00620         else if (FSR2_data>=fsr2_vmax)
00621         {
00622             FSR2_data=1.;
00623             
00624         }
00625         
00626         p2_calib_value_FSR1=FSR2_data; 
00627         p2_value_FSR1=0.;
00628         
00629                 
00630         //Debug only
00631         p3_calib_value_FSR1=p1_calib_value_FSR1;
00632         p3_value_FSR1=0.;
00633         
00634         ForceVal.sen3=p1_calib_value_FSR1;
00635         ForceVal.sen4=p2_calib_value_FSR1;
00636 
00637 }
00638 
00639 
00640 void PhidgetClass::publishFSRValues()
00641 {
00642     //Publish readings in meters
00643     ForceVal.header.frame_id = "base_link";
00644     ForceVal.header.stamp = ros::Time::now();
00645     ForceVal_publisher.publish(ForceVal);
00646     LC_pub();
00647 }
00648 
00649 
00653 int PhidgetClass::readLCSensors()
00654 {
00655     CPhidgetInterfaceKit_getSensorValue(ifKit,lc12_index,&lc1_value);
00656     CPhidgetInterfaceKit_getSensorValue(ifKit,lc34_index,&lc2_value);
00657 
00658 //     cout<<"LC_1: "<<lc1_value<<endl;
00659 //     cout<<"sensor: "<<lc2_index<<" value: "<<lc2_value<<endl;
00660 }
00661 
00662 
00663 int PhidgetClass::calibrateLCSensors()
00664 {    
00665         // Pedal 1:
00666         lc1_v=lc1_value/lc1_max;
00667         lc1_vmin=lc1_min/lc1_max;
00668         lc1_vmax=lc1_max/lc1_max;  
00669         
00670         LC1_data=(lc1_v-lc1_vmin)/(lc1_vmax-lc1_vmin);
00671 
00672         if (LC1_data<=lc1_vmin)
00673         {
00674             LC1_data=0.;
00675         }
00676         else if (LC1_data>=lc1_vmax)
00677         {
00678             LC1_data=1.;
00679         }
00680         
00681         p1_calib_value_LC1=LC1_data; 
00682         p1_value_LC1=0.;
00683         
00684                 
00685         // Pedal 2: 
00686         lc2_v=lc2_value/lc2_max;
00687         lc2_vmin=lc2_min/lc2_max;
00688         lc2_vmax=lc2_max/lc2_max;  
00689         
00690         LC2_data=(lc2_v-lc2_vmin)/(lc2_vmax-lc2_vmin);
00691 
00692         if (LC2_data<=lc2_vmin)
00693         {
00694             LC2_data=0.;
00695         }
00696         else if (LC2_data>=lc2_vmax)
00697         {
00698             LC2_data=1.;
00699         }
00700         
00701         p2_calib_value_LC1=LC2_data; 
00702         p2_value_LC1=0.;
00703         
00704                 
00705         //Debug only
00706         p3_calib_value_LC1=p1_calib_value_LC1;
00707         p3_value_LC1=0.;    
00708         
00709         ForceVal.sen1=LC1_data;
00710         ForceVal.sen2=LC2_data;
00711 }
00712 
00713 
00714 void PhidgetClass::publishLCValues()
00715 {
00716     //Publish readings in meters
00717     ForceVal.header.frame_id = "base_link";
00718     ForceVal.header.stamp = ros::Time::now();
00719     ForceVal_publisher.publish(ForceVal);    
00720     FSR_pub();
00721 }
00722 
00723 
00724 void get_pedal_data(PhidgetClass& pClass, vector<Pedal::Ptr> pedals, int num_pedals)
00725 {
00726     //Get data from pedals and save on pClass
00727     if(num_pedals==3 || num_pedals==2 || num_pedals==1)
00728     {
00729         pClass.lc1_max= pedals[0]->sensors[0]->calibration.max;
00730         pClass.lc1_min= pedals[0]->sensors[0]->calibration.min;
00731         pClass.fsr1_max= pedals[0]->sensors[1]->calibration.max;
00732         pClass.fsr1_min= pedals[0]->sensors[1]->calibration.min;
00733         
00734         if(num_pedals==3 || num_pedals==2)
00735         {
00736             pClass.lc2_max= pedals[1]->sensors[0]->calibration.max;
00737             pClass.lc2_min= pedals[1]->sensors[0]->calibration.min;
00738             pClass.fsr2_max= pedals[1]->sensors[1]->calibration.max;
00739             pClass.fsr2_min= pedals[1]->sensors[1]->calibration.min; 
00740         }
00741         if(num_pedals==3)
00742         {
00743             pClass.lc3_max= pedals[2]->sensors[0]->calibration.max;
00744             pClass.lc3_min= pedals[2]->sensors[0]->calibration.min;
00745             pClass.fsr3_max= pedals[2]->sensors[1]->calibration.max;
00746             pClass.fsr3_min= pedals[2]->sensors[1]->calibration.min;          
00747         } 
00748     }
00749 }
00750 
00751 
00755 int main(int argc, char* argv[])
00756 {     
00757     // Ros Init
00758     ros::init(argc, argv, "pedal_monitor_node");
00759     ros::NodeHandle node("~");
00760     
00761     boost::asio::io_service io_service;
00762     
00763     PhidgetClass pClass(io_service,node);
00764     pClass.start();
00765 
00766     // Get calibration data
00767     // File name and username
00768     string file_name;
00769     node.param("file_name",file_name,std::string("Not found"));
00770 
00771     string user;
00772     ptree pt;
00773     
00774     try
00775     {
00776         read_xml(file_name, pt, boost::property_tree::xml_parser::trim_whitespace);
00777     }catch(boost::property_tree::xml_parser::xml_parser_error& error)
00778     {
00779 //         cout<<"Cannot read XML file:\n\t"<<error.what()<<endl;
00780         file_name = ros::package::getPath("pedal_monitor") + "/src/calibration_test.xml";
00781     }
00782     
00783     // Read existing file.xml
00784     vector<Pedal::Ptr> pedals = readPedalCalibration(file_name,user,pt);
00785     
00786 //     cout<<"Calibration for user: "<<user<< endl;
00787     cout<<"Number of pedals: "<<pedals.size()<<endl;
00788     
00789     // Get data from pedals
00790     get_pedal_data(pClass,pedals,pedals.size());
00791   
00792      // Fix division when MAX = 0
00793     if(pClass.lc1_max<=0.)
00794     {
00795         pClass.lc1_max=0.01;
00796     }
00797     if(pClass.lc2_max<=0.)
00798     {
00799         pClass.lc2_max=0.01;
00800     }
00801     if(pClass.fsr1_max<=0.)
00802     {
00803         pClass.fsr1_max=0.01;
00804     }
00805     if(pClass.fsr2_max<=0.)
00806     {
00807         pClass.fsr2_max=0.01;
00808     }
00809     if(pClass.ir1_max<=0.)
00810     {
00811         pClass.ir1_max=0.01;
00812     }
00813     if(pClass.ir2_max<=0.)
00814     {
00815         pClass.ir2_max=0.01;
00816     }
00817        
00818     boost::thread thread(boost::bind(&boost::asio::io_service::run, &io_service));
00819     
00820     ros::spin();
00821     
00822     return 0;
00823 }


pedal_monitor
Author(s): Pedro Mendes
autogenerated on Thu Nov 20 2014 11:35:50