cob_voltage_control_ros.cpp
Go to the documentation of this file.
00001 // ROS includes
00002 #include <ros/ros.h>
00003 
00004 // ROS message includes
00005 #include <pr2_msgs/PowerBoardState.h>
00006 #include <pr2_msgs/PowerState.h>
00007 #include <cob_relayboard/EmergencyStopState.h>
00008 #include <std_msgs/Bool.h>
00009 #include <std_msgs/Float64.h>
00010 
00011 #include <cob_voltage_control_common.cpp>
00012 #include <cob_phidgets/AnalogSensor.h>
00013 #include <cob_phidgets/DigitalSensor.h>
00014 
00015 
00016 class cob_voltage_control_ros
00017 {
00018 
00019     private:
00020         int EM_stop_status_;
00021 
00022     public:
00023         ros::NodeHandle n_;
00024 
00025         ros::Publisher topicPub_em_stop_state_;
00026         ros::Publisher topicPub_powerstate;
00027 
00028         ros::Publisher topicPub_current_measurement_;
00029         ros::Publisher topicPub_Current;
00030 
00031         ros::Publisher topicPub_Voltage;
00032         ros::Subscriber topicSub_AnalogInputs;
00033         ros::Subscriber topicSub_DigitalInputs;
00034 
00035         cob_voltage_control_data component_data_;
00036         cob_voltage_control_config component_config_;
00037         cob_voltage_control_impl component_implementation_;
00038 
00039         //
00040         bool last_rear_em_state;
00041         bool last_front_em_state;
00042 
00043         // possible states of emergency stop
00044         enum
00045         {
00046           ST_EM_FREE = 0,
00047           ST_EM_ACTIVE = 1,
00048           ST_EM_CONFIRMED = 2
00049         };
00050 
00051         cob_voltage_control_ros()
00052         {
00053             topicPub_powerstate = n_.advertise<pr2_msgs::PowerBoardState>("pub_em_stop_state_", 1);
00054             topicPub_em_stop_state_ = n_.advertise<cob_relayboard::EmergencyStopState>("pub_relayboard_state_", 1);
00055 
00056             topicPub_Current = n_.advertise<std_msgs::Float64>("/power_board/current", 10);
00057 
00058             topicPub_Voltage = n_.advertise<std_msgs::Float64>("/power_board/voltage", 10);
00059             topicSub_AnalogInputs = n_.subscribe("/analog_sensors", 10, &cob_voltage_control_ros::analogPhidgetSignalsCallback, this);
00060             topicSub_DigitalInputs = n_.subscribe("/digital_sensors", 10, &cob_voltage_control_ros::digitalPhidgetSignalsCallback, this);
00061                 
00062             n_.param("battery_max_voltage", component_config_.max_voltage, 50.0);
00063             n_.param("battery_min_voltage", component_config_.min_voltage, 44.0);
00064             n_.param("robot_max_voltage", component_config_.max_voltage_res, 70.0);
00065             n_.param("voltage_analog_port", component_config_.num_voltage_port, 1);
00066             n_.param("em_stop_dio_port", component_config_.num_em_stop_port, 0); 
00067             n_.param("scanner_stop_dio_port", component_config_.num_scanner_em_port, 1);
00068         
00069             last_rear_em_state = false;
00070             last_front_em_state = false;
00071 
00072             EM_stop_status_ = ST_EM_ACTIVE;
00073             component_data_.out_pub_relayboard_state.scanner_stop = false;
00074         }
00075 
00076         void configure()
00077         {
00078             component_implementation_.configure();
00079         }
00080 
00081         void update()
00082         {
00083             component_implementation_.update(component_data_, component_config_);
00084             topicPub_Voltage.publish(component_data_.out_pub_voltage);
00085             topicPub_Current.publish(component_data_.out_pub_current);
00086             topicPub_powerstate.publish(component_data_.out_pub_em_stop_state_);
00087             topicPub_em_stop_state_.publish(component_data_.out_pub_relayboard_state);
00088         }
00089         
00090         void analogPhidgetSignalsCallback(const cob_phidgets::AnalogSensorConstPtr &msg)
00091         {
00092             for(int i = 0; i < msg->uri.size(); i++)
00093             {
00094                 std::cout << "u" << msg->uri[i] << std::endl;
00095                 if( msg->uri[i] == "bat1")
00096                 {
00097                     component_data_.in_phidget_voltage = msg->value[i];
00098                     break;
00099                 }
00100             }
00101         }
00102 
00103         void digitalPhidgetSignalsCallback(const cob_phidgets::DigitalSensorConstPtr &msg)
00104         {
00105             bool front_em_active = false;
00106             bool rear_em_active = false;
00107             static bool em_caused_by_button = false;
00108             cob_relayboard::EmergencyStopState EM_msg;
00109             bool EM_signal = false;
00110             bool got_message = false;
00111 
00112             for(int i = 0; i < msg->uri.size(); i++)
00113             {
00114                 if( msg->uri[i] == "em_stop_laser_rear")
00115                 {
00116                     rear_em_active = !((bool)msg->state[i]);
00117                     got_message = true;
00118                 }
00119                 else if( msg->uri[i] == "em_stop_laser_front")
00120                 {
00121                     front_em_active = !((bool)msg->state[i]);
00122                     got_message = true;
00123                 }
00124             }
00125             if(got_message)
00126             {
00127                                 if( (front_em_active && rear_em_active) && (!last_front_em_state && !last_rear_em_state))
00128                                 {
00129                                         component_data_.out_pub_relayboard_state.emergency_button_stop = true;
00130                                         em_caused_by_button = true;
00131                                 }
00132                                 else if((!front_em_active && !rear_em_active) && (last_front_em_state && last_rear_em_state))
00133                                 {
00134                                         component_data_.out_pub_relayboard_state.emergency_button_stop = false;
00135                                         em_caused_by_button = false;
00136                                 }
00137                                 else if((front_em_active != rear_em_active) && em_caused_by_button)
00138                                 {
00139                                         component_data_.out_pub_relayboard_state.emergency_button_stop = false;
00140                                         em_caused_by_button = false;
00141                                         component_data_.out_pub_relayboard_state.scanner_stop = (bool)(front_em_active | rear_em_active);
00142                                 }
00143                                 else
00144                                 {
00145                                         component_data_.out_pub_relayboard_state.scanner_stop = (bool)(front_em_active | rear_em_active);
00146                                         ROS_INFO_STREAM("scanner_stop: "<<component_data_.out_pub_relayboard_state.scanner_stop);
00147                                 }
00148 
00149                                 EM_signal = component_data_.out_pub_relayboard_state.scanner_stop | component_data_.out_pub_relayboard_state.emergency_button_stop;
00150 
00151                                 switch (EM_stop_status_)
00152                                 {
00153                                         case ST_EM_FREE:
00154                                         {
00155                                                 if (EM_signal == true)
00156                                                 {
00157                                                         ROS_INFO("Emergency stop was issued");
00158                                                         EM_stop_status_ = EM_msg.EMSTOP;
00159                                                 }
00160                                                 break;
00161                                         }
00162                                         case ST_EM_ACTIVE:
00163                                         {
00164                                                 if (EM_signal == false)
00165                                                 {
00166                                                         ROS_INFO("Emergency stop was confirmed");
00167                                                         EM_stop_status_ = EM_msg.EMCONFIRMED;
00168                                                 }
00169                                                 break;
00170                                         }
00171                                         case ST_EM_CONFIRMED:
00172                                         {
00173                                                 if (EM_signal == true)
00174                                                 {
00175                                                         ROS_INFO("Emergency stop was issued");
00176                                                         EM_stop_status_ = EM_msg.EMSTOP;
00177                                                 }
00178                                                 else
00179                                                 {
00180                                                         ROS_INFO("Emergency stop released");
00181                                                         EM_stop_status_ = EM_msg.EMFREE;
00182                                                 }
00183                                                 break;
00184                                         }
00185                                 };
00186 
00187 
00188                                 component_data_.out_pub_relayboard_state.emergency_state = EM_stop_status_;
00189 
00190                                 last_front_em_state = front_em_active;
00191                                 last_rear_em_state = rear_em_active;
00192                         }
00193         }
00194 };
00195 
00196 int main(int argc, char** argv)
00197 {
00198 
00199     ros::init(argc, argv, "cob_voltage_control");
00200 
00201     cob_voltage_control_ros node;
00202     node.configure();
00203 
00204     ros::Rate loop_rate(100); // Hz // if cycle time == 0 do a spin() here without calling node.update() 
00205 
00206     while(node.n_.ok())
00207     {
00208         node.update();
00209         loop_rate.sleep();
00210         ros::spinOnce();
00211     }
00212     return 0;
00213 }


cob_voltage_control
Author(s): Alexander Bubeck
autogenerated on Thu Aug 27 2015 12:45:45