Tekkotsu Homepage
Demos
Overview
Downloads
Dev. Resources
Reference
Credits

VisualNavErrMonTask.cc

Go to the documentation of this file.
00001 // INCLUDES
00002 // c++
00003 #include <cmath>
00004 #include <sstream>
00005 
00006 // tekkodu
00007 #include "Kodu/KoduWorld.h"
00008 #include "Kodu/PerceptualTasks/PerceptualTaskBase.h"
00009 #include "Kodu/PerceptualTasks/VisualNavErrMonTask.h"
00010 #include "Kodu/Primitives/PerceptionSearch.h"
00011 
00012 //******** temp fix until base object is done
00013 #include "Kodu/Primitives/KoduConditionBump.h"
00014 
00015 // tekkotsu
00016 #include "DualCoding/VRmixin.h"
00017 
00018 namespace Kodu {
00019 
00020     //unsigned int const VisualNavErrMonTask::kMaxErrorOccurences = 1;
00021     unsigned int VisualNavErrMonTask::idCount = 30000;
00022 
00023     bool VisualNavErrMonTask::canExecute(const KoduWorld& kWorldState) {
00024         return (kWorldState.thisAgent.bodyIsInMotion()
00025             && kWorldState.thisAgent.isExecutingMotionAction()
00026             && (distanceInBetweenAgentAndObject(targets[0]) > KoduConditionBump::kMaxDistanceAwayToSenseBump)
00027             && !IsBehindAgent()(targets[0]));
00028     }
00029 
00030     void VisualNavErrMonTask::examineTaskResults() {
00031         // get all the objects in the local shape space
00032         std::vector<DualCoding::ShapeRoot> lclShapes(DualCoding::VRmixin::localShS);
00033         // import the target shape into the local shape space
00034         DualCoding::ShapeRoot lclTarget = DualCoding::VRmixin::mapBuilder->importWorldToLocal(targets[0]);
00035         // iterate over all the shapes in the local shape space for a match
00036         size_t const kNumbOfShapes = lclShapes.size();
00037         std::cout << "VisualNavErrorMon check target...";
00038         for (size_t i = 0; i < kNumbOfShapes; i++) {
00039             if (lclShapes[i]->isMatchFor(lclTarget)) {
00040                 std::cout << "found a match! xy-dist = ";
00041                 float dist = lclShapes[i]->getCentroid().xyDistanceFrom(lclTarget->getCentroid());
00042                 std::cout << dist << "mm\n";
00043                 //errorCount = 0;
00044                 return;
00045             }
00046         }
00047 
00048         // ***NOTE: execution of the remaining portion of this function means a match was not found
00049         // check if the robot turned; sometimes that can cause an error (no match)
00050         const float kTurningError = 3.0f * M_PI / 180.0f;
00051         float agentCurrentOrientation = DualCoding::VRmixin::theAgent->getOrientation();
00052         if (std::fabs(AngSignPi(agentLastOrientation - agentCurrentOrientation)) > kTurningError) {
00053             std::cout << "Turning may have caused a matching error... ignoring error\n";
00054             return;
00055         }
00056 
00057         // increment the error count since the robot did not correctly identify the object
00058         //errorCount++;
00059         //std::stringstream stream;
00060         //stream << "task #" << id << ": recording (error) strike #" << errorCount << ". ";
00061         //if (errorCount == kMaxErrorOccurences) {
00062         //    taskStatus = TS_FAILURE;
00063         //    stream << " Task failed!";
00064         //}
00065         //std::cout << stream.str() << std::endl;
00066         std::cout << "task #" << id << ": navigation task failed.\n";
00067         // taskStatus = TS_FAILURE;  // *** HACK TO MAKE DEMO WORK FOR NOW
00068     }
00069 
00070     const DualCoding::MapBuilderRequest& VisualNavErrMonTask::getMapBuilderRequest() {
00071 
00072         // save the agent's current orientation
00073         agentLastOrientation = DualCoding::VRmixin::theAgent->getOrientation();
00074 
00075         mapreq = DualCoding::MapBuilderRequest(DualCoding::MapBuilderRequest::localMap);
00076         const DualCoding::Point& kTargetPt = targets[0]->getCentroid();
00077         const DualCoding::Point& kAgentPt = DualCoding::VRmixin::theAgent->getCentroid();
00078         //const float kAgentOrientation = DualCoding::VRmixin::theAgent->getOrientation();
00079         mapreq.addObjectColor(targets[0]->getType(), targets[0]->getColor());
00080         cout << "getMapBuilderRequest using target " << targets[0] << endl;
00081         float dtheta = bearingFromAgentToPoint(kTargetPt);
00082         float h = kTargetPt.xyDistanceFrom(kAgentPt);
00083         float x = cos(dtheta) * h;
00084         float y = sin(dtheta) * h;
00085         float z = 0.0f;
00086 
00087         switch (targets[0]->getType()) {
00088             case DualCoding::cylinderDataType:
00089                 z = ShapeRootTypeConst(targets[0], CylinderData)->getHeight() / 2.0f;
00090                 break;
00091 
00092             default:
00093                 break;
00094         }
00095 
00096         NEW_SHAPE(gazePoint, PointData,
00097             new PointData(DualCoding::VRmixin::localShS,
00098                 DualCoding::Point(x, y, z, DualCoding::egocentric)));
00099         mapreq.searchArea = gazePoint;
00100         return mapreq;
00101     }
00102 
00103     bool VisualNavErrMonTask::taskIsComplete(const KoduWorld& kWorldState) {
00104         switch (taskStatus) {
00105             case PerceptualTaskBase::TS_IN_PROGRESS:
00106             {
00107                 if (!kWorldState.thisAgent.isExecutingMotionAction()) {
00108                     taskStatus = TS_COMPLETE;
00109                     return true;
00110                 }
00111 
00112                 //******** temp fix until base object is done
00113                 if (distanceInBetweenAgentAndObject(targets[0])
00114                     < KoduConditionBump::kMaxDistanceAwayToSenseBump)
00115                 {
00116                     taskStatus = TS_COMPLETE;
00117                     return true;
00118                 }
00119                 //*******************
00120 
00121                 return false;
00122             }
00123 
00124             case PerceptualTaskBase::TS_SUCCESSFUL:
00125             case PerceptualTaskBase::TS_FAILURE:
00126             case PerceptualTaskBase::TS_COMPLETE:
00127                 return true;
00128 
00129             default:
00130                 return false;
00131         }
00132     }
00133 }

Tekkotsu v5.1CVS
Generated Mon May 9 04:58:52 2016 by Doxygen 1.6.3