numatac_can_node.cpp
Go to the documentation of this file.
00001 
00025 #include <string>
00026 #include <vector>
00027 
00028 #include "ros/ros.h"
00029 #include "numatac_can_driver/numatac_can_driver.h"
00030 #include "numatac_can_driver/HandPressure.h"
00031 
00032 class NumaTacCANNode
00033 {
00034   public:
00035     NumaTacCANNode(ros::NodeHandle& nh, ros::NodeHandle& pnh,
00036       numatac_can_driver::NumaTacCANDriver& driver, uint8_t number_of_sensors, bool tare) :
00037       driver_(driver),
00038       number_of_sensors_(number_of_sensors),
00039       tare_(tare)
00040     {
00041       pressure_msg_.fingers.resize(number_of_sensors_);
00042       pressure_pub_ = nh.advertise<numatac_can_driver::HandPressure>("hand_pressure", 100);
00043 
00044       tare_pac_.resize(number_of_sensors_);
00045       tare_pdc_.resize(number_of_sensors_);
00046     }
00047 
00048     bool connectIfNotConnected()
00049     {
00050       if (!driver_.isConnected())
00051       {
00052         if (!driver_.connect())
00053         {
00054           ROS_ERROR("Error connecting to CAN device. Retrying in 1 second.");
00055           return false;
00056         }
00057         else
00058         {
00059           ROS_INFO("Connection to CAN device successful.");
00060         }
00061       }
00062       return true;
00063     }
00064 
00065     void run()
00066     {
00067       ros::Rate rate(1000);
00068       while (ros::ok())
00069       {
00070         if (!connectIfNotConnected())
00071         {
00072           ros::Duration(1.0).sleep();
00073           continue;
00074         }
00075 
00076         while (driver_.getData())
00077         {
00078         }
00079 
00080         if (tare_)
00081         {
00082           for (int i = 0; i < number_of_sensors_; i++)
00083           {
00084             tare_pac_[i] = driver_.getPAC(i);
00085             tare_pdc_[i] = driver_.getPDC(i);
00086           }
00087           tare_ = false;
00088           ROS_INFO("Tare complete.");
00089         }
00090         else
00091         {
00092           pressure_msg_.header.stamp = ros::Time::now();
00093 
00094           for (int i = 0; i < number_of_sensors_; i++)
00095           {
00096             pressure_msg_.fingers[i].fluid_pressure = (driver_.getPDC(i) - tare_pdc_[i]) * 12.94;
00097             pressure_msg_.fingers[i].dynamic_pressure = (driver_.getPAC(i) - tare_pac_[i]) * 0.13;
00098             pressure_msg_.fingers[i].finger_number = i + 1;
00099           }
00100           pressure_pub_.publish(pressure_msg_);
00101         }
00102 
00103         rate.sleep();
00104       }
00105     }
00106 
00107   private:
00108     uint8_t number_of_sensors_;
00109     bool tare_;
00110     std::vector<int16_t> tare_pac_;
00111     std::vector<uint16_t> tare_pdc_;
00112     numatac_can_driver::NumaTacCANDriver& driver_;
00113     numatac_can_driver::HandPressure pressure_msg_;
00114     ros::Publisher pressure_pub_;
00115 };
00116 
00117 int main(int argc, char **argv)
00118 {
00119   ros::init(argc, argv, "numatac_can_driver");
00120   ros::NodeHandle nh, pnh("~");
00121 
00122   int number_of_sensors;
00123   std::string canbus_dev;
00124   bool tare;
00125 
00126   pnh.param<std::string>("canbus_dev", canbus_dev, "can0");
00127   pnh.param<int>("number_of_sensors", number_of_sensors, 3);
00128   pnh.param<bool>("tare", tare, false);
00129 
00130   numatac_can_driver::NumaTacCANDriver driver(canbus_dev, number_of_sensors);
00131 
00132   NumaTacCANNode node(nh, pnh, driver, number_of_sensors, tare);
00133 
00134   node.run();
00135 }


numatac_can_driver
Author(s):
autogenerated on Thu Jun 6 2019 21:19:30