Project: engagement_recognition License: BSD Dependencies:
Used by:
None |
engagement_recognition/src/recognition/actors/robot.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 "robot.h" 00036 00037 #include <ros/ros.h> 00038 #include "../objects/engagement_objects.h" 00039 #include "../lib/engagement_params.h" 00040 00041 Robot *Robot::instance_ = NULL; 00042 00043 Robot::Robot() 00044 { 00045 // Create the prefix for the logger 00046 std::string prefix = std::string(EngagementParams::getRecogTag()) + 00047 std::string(EngagementParams::getActorTag(engagement::ROBOT_TYPE)); 00048 00049 // Create the logger for the robot class 00050 this->setLogger(prefix); 00051 00052 this->createSemaphores(); 00053 00054 // Initiate the values 00055 this->speaking_ = false; 00056 this->facial_gaze_ = std::string(""); 00057 this->looking_ = std::vector<std::string>(); 00058 this->pointing_ = std::vector<std::string>(); 00059 this->waiting_ = false; 00060 } 00061 00062 void Robot::destroy() { 00063 if (Robot::instance_ != NULL) 00064 { 00065 // First delete any semaphores 00066 delete Robot::instance_->semaphore_; 00067 00068 delete Robot::instance_; 00069 Robot::instance_ = NULL; 00070 } 00071 } 00072 00073 Robot* Robot::getRobot() 00074 { 00075 if (Robot::instance_ == NULL) 00076 Robot::instance_ = new Robot(); 00077 00078 return Robot::instance_; 00079 } 00080 00081 void Robot::createSemaphores() 00082 { 00083 this->semaphore_ = new Semaphore(1); 00084 } 00085 00086 bool Robot::isSpeaking() 00087 { 00088 Robot *inst = Robot::getRobot(); 00089 00090 inst->semaphore_->wait(); 00091 bool temp = inst->speaking_; 00092 inst->semaphore_->post(); 00093 00094 return temp; 00095 } 00096 00097 bool Robot::isFacialGaze(std::string actor) 00098 { 00099 Robot *inst = Robot::getRobot(); 00100 00101 inst->semaphore_->wait(); 00102 std::string temp = inst->facial_gaze_; 00103 inst->semaphore_->post(); 00104 00105 return (temp == actor); 00106 } 00107 00108 std::vector<std::string> Robot::getLooking() 00109 { 00110 Robot *inst = Robot::getRobot(); 00111 00112 inst->semaphore_->wait(); 00113 std::vector<std::string> temp = inst->looking_; 00114 inst->semaphore_->post(); 00115 00116 return temp; 00117 } 00118 00119 std::vector<std::string> Robot::getPointing() 00120 { 00121 Robot *inst = Robot::getRobot(); 00122 00123 inst->semaphore_->wait(); 00124 std::vector<std::string> temp = inst->pointing_; 00125 inst->semaphore_->post(); 00126 00127 return temp; 00128 } 00129 00130 bool Robot::isLooking(std::vector<std::string> objects) 00131 { 00132 Robot *inst = Robot::getRobot(); 00133 00134 inst->semaphore_->wait(); 00135 std::vector<std::string> temp = inst->looking_; 00136 inst->semaphore_->post(); 00137 00138 // Make sure the robot is looking somewhere 00139 // Then, return if the set of objects are identical 00140 if (temp.size() > 0) 00141 return objects::same(temp, objects); 00142 00143 return false; 00144 } 00145 00146 bool Robot::isPointing(std::vector<std::string> objects) 00147 { 00148 Robot *inst = Robot::getRobot(); 00149 00150 inst->semaphore_->wait(); 00151 std::vector<std::string> temp = inst->pointing_; 00152 inst->semaphore_->post(); 00153 00154 // Make sure the robot is pointing somewhere 00155 // Then, return if the set of objects are identical 00156 if (temp.size() > 0) 00157 return objects::same(temp, objects); 00158 00159 return false; 00160 } 00161 00162 void Robot::setSpeaking(bool start) 00163 { 00164 Robot *inst = Robot::getRobot(); 00165 00166 // Set the robot's speaking flag accordingly 00167 inst->semaphore_->wait(); 00168 inst->speaking_ = start; 00169 inst->semaphore_->post(); 00170 } 00171 00172 void Robot::setFacialGaze(bool start, std::string actor) 00173 { 00174 Robot *inst = Robot::getRobot(); 00175 00176 inst->semaphore_->wait(); 00177 std::string temp = inst->facial_gaze_; 00178 inst->semaphore_->post(); 00179 00180 if (start && actor != NO_FACIAL_GAZE) 00181 { 00182 inst->semaphore_->wait(); 00183 inst->facial_gaze_ = std::string(actor); 00184 00185 // Clear the vector of objects the robot is looking at 00186 inst->looking_.clear(); 00187 inst->semaphore_->post(); 00188 00189 inst->debug(9, "Robot is making facial gaze with [%s].", 00190 actor.c_str()); 00191 } 00192 else if (temp != NO_FACIAL_GAZE) 00193 { 00194 inst->debug(9, "Robot stopped making facial gaze with [%s].", 00195 inst->facial_gaze_.c_str()); 00196 00197 // The robot is no longer making facial gaze with an actor 00198 inst->semaphore_->wait(); 00199 inst->facial_gaze_ = std::string(NO_FACIAL_GAZE); 00200 inst->semaphore_->post(); 00201 } 00202 } 00203 00204 void Robot::setLookAt(bool start, std::vector<std::string> objects) 00205 { 00206 Robot *inst = Robot::getRobot(); 00207 00208 if (start) 00209 { 00210 // Set the vector of objects the robot is looking at to 00211 // the new objects 00212 inst->semaphore_->wait(); 00213 inst->looking_ = std::vector<std::string>(objects); 00214 00215 std::string temp = inst->facial_gaze_; 00216 inst->semaphore_->post(); 00217 00218 // The robot is not making facial gaze anymore 00219 inst->setFacialGaze(false, temp); 00220 00221 // Create meaningful output 00222 std::stringstream out; 00223 for (unsigned i = 0; i < objects.size(); i++) 00224 { 00225 // Only add a comma if it's not the first element 00226 if (i != 0) 00227 out << "," << objects.at(i); 00228 else 00229 out << objects.at(i); 00230 } 00231 00232 // Display the objects the robot is looking at 00233 inst->debug(9, "Robot started looking at [%s].", out.str().c_str()); 00234 } 00235 else 00236 { 00237 // Create meaningful output 00238 std::stringstream out; 00239 00240 inst->semaphore_->wait(); 00241 std::vector<std::string> temp = inst->looking_; 00242 inst->semaphore_->post(); 00243 00244 // Traverse the incoming objects 00245 for (unsigned i = 0; i < objects.size(); i++) 00246 { 00247 // Traverse all the objects we're looking at 00248 for (unsigned c = 0; c < temp.size(); c++) 00249 { 00250 // If the object matches remove it from the vector 00251 if (temp.at(c) == objects.at(i)) 00252 { 00253 temp.erase(inst->looking_.begin() + c); 00254 break; 00255 } 00256 } 00257 00258 inst->semaphore_->wait(); 00259 inst->looking_ = temp; 00260 inst->semaphore_->post(); 00261 00262 // Only add a comma if it's not the first element 00263 if (i != 0) 00264 out << "," << objects.at(i); 00265 else 00266 out << objects.at(i); 00267 } 00268 00269 // Display the objects the robot is looking at 00270 inst->debug(9, "Robot stopped looking at [%s].", out.str().c_str()); 00271 } 00272 } 00273 00274 void Robot::setPointAt(bool start, std::vector<std::string> objects) 00275 { 00276 Robot *inst = Robot::getRobot(); 00277 00278 // If the pointing is starting 00279 if (start) 00280 { 00281 inst->semaphore_->wait(); 00282 std::vector<std::string> temp = inst->pointing_; 00283 inst->semaphore_->post(); 00284 00285 // Create meaningful output 00286 std::stringstream out; 00287 for (unsigned i = 0; i < objects.size(); i++) 00288 { 00289 bool found = false; 00290 // Traverse through the objects currently being looked at 00291 for (unsigned c = 0; c < temp.size(); c++) 00292 { 00293 // Make sure the new objects don't already exist 00294 if (objects.at(i) == temp.at(c)) 00295 { 00296 found = true; 00297 break; 00298 } 00299 } 00300 00301 // Only add objects that have not been found yet 00302 if (!found) 00303 { 00304 // Only add a comma if it's not the first element 00305 if (i != 0) 00306 out << "," << objects.at(i); 00307 else 00308 out << objects.at(i); 00309 00310 // The robot is now pointing at inst object 00311 temp.push_back(objects.at(i)); 00312 } 00313 } 00314 00315 inst->semaphore_->wait(); 00316 inst->pointing_ = temp; 00317 inst->semaphore_->post(); 00318 00319 // Display the objects the robot is looking at and which arm is being used 00320 inst->debug(9, "Robot started pointing at [%s]", out.str().c_str()); 00321 } 00322 else 00323 { 00324 // The robot has stopped pointing at objects 00325 // Create meaningful output 00326 std::stringstream out; 00327 00328 inst->semaphore_->wait(); 00329 std::vector<std::string> temp = inst->pointing_; 00330 inst->semaphore_->post(); 00331 00332 // Traverse the incoming objects 00333 for (unsigned i = 0; i < objects.size(); i++) 00334 { 00335 // Traverse all the objects we're pointing at 00336 for (unsigned c = 0; c < temp.size(); c++) 00337 { 00338 // If the object matches remove it from the vector 00339 if (temp.at(c) == objects.at(i)) 00340 { 00341 temp.erase(temp.begin() + c); 00342 break; 00343 } 00344 } 00345 00346 inst->semaphore_->wait(); 00347 inst->pointing_ = temp; 00348 inst->semaphore_->post(); 00349 00350 // Only add a comma if it's not the first element 00351 if (i != 0) 00352 out << "," << objects.at(i); 00353 else 00354 out << objects.at(i); 00355 } 00356 00357 // Display the objects the robot is looking at 00358 inst->debug(9, "Robot stopped pointing at [%s].", out.str().c_str()); 00359 } 00360 } 00361 00362 void Robot::donePoint() 00363 { 00364 Robot *inst = Robot::getRobot(); 00365 00366 // Clear all the objects out of the vector depending on the arm 00367 inst->semaphore_->wait(); 00368 inst->pointing_.clear(); 00369 inst->semaphore_->post(); 00370 00371 inst->debug(9, "Robot is no longer pointing at any objects."); 00372 } 00373 00374 bool Robot::isWaiting() 00375 { 00376 Robot *inst = Robot::getRobot(); 00377 00378 inst->semaphore_->wait(); 00379 bool temp = inst->waiting_; 00380 inst->semaphore_->post(); 00381 00382 return temp; 00383 } 00384 00385 void Robot::setWaiting(bool state) 00386 { 00387 Robot *inst = Robot::getRobot(); 00388 00389 inst->semaphore_->wait(); 00390 bool temp = inst->waiting_; 00391 inst->semaphore_->post(); 00392 00393 // Make sure the state is different 00394 if (temp != state) 00395 { 00396 inst->semaphore_->wait(); 00397 inst->waiting_ = state; 00398 inst->semaphore_->post(); 00399 } 00400 00401 inst->debug(10, "Set robot's waiting state to [%s]", 00402 (state ? "true" : "false")); 00403 } |