Project: engagement_recognition License: BSD Dependencies:
Used by:
None |
engagement_recognition/src/recognition/recognizers/adjacency_pair/adjacency_pair_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 "adjacency_pair_recognizer.h" 00036 00037 #include "../../events/event_factory.h" 00038 #include "../../lib/engagement_macros.h" 00039 00040 AdjacencyPairRecognizer::AdjacencyPairRecognizer() : 00041 Recognizer(AdjacencyPairRecognizer::type(), NULL) 00042 { 00043 // Set the logger based on this type of recognizer 00044 this->setLogger(EngagementParams::getAPTag()); 00045 00046 // Load the max delay parameter 00047 this->getMaxDelayParam(EngagementParams::getConfAdjacencyPairHandle(), 2.0); 00048 00049 // Create the start state, and set the state to it 00050 this->setStartState(); 00051 00052 // Grab the recognition/human NodeHandle 00053 ros::NodeHandle *recog_human = EngagementParams::getRecogHumanHandle(); 00054 00055 // Advertise the human initiated topic 00056 hi_adjacency_pair_pub_ = recog_human->advertise<engagement_msgs::HumanAdjacencyPair>( 00057 engagement::RECOG_ADJACENCY_PAIR_TOPIC, MAX_BUFFER); 00058 00059 this->warn("Initialized recognizer to an empty actor."); 00060 } 00061 00062 AdjacencyPairRecognizer::AdjacencyPairRecognizer(std::string actor, 00063 EventSink *sink) : 00064 Recognizer(AdjacencyPairRecognizer::type(), sink) 00065 { 00066 // Set the logger based on this type of recognizer 00067 this->setLogger(sink->getLogger(), EngagementParams::getAPTag()); 00068 00069 // Load the max delay parameter 00070 this->getMaxDelayParam(EngagementParams::getConfAdjacencyPairHandle(), 2.0); 00071 00072 // Set the actor for this recognizer 00073 // Do this BEFORE setting the state 00074 setActor(actor); 00075 00076 // Grab the recognition/human NodeHandle 00077 ros::NodeHandle *recog_human = EngagementParams::getRecogHumanHandle(); 00078 00079 // Advertise the human initiated topic 00080 hi_adjacency_pair_pub_ = recog_human->advertise<engagement_msgs::HumanAdjacencyPair>( 00081 engagement::RECOG_ADJACENCY_PAIR_TOPIC, MAX_BUFFER); 00082 00083 // Create the start state and set the state to it 00084 this->setStartState(); 00085 } 00086 00087 void AdjacencyPairRecognizer::initialize(initiator::Initiator initiator) 00088 { 00089 Recognizer *rec = (Recognizer*)this; 00090 00091 // If the human was the initiator, send a human initiated message 00092 if (initiator == initiator::HUMAN) 00093 { 00094 // Grab the internal node handle 00095 ros::NodeHandle *n = EngagementParams::getConfAdjacencyPairHandle(); 00096 00097 // Grab the max delay from the parameter server 00098 double max_delay = this->getMaxDelayParam(n, 2.0); 00099 00100 // Only send human initiated events if the recognizer is not degraded 00101 if (!EngagementParams::isDegraded()) 00102 { 00103 // Create the human initiated adjacency pair message 00104 engagement_msgs::HumanAdjacencyPair ap_msg; 00105 00106 // Set the actor, the timeout duration, and the 00107 // human's utterance for this message 00108 ap_msg.actor.id = this->getActor()->getID(); 00109 ap_msg.timeout = max_delay; 00110 00111 // Store the initial action and data 00112 ap_msg.action.value = this->initial_action_.value; 00113 ap_msg.action.data = this->initial_action_.data; 00114 00115 this->debug(5, "Publishing human initiated event."); 00116 // Publish the message 00117 this->hi_adjacency_pair_pub_.publish(ap_msg); 00118 00119 // Reset the initial action now that we've used it 00120 this->initial_action_ = engagement_msgs::APAction(); 00121 } 00122 } 00123 00124 // Initialize this recognizer with the initiator 00125 rec->initialize(initiator); 00126 } 00127 00128 void AdjacencyPairRecognizer::transition(Event event) 00129 { 00130 // Grab the name from the event 00131 std::string event_name = event.getName(); 00132 std::vector<std::string> additional = event.getAdditional(); 00133 00134 // Keep track of what action was used to succeed the event 00135 Action action; 00136 std::string data; 00137 00138 // Grab the action and data (should be two elements) 00139 if (additional.size() >= 1) 00140 { 00141 // Grab the action 00142 action = action.fromString(additional.at(0)); 00143 00144 // Grab the data, if it exists 00145 if (additional.size() == 2) 00146 data = additional.at(1); 00147 } 00148 00149 // Check the event cases 00150 if (EventFactory::isRobotInitiated(event)) 00151 { 00152 this->debug(8, "ROBOT INITIATED EVENT!"); 00153 00154 // Unblock the semaphore using cycle status enter the robot floor state 00155 this->unblock(ServiceResponse::cycle(), Action::noAction()); 00156 this->setState(factory::AP_ROBOT_WAITING); 00157 00158 // Initialize the connection event 00159 this->initialize(initiator::ROBOT); 00160 } 00161 else if (EventFactory::isHumanInitiated(event)) 00162 { 00163 this->debug(8, "HUMAN INITIATED EVENT"); 00164 00165 // Reset the timer and enter the human floor state 00166 this->unblock(ServiceResponse::cycle(), Action::noAction()); 00167 this->setState(factory::AP_HUMAN_WAITING); 00168 00169 // Store the additional data as the initial action 00170 Action act = Action::fromString(additional.at(0)); 00171 if (additional.size() == 2) { 00172 this->initial_action_.value = act.toValue(); 00173 this->initial_action_.data = additional.at(1); 00174 } 00175 else 00176 this->initial_action_ = engagement_msgs::APAction(); 00177 00178 // Initialize the connection event 00179 this->initialize(initiator::HUMAN); 00180 } 00181 else if (EventFactory::isSuccess(event)) 00182 { 00183 this->debug(8, "SUCCESS EVENT"); 00184 00185 // If we're in Robot Waiting go to Human Response, 00186 // if we're in Human Waiting go to Robot Response 00187 if (this->getStateName() == StateFactory::getType(factory::AP_ROBOT_WAITING)) 00188 this->setState(factory::AP_HUMAN_RESPONSE); 00189 else if (this->getStateName() == StateFactory::getType(factory::AP_HUMAN_WAITING)) 00190 this->setState(factory::AP_ROBOT_RESPONSE); 00191 00192 // The connection event succeeds, and set the end delay 00193 this->succeed(action); 00194 this->end_delay_ = CURRENT_TIME; 00195 } 00196 else if (EventFactory::isCompleted(event)) 00197 { 00198 this->debug(8, "COMPLETED EVENT"); 00199 00200 // Succeed if we're in the robot waiting state 00201 if (this->getStateName() == StateFactory::getType(factory::AP_ROBOT_WAITING) 00202 || this->getStateName() == StateFactory::getType(factory::AP_HUMAN_WAITING)) 00203 { 00204 this->succeed(action); 00205 } 00206 00207 // Set the state to the start state 00208 this->setStartState(); 00209 00210 // Complete the connection event successfully 00211 this->complete(true); 00212 } 00213 else if (EventFactory::isFailed(event)) 00214 { 00215 this->debug(8, "FAILED EVENT"); 00216 00217 // Set the state to the start state 00218 this->setStartState(); 00219 00220 // Complete the connection event unsuccessfully 00221 this->complete(false); 00222 } 00223 } 00224 00225 std::string AdjacencyPairRecognizer::type() 00226 { 00227 return engagement::ADJACENCY_PAIR_TYPE; 00228 } |