Project: engagement_recognition License: BSD Dependencies:
Used by:
None |
engagement_recognition/src/recognition/recognizers/directed_gaze/directed_gaze_recognizer.cppGo to the documentation of this file.00001 /* 00002 * Software License Agreement (BSD License) 00003 * 00004 * Copyright (c) 2010, Worcester Polytechnic Institute 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 Worcester Polytechnic Institute. nor the names 00018 * of its contributors may be used to endorse or promote products 00019 * derived 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 // @author Brett Ponsler (bponsler (at) wpi (dot) edu) 00035 #include "directed_gaze_recognizer.h" 00036 00037 #include "../../events/event_factory.h" 00038 #include "../../lib/engagement_params.h" 00039 #include "../../lib/engagement_macros.h" 00040 #include "../../objects/engagement_objects.h" 00041 00042 DirectedGazeRecognizer::DirectedGazeRecognizer() : 00043 Recognizer(DirectedGazeRecognizer::type(), NULL) 00044 { 00045 // Set the logger based on this type of recognizer 00046 this->setLogger(EngagementParams::getDGTag("")); 00047 00048 // Load the max delay parameter 00049 this->getMaxDelayParam(EngagementParams::getConfDirGazeHandle(), 2.0); 00050 00051 // Cast ourselves into a Recognizer 00052 Recognizer *recognizer = (Recognizer*)this; 00053 00054 // Create the start state, and set the state to it 00055 recognizer->setStartState(); 00056 this->object_ = std::string(""); 00057 00058 // Grab the recognition/human NodeHandle 00059 ros::NodeHandle *recog_human = EngagementParams::getRecogHumanHandle(); 00060 00061 // Advertise the human initiated directed gaze topic 00062 hi_dir_gaze_pub_ = recog_human->advertise<engagement_msgs::HumanDirectedGaze>( 00063 engagement::RECOG_DIRECTED_GAZE_TOPIC, MAX_BUFFER); 00064 00065 this->warn("Initialized recognizer to an empty actor, and object."); 00066 } 00067 00068 DirectedGazeRecognizer::DirectedGazeRecognizer(std::string actor, 00069 std::string object, 00070 EventSink *sink) : 00071 Recognizer(DirectedGazeRecognizer::type(), sink) 00072 { 00073 // Set the logger based on this type of recognizer 00074 this->setLogger(sink->getLogger(), EngagementParams::getDGTag(object)); 00075 00076 // Load the max delay parameter 00077 this->getMaxDelayParam(EngagementParams::getConfDirGazeHandle(), 2.0); 00078 00079 // Set the actor for this recognizer 00080 // Do this BEFORE setting the state 00081 setActor(actor); 00082 this->object_ = std::string(object); 00083 00084 // Grab the recognition/human NodeHandle 00085 ros::NodeHandle *recog_human = EngagementParams::getRecogHumanHandle(); 00086 00087 // Advertise the human initiated directed gaze topic 00088 hi_dir_gaze_pub_ = recog_human->advertise<engagement_msgs::HumanDirectedGaze>( 00089 engagement::RECOG_DIRECTED_GAZE_TOPIC, MAX_BUFFER); 00090 00091 // Create the start state and set the state to it 00092 this->setStartState(); 00093 } 00094 00095 void DirectedGazeRecognizer::initialize(initiator::Initiator initiator) 00096 { 00097 Recognizer *rec = (Recognizer*)this; 00098 00099 // If the human was the initiator, send a human initiated message 00100 if (initiator == initiator::HUMAN) 00101 { 00102 // Grab the internal node handle 00103 ros::NodeHandle *n = EngagementParams::getConfDirGazeHandle(); 00104 00105 // Grab the max delay from the parameter server 00106 double max_delay = this->getMaxDelayParam(n, 2.0); 00107 00108 // Only send human initiated events if the recognizer is not degraded 00109 if (!EngagementParams::isDegraded()) 00110 { 00111 // Create the human initiated directed gaze message 00112 engagement_msgs::HumanDirectedGaze dg_msg; 00113 00114 // Set the actor, the timeout duration, and the objects for this message 00115 dg_msg.actor.id = this->getActor()->getID(); 00116 dg_msg.timeout = max_delay; 00117 dg_msg.objects = std::vector<bml_msgs::Entity>(); 00118 dg_msg.objects.push_back(objects::objectToEntity(this->object_)); 00119 dg_msg.action = this->action_.toValue(); 00120 00121 this->debug(5, "Publishing human initiated event for action [%s].", 00122 this->action_.toString()); 00123 // Publish the message 00124 this->hi_dir_gaze_pub_.publish(dg_msg); 00125 } 00126 } 00127 00128 // Initialize this recognizer with the initiator 00129 rec->initialize(initiator); 00130 } 00131 00132 void DirectedGazeRecognizer::transition(Event event) 00133 { 00134 std::vector<std::string> additional = event.getAdditional(); 00135 00136 // Check the event cases 00137 if (EventFactory::isRobotInitiated(event)) 00138 { 00139 this->debug(8, "ROBOT INITIATED EVENT!"); 00140 00141 // Unblock the semaphore and enter the robot directing state 00142 this->unblock(ServiceResponse::cycle(), Action::noAction()); 00143 this->setState(factory::DG_ROBOT_DIRECTING); 00144 00145 // The robot directs gaze with points 00146 this->action_ = Action::point(); 00147 this->action_.setData(this->object_); 00148 00149 // Initialize the connection event 00150 this->initialize(initiator::ROBOT); 00151 this->start_delay_ = CURRENT_TIME; 00152 } 00153 else if (EventFactory::isHumanInitiated(event)) 00154 { 00155 this->debug(8, "HUMAN INITIATED EVENT"); 00156 00157 // If we're currently in the robot directing state, then we fail 00158 if (this->getStateName() 00159 == StateFactory::getType(factory::DG_ROBOT_DIRECTING)) 00160 { 00161 // The connection event failed 00162 this->complete(false); 00163 } 00164 00165 // Unblock the semaphore and enter the human directing state 00166 this->unblock(ServiceResponse::cycle(), Action::noAction()); 00167 this->setState(factory::DG_HUMAN_DIRECTING); 00168 00169 if (additional.size() > 0) 00170 { 00171 this->action_ = Action::fromString(additional.at(0)); 00172 this->action_.setData(this->object_); 00173 } 00174 00175 // Initialize the connection event 00176 this->initialize(initiator::HUMAN); 00177 this->start_delay_ = CURRENT_TIME; 00178 } 00179 else if (EventFactory::isSharedGaze(event)) 00180 { 00181 this->debug(8, "SHARED GAZE EVENT"); 00182 00183 // Move to the shared gaze state 00184 this->setState(factory::DG_SHARED_GAZE); 00185 00186 // We have succeeded (look always completes DG) 00187 this->succeed(Action::look(this->object_)); 00188 this->end_delay_ = CURRENT_TIME; 00189 } 00190 else if (EventFactory::isCycle(event)) 00191 { 00192 this->debug(8, "CYCLE EVENT"); 00193 00194 // Unblock the semaphore so it re-blocks 00195 this->unblock(ServiceResponse::cycle(), Action::noAction()); 00196 00197 // Reset the start delay 00198 this->start_delay_ = CURRENT_TIME; 00199 } 00200 else if (EventFactory::isSuccess(event)) 00201 { 00202 this->debug(8, "SUCCESS EVENT"); 00203 00204 // Transition to the start state unless we're already there, which 00205 // we then move to the shared gaze state 00206 if (this->getStateName() != StateFactory::getType(factory::DG_START)) 00207 { 00208 // Move to the start state 00209 this->setStartState(); 00210 00211 // Complete the connection event successfully 00212 this->complete(true); 00213 } 00214 else 00215 { 00216 // Move to the shared gaze state 00217 this->setState(factory::DG_SHARED_GAZE); 00218 00219 // Set the start time, and end and start delay 00220 // (so we have a delay of zero) 00221 this->start_time_ = CURRENT_TIME; 00222 this->start_delay_ = this->start_time_; 00223 this->end_delay_ = this->start_delay_; 00224 00225 // We have succeeded (look always completes DG) 00226 this->succeed(Action::look(this->object_)); 00227 } 00228 } 00229 else if (EventFactory::isFailed(event)) 00230 { 00231 this->debug(8, "FAILED EVENT"); 00232 00233 // Move to the start state 00234 this->setStartState(); 00235 00236 // Complete the connection event unsuccessfully 00237 this->complete(false); 00238 } 00239 } 00240 00241 std::string DirectedGazeRecognizer::getObject() 00242 { 00243 return this->object_; 00244 } 00245 00246 std::string DirectedGazeRecognizer::type() 00247 { 00248 return engagement::DIRECTED_GAZE_TYPE; 00249 } |