ursa_node.cpp
Go to the documentation of this file.
00001 
00027 #include "ursa_driver/ursa_driver.h"
00028 #include "ros/ros.h"
00029 #include "ursa_driver/ursa_counts.h"
00030 #include "ursa_driver/ursa_spectra.h"
00031 #include "std_srvs/Empty.h"
00032 #include <std_msgs/String.h>
00033 
00034 int32_t baud;
00035 std::string port = "";
00036 std::string detector_frame = "";
00037 
00038 int HV = 0;
00039 double gain = 0;
00040 int threshold = 0;
00041 double shaping_time = 1;
00042 ursa::shaping_time real_shaping_time;
00043 std::string input_polarity = "";
00044 ursa::inputs real_input;
00045 int ramp = 6;
00046 
00047 std::map<double, ursa::shaping_time> shape_map;
00048 std::map<std::string, ursa::inputs> input_map;
00049 
00050 bool load_prev;
00051 bool GMmode;
00052 bool imeadiate;
00053 
00054 void fill_maps();
00055 int get_params(ros::NodeHandle nh);
00056 void timerCallback(const ros::TimerEvent& event);
00057 bool startAcquireCB(std_srvs::Empty::Request& request,
00058                     std_srvs::Empty::Response& response);
00059 bool stopAcquireCB(std_srvs::Empty::Request& request,
00060                    std_srvs::Empty::Response& response);
00061 bool clearSpectraCB(std_srvs::Empty::Request& request,
00062                     std_srvs::Empty::Response& response);
00063 
00064 ursa::Interface * my_ursa;
00065 ros::Publisher publisher;
00066 ros::Timer timer;
00067 
00068 int main(int argc, char **argv) {
00069   ros::init(argc, argv, "ursa_driver");
00070   ros::NodeHandle nh("~");
00071 
00072   if (!get_params(nh))
00073     return (-1);
00074 
00075   my_ursa = new ursa::Interface(port.c_str(), baud);
00076   my_ursa->connect();
00077   if (my_ursa->connected())
00078     ROS_INFO("URSA Connected");
00079   else
00080     return (-1);
00081 
00082   if (GMmode)
00083     publisher = nh.advertise<ursa_driver::ursa_counts>("counts", 10);
00084   else
00085     publisher = nh.advertise<ursa_driver::ursa_spectra>("spectra", 10);
00086 
00087   ros::ServiceServer startSrv = nh.advertiseService("startAcquire",
00088                                                     startAcquireCB);
00089   ros::ServiceServer stopSrv = nh.advertiseService("stopAcquire",
00090                                                    stopAcquireCB);
00091   ros::ServiceServer spectraSrv = nh.advertiseService("clearSpectra",
00092                                                       clearSpectraCB);
00093   timer = nh.createTimer(ros::Duration(1.0), timerCallback, false, false); 
00094 
00095   if (load_prev)
00096   {
00097     my_ursa->loadPrevSettings();
00098   }
00099   else
00100   {
00101     my_ursa->setGain(gain);
00102     my_ursa->setThresholdOffset(threshold);
00103     my_ursa->setShapingTime(real_shaping_time);
00104     my_ursa->setInput(real_input);
00105     my_ursa->setRamp(ramp);
00106     my_ursa->setVoltage(HV);
00107   }
00108 
00109   if (imeadiate)
00110   {
00111     if (GMmode)
00112       my_ursa->startGM();
00113     else
00114       my_ursa->startAcquire();
00115     timer.start();
00116   }
00117 
00118   ros::spin();
00119   my_ursa->stopAcquire();
00120   my_ursa->setVoltage(0);
00121 }
00122 
00123 bool startAcquireCB(std_srvs::Empty::Request& request,
00124                     std_srvs::Empty::Response& response) {
00125   if (GMmode)
00126     my_ursa->startGM();
00127   else
00128     my_ursa->startAcquire();
00129   timer.start();
00130   return (true);
00131 }
00132 
00133 bool stopAcquireCB(std_srvs::Empty::Request& request,
00134                    std_srvs::Empty::Response& response) {
00135   timer.stop();
00136   if (GMmode)
00137     my_ursa->stopGM();
00138   else
00139     my_ursa->stopAcquire();
00140   return (true);
00141 }
00142 
00143 bool clearSpectraCB(std_srvs::Empty::Request& request,
00144                     std_srvs::Empty::Response& response) {
00145   my_ursa->clearSpectra();
00146   return (true);
00147 }
00148 
00149 void timerCallback(const ros::TimerEvent& event) {
00150   ROS_DEBUG("Hit timer callback.");
00151   ros::Time now = ros::Time::now();
00152   if (GMmode)
00153   {
00154     ursa_driver::ursa_counts temp;
00155     temp.header.stamp = now;
00156     temp.header.frame_id = detector_frame;
00157     temp.counts = my_ursa->requestCounts();
00158     publisher.publish(temp);
00159   }
00160   else
00161   {
00162     ursa_driver::ursa_spectra temp;
00163     temp.header.stamp = now;
00164     temp.header.frame_id = detector_frame;
00165     my_ursa->read();
00166     my_ursa->getSpectra(&temp.bins);
00167     publisher.publish(temp);
00168   }
00169 }
00170 
00171 int get_params(ros::NodeHandle nh) {
00172   nh.param("load_previous_settings", load_prev, false);
00173 
00174   if (!load_prev)
00175   {
00176     if (!nh.getParam("high_voltage", HV))
00177     {
00178       ROS_ERROR("High voltage must be set.");
00179       return (-1);
00180     }
00181     if (!nh.getParam("gain", gain))
00182     {
00183       ROS_ERROR("Gain must be set.");
00184       return (-1);
00185     }
00186     if (!nh.getParam("threshold", threshold))
00187     {
00188       ROS_ERROR("Threshold must be set.");
00189       return (-1);
00190     }
00191     if (!nh.getParam("shaping_time", shaping_time))
00192     {
00193       ROS_ERROR("Shaping time must be set.");
00194       return (-1);
00195     }
00196     if (!nh.getParam("input_and_polarity", input_polarity))
00197     {
00198       ROS_ERROR("Input and Polartity must be set.");
00199       return (-1);
00200     }
00201     if (!nh.getParam("ramping_time", ramp))
00202     {
00203       ROS_ERROR("Ramping time must be set.");
00204       return (-1);
00205     }
00206 
00207     fill_maps();
00208 
00209     if (shape_map.find(shaping_time) == shape_map.end())
00210     {
00211       ROS_ERROR("Shaping time must be valid. Input as double in microseconds.");
00212       return (-1);
00213     }
00214 
00215     if (input_map.find(input_polarity) == input_map.end())
00216     {
00217       ROS_ERROR(
00218           "Input and polarity must be valid. Input as string in the format \"intput1_negative\""
00219           " if using a pre-shaped positive input \"shaped_input\".");
00220       return (-1);
00221     }
00222 
00223     real_shaping_time = shape_map[shaping_time];
00224     real_input = input_map[input_polarity];
00225   }
00226 
00227   nh.param<std::string>("port", port, "/dev/ttyUSB0");
00228   nh.param("baud", baud, 115200);
00229 
00230   nh.param("use_GM_mode", GMmode, false);
00231   nh.param("imeadiate_mode", imeadiate, false);
00232   nh.param<std::string>("detector_frame", detector_frame, "rad_link");
00233   return (1);
00234 }
00235 
00236 void fill_maps() {
00237   shape_map[0.25] = ursa::TIME0_25uS;
00238   shape_map[0.5] = ursa::TIME0_5uS;
00239   shape_map[1] = ursa::TIME1uS;
00240   shape_map[2] = ursa::TIME2uS;
00241   shape_map[4] = ursa::TIME4uS;
00242   shape_map[6] = ursa::TIME6uS;
00243   shape_map[8] = ursa::TIME8uS;
00244   shape_map[10] = ursa::TIME10uS;
00245 
00246   input_map["input1_negative"] = ursa::INPUT1NEG;
00247   input_map["input1_positive"] = ursa::INPUT1POS;
00248   input_map["input2_negative"] = ursa::INPUT2NEG;
00249   input_map["input2_positive"] = ursa::INPUT1POS;
00250   input_map["shaped_input"] = ursa::INPUTXPOS;
00251 }
00252 


ursa_driver
Author(s): Mike Hosmar
autogenerated on Sat Jun 8 2019 20:57:48