00001 /********************************************************************* 00002 * Software License Agreement (BSD License) 00003 * 00004 * Copyright (c) 2008, Willow Garage, Inc. 00005 * All rights reserved. 00006 * 00007 * Redistribution and use in source and binary forms, with or without 00008 * modification, are permitted provided that the following conditions 00009 * are met: 00010 * 00011 * * Redistributions of source code must retain the above copyright 00012 * notice, this list of conditions and the following disclaimer. 00013 * * Redistributions in binary form must reproduce the above 00014 * copyright notice, this list of conditions and the following 00015 * disclaimer in the documentation and/or other materials provided 00016 * with the distribution. 00017 * * Neither the name of the Willow Garage nor the names of its 00018 * contributors may be used to endorse or promote products derived 00019 * from this software without specific prior written permission. 00020 * 00021 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00022 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00023 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 00024 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 00025 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 00026 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 00027 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00028 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00029 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00030 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 00031 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00032 * POSSIBILITY OF SUCH DAMAGE. 00033 *********************************************************************/ 00034 00035 #ifndef RIQ_HAND_ETHERCAT_HARDWARE__RIQ_HAND_H 00036 #define RIQ_HAND_ETHERCAT_HARDWARE__RIQ_HAND_H 00037 00038 #include "ethercat_hardware/ethercat_device.h" 00039 #include "riq_msgs/RIQHandCommand.h" 00040 #include "riq_msgs/RIQHandState.h" 00041 #include "riq_hand_ethercat_hardware/ethercat_al.h" 00042 #include <boost/thread/mutex.hpp> 00043 #include "realtime_tools/realtime_publisher.h" 00044 00045 00046 namespace riq_hand_ethercat_hardware 00047 { 00048 00049 struct RIQHandDiagnostics 00050 { 00051 RIQHandDiagnostics(); 00052 ECatALStatusAll al_status_; 00053 bool al_status_good_; 00054 }; 00055 00056 class RIQHand : public EthercatDevice 00057 { 00058 public: 00059 RIQHand(); 00060 ~RIQHand(); 00061 void construct(EtherCAT_SlaveHandler *sh, int &start_address); 00062 int initialize(pr2_hardware_interface::HardwareInterface *, bool); 00063 void packCommand(unsigned char *buffer, bool halt, bool reset); 00064 bool unpackState(unsigned char *this_buffer, unsigned char *prev_buffer); 00065 void diagnostics(diagnostic_updater::DiagnosticStatusWrapper &d, unsigned char *); 00066 void collectDiagnostics(EthercatCom *com); 00067 00068 static const unsigned COMMAND_PHY_ADDR = 0x1100; 00069 static const unsigned COMMAND_SIZE = 12; 00070 static const unsigned STATUS_PHY_ADDR = 0x1D00; 00071 static const unsigned STATUS_SIZE = 12; 00072 00073 static const unsigned MBX_COMMAND_PHY_ADDR = 0x1000; 00074 static const unsigned MBX_COMMAND_SIZE = 128; 00075 static const unsigned MBX_STATUS_PHY_ADDR = 0x1080; 00076 static const unsigned MBX_STATUS_SIZE = 128; 00077 00078 enum {PRODUCT_CODE = 11}; 00079 00080 protected: 00081 00083 static void sizeAssert(); 00084 00086 static void disableWatchdog(EtherCAT_SlaveHandler *sh); 00087 00089 void commandCallback(const riq_msgs::RIQHandCommand::ConstPtr& msg); 00090 ros::Subscriber commandSubscriber_; 00091 00093 realtime_tools::RealtimePublisher<riq_msgs::RIQHandState> publisher_; 00094 unsigned state_cycle_count_; 00095 static const unsigned STATE_PUBLISH_PERIOD = 10; 00096 00097 bool should_publish_; 00098 00099 00101 boost::mutex command_mutex_; 00102 00104 riq_msgs::RIQHandCommand new_command_; 00106 riq_msgs::RIQHandCommand command_; 00107 00109 unsigned scaleAndSaturate255(double value); 00110 00112 RIQHandDiagnostics collect_diagnostics_; 00114 boost::mutex diagnostics_mutex_; 00115 00117 RIQHandDiagnostics publish_diagnostics_; 00118 00120 // Watchdog reset will performed by non-realtime thread and flag will be 00121 // cleared when reset is completed successfully 00122 volatile bool needs_watchdog_reset_; 00123 00125 // Outwardly, reset is just like calibration (homing) of PR2. 00126 // When device is released from reset, it moves finger back and forth to find end-points 00127 bool needs_reset_; 00129 int needs_reset_countdown_; 00130 00131 00133 // Used by diagnostics publish thread to determine what error message to display. 00134 volatile unsigned latched_fault_code_; 00135 00136 // When a FAULT_MAJOR_SCISSORS_DISPLACEMENT, FAULT_MAJOR_FINGER_DISPLACEMENT, or watchdog 00137 // timeout has occurred, the software will clear the error code once reset_motors is called. 00138 // However, the fault code from the device will still be present for some number of cycles 00139 // when initializing. These downcounters prevents driver from imediately triggering 00140 // halt state after after reset_motors is called because fault code has not yet cleared 00142 int after_reset_countdown_; 00144 int after_watchdog_reset_countdown_; 00145 00147 bool halted_; 00148 }; 00149 00150 }; //namespace riq_hand_ethercat_hardware 00151 00152 #endif /* RIQ_HAND_ETHERCAT_HARDWARE__RIQ_HAND_H */