volt_distr_creator.cpp
Go to the documentation of this file.
00001 /*
00002  * volt_distr_creator.cpp
00003  * Copyright 2013 University of Massachusetts Lowell
00004  * Author: Jonathan Hasenzahl
00005  */
00006 
00007 #include "volt_distr/volt_distr_creator.h"
00008 #include "ros/ros.h"
00009 
00010 VoltDistrCreator::VoltDistrCreator()
00011 {
00012     do_truncate_volts_ = false;
00013     negatives_.fill(0);
00014     total_dishes_ = 0;
00015 }
00016 
00025 void VoltDistrCreator::add(const neuro_recv::dish_state& d)
00026 {
00027     total_dishes_++;
00028 
00029     for (int i = 0; i < 60; i++)
00030     {
00031         double volt = d.samples[i];
00032 
00033         // Check to see if the voltage is negative
00034         if (volt < 0.0)
00035             negatives_[i]++;
00036 
00037         // Truncate the voltage if appropriate
00038         if (do_truncate_volts_)
00039             volt = truncate(volt);
00040 
00041         if (volts_.count(volt) > 0)
00042         {
00043             // If the voltage has already been logged, increment the count for
00044             // this channel
00045             volts_[volt][i] += 1;
00046         }
00047         else
00048         {
00049             // Otherwise create a new pair for the voltage with a count of 1
00050             // for this channel
00051             std::vector<int> channels(60, 0);
00052             channels[i] = 1;
00053 
00054             std::pair<double, std::vector<int> > new_volt(volt, channels);
00055             volts_.insert(new_volt);
00056         }
00057     }
00058 }
00059 
00064 void VoltDistrCreator::toFile(const std::string& file_path)
00065 {
00066     log_file_.open(file_path.c_str(), std::ios_base::trunc | std::ios_base::out);
00067 
00068     if (!log_file_.is_open())
00069     {
00070         ROS_ERROR("Cannot open %s. CSV logging will be disabled.", file_path.c_str());
00071         return;
00072     }
00073 
00074     // Write the header
00075     log_file_ << "voltage,";
00076     for (int i = 0; i < 60; i++)
00077         log_file_ << "channel_" << i << ',';
00078     log_file_ << '\n';
00079 
00080     // Write the data
00081     std::map<double, std::vector<int> >::iterator it;
00082     for (it = volts_.begin(); it != volts_.end(); it++)
00083     {
00084         log_file_ << (*it).first << ',';
00085         for (int i = 0; i < 60; i++)
00086             log_file_ << (*it).second[i] << ',';
00087         log_file_ << '\n';
00088     }
00089 
00090     log_file_.close();
00091 }
00092 
00098 boost::array<double, 60> VoltDistrCreator::getPercents()
00099 {
00100     boost::array<double, 60> percents;
00101     for (int i = 0; i < 60; i++)
00102     {
00103         percents[i] = static_cast<double>(negatives_[i]) / total_dishes_;
00104         //printf("%d: %f = %d / %d\n", i, percents[i], negatives_[i], total_dishes_);
00105     }
00106     return percents;
00107 }


volt_distr
Author(s): Jonathan Hasenzahl
autogenerated on Sun Jan 5 2014 11:12:31