ridgeback_hardware.cpp
Go to the documentation of this file.
00001 
00035 #include <vector>
00036 #include "boost/assign.hpp"
00037 #include "boost/shared_ptr.hpp"
00038 #include "ridgeback_base/ridgeback_hardware.h"
00039 #include "puma_motor_driver/driver.h"
00040 #include "puma_motor_driver/multi_driver_node.h"
00041 
00042 namespace ridgeback_base
00043 {
00044 
00045 RidgebackHardware::RidgebackHardware(ros::NodeHandle& nh, ros::NodeHandle& pnh,
00046                                      puma_motor_driver::Gateway& gateway):
00047   nh_(nh),
00048   pnh_(pnh),
00049   gateway_(gateway),
00050   active_(false)
00051 {
00052   pnh_.param<double>("gear_ratio", gear_ratio_, 34.97);
00053   pnh_.param<int>("encoder_cpr", encoder_cpr_, 1024);
00054 
00055   ros::V_string joint_names = boost::assign::list_of("front_left_wheel")
00056       ("front_right_wheel")("rear_left_wheel")("rear_right_wheel");
00057   std::vector<uint8_t> joint_canids = boost::assign::list_of(5)(4)(2)(3);
00058   std::vector<float> joint_directions = boost::assign::list_of(-1)(1)(-1)(1);
00059 
00060   for (uint8_t i = 0; i < joint_names.size(); i++)
00061   {
00062     hardware_interface::JointStateHandle joint_state_handle(joint_names[i],
00063         &joints_[i].position, &joints_[i].velocity, &joints_[i].effort);
00064     joint_state_interface_.registerHandle(joint_state_handle);
00065 
00066     hardware_interface::JointHandle joint_handle(
00067         joint_state_handle, &joints_[i].velocity_command);
00068     velocity_joint_interface_.registerHandle(joint_handle);
00069 
00070     puma_motor_driver::Driver driver(gateway_, joint_canids[i], joint_names[i]);
00071     driver.clearStatusCache();
00072     driver.setEncoderCPR(encoder_cpr_);
00073     driver.setGearRatio(gear_ratio_ * joint_directions[i]);
00074     driver.setMode(puma_motor_msgs::Status::MODE_SPEED, 0.1, 0.01, 0.0);
00075     drivers_.push_back(driver);
00076   }
00077 
00078   registerInterface(&joint_state_interface_);
00079   registerInterface(&velocity_joint_interface_);
00080 
00081   multi_driver_node_.reset(new puma_motor_driver::MultiDriverNode(nh_, drivers_));
00082 }
00083 
00090 void RidgebackHardware::updateJointsFromHardware()
00091 {
00092   uint8_t index = 0;
00093   BOOST_FOREACH(puma_motor_driver::Driver& driver, drivers_)
00094   {
00095     Joint* f = &joints_[index];
00096     f->effort = driver.lastCurrent();
00097     f->position = driver.lastPosition();
00098     f->velocity = driver.lastSpeed();
00099     index++;
00100   }
00101 }
00102 
00103 bool RidgebackHardware::isActive()
00104 {
00105   if (active_ == false
00106      && drivers_[0].isConfigured() == true
00107      && drivers_[1].isConfigured() == true
00108      && drivers_[2].isConfigured() == true
00109      && drivers_[3].isConfigured() == true)
00110   {
00111     active_ = true;
00112     multi_driver_node_->activePublishers(active_);
00113     ROS_INFO("Ridgeback Hardware Active.");
00114   }
00115   else if ((drivers_[0].isConfigured() == false
00116     || drivers_[1].isConfigured() == false
00117     || drivers_[2].isConfigured() == false
00118     || drivers_[3].isConfigured() == false)
00119     && active_ == true)
00120   {
00121     active_ = false;
00122     ROS_WARN("Ridgeback Hardware Inactive.");
00123   }
00124 
00125   return active_;
00126 }
00127 
00128 void RidgebackHardware::requestData()
00129 {
00130   BOOST_FOREACH(puma_motor_driver::Driver& driver, drivers_)
00131   {
00132     driver.requestFeedbackPowerState();
00133   }
00134 }
00135 void RidgebackHardware::powerHasNotReset()
00136 {
00137   // Checks to see if power flag has been reset for each driver
00138   BOOST_FOREACH(puma_motor_driver::Driver& driver, drivers_)
00139   {
00140     if (driver.lastPower() != 0)
00141     {
00142       active_ = false;
00143       ROS_WARN("There was a power rest on Dev: %d, will reconfigure all drivers.", driver.deviceNumber());
00144       multi_driver_node_->activePublishers(active_);
00145       BOOST_FOREACH(puma_motor_driver::Driver& driver, drivers_)
00146       {
00147         driver.resetConfiguration();
00148       }
00149     }
00150   }
00151 }
00152 
00153 void RidgebackHardware::configure()
00154 {
00155   BOOST_FOREACH(puma_motor_driver::Driver& driver, drivers_)
00156   {
00157     driver.configureParams();
00158   }
00159 }
00160 void RidgebackHardware::verify()
00161 {
00162   BOOST_FOREACH(puma_motor_driver::Driver& driver, drivers_)
00163   {
00164     driver.verifyParams();
00165   }
00166 }
00167 
00168 bool RidgebackHardware::inReset()
00169 {
00170   return !active_;
00171 }
00172 
00173 void RidgebackHardware::init()
00174 {
00175   while (!connectIfNotConnected())
00176   {
00177     ros::Duration(1.0).sleep();
00178   }
00179 }
00180 
00181 void RidgebackHardware::canRead()
00182 {
00183   // Process all received messages through the connected driver instances.
00184   puma_motor_driver::Message recv_msg;
00185   while (gateway_.recv(&recv_msg))
00186   {
00187     BOOST_FOREACH(puma_motor_driver::Driver& driver, drivers_)
00188     {
00189       driver.processMessage(recv_msg);
00190     }
00191   }
00192 }
00193 
00194 void RidgebackHardware::canSend()
00195 {
00196   gateway_.sendAllQueued();
00197 }
00198 
00199 bool RidgebackHardware::connectIfNotConnected()
00200 {
00201   if (!gateway_.isConnected())
00202   {
00203     if (!gateway_.connect())
00204     {
00205       ROS_ERROR("Error connecting to motor driver gateway. Retrying in 1 second.");
00206       return false;
00207     }
00208     else
00209     {
00210       ROS_INFO("Connection to motor driver gateway successful.");
00211     }
00212   }
00213   return true;
00214 }
00215 
00221 void RidgebackHardware::command()
00222 {
00223   uint8_t i = 0;
00224   BOOST_FOREACH(puma_motor_driver::Driver& driver, drivers_)
00225   {
00226     driver.commandSpeed(joints_[i].velocity_command);
00227     i++;
00228   }
00229 }
00230 
00231 std::vector<puma_motor_driver::Driver>& RidgebackHardware::getDrivers()
00232 {
00233     return drivers_;
00234 }
00235 
00236 }  // namespace ridgeback_base


ridgeback_base
Author(s): Mike Purvis , Tony Baltovski
autogenerated on Sun Mar 24 2019 03:01:13