m4atx_battery_monitor.cpp
Go to the documentation of this file.
00001 
00011 #include <ros/ros.h>
00012 #include <m4atx_battery_monitor/PowerReading.h>
00013 #include <usb.h>
00014 
00015 extern "C"
00016 {
00017 #include "m4atx_battery_monitor/m4api.h"
00018 }
00019 
00027 int main(int argc, char **argv)
00028 {
00029   // initialize the node
00030   ros::init(argc, argv, "m4atx_battery_monitor");
00031 
00032   // main node handle
00033   ros::NodeHandle node;
00034   ros::Time next_check = ros::Time::now();
00035 
00036   // attempt to initialize the USB device
00037   struct usb_dev_handle *dev = m4Init();
00038   if (!dev)
00039   {
00040     ROS_ERROR("Could not initialize the M4-ATX.");
00041     return EXIT_FAILURE;
00042   }
00043 
00044   // grab the parameters
00045   ros::NodeHandle private_nh("~");
00046   double diag_frequency;
00047   private_nh.param<double>("diag_frequency", diag_frequency, 1.0);
00048   double input_nominal;
00049   private_nh.param<double>("input_nominal", input_nominal, 13.8);
00050   double battery_dead_voltage;
00051   private_nh.param<double>("battery_dead_voltage", battery_dead_voltage, 10.5);
00052 
00053   // setup the publishers
00054   ros::Publisher diag_pub = node.advertise<m4atx_battery_monitor::PowerReading>("battery_status_m4atx", 1);
00055 
00056   // main publish loop
00057   ros::Rate loop_rate(diag_frequency);
00058   while (ros::ok())
00059   {
00060     m4Diagnostics diag;
00061     m4atx_battery_monitor::PowerReading reading;
00062 
00063     // attempt to red the battery
00064     if (m4GetDiag(dev, &diag))
00065     {
00066       ROS_ERROR("Error reading from the M4-ATX.");
00067       return EXIT_FAILURE;
00068     }
00069 
00070     // parse the feedback
00071     reading.volts_read_item.push_back("VIN");
00072     reading.volts_read_value.push_back(diag.vin);
00073     reading.volts_read_item.push_back("12V");
00074     reading.volts_read_value.push_back(diag.v12);
00075     reading.volts_read_item.push_back("5V");
00076     reading.volts_read_value.push_back(diag.v5);
00077     reading.volts_read_item.push_back("3.3V");
00078     reading.volts_read_value.push_back(diag.v33);
00079     reading.volts_read_item.push_back("VIGN");
00080     reading.volts_read_value.push_back(diag.vign);
00081     reading.volts_full_item.push_back("VIN");
00082     reading.volts_full_value.push_back(input_nominal);
00083     reading.volts_full_item.push_back("12V");
00084     reading.volts_full_value.push_back(12.0);
00085     reading.volts_full_item.push_back("5V");
00086     reading.volts_full_value.push_back(5.0);
00087     reading.volts_full_item.push_back("3.3V");
00088     reading.volts_full_value.push_back(3.3);
00089     reading.temperature.push_back(diag.temp);
00090 
00091     // calculate the soc value
00092     double soc = (diag.vin - battery_dead_voltage) / (input_nominal - battery_dead_voltage);
00093     soc *= 100;
00094     reading.input_soc = soc;
00095     reading.header.stamp = ros::Time::now();
00096 
00097     // publish the feedback
00098     diag_pub.publish(reading);
00099 
00100     // check if we need to say something to the user
00101     if (ros::Time::now() > next_check)
00102     {
00103       if (soc < 5)
00104       {
00105         next_check = ros::Time::now() + ros::Duration(5.0 * 60); //5 min
00106         system("espeak \"Computer battery has 5 percent remaining.\"");
00107       }
00108       else if (soc < 10)
00109       {
00110         next_check = ros::Time::now() + ros::Duration(10.0 * 60); //10 min
00111         system("espeak \"Computer battery has 10 percent remaining.\"");
00112       }
00113       else if (soc < 20)
00114       {
00115         next_check = ros::Time::now() + ros::Duration(15.0 * 60); //15 min
00116         system("espeak \"Computer battery has 20 percent remaining.\"");
00117       }
00118       else
00119       {
00120         next_check = ros::Time::now() + ros::Duration(20.0 * 60); //20 min
00121       }
00122     }
00123 
00124     loop_rate.sleep();
00125   }
00126 
00127   //close the USB handle
00128   usb_close(dev);
00129 
00130   return EXIT_SUCCESS;
00131 }


m4atx_battery_monitor
Author(s): Ken Tossell , Chris Dunkers , Russell Toris
autogenerated on Sun Mar 6 2016 12:02:34