Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007 #include "neuro_recv/csv_recv.h"
00008 #include <cstdio>
00009
00016 void CsvReceiver::init()
00017 {
00018 if (getParams())
00019 file_.open(file_name_.c_str());
00020 else
00021 return;
00022
00023 if (file_.is_open())
00024 {
00025
00026 initPubs();
00027
00028
00029
00030 for (int i = 0; i < skip_lines_; i++)
00031 file_.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
00032
00033
00034 publishBuffer();
00035
00036
00037 offset_ = ros::Time::now() - ros::Time(0);
00038
00039
00040 publish();
00041
00042 file_.close();
00043
00044 ROS_INFO("Reached end of CSV file");
00045 }
00046 else
00047 ROS_FATAL("Could not open file");
00048 }
00049
00054 bool CsvReceiver::getParams()
00055 {
00056
00057 if (!n_.getParam("csv_file_path", file_name_))
00058 {
00059 ROS_FATAL("Could not load csv_file_path parameter");
00060 return false;
00061 }
00062
00063
00064 if (!n_.getParam("do_volt_distr", do_volt_distr_))
00065 {
00066 ROS_WARN("Could not load do_volt_distr parameter, default is true");
00067 do_volt_distr_ = true;
00068 }
00069
00070
00071 if (!n_.getParam("do_burst_calc", do_burst_calc_))
00072 {
00073 ROS_WARN("Could not load do_burst_calc parameter, default is true");
00074 do_burst_calc_ = true;
00075 }
00076
00077
00078 if (!n_.getParam("csv_skip_lines", skip_lines_))
00079 {
00080 ROS_WARN("Could not load skip_lines parameter, default is 1");
00081 skip_lines_ = 1;
00082 }
00083
00084
00085 if (!n_.getParam("buffer_size", buffer_size_))
00086 {
00087 ROS_WARN("Could not load buffer_size parameter, default is 1000");
00088 buffer_size_ = 1000;
00089 }
00090
00091
00092 if (!n_.getParam("loop_rate", loop_rate_))
00093 {
00094 ROS_WARN("Could not load loop_rate parameter, default is 200");
00095 loop_rate_ = 200;
00096 }
00097
00098 return true;
00099 }
00100
00104 void CsvReceiver::initPubs()
00105 {
00106 ROS_INFO("Waiting for subscribers...");
00107
00108 if (do_volt_distr_)
00109 {
00110
00111 dish_pub_volt_ = n_.advertise<neuro_recv::dish_state>("dish_states_to_volt_distr",
00112 1000);
00113 while (dish_pub_volt_.getNumSubscribers() < 1 && ros::ok());
00114 }
00115
00116 if (do_burst_calc_)
00117 {
00118
00119 dish_pub_viz_ = n_.advertise<neuro_recv::dish_state>("dish_states_to_dish_viz",
00120 1000);
00121 dish_pub_burst_ = n_.advertise<neuro_recv::dish_state>("dish_states_to_burst_creator",
00122 1000);
00123 while (dish_pub_viz_.getNumSubscribers() < 1 &&
00124 dish_pub_burst_.getNumSubscribers() < 1 && ros::ok());
00125 }
00126 }
00127
00134 void CsvReceiver::publishBuffer()
00135 {
00136 ROS_INFO("Publishing buffer dishes...");
00137
00138 ros::Rate loop_rate(loop_rate_);
00139 std::string line;
00140
00141 if (do_burst_calc_)
00142 {
00143 for (int i = 0; i < buffer_size_ && ros::ok(); i++)
00144 {
00145 getline(file_, line);
00146 dish_pub_burst_.publish(parse(line, false));
00147 loop_rate.sleep();
00148 }
00149 }
00150 else
00151 {
00152 for (int i = 0; i < buffer_size_ && ros::ok(); i++)
00153 file_.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
00154 }
00155 }
00156
00163 void CsvReceiver::publish()
00164 {
00165 ROS_INFO("Publishing dishes...");
00166
00167 ros::Rate loop_rate(loop_rate_);
00168 std::string line;
00169
00170 if (do_volt_distr_ && do_burst_calc_)
00171 {
00172 while (getline(file_, line) && ros::ok())
00173 {
00174 neuro_recv::dish_state dish = parse(line, true);
00175 dish_pub_volt_.publish(dish);
00176 dish_pub_viz_.publish(dish);
00177 dish_pub_burst_.publish(dish);
00178 loop_rate.sleep();
00179 }
00180 }
00181 else if (do_volt_distr_)
00182 {
00183 while (getline(file_, line) && ros::ok())
00184 {
00185 neuro_recv::dish_state dish = parse(line, true);
00186 dish_pub_volt_.publish(dish);
00187 loop_rate.sleep();
00188 }
00189 }
00190 else if (do_burst_calc_)
00191 {
00192 while (getline(file_, line) && ros::ok())
00193 {
00194 neuro_recv::dish_state dish = parse(line, true);
00195 dish_pub_viz_.publish(dish);
00196 dish_pub_burst_.publish(dish);
00197 loop_rate.sleep();
00198 }
00199 }
00200
00201
00202 neuro_recv::dish_state end;
00203 end.last_dish = true;
00204 if (do_volt_distr_)
00205 dish_pub_volt_.publish(end);
00206 if (do_burst_calc_)
00207 dish_pub_burst_.publish(end);
00208 }
00209
00221 const neuro_recv::dish_state CsvReceiver::parse(const std::string& s,
00222 bool record_time)
00223 {
00224
00225 int n = 0;
00226 int pos = s.find(',', n) + 1;
00227
00228 neuro_recv::dish_state dish;
00229 if (record_time)
00230 dish.header.stamp = ros::Time::now() - offset_;
00231
00232 for (int i = 0; i < 60; i++)
00233 {
00234 n = s.find(',', pos) - pos;
00235 dish.samples[i] = atof(s.substr(pos, n).c_str());
00236 pos += n + 1;
00237 }
00238
00239 return dish;
00240 }
00241
00245 int main(int argc, char** argv)
00246 {
00247 ros::init(argc, argv, "csv_receiver");
00248 CsvReceiver csv_receiver;
00249 return 0;
00250 }