-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNodeQuery.py
543 lines (493 loc) · 15.5 KB
/
NodeQuery.py
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
from .BasePrompt import BasePrompt
import copy
class NodeQuery(BasePrompt):
def __init__(self) -> None:
super().__init__()
self.background = """
Act as an expert software developer.
You are diligent and tireless!
You NEVER leave comments describing code without implementing it!
You always COMPLETELY IMPLEMENT the needed code!
Always use best practices when coding.
Respect and use existing conventions, libraries, etc that are already present in the code base.
You are assisting a behavior-tree designer, who designs behaviors in the hunav_agent_manager repository using the BehaviorTree.cpp, The map of the BehaviorTree.CPP repository is given below:
The map of the hunav_agent_manager repository is given below:
hunav_agent_manager/src/agent_manager.cpp:
⋮...
│namespace hunav
│{
│
⋮...
│ AgentManager::AgentManager()
⋮...
│ void AgentManager::init()
⋮...
│ bool AgentManager::isRobotVisible(int id, double dist)
│ {
│ std::lock_guardstd::mutex guard(mutex_);
⋮...
│ void AgentManager::lookAtTheRobot(int id)
│ {
│
│ std::lock_guardstd::mutex guard(mutex_);
⋮...
│ void AgentManager::approximateRobot(int id, double dt)
│ {
│
│ std::lock_guardstd::mutex guard(mutex_);
│
⋮...
│ void AgentManager::blockRobot(int id, double dt)
│ {
│
│ std::lock_guardstd::mutex guard(mutex_);
│
⋮...
│ void AgentManager::avoidRobot(int id, double dt)
│ {
│ std::lock_guardstd::mutex guard(mutex_);
│
⋮...
│ bool AgentManager::goalReached(int id)
│ {
│ std::lock_guardstd::mutex guard(mutex_);
⋮...
│ bool AgentManager::updateGoal(int id)
│ {
│ std::lock_guardstd::mutex guard(mutex_);
│
⋮...
│ hunav_msgs::msg::Agents AgentManager::getUpdatedAgentsMsg()
│ {
⋮...
│ std::lock_guardstd::mutex guard(mutex_);
⋮...
│ void AgentManager::computeForces(int id)
⋮...
│ void AgentManager::computeForces()
⋮...
│ void AgentManager::updateAllAgents(
│ const hunav_msgs::msg::Agent::SharedPtr robot_msg,
│ const hunav_msgs::msg::Agents::SharedPtr agents_msg)
│ {
│
│ std::lock_guardstd::mutex guard(mutex_);
│
⋮...
│ void AgentManager::updateAgentsAndRobot(
│ const hunav_msgs::msg::Agents::SharedPtr agents_msg)
│ {
│
│ std::lock_guardstd::mutex guard(mutex_);
│
⋮...
│ void AgentManager::updatePosition(int id, double dt)
│ {
│
│ std::lock_guardstd::mutex guard(mutex_);
│
⋮...
hunav_agent_manager/src/bt_functions.cpp:
⋮...
│#include "hunav_agent_manager/bt_functions.hpp"
│
│namespace hunav
│{
│
│ BTfunctions::BTfunctions()
⋮...
│ void BTfunctions::init()
⋮...
hunav_agent_manager/src/time_expired_condition.cpp:
⋮...
│namespace hunav {
│
│TimeExpiredCondition::TimeExpiredCondition(const std::string &condition_name,
│ const BT::NodeConfiguration &conf)
│ : BT::ConditionNode(condition_name, conf), period_(1.0) {
│ getInput("seconds", period_);
│ // node_ = config().blackboard->getrclcpp::Node::SharedPtr("node");
│ // start_ = node_->now();
│ getInput("ts", dt_);
│ getInput("only_once", only_once_);
⋮...
The designer will write Behavior Trees in XML for the humans in the task and might define custom actions, conditions, decorators or sub-trees. You will be given the [DESCRIPTION] of what these custom nodes must do and your job is to write corresponding functions using the BehaviorTree.cpp package.
Code for each custom node consists of 3 components:
1. NODE NAME: The name of the node, which will be used to call the node in the BehaviorTree.cpp file.
2. NODE TYPE: The type of the node, which can be either: ACTION or CONDITION
3. NODE DEFINITION: A definition of the logic of the behavior of the node, that is, when the node returns SUCCESS or FAILURE when the node is ticked. THIS FUNCTION WILL BE ADDED TO hunav_agent_manager/src/bt_functions.cpp
4. NODE HEADER: The header for each custom node to be added to the hunav_agent_manager/include/bt_functions.hpp file
5. PORTS USED: The input ports used by the node. These can be either: ((agent_id),(agent_id,distance),(agent_id,time_step))
6. AUX FUNCTIONS: Any auxiliary functions needed for running the logic of the node. THESE FUNCTIONS WILL BE ADDED TO hunav_agent_manager/src/agent_manager.cpp
7. AUX FUNCTION HEADERS: The header for each auxillary function to be added to hunav_agent_manager/include/agent_manager.hpp
Examples of NODE DEFINITION are given below:
//check if the robot is visible to an agent
BT::NodeStatus BTfunctions::robotVisible(BT::TreeNode &self)
{
auto idmsg = self.getInput<int>("agent_id");
if (!idmsg)
{
throw BT::RuntimeError("RobotVisible. missing required input [agent_id]: ",
idmsg.error());
}
auto dmsg = self.getInput<double>("distance");
if (!dmsg)
{
throw BT::RuntimeError("RobotVisible. missing required input [distance]: ",
dmsg.error());
}
int id = idmsg.value();
double dist = dmsg.value();
if (agent_manager_.isRobotVisible(id, dist))
{
return BT::NodeStatus::SUCCESS;
}
else
{
return BT::NodeStatus::FAILURE;
}
}
//check if an agent has reached their goal
BT::NodeStatus BTfunctions::goalReached(BT::TreeNode &self)
{
auto msg = self.getInput<int>("agent_id");
if (!msg)
{
throw BT::RuntimeError("GoalReached. missing required input [agent_id]: ",
msg.error());
}
int id = msg.value();
if (agent_manager_.goalReached(id))
{
return BT::NodeStatus::SUCCESS;
}
else
{
return BT::NodeStatus::FAILURE;
}
}
//update the goal of an agent by popping the top goal
BT::NodeStatus BTfunctions::updateGoal(BT::TreeNode &self)
{
auto msg = self.getInput<int>("agent_id");
if (!msg)
{
throw BT::RuntimeError("UpdateGoal. missing required input [agent_id]: ",
msg.error());
}
int id = msg.value();
if (agent_manager_.updateGoal(id))
{
return BT::NodeStatus::SUCCESS;
}
else
{
return BT::NodeStatus::FAILURE;
}
}}
//Regular Navigation
BT::NodeStatus BTfunctions::regularNav(BT::TreeNode &self)
{
auto msg = self.getInput<int>("agent_id");
auto msg2 = self.getInput<double>("time_step");
if (!msg)
{
throw BT::RuntimeError("RegularNav. missing required input [agent_id]: ",
msg.error());
}
if (!msg2)
{
throw BT::RuntimeError("RegularNav. missing required input [time_step]: ",
msg2.error());
}
int id = msg.value();
double dt = msg2.value();
agent_manager_.updatePosition(id, dt); //update the position of an agent in the simulator
return BT::NodeStatus::SUCCESS;
}
//Surprised Navigation
BT::NodeStatus BTfunctions::surprisedNav(BT::TreeNode &self)
{
auto msg = self.getInput<int>("agent_id");
if (!msg)
{
throw BT::RuntimeError("SurprisedNav. missing required input [agent_id]: ",
msg.error());
}
int id = msg.value();
agent_manager_.lookAtTheRobot(id);
return BT::NodeStatus::SUCCESS;
}
The AUX FUNCTIONS currently available are given below:
float AgentManager::robotSquaredDistance(int id)
{
float xa = agents_[id].sfmAgent.position.getX(); // position.position.x;
float ya = agents_[id].sfmAgent.position.getY(); // position.y;
float xr = robot_.sfmAgent.position.getX(); // position.x;
float yr = robot_.sfmAgent.position.getY(); // position.y;
return (xr - xa) * (xr - xa) + (yr - ya) * (yr - ya);
}
bool AgentManager::lineOfSight(int id)
{
float ax = agents_[id].sfmAgent.position.getX();
float ay = agents_[id].sfmAgent.position.getY();
float rx = robot_.sfmAgent.position.getX();
float ry = robot_.sfmAgent.position.getY();
double yaw = agents_[id].sfmAgent.yaw.toRadian();
float nrx = (rx - ax) * cos(yaw) + (ry - ay) * sin(yaw);
float nry = -(rx - ax) * sin(yaw) + (ry - ay) * cos(yaw);
float rangle = atan2(nry, nrx);
if (abs(rangle) > (M_PI / 2.0 + 0.17))
{
return false;
}
else
{
return true;
}
}
bool AgentManager::isRobotVisible(int id, double dist)
{
std::lock_guardstd::mutex guard(mutex_);
float squared_dist = robotSquaredDistance(id);
if (squared_dist <= (dist * dist))
{
return lineOfSight(id);
}
else
{
return false;
}
}
void AgentManager::lookAtTheRobot(int id)
{
std::lock_guard<std::mutex> guard(mutex_);
// Robot position
float rx = robot_.sfmAgent.position.getX();
float ry = robot_.sfmAgent.position.getY();
// Agent position
float ax = agents_[id].sfmAgent.position.getX();
float ay = agents_[id].sfmAgent.position.getY();
float ah = agents_[id].sfmAgent.yaw.toRadian();
// Transform robot position to agent coords system
float nrx = (rx - ax) * cos(ah) + (ry - ay) * sin(ah);
float nry = -(rx - ax) * sin(ah) + (ry - ay) * cos(ah);
utils::Angle robotYaw;
robotYaw.setRadian(atan2(nry, nrx));
float max_ang_vel = M_PI; // rad/secs
utils::Angle max_angle =
utils::Angle::fromRadian(max_ang_vel * 0.01);
if (robotYaw.sign() < 0)
max_angle.setRadian(max_angle.toRadian() * (-1));
// Update the agent angle
if (fabs(robotYaw.toRadian()) > max_angle.toRadian())
{
agents_[id].sfmAgent.yaw = (agents_[id].sfmAgent.yaw + max_angle);
}
else
{
agents_[id].sfmAgent.yaw = agents_[id].sfmAgent.yaw + robotYaw;
}
agents_[id].behavior_state = 1;
}
void AgentManager::blockRobot(int id, double dt)
{
std::lock_guard<std::mutex> guard(mutex_);
agents_[id].behavior_state = 1;
// Robot position
float rx = robot_.sfmAgent.position.getX();
float ry = robot_.sfmAgent.position.getY();
float h = robot_.sfmAgent.yaw.toRadian();
// Store the initial set o goals
std::list<sfm::Goal> gls = agents_[id].sfmAgent.goals;
// Change the agent goal.
// We should compute a goal in front of the robot heading.
float newgx = rx + 1.1 * sin(h);
float newgy = ry + 1.1 * cos(h);
sfm::Goal g;
g.center.set(newgx, newgy);
g.radius = 0.05; // robot_.sfmAgent.radius;
agents_[id].sfmAgent.goals.push_front(g);
// change agent vel according to the proximity of the robot
// move slowly when close
float ini_desired_vel = agents_[id].sfmAgent.desiredVelocity;
agents_[id].sfmAgent.desiredVelocity = 2.0;
// recompute forces
computeForces(id);
// update position
sfm::SFM.updatePosition(agents_[id].sfmAgent, dt);
// if the agent is close to the robot,
// look at the robot
float dist = sqrt(robotSquaredDistance(id));
if (dist <= 1.2)
{
// Agent position
float ax = agents_[id].sfmAgent.position.getX();
float ay = agents_[id].sfmAgent.position.getY();
float ah = agents_[id].sfmAgent.yaw.toRadian();
// Transform robot position to agent coords system
float nrx = (rx - ax) * cos(ah) + (ry - ay) * sin(ah);
float nry = -(rx - ax) * sin(ah) + (ry - ay) * cos(ah);
utils::Angle robotYaw; // = utils::Angle::fromRadian(atan2(nry, nrx));
robotYaw.setRadian(atan2(nry, nrx));
agents_[id].sfmAgent.yaw = agents_[id].sfmAgent.yaw + robotYaw;
}
// restore the goals and the velocity
agents_[id].sfmAgent.goals = gls;
agents_[id].sfmAgent.desiredVelocity = ini_desired_vel;
}
void AgentManager::avoidRobot(int id, double dt)
{
std::lock_guardstd::mutex guard(mutex_);
agents_[id].behavior_state = 1;
// we decrease the maximum velocity
double init_vel = agents_[id].sfmAgent.desiredVelocity;
agents_[id].sfmAgent.desiredVelocity = 0.6;
computeForces(id);
// We add an extra repulsive force from the robot
utils::Vector2d minDiff =
agents_[id].sfmAgent.position - robot_.sfmAgent.position;
double distance = minDiff.norm() - agents_[id].sfmAgent.radius;
utils::Vector2d Scaryforce =
20.0 * (agents_[id].sfmAgent.params.forceSigmaObstacle / distance) *
minDiff.normalized();
agents_[id].sfmAgent.forces.globalForce += Scaryforce;
// update position
sfm::SFM.updatePosition(agents_[id].sfmAgent, dt);
// restore desired vel
agents_[id].sfmAgent.desiredVelocity = init_vel;
}
bool AgentManager::goalReached(int id)
{
std::lock_guardstd::mutex guard(mutex_);
if (!agents_[id].sfmAgent.goals.empty() &&
(agents_[id].sfmAgent.goals.front().center -
agents_[id].sfmAgent.position)
.norm() <= (agents_[id].sfmAgent.goals.front().radius + 0.1))
{
return true;
}
else
return false;
}
bool AgentManager::updateGoal(int id)
{
std::lock_guardstd::mutex guard(mutex_);
sfm::Goal g = agents_[id].sfmAgent.goals.front();
agents_[id].sfmAgent.goals.pop_front();
if (agents_[id].sfmAgent.cyclicGoals)
{
agents_[id].sfmAgent.goals.push_back(g);
}
return true;
}
void AgentManager::updatePosition(int id, double dt)
{
std::lock_guardstd::mutex guard(mutex_);
agents_[id].behavior_state = 0;
sfm::SFM.updatePosition(agents_[id].sfmAgent, dt);
}
###
Output the custom nodes and auxillary functions in the JSON format shown in the example below:
Note that the function name is in camel case while the node name is in pascal case.
###
USER: [DESCRIPTION]: Write a BT node called curiousNav that performs the following logic: Agent approaches the robot slowly and when the agent is closer than 1.5 units, the agent stops and looks at the robot.
ASSISTANT:
{
NODE_NAME: 'curiousNav',
NODE_TYPE:'ACTION',
NODE_DEFINITION:
""
BT::NodeStatus BTfunctions::curiousNav(BT::TreeNode &self)
{
auto msg = self.getInput<int>("agent_id");
auto msg2 = self.getInput<double>("time_step");
if (!msg)
{
throw BT::RuntimeError("CuriousNav. missing required input [agent_id]: ",
msg.error());
}
if (!msg2)
{
throw BT::RuntimeError("CuriousNav. missing required input [time_step]: ",
msg2.error());
}
int id = msg.value();
double dt = msg2.value();
agent_manager_.approximateRobot(id, dt);
return BT::NodeStatus::SUCCESS;
}
"",
NODE_HEADER:
"BT::NodeStatus curiousNav(BT::TreeNode &self);",
,
PORTS_USED:[
"agent_id",
"time_step",
],
AUX_FUNCTIONS:[
""void AgentManager::approximateRobot(int id, double dt)
{
std::lock_guard<std::mutex> guard(mutex_);
agents_[id].behavior_state = 1;
// Robot position
float rx = robot_.sfmAgent.position.getX();
float ry = robot_.sfmAgent.position.getY();
float dist = sqrt(robotSquaredDistance(id));
// if the agent is close to the robot,
// stop and look at the robot
if (dist <= 1.5)
{
// Agent position
float ax = agents_[id].sfmAgent.position.getX();
float ay = agents_[id].sfmAgent.position.getY();
float ah = agents_[id].sfmAgent.yaw.toRadian();
// Transform robot position to agent coords system
float nrx = (rx - ax) * cos(ah) + (ry - ay) * sin(ah);
float nry = -(rx - ax) * sin(ah) + (ry - ay) * cos(ah);
utils::Angle robotYaw; // = utils::Angle::fromRadian(atan2(nry, nrx));
robotYaw.setRadian(atan2(nry, nrx));
agents_[id].sfmAgent.yaw = agents_[id].sfmAgent.yaw + robotYaw;
}
else
{
// Change the agent goal
sfm::Goal g;
g.center.set(rx, ry);
g.radius = robot_.sfmAgent.radius;
agents_[id].sfmAgent.goals.push_front(g);
// change agent vel according to the proximity of the robot
// move slowly when close
float ini_desired_vel = agents_[id].sfmAgent.desiredVelocity;
agents_[id].sfmAgent.desiredVelocity = 1.8 * (dist / max_dist_view_);
// recompute forces
computeForces(id);
// update position
sfm::SFM.updatePosition(agents_[id].sfmAgent, dt);
// restore values just in case the approximation
// ends in the next iteration
agents_[id].sfmAgent.goals.pop_front();
agents_[id].sfmAgent.desiredVelocity = ini_desired_vel;
}
}
""
],
AUX_FUNCTION_HEADERS: [
"void approximateRobot(int id, double dt);"
]
}
###
USER: <NODE_QUERY>
ASSISTANT:"""
def get_full_prompt(self,**kwargs):
full_prompt = copy.deepcopy(self.background)
full_prompt = full_prompt.replace('<NODE_QUERY>',kwargs['description'])
return dict(
system = self.system_prompt,
user = [
{
'type': 'text',
'content': full_prompt
}
]
)