segway_rmp200_driver_node.cpp
Go to the documentation of this file.
00001 #include "segway_rmp200_driver_node.h"
00002 
00003 using namespace segway_rmp200_node;
00004 
00005 SegwayRmp200DriverNode::SegwayRmp200DriverNode(ros::NodeHandle &nh) try : 
00006   iri_base_driver::IriBaseNodeDriver<SegwayRmp200Driver>(nh)
00007 {
00008   event_server_ = CEventServer::instance();
00009 
00010   //init class attributes if necessary
00011   this->loop_rate_ = 50;//in [Hz]
00012 
00013   // [init publishers]
00014   this->status_publisher_   = this->public_node_handle_.advertise<iri_segway_rmp_msgs::SegwayRMP200Status>("status", 100);
00015 
00016   // [init subscribers]
00017   this->cmd_platform_subscriber_ = this->public_node_handle_.subscribe("cmd_vel", 100, &SegwayRmp200DriverNode::cmd_platform_callback, this);
00018   
00019   // [init services]
00020   
00021   // [init clients]
00022   
00023   // [init action servers]
00024   
00025   // [init action clients]
00026 }
00027 catch (CException & e)
00028 {
00029   ROS_FATAL("'%s'",e.what().c_str());
00030 }
00031 
00032 void SegwayRmp200DriverNode::mainNodeThread(void)
00033 {
00034   //lock access to driver if necessary
00035   this->driver_.lock();
00036 
00037   segway_event_id_ = event_server_->wait_first(driver_.getSegwayEvents());
00038 
00039   switch(segway_event_id_)
00040   {
00041     case SegwayRmp200Driver::NO_USB:
00042       ROS_WARN("The USB cable has been disconnected");
00043       driver_.goClosed();
00044       break;
00045 
00046     case SegwayRmp200Driver::POWER_OFF:
00047       ROS_WARN("The segway platform power is off");
00048       break;
00049 
00050     case SegwayRmp200Driver::NO_HEART_BEAT:
00051       ROS_WARN("No heart beat detected");
00052       break;
00053 
00054     case SegwayRmp200Driver::NEW_STATUS:
00055       //ROS_DEBUG("New Status message!");
00056       updateSegwayStatus();
00057         this->segway_status_msg_.header.seq = 0;
00058         this->segway_status_msg_.header.stamp = ros::Time::now();
00059         this->segway_status_msg_.header.frame_id = "segway";
00060       status_publisher_.publish(this->segway_status_msg_);
00061       break;
00062   }
00063   
00064   // [fill msg Header if necessary]
00065 
00066   // [fill msg structures]
00067   
00068   // [fill srv structure and make request to the server]
00069   
00070   // [fill action structure and make request to the action server]
00071 
00072   // [publish messages]
00073 
00074   //unlock access to driver if previously blocked
00075   this->driver_.unlock();
00076 }
00077 
00078 void SegwayRmp200DriverNode::updateSegwayStatus()
00079 {
00080   this->segway_status_msg_ = this->driver_.get_status();
00081 }
00082 
00083 /*  [subscriber callbacks] */
00084 void SegwayRmp200DriverNode::cmd_platform_callback(const geometry_msgs::Twist::ConstPtr& msg) 
00085 {
00086   ROS_DEBUG("SegwayRmp200DriverNode::cmd_platform_callback");
00087 
00088   //lock access to driver if necessary 
00089   this->driver_.lock();
00090 
00091   if(this->driver_.isRunning())
00092   {
00093     double vT = msg->linear.x;//sqrt(msg->linear.x*msg->linear.x + msg->linear.y*msg->linear.y);
00094     double vR = msg->angular.z;
00095     ROS_INFO("New Command Received: vT=%f vR=%f", vT, vR);
00096 
00097     //request platform movement
00098     this->driver_.move_platform(vT, vR);
00099   }
00100   else
00101   {
00102     ROS_WARN("Segway driver has not been started yet");
00103   }
00104 
00105   //unlock access to driver if previously blocked 
00106   this->driver_.unlock(); 
00107 }
00108 
00109 /*  [service callbacks] */
00110 
00111 /*  [action callbacks] */
00112 
00113 /*  [action requests] */
00114 
00115 void SegwayRmp200DriverNode::postNodeOpenHook(void)
00116 {
00117 }
00118 
00119 void SegwayRmp200DriverNode::check_configuration(diagnostic_updater::DiagnosticStatusWrapper &stat)
00120 {
00121   switch(segway_event_id_)
00122   {
00123     case SegwayRmp200Driver::NO_USB:
00124       stat.summary(diagnostic_msgs::DiagnosticStatus::ERROR, "USB Cable disconected.");
00125       break;
00126 
00127     case SegwayRmp200Driver::POWER_OFF:
00128       stat.summary(diagnostic_msgs::DiagnosticStatus::WARN, "Platform is powered off.");
00129       break;
00130 
00131     case SegwayRmp200Driver::NO_HEART_BEAT:
00132       stat.summary(diagnostic_msgs::DiagnosticStatus::WARN, "No heart beat received.");
00133       break;
00134 
00135     case SegwayRmp200Driver::NEW_STATUS:
00136       stat.summary(diagnostic_msgs::DiagnosticStatus::OK, "Everything is working properly.");
00137       break;
00138   }
00139   stat.add("Current Segway event: ", driver_.getSegwayEventName(segway_event_id_));
00140 }
00141 
00142 void SegwayRmp200DriverNode::ui_battery_check(diagnostic_updater::DiagnosticStatusWrapper &stat)
00143 {
00144   float ui_battery=0.0;
00145 
00146   ui_battery = this->driver_.get_ui_battery_voltage();
00147   
00148   if(ui_battery>=6.0)
00149     stat.summary(diagnostic_msgs::DiagnosticStatus::OK, "UI battery level is okay.");
00150 
00151   else if(ui_battery>4.0 && ui_battery<6.0)
00152     stat.summary(diagnostic_msgs::DiagnosticStatus::WARN, "UI battery level is low.");
00153 
00154   else
00155     stat.summary(diagnostic_msgs::DiagnosticStatus::ERROR, "UI battery extremelly low, please recharge it as soon as possible");
00156 
00157   stat.add("UI battery level: ", ui_battery);
00158 }
00159 
00160 void SegwayRmp200DriverNode::pb_battery_check(diagnostic_updater::DiagnosticStatusWrapper &stat)
00161 {
00162   float pb_battery=0.0;
00163 
00164   pb_battery = this->driver_.get_powerbase_battery_voltage();
00165   
00166   if(pb_battery>=70.0)
00167     stat.summary(diagnostic_msgs::DiagnosticStatus::OK, "Powerbase battery level is okay.");
00168   
00169   else if(pb_battery>60.0 && pb_battery<70.0)
00170     stat.summary(diagnostic_msgs::DiagnosticStatus::WARN, "Powerbase battery level is low.");
00171   
00172   else
00173     stat.summary(diagnostic_msgs::DiagnosticStatus::ERROR, "Powerbase battery extremelly low, please recharge it as soon as possible");
00174   
00175   stat.add("Powerbase battery level: ", pb_battery);
00176 }
00177 
00178 void SegwayRmp200DriverNode::addNodeDiagnostics(void)
00179 {
00180   this->diagnostic_.add("Error event check", this, &SegwayRmp200DriverNode::check_configuration);
00181   this->diagnostic_.add("UI battery level check", this, &SegwayRmp200DriverNode::ui_battery_check);
00182   this->diagnostic_.add("Powerbase battery level check", this, &SegwayRmp200DriverNode::pb_battery_check);
00183 }
00184 
00185 void SegwayRmp200DriverNode::addNodeOpenedTests(void)
00186 {
00187 }
00188 
00189 void SegwayRmp200DriverNode::addNodeStoppedTests(void)
00190 {
00191 }
00192 
00193 void SegwayRmp200DriverNode::addNodeRunningTests(void)
00194 {
00195 }
00196 
00197 void SegwayRmp200DriverNode::reconfigureNodeHook(int level)
00198 {
00199 }
00200 
00201 SegwayRmp200DriverNode::~SegwayRmp200DriverNode()
00202 {
00203   //[free dynamic memory]
00204 }
00205 
00206 /* main function */
00207 int main(int argc,char *argv[])
00208 {
00209   return driver_base::main<SegwayRmp200DriverNode>(argc,argv,"segway_rmp200_driver_node");
00210 }
00211 


iri_segway_rmp200
Author(s): IRI Lab
autogenerated on Fri Dec 6 2013 20:40:23