batteries_monitor.cpp
Go to the documentation of this file.
00001 #include "ros/ros.h"
00002 #include <iostream>
00003 #include "komodo_batteries_monitor/batteries_monitor.h"
00004 #include "std_msgs/Float32.h"
00005 #include "dynamixel_msgs/MotorStateList.h"
00006 #include "dynamixel_msgs/MotorState.h"
00007 
00008 ros::Publisher batteries_pub;
00009 
00010 ros::Timer rover_timer;
00011 bool rover_timer_running = true;
00012 
00013 ros::Timer arm_timer;
00014 bool arm_timer_running = true;
00015 
00016 ros::Timer pc_timer;
00017 bool pc_timer_running = true;
00018 bool pc_first = true, arm_first = true, rover_first = true;
00019 
00020 komodo_batteries_monitor::batteries_monitor bat_array;
00021 
00022 double pc_bat_voltage_divider, pc_bat_min_voltage, pc_bat_max_voltage,
00023                 arm_bat_min_voltage, arm_bat_max_voltage, rover_bat_min_voltage,
00024                 rover_bat_max_voltage;
00025 bool turn_off_enable = true;
00026 int countdown = 29;
00027 
00028 void rover_timerCallback(const ros::TimerEvent&) {
00029 
00030         //ROS_ERROR("ROVER BATTERY UNDER VOLTAGE!!!!");
00031         ROS_ERROR("Rover battery warnning! LOW VOLTAGE!!! [V=%.1f]",
00032                         bat_array.rover_bat_v);
00033         rover_timer_running = false;
00034 
00035 }
00036 void arm_timerCallback(const ros::TimerEvent&) {
00037 
00038         //ROS_ERROR("ARM BATTERY UNDER VOLTAGE!!!!");
00039         ROS_ERROR("Arm battery warnning! LOW VOLTAGE!!! [V=%.1f]",
00040                         bat_array.arm_bat_v);
00041         arm_timer_running = false;
00042 
00043 }
00044 void pc_timerCallback(const ros::TimerEvent&) {
00045 
00046         if (turn_off_enable) {
00047 
00048                 if (countdown > 0) {
00049                         ROS_ERROR(
00050                                         "PC battery warnning! LOW VOLTAGE!!! Turning computer off in %d seconds...  [V=%.1f]",
00051                                         countdown, bat_array.pc_bat_v);
00052                         countdown = countdown - 1;
00053 
00054                 } else {
00055                         ROS_ERROR(
00056                                         "PC battery warnning! LOW VOLTAGE!!! Turning computer off now...  [V=%.1f]",
00057                                         bat_array.pc_bat_v);
00058                         //system("sudo shutdown -c");
00059                         system("sudo shutdown -h now");
00060                         pc_timer.stop();
00061                         pc_timer_running = false;
00062                 }
00063         } else {
00064 
00065                 if (countdown > 0) {
00066                         ROS_ERROR(
00067                                         "PC battery warnning! LOW VOLTAGE!!! Turning computer off in %d seconds...  [V=%.1f]",
00068                                         countdown, bat_array.pc_bat_v);
00069                         countdown = countdown - 1;
00070 
00071                 } else {
00072 
00073                         ROS_ERROR(
00074                                         "PC battery warnning! LOW VOLTAGE!!! Turning computer off disabled...  [V=%.1f]",
00075                                         bat_array.pc_bat_v);
00076                         pc_timer_running = false;
00077                         countdown = 29;
00078                 }
00079         }
00080 
00081 }
00082 
00083 void pc_bat_Callback(const std_msgs::Float32::ConstPtr& bat) {
00084         bat_array.pc_bat_v = bat->data * (pc_bat_voltage_divider);
00085         bat_array.pc_bat_time = ros::Time::now();
00086 
00087         if (bat_array.pc_bat_v < pc_bat_min_voltage) {
00088                 if (pc_timer_running == false) {
00089                         if (pc_first)
00090                                 pc_first = false;
00091                         pc_timer_running = true;
00092                         pc_timer.stop();
00093                         pc_timer.start();
00094                         ROS_ERROR(
00095                                         "PC battery warnning! LOW VOLTAGE!!! Turning computer off in 30 seconds...  [V=%.1f]",
00096                                         bat_array.pc_bat_v);
00097                 } else {
00098                         if (pc_first) {
00099                                 pc_first = false;
00100                                 pc_timer_running = true;
00101                                 pc_timer.stop();
00102                                 pc_timer.start();
00103                                 ROS_ERROR(
00104                                                 "PC battery warnning! LOW VOLTAGE!!! Turning computer off in 30 seconds...  [V=%.1f]",
00105                                                 bat_array.pc_bat_v);
00106                         }
00107                 }
00108         } else {
00109                 if (pc_timer_running == true) {
00110                         pc_timer.stop();
00111                         pc_timer_running = false;
00112                         ROS_INFO("PC battery OK [V=%.1f]", bat_array.pc_bat_v);
00113                         countdown = 29;
00114                 }
00115 
00116         }
00117 
00118         batteries_pub.publish(bat_array);
00119 
00120 }
00121 
00122 void rover_bat_Callback(const std_msgs::Float32::ConstPtr& bat) {
00123         bat_array.rover_bat_v = bat->data;
00124         bat_array.rover_bat_time = ros::Time::now();
00125 
00126         if (bat_array.rover_bat_v < rover_bat_min_voltage) {
00127                 if (rover_timer_running == false) {
00128                         if (rover_first)
00129                                 rover_first = false;
00130                         rover_timer_running = true;
00131                         rover_timer.stop();
00132                         rover_timer.start();
00133                 } else {
00134                         if (rover_first) {
00135                                 rover_first = false;
00136                                 rover_timer_running = true;
00137                                 rover_timer.stop();
00138                                 rover_timer.start();
00139                         }
00140                         //ROS_ERROR("You should turn the rover off...[V=%.1f]", bat_array.rover_bat_v);
00141                 }
00142         } else {
00143                 if (rover_timer_running == true) {
00144                         rover_timer.stop();
00145                         rover_timer_running = false;
00146                         ROS_INFO("Rover battery OK [V=%.1f]", bat_array.rover_bat_v);
00147                 }
00148 
00149         }
00150         //      ROS_INFO("array:%f   %f   %f",bat_array.pc_bat_v, bat_array.arm_bat_v , bat_array.rover_bat_v);
00151         batteries_pub.publish(bat_array);
00152 
00153 }
00154 void arm_bat_Callback(const dynamixel_msgs::MotorStateList::ConstPtr& bat) {
00155 
00156         dynamixel_msgs::MotorState m1 = bat->motor_states[0];
00157         bat_array.arm_bat_v = m1.voltage;
00158         bat_array.arm_bat_time = ros::Time::now();
00159 
00160         //ROS_INFO("array:%f   %f   %f", bat_array.pc_bat_v, bat_array.arm_bat_v,bat_array.rover_bat_v);
00161 
00162         if (bat_array.arm_bat_v < arm_bat_min_voltage) {
00163                 if (arm_timer_running == false) {
00164                         if (arm_first)
00165                                 arm_first = false;
00166                         arm_timer_running = true;
00167                         arm_timer.stop();
00168                         arm_timer.start();
00169                 } else {
00170                         if (arm_first) {
00171                                 arm_first = false;
00172                                 arm_timer_running = true;
00173                                 arm_timer.stop();
00174                                 arm_timer.start();
00175                         }
00176                         //ROS_ERROR("You should turn the arm off...[V=%.1f]",bat_array.arm_bat_v);
00177                 }
00178         } else {
00179                 if (arm_timer_running == true) {
00180                         arm_timer.stop();
00181                         arm_timer_running = false;
00182                         ROS_INFO("Arm battery OK [V=%.1f]", bat_array.arm_bat_v);
00183                 }
00184 
00185         }
00186         batteries_pub.publish(bat_array);
00187 }
00188 
00189 int main(int argc, char **argv) {
00190 
00191         // ros::init(argc, argv, "apm_talker");
00192         ros::init(argc, argv, "batteries_monitor_node");
00193 
00194         ros::NodeHandle n("~");
00195 
00196         n.param("pc_bat_voltage_divider", pc_bat_voltage_divider, 3.0);
00197         n.param("pc_bat_min_voltage", pc_bat_min_voltage, 11.0);
00198         //n.param("pc_bat_max_voltage", pc_bat_max_voltage, 25.0);
00199 
00200         n.param("arm_bat_min_voltage", arm_bat_min_voltage, 11.0);
00201         //n.param("arm_bat_max_voltage", arm_bat_max_voltage, 25.0);
00202 
00203         n.param("rover_bat_min_voltage", rover_bat_min_voltage, 22.0);
00204         //n.param("rover_bat_max_voltage", rover_bat_max_voltage, 25.0);
00205         n.param("turn_off_enable", turn_off_enable, true);
00206 
00207         ros::Subscriber pc_bat_sub = n.subscribe("/pc_bat", 10, pc_bat_Callback);
00208 
00209         ros::Subscriber rover_bat_sub = n.subscribe("/rover_bat", 10,
00210                         rover_bat_Callback);
00211 
00212         ros::Subscriber arm_bat_sub = n.subscribe("/motor_states/arm_port", 10,
00213                         arm_bat_Callback);
00214 
00215         batteries_pub = n.advertise < komodo_batteries_monitor::batteries_monitor
00216                         > ("/batteries_monitor", 10);
00217 
00218         rover_timer = n.createTimer(ros::Duration(5), rover_timerCallback, true);
00219         rover_timer.stop();
00220         pc_timer = n.createTimer(ros::Duration(1), pc_timerCallback, false);
00221         pc_timer.stop();
00222         arm_timer = n.createTimer(ros::Duration(5), arm_timerCallback, true);
00223         arm_timer.stop();
00224         //bat_array.header=;
00225         bat_array.pc_bat_v = -1;
00226         bat_array.arm_bat_v = -1;
00227         bat_array.rover_bat_v = -1;
00228 
00229         //ROS_INFO("array:%f   %f   %f",bat_array.pc_bat_v, bat_array.arm_bat_v , bat_array.rover_bat_v);
00230         ros::spin();
00231 
00232         return 0;
00233 }
00234 


komodo_batteries_monitor
Author(s): RoboTiCan
autogenerated on Tue Jan 7 2014 11:19:57