00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00060 #include "ethercat_trigger_controllers/trigger_controller.h"
00061 #include "ros/console.h"
00062 #include "pluginlib/class_list_macros.h"
00063
00064 PLUGINLIB_DECLARE_CLASS(ethercat_trigger_controllers, TriggerController, controller::TriggerController, pr2_controller_interface::Controller)
00065
00066 using std::string;
00067 using namespace controller;
00068
00069 TriggerController::TriggerController()
00070 {
00071 ROS_DEBUG("creating controller...");
00072 }
00073
00074 TriggerController::~TriggerController()
00075 {
00076 }
00077
00078 double TriggerController::getTick()
00079 {
00080 return getTick(robot_->getTime(), config_);
00081 }
00082
00083 void TriggerController::update()
00084 {
00085 ros::Time curtime = robot_->getTime();
00086 double tick = getTick(curtime, config_);
00087 bool active = false;
00088
00089 if (config_.running)
00090 {
00091 if (config_.pulsed)
00092 {
00093 active = (floor(prev_tick_) != floor(tick));
00094
00095
00096 }
00097 else
00098 {
00099 active = fmod(tick, 1) < config_.duty_cycle;
00100
00101
00102 }
00103 }
00104
00105
00106
00107
00108 if (digital_out_command_->data_ != last_out_)
00109 {
00110
00111 }
00112
00113 digital_out_command_->data_ = active ^ config_.active_low;
00114
00115 if (last_out_ && !digital_out_command_->data_)
00116 {
00117 if (falling_edge_pub_ && falling_edge_pub_->trylock())
00118 {
00119 falling_edge_pub_->msg_.stamp = curtime;
00120 falling_edge_pub_->unlockAndPublish();
00121
00122 }
00123 else
00124 {
00125
00126 }
00127 }
00128 else if (!last_out_ && digital_out_command_->data_)
00129 {
00130 if (rising_edge_pub_ && rising_edge_pub_->trylock())
00131 {
00132 rising_edge_pub_->msg_.stamp = curtime;
00133 rising_edge_pub_->unlockAndPublish();
00134
00135 }
00136 else
00137 {
00138
00139 }
00140 }
00141
00142
00143
00144 last_out_ = digital_out_command_->data_;
00145
00146 prev_tick_ = tick;
00147 }
00148
00149 bool TriggerController::init(pr2_mechanism_model::RobotState *robot, ros::NodeHandle& n)
00150 {
00151 node_handle_ = n;
00152
00153 ROS_DEBUG("LOADING TRIGGER CONTROLLER NODE");
00154
00155
00156
00157 assert(robot);
00158 robot_=robot;
00159
00160 ROS_DEBUG("TriggerController::init starting");
00161
00162
00163
00164 if (!n.getParam("actuator", actuator_name_)){
00165 ROS_ERROR("TriggerController was not given an actuator.");
00166 return false;
00167 }
00168
00169 pr2_hardware_interface::DigitalOut *digital_out = robot_->model_->hw_->getDigitalOut(actuator_name_);
00170 if (!digital_out)
00171 {
00172 ROS_ERROR("TriggerController could not find digital out named \"%s\".",
00173 actuator_name_.c_str());
00174 return false;
00175 }
00176
00177 digital_out_command_ = &digital_out->command_;
00178 digital_out_command_->data_ = false;
00179 last_out_ = false;
00180
00181
00182
00183 #define bparam(name, var, val) \
00184 {\
00185 bool tmp;\
00186 n.param(name, tmp, val);\
00187 var = tmp;\
00188 }
00189 n.param("rep_rate", config_.rep_rate, 1.);
00190 n.param("phase", config_.phase, 0.);
00191 n.param("duty_cycle", config_.duty_cycle, .5);
00192 bparam("active_low", config_.active_low, false);
00193 bparam("running", config_.running, false);
00194 bparam("pulsed", config_.pulsed, true);
00195 #undef bparam
00196
00197 prev_tick_ = getTick();
00198
00199 set_waveform_handle_ = node_handle_.advertiseService("set_waveform", &TriggerController::setWaveformSrv, this);
00200
00201 rising_edge_pub_.reset(new realtime_tools::RealtimePublisher<roslib::Header>(n, "rising_edge_timestamps", 10));
00202 falling_edge_pub_.reset(new realtime_tools::RealtimePublisher<roslib::Header>(n, "falling_edge_timestamps", 10));
00203
00204 ROS_DEBUG("TriggerController::init completed successfully"
00205 " rr=%f ph=%f al=%i r=%i p=%i dc=%f.",
00206 config_.rep_rate, config_.phase, config_.active_low, config_.running, config_.pulsed, config_.duty_cycle);
00207
00208 return true;
00209 }
00210
00211 bool TriggerController::setWaveformSrv(
00212 trigger_configuration &req,
00213 ethercat_trigger_controllers::SetWaveform::Response &resp)
00214 {
00215
00216
00217 config_.running = false;
00218 config_.rep_rate = req.rep_rate;
00219 config_.phase = req.phase;
00220 config_.duty_cycle = req.duty_cycle;
00221 config_.active_low = !!req.active_low;
00222 config_.pulsed = !!req.pulsed;
00223 config_.running = !!req.running;
00224
00225 ROS_DEBUG("TriggerController::setWaveformSrv completed successfully"
00226 " rr=%f ph=%f al=%i r=%i p=%i dc=%f.", config_.rep_rate, config_.phase,
00227 config_.active_low, config_.running, config_.pulsed, config_.duty_cycle);
00228
00229 return true;
00230 }