This repository has been archived by the owner on Jul 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
KoduAgent.cc
225 lines (190 loc) · 7.61 KB
/
KoduAgent.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
// INCLUDES
// tekkodu
#include "Kodu/KoduPage.h"
#include "Kodu/General/GeneralFncs.h"
#include "Kodu/PerceptualTasks/PerceptualTaskBase.h"
#include "Kodu/Primitives/PerceptionSearch.h"
#include "Kodu/KoduAgent.h"
// tekkotsu
#include "DualCoding/ShapeTypes.h"
#include "Shared/get_time.h"
#include "Shared/mathutils.h"
using namespace DualCoding;
namespace Kodu {
KoduAgent::KoduAgent()
: gripperObject(),
agentIsExecutingManipAction(false),
agentWantsToDropObject(false),
agentWantsToGrabObject(false),
targetObjectIsInGripper(false),
agentIsExecutingMotionAction(false),
currMotionCmd(),
distanceTravelled(0.0f),
pages(),
currPageIndex(0),
newReqdPage(0),
ptasks(),
scoreQueue(),
stringToSpeak(""),
playQueue(),
agentGazePoints()
{
// generate the gaze points
generateGazePoints();
}
KoduAgent::~KoduAgent() {
GeneralFncs::destroyAllPtrsInQueue(ptasks);
GeneralFncs::destroyAllPtrsInVector(pages);
stringToSpeak.clear();
}
KoduAgent& KoduAgent::operator=(const KoduAgent& kAgent) {
if (this != &kAgent) {
gripperObject = kAgent.gripperObject;
agentIsExecutingManipAction = kAgent.agentIsExecutingManipAction;
agentWantsToDropObject = kAgent.agentWantsToDropObject;
agentWantsToGrabObject = kAgent.agentWantsToGrabObject;
targetObjectIsInGripper = kAgent.targetObjectIsInGripper;
agentIsExecutingMotionAction = kAgent.agentIsExecutingMotionAction;
currMotionCmd = kAgent.currMotionCmd;
distanceTravelled = kAgent.distanceTravelled;
pages = kAgent.pages;
currPageIndex = kAgent.currPageIndex;
newReqdPage = kAgent.newReqdPage;
ptasks = kAgent.ptasks;
scoreQueue = kAgent.scoreQueue;
stringToSpeak = kAgent.stringToSpeak;
playQueue = kAgent.playQueue;
agentGazePoints = kAgent.agentGazePoints;
}
return *this;
}
/// ================================ Static initializations ================================ ///
const float KoduAgent::kLocalizationDistanceThreshold = 1000.0f;
/// ================================ Gaze functions ================================ ///
void KoduAgent::generateGazePoints() {
// SA: the search area (in degrees)--(from -SA/2 to SA/2)
const float kSearchArea = mathutils::deg2rad(200.0f);
const float kAngleIncrement = mathutils::deg2rad(25.0f);
// the search radius
float radius = 750.0f;
// the beginning angle
float currAngle = -1.0f * kSearchArea / 2.0f;
// loop until all the search points have been generated
while (currAngle <= (kSearchArea / 2.0f)) {
agentGazePoints.push_back(
Point(
cos(currAngle) * radius, // x-value
sin(currAngle) * radius, // y-value
0.0f, // z-value
DualCoding::egocentric // point is relative to agent body
));
currAngle += kAngleIncrement; // increment the current angle after generating each point
}
}
const std::vector<Point>& KoduAgent::getGazePoints() const {
return agentGazePoints;
}
/// ================================ Grasper functions ================================ ///
bool KoduAgent::isExecutingManipAction() const {
return agentIsExecutingManipAction;
}
bool KoduAgent::isHoldingAnObject() const {
return targetObjectIsInGripper;
}
void KoduAgent::manipulationComplete() {
// check the type of manipulation completed
if (agentWantsToDropObject) {
setWantsToDropObjectFlag(false); // the robot no longer "wants to drop an object"
setTargetInGripperFlag(false); // the object is no longer in the gripper
}
// else, it must be the grab action
else {
setWantsToGrabObjectFlag(false); // the robot no longer "wants to grab an object"
setTargetInGripperFlag(); // (implicit true) object is in the gripper
}
setIsExecutingManipActionFlag(false); // the manipulation action is no longer executing
}
void KoduAgent::setIsExecutingManipActionFlag(bool bval) {
agentIsExecutingManipAction = bval;
}
void KoduAgent::setTargetInGripperFlag(bool bval) {
targetObjectIsInGripper = bval;
}
void KoduAgent::setWantsToDropObjectFlag(bool bval) {
agentWantsToDropObject = bval;
}
void KoduAgent::setWantsToGrabObjectFlag(bool bval) {
agentWantsToGrabObject = bval;
}
void KoduAgent::signalDropActionStart() {
setIsExecutingManipActionFlag(); // states the robot is executing a manipulation action
setWantsToDropObjectFlag(); // the robot wants to drop an object
}
void KoduAgent::signalGrabActionStart() {
setIsExecutingManipActionFlag(); // states the robot is executing a manipulation action
setWantsToGrabObjectFlag(); // the robot wants to grab an object
}
bool KoduAgent::wantsToDropObject() const {
return agentWantsToDropObject;
}
bool KoduAgent::wantsToGrabObject() const {
return agentWantsToGrabObject;
}
/// ================================ Motion functions ================================ ///
bool KoduAgent::bodyIsInMotion() const {
return VRmixin::isWalkingFlag;
}
bool KoduAgent::hasMotionCommand() const {
return currMotionCmd.isValid();
}
bool KoduAgent::isExecutingMotionAction() const {
return agentIsExecutingMotionAction;
}
bool KoduAgent::needsToLocalize() const {
return (distanceTravelled >= kLocalizationDistanceThreshold);
}
void KoduAgent::motionComplete() {
// make sure the current motion command is invalid
currMotionCmd.invalidate();
setIsExecutingMotionActionFlag(false);
}
void KoduAgent::signalMotionActionStart() {
setIsExecutingMotionActionFlag();
}
void KoduAgent::setIsExecutingMotionActionFlag(bool bval) {
agentIsExecutingMotionAction = bval;
}
void KoduAgent::setMotionCommand(const MotionCommand& kCmd) {
currMotionCmd = kCmd;
}
/// ================================ Scoring functions ================================ ///
bool KoduAgent::hasNewScoreChanges() const {
return (!scoreQueue.empty());
}
/// ================================ Page functions ================================ ///
KoduPage* KoduAgent::getCurrentPage() const {
return pages[currPageIndex];
}
KoduPage* KoduAgent::getPage(unsigned int pageNumber) const {
return getPageInPos(pageNumber - 1);
}
KoduPage* KoduAgent::getPageInPos(unsigned int pageIndex) const {
if (pageIndex < pages.size()) {
return pages[pageIndex];
} else {
std::cout << "Page index \"" << pageIndex << "\" is out of bounds!\nReturning NULL...\n";
return NULL;
}
}
bool KoduAgent::hasNewPageNumber() const {
return (newReqdPage > 0);
}
/// ================================ Speech functions ================================ ///
bool KoduAgent::hasTextToSay() const {
return (!stringToSpeak.empty());
}
/// ================================ Sound functions ================================ ///
bool KoduAgent::hasSoundsToPlay() const {
return (!playQueue.empty());
}
}