00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #include "ArExport.h"
00027 #include "ariaOSDef.h"
00028 #include "ArPriorityResolver.h"
00029 #include "ArAction.h"
00030 #include "ArRobot.h"
00031
00032 AREXPORT ArPriorityResolver::ArPriorityResolver() :
00033 ArResolver("ArPriorityResolver", "Resolves strictly by using priority, the highest priority action to act is the one that gets to go. Does no mixing of any variety.")
00034 {
00035
00036
00037 }
00038
00039
00040 AREXPORT ArPriorityResolver::~ArPriorityResolver()
00041 {
00042 }
00043
00044 AREXPORT ArActionDesired *ArPriorityResolver::resolve(
00045 ArResolver::ActionMap *actions, ArRobot *robot, bool logActions)
00046 {
00047 ArResolver::ActionMap::reverse_iterator it;
00048 ArAction *action;
00049 ArActionDesired *act;
00050 ArActionDesired averaging;
00051 bool first = true;
00052 int lastPriority;
00053 bool printedFirst = true;
00054 int printedLast;
00055
00056 if (actions == NULL)
00057 return NULL;
00058
00059 myActionDesired.reset();
00060 averaging.reset();
00061 averaging.startAverage();
00062 for (it = actions->rbegin(); it != actions->rend(); ++it)
00063 {
00064 action = (*it).second;
00065 if (action != NULL && action->isActive())
00066 {
00067 act = action->fire(myActionDesired);
00068 if (robot != NULL && act != NULL)
00069 act->accountForRobotHeading(robot->getTh());
00070 if (first || (*it).first != lastPriority)
00071 {
00072 averaging.endAverage();
00073 myActionDesired.merge(&averaging);
00074
00075 averaging.reset();
00076 averaging.startAverage();
00077 first = false;
00078 lastPriority = (*it).first;
00079 }
00080 averaging.addAverage(act);
00081 if (logActions && act != NULL && act->isAnythingDesired())
00082 {
00083 if (printedFirst || printedLast != (*it).first)
00084 {
00085 ArLog::log(ArLog::Terse, "Priority %d:", (*it).first);
00086 printedLast = (*it).first;
00087 printedFirst = false;
00088 }
00089 ArLog::log(ArLog::Terse, "Action: %s", action->getName());
00090 act->log();
00091 }
00092
00093
00094 }
00095 }
00096 averaging.endAverage();
00097 myActionDesired.merge(&averaging);
00098
00099
00100
00101
00102
00103
00104 return &myActionDesired;
00105 }
00106
00107