multi_driver_node.cpp
Go to the documentation of this file.
00001 
00024 #include "puma_motor_driver/multi_driver_node.h"
00025 #include "puma_motor_driver/driver.h"
00026 #include "puma_motor_msgs/MultiStatus.h"
00027 #include "puma_motor_msgs/Status.h"
00028 #include "puma_motor_msgs/MultiFeedback.h"
00029 #include "puma_motor_msgs/Feedback.h"
00030 #include "boost/foreach.hpp"
00031 #include <cstring>
00032 #include <vector>
00033 #include <ros/ros.h>
00034 
00035 namespace puma_motor_driver
00036 {
00037 
00038 MultiDriverNode::MultiDriverNode(ros::NodeHandle& nh, std::vector<puma_motor_driver::Driver>& drivers)
00039   : nh_(nh), drivers_(drivers), active_(false)
00040   {
00041     feedback_pub_ = nh_.advertise<puma_motor_msgs::MultiFeedback>("feedback", 5);
00042     status_pub_ = nh_.advertise<puma_motor_msgs::MultiStatus>("status", 5);
00043 
00044     feedback_msg_.drivers_feedback.resize(drivers_.size());
00045     status_msg_.drivers.resize(drivers_.size());
00046 
00047     feedback_pub_timer_ = nh_.createTimer(ros::Duration(1.0/25), &MultiDriverNode::feedbackTimerCb, this);
00048     status_pub_timer_ = nh_.createTimer(ros::Duration(1.0/1), &MultiDriverNode::statusTimerCb, this);
00049   }
00050 
00051 void MultiDriverNode::publishFeedback()
00052 {
00053   // Prepare output feedback message to ROS.
00054   uint8_t feedback_index = 0;
00055   BOOST_FOREACH(puma_motor_driver::Driver& driver, drivers_)
00056   {
00057     puma_motor_msgs::Feedback* f = &feedback_msg_.drivers_feedback[feedback_index];
00058     f->device_number = driver.deviceNumber();
00059     f->device_name = driver.deviceName();
00060     f->duty_cycle = driver.lastDutyCycle();
00061     f->current = driver.lastCurrent();
00062     f->travel = driver.lastPosition();
00063     f->speed = driver.lastSpeed();
00064     f->setpoint = driver.lastSetpoint();
00065 
00066     feedback_index++;
00067   }
00068   feedback_msg_.header.stamp = ros::Time::now();
00069   feedback_pub_.publish(feedback_msg_);
00070 }
00071 
00072 void MultiDriverNode::publishStatus()
00073 {
00074   // Prepare output status message to ROS.
00075   uint8_t status_index = 0;
00076   BOOST_FOREACH(puma_motor_driver::Driver& driver, drivers_)
00077   {
00078     puma_motor_msgs::Status* s = &status_msg_.drivers[status_index];
00079     s->device_number = driver.deviceNumber();
00080     s->device_name = driver.deviceName();
00081     s->bus_voltage = driver.lastBusVoltage();
00082     s->output_voltage = driver.lastOutVoltage();
00083     s->analog_input = driver.lastAnalogInput();
00084     s->temperature = driver.lastTemperature();
00085     s->mode = driver.lastMode();
00086     s->fault = driver.lastFault();
00087 
00088     status_index++;
00089   }
00090   status_msg_.header.stamp = ros::Time::now();
00091   status_pub_.publish(status_msg_);
00092 }
00093 
00094 void MultiDriverNode::statusTimerCb(const ros::TimerEvent&)
00095 {
00096   if (active_)
00097   {
00098     publishStatus();
00099   }
00100 }
00101 
00102 void MultiDriverNode::feedbackTimerCb(const ros::TimerEvent&)
00103 {
00104   if (active_)
00105   {
00106     publishFeedback();
00107   }
00108 }
00109 
00110 void MultiDriverNode::activePublishers(bool activate)
00111 {
00112   active_ = activate;
00113 }
00114 
00115 }  // namespace puma_motor_driver


puma_motor_driver
Author(s):
autogenerated on Sat Jun 8 2019 18:55:15