Project: engagement_recognition License: BSD Dependencies:
Used by:
None |
engagement_recognition/src/recognition/recognizers/directed_gaze/dg_human_directing_state.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 "dg_human_directing_state.h" 00036 00037 #include "../../events/event_factory.h" 00038 #include "../../lib/engagement_params.h" 00039 #include "../../objects/engagement_objects.h" 00040 00041 DGHumanDirectingState::DGHumanDirectingState() : 00042 State(DGHumanDirectingState::type(), "", NULL) 00043 { 00044 // Set the logger based on this type of recognizer 00045 this->setLogger(EngagementParams::getStateTag(DGHumanDirectingState::type())); 00046 00047 this->warn("Initialized with an invalid actor, and EventSink."); 00048 } 00049 00050 DGHumanDirectingState::DGHumanDirectingState(std::string actor, EventSink *sink) : 00051 State(DGHumanDirectingState::type(), actor, sink) 00052 { 00053 // Set the logger based on this type of recognizer 00054 this->setLogger(sink->getLogger(), 00055 EngagementParams::getStateTag(DGHumanDirectingState::type())); 00056 } 00057 00058 DGHumanDirectingState::~DGHumanDirectingState() 00059 { 00060 } 00061 00062 Event DGHumanDirectingState::received(std::string topic, 00063 engagement_msgs::ActorIDStatus *message) 00064 { 00065 if (topic == engagement::RECOGNITION_NAMESPACE + "/" + 00066 engagement::HUMAN_NAMESPACE + "/" + 00067 engagement::RECOG_STATUS_TOPIC.c_str()) 00068 return this->engageStatusCallback(message); 00069 00070 return Event(); 00071 } 00072 00073 Event DGHumanDirectingState::received(std::string topic, 00074 engagement_msgs::Performance *message) 00075 { 00076 if (topic == engagement::PERFORMING_NAMESPACE + "/" + engagement::PERFORMING_LOOK_TOPIC) 00077 return this->performingLookCallback(message); 00078 else if (topic == engagement::VISION_NAMESPACE + "/" + engagement::PERFORMING_LOOK_TOPIC) 00079 return this->visionLookCallback(message); 00080 else if (topic == engagement::VISION_NAMESPACE + "/" + engagement::PERFORMING_POINT_TOPIC) 00081 return this->visionPointCallback(message); 00082 00083 return Event(); 00084 } 00085 00086 Event DGHumanDirectingState::received(std::string topic, 00087 engagement_srvs::RobotDirectedGaze::Request *message) 00088 { 00089 if (topic == engagement::RECOGNITION_NAMESPACE + "/" + 00090 engagement::ROBOT_NAMESPACE + "/" + engagement::RECOG_DIRECTED_GAZE_TOPIC) 00091 return this->recogDirGazeCallback(message); 00092 00093 return Event(); 00094 } 00095 00096 Event DGHumanDirectingState::engageStatusCallback(const engagement_msgs::ActorIDStatus *msg) 00097 { 00098 this->debug(25, "Entered %s %s %s", 00099 engagement::RECOGNITION_NAMESPACE.c_str(), 00100 engagement::HUMAN_NAMESPACE.c_str(), 00101 engagement::RECOG_STATUS_TOPIC.c_str()); 00102 00103 // Make sure we have a valid message 00104 if (msg) 00105 { 00106 // Grab the message type from the constant pointer 00107 engagement_msgs::ActorIDStatus temp_msg = *msg; 00108 00109 // Cast ourselves into a state 00110 State *state = (State*)this; 00111 00112 // Grab the actor from the state 00113 Actor *actor = state->getActor(); 00114 00115 // Make sure the actor in the message is the same 00116 // and that the message flag is FALSE (unengaged) 00117 if (actor != NULL && temp_msg.actor.id == actor->getID() && 00118 temp_msg.status.value == bml_msgs::Flag::FALSE) 00119 { 00120 // The actor for this recognizer has become unengaged 00121 // the connection event has failed. 00122 this->debug(7, "Received %s message.", 00123 engagement::RECOG_STATUS_TOPIC.c_str()); 00124 00125 // Create the failure event 00126 return EventFactory::failed(); 00127 } 00128 } 00129 00130 return Event(); 00131 } 00132 00133 Event DGHumanDirectingState::performingLookCallback(const engagement_msgs::Performance *msg) 00134 { 00135 this->debug(25, "Entered %s %s", 00136 engagement::PERFORMING_NAMESPACE.c_str(), 00137 engagement::PERFORMING_LOOK_TOPIC.c_str()); 00138 00139 // Make sure we have a valid message 00140 if (msg) 00141 { 00142 // Grab the message type from the constant pointer 00143 engagement_msgs::Performance temp_msg = *msg; 00144 00145 // Grab the objects from the entities in the message 00146 std::vector<std::string> objects = 00147 objects::getObjectsFromEntities(temp_msg.objects); 00148 00149 // Check the message begin flag to be robot BEGIN (begin looking) 00150 // and there are actually objects 00151 if (temp_msg.actor.id == temp_msg.actor.ROBOT && 00152 objects::areObjects(temp_msg.objects) && 00153 temp_msg.begin.value == bml_msgs::Flag::BEGIN) 00154 { 00155 // Success is contingent upon the objects being the same 00156 if (this->compareAdditional(objects, EventSink::CONTAINS)) 00157 { 00158 this->debug(7, "Received %s %s message.", 00159 engagement::PERFORMING_NAMESPACE.c_str(), 00160 engagement::PERFORMING_LOOK_TOPIC.c_str()); 00161 00162 // Create the shared gaze event 00163 return EventFactory::sharedGaze(); 00164 } 00165 } 00166 } 00167 00168 return Event(); 00169 } 00170 00171 Event DGHumanDirectingState::recogDirGazeCallback(const engagement_srvs::RobotDirectedGaze::Request *msg) 00172 { 00173 this->debug(25, "Entered %s %s", 00174 engagement::RECOGNITION_NAMESPACE.c_str(), 00175 engagement::RECOG_DIRECTED_GAZE_TOPIC.c_str()); 00176 00177 // Make sure we have a valid message 00178 if (msg) 00179 { 00180 // Grab the message type from the constant pointer 00181 engagement_srvs::RobotDirectedGaze::Request temp_msg = *msg; 00182 00183 // Cast ourselves into an State 00184 State *state = (State*)this; 00185 00186 // Grab the actor from the state 00187 Actor *actor = state->getActor(); 00188 00189 this->debug(7, "Received %s %s message.", 00190 engagement::RECOGNITION_NAMESPACE.c_str(), 00191 engagement::RECOG_DIRECTED_GAZE_TOPIC.c_str()); 00192 00193 // If this message is related to the same or different actor 00194 if (actor != NULL && temp_msg.actor.id == actor->getID()) 00195 return EventFactory::sharedGaze(); 00196 else 00197 return EventFactory::failed(); 00198 } 00199 00200 return Event(); 00201 } 00202 00203 Event DGHumanDirectingState::visionLookCallback(const engagement_msgs::Performance *msg) 00204 { 00205 this->debug(25, "Entered %s %s", 00206 engagement::VISION_NAMESPACE.c_str(), 00207 engagement::PERFORMING_LOOK_TOPIC.c_str()); 00208 00209 // Make sure we have a valid message 00210 if (msg) 00211 { 00212 // Grab the message type from the constant pointer 00213 engagement_msgs::Performance temp_msg = *msg; 00214 00215 // Cast ourselves into a state 00216 State *state = (State*)this; 00217 00218 // Grab the actor from the state 00219 Actor *actor = state->getActor(); 00220 std::vector<std::string> objects = 00221 objects::getObjectsFromEntities(temp_msg.objects); 00222 00223 // Make sure the message actor is our actor and that the message 00224 // done field is BEGIN (begin look) 00225 if (actor != NULL && temp_msg.actor.id == actor->getID() && 00226 temp_msg.begin.value == bml_msgs::Flag::BEGIN) 00227 { 00228 // Failure is contingent upon the human NOT pointing at the same objects 00229 if (!this->compareAdditional(actor->getPointing(), 00230 EventSink::CONTAINS)) 00231 { 00232 // If the actor is not pointing at the same objects, then we fail 00233 if (!actor->isPointing(objects)) 00234 { 00235 this->debug(7, "Received begin %s %s message.", 00236 engagement::VISION_NAMESPACE.c_str(), 00237 engagement::PERFORMING_LOOK_TOPIC.c_str()); 00238 00239 // Create the failure event 00240 return EventFactory::failed(); 00241 } 00242 } 00243 } 00244 else if (actor != NULL && temp_msg.actor.id == actor->getID() && 00245 temp_msg.begin.value == bml_msgs::Flag::END) 00246 { 00247 // If the actor is not pointing at these objects we fail 00248 if (!actor->isPointing(objects)) 00249 { 00250 this->debug(7, "Received end %s %s message.", 00251 engagement::VISION_NAMESPACE.c_str(), 00252 engagement::PERFORMING_LOOK_TOPIC.c_str()); 00253 00254 return EventFactory::failed(); 00255 } 00256 } 00257 } 00258 00259 return Event(); 00260 } 00261 00262 Event DGHumanDirectingState::visionPointCallback(const engagement_msgs::Performance *msg) 00263 { 00264 this->debug(25, "Entered %s %s", 00265 engagement::VISION_NAMESPACE.c_str(), 00266 engagement::PERFORMING_POINT_TOPIC.c_str()); 00267 00268 // Make sure we have a valid message 00269 if (msg) 00270 { 00271 // Grab the message type from the constant pointer 00272 engagement_msgs::Performance temp_msg = *msg; 00273 00274 // Cast ourselves into a state 00275 State *state = (State*)this; 00276 00277 // Grab the actor from the state 00278 Actor *actor = state->getActor(); 00279 std::vector<std::string> objects = 00280 objects::getObjectsFromEntities(temp_msg.objects); 00281 00282 // Make sure the message actor is our actor and that the message 00283 // done field is BEGIN (begin point) 00284 if (actor != NULL && temp_msg.actor.id == actor->getID() && 00285 temp_msg.begin.value == bml_msgs::Flag::BEGIN) 00286 { 00287 // State change depends upon whether or not the objects are the same 00288 if (this->compareAdditional(objects, EventSink::CONTAINS)) 00289 { 00290 this->debug(7, "Received begin %s %s message.", 00291 engagement::VISION_NAMESPACE.c_str(), 00292 engagement::PERFORMING_POINT_TOPIC.c_str()); 00293 00294 // Create the cycle event 00295 return EventFactory::cycle(); 00296 } 00297 } 00298 else if (actor != NULL && temp_msg.actor.id == actor->getID() && 00299 temp_msg.begin.value == bml_msgs::Flag::END) 00300 { 00301 if (!actor->isLooking(objects)) 00302 { 00303 this->debug(7, "Received end %s %s message.", 00304 engagement::VISION_NAMESPACE.c_str(), 00305 engagement::PERFORMING_POINT_TOPIC.c_str()); 00306 00307 // Create the failure event 00308 return EventFactory::failed(); 00309 } 00310 } 00311 } 00312 00313 return Event(); 00314 } 00315 00316 std::string DGHumanDirectingState::type() 00317 { 00318 return "dg_human_directing"; 00319 } |