projector_controller.cpp
Go to the documentation of this file.
00001 
00002 /*********************************************************************
00003  * Software License Agreement (BSD License)
00004  *
00005  *  Copyright (c) 2009, Willow Garage, Inc.
00006  *  All rights reserved.
00007  *
00008  *  Redistribution and use in source and binary forms, with or without
00009  *  modification, are permitted provided that the following conditions
00010  *  are met:
00011  *
00012  *   * Redistributions of source code must retain the above copyright
00013  *     notice, this list of conditions and the following disclaimer.
00014  *   * Redistributions in binary form must reproduce the above
00015  *     copyright notice, this list of conditions and the following
00016  *     disclaimer in the documentation and/or other materials provided
00017  *     with the distribution.
00018  *   * Neither the name of the Willow Garage nor the names of its
00019  *     contributors may be used to endorse or promote products derived
00020  *     from this software without specific prior written permission.
00021  *
00022  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00023  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00024  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00025  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00026  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00027  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00028  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00029  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00030  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00031  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00032  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00033  *  POSSIBILITY OF SUCH DAMAGE.
00034  *********************************************************************/
00035 #include "ethercat_trigger_controllers/projector_controller.h"
00036 #include "ros/console.h"
00037 #include "pluginlib/class_list_macros.h"
00038 
00039 PLUGINLIB_EXPORT_CLASS( controller::ProjectorController, pr2_controller_interface::Controller)
00040 
00041 using std::string;
00042 using namespace controller;
00043 
00044 ProjectorController::ProjectorController()
00045 {
00046   ROS_DEBUG("creating controller...");
00047 }
00048 
00049 ProjectorController::~ProjectorController()
00050 {
00051 }
00052 
00053 void ProjectorController::update()
00054 {
00056   uint32_t rising = projector_->state_.rising_timestamp_us_;
00057   uint32_t falling = projector_->state_.falling_timestamp_us_;
00058   double curtime = robot_->getTime().toSec();
00059   double delta = curtime - start_time_;
00060   delta -= fmod(delta, 0.001);
00061   
00062   projector_->command_.current_ = current_setting_;
00063 
00064   if (falling != old_falling_)
00065   {
00066     old_falling_ = falling;
00067     if (falling_edge_pub_ && falling_edge_pub_->trylock())
00068     {
00069       //falling_edge_pub_->msg_.stamp = ros::Time(curtime - (stamp - falling) * 1e-6);
00070       //falling_edge_pub_->msg_.stamp = ros::Time((stamp - falling) * 1e-6);
00071       falling_edge_pub_->msg_.stamp = ros::Time(delta);
00072       falling_edge_pub_->unlockAndPublish();
00073     }
00074   }
00075   if (rising != old_rising_)
00076   {
00077     old_rising_ = rising;
00078     if (rising_edge_pub_ && rising_edge_pub_->trylock())
00079     {
00080       //rising_edge_pub_->msg_.stamp = ros::Time(curtime - (stamp - rising) * 1e-6);
00081       //rising_edge_pub_->msg_.stamp = ros::Time((stamp - rising) * 1e-6);
00082       rising_edge_pub_->msg_.stamp = ros::Time(delta);
00083       rising_edge_pub_->unlockAndPublish();
00084     }
00085   }
00086 }
00087 
00088 void ProjectorController::starting()
00089 {
00090   projector_->command_.enable_ = true;
00091   projector_->command_.pulse_replicator_ = false;
00092   //projector_->command_.M_ = 0xf;
00093   old_rising_ = projector_->state_.rising_timestamp_us_;
00094   old_falling_ = projector_->state_.falling_timestamp_us_;
00095   start_time_ = 0;//robot_->getTime().toSec();
00096 }
00097 
00098 void ProjectorController::stopping()
00099 {
00100   projector_->command_.enable_ = false;
00101   projector_->command_.pulse_replicator_ = true;
00102   //projector_->command_.M_ = 0x0;
00103   projector_->command_.current_ = 0;
00104 }
00105 
00106 bool ProjectorController::init(pr2_mechanism_model::RobotState *robot, ros::NodeHandle& n)
00107 {
00108   node_handle_ = n;
00109 
00110   assert(robot);
00111   robot_=robot;
00112 
00113   ROS_DEBUG("ProjectorController::init starting");
00114 
00115   // Get the actuator name.
00116 
00117   if (!n.getParam("actuator", actuator_name_)){
00118     ROS_ERROR("ProjectorController was not given an actuator.");
00119     return false;
00120   }
00121 
00122   rising_edge_pub_.reset(new realtime_tools::RealtimePublisher<std_msgs::Header>(n, "rising_edge_timestamps", 10));
00123   falling_edge_pub_.reset(new realtime_tools::RealtimePublisher<std_msgs::Header>(n, "falling_edge_timestamps", 10));
00124 
00125   projector_ = robot_->model_->hw_->getProjector(actuator_name_);
00126   ROS_DEBUG("Got projector: %p\n", projector_);
00127   if (!projector_)
00128   {
00129     ROS_ERROR("ProjectorController could not find digital out named \"%s\".",
00130         actuator_name_.c_str());
00131     return false;
00132   } 
00133 
00134   n.param("current", current_setting_, 27.0);
00135   ROS_DEBUG("Projector current = %f", current_setting_);
00136     
00137   return true;
00138 }


ethercat_trigger_controllers
Author(s): Blaise Gassend
autogenerated on Sat Jun 8 2019 20:49:12