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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
---
sidebar_position: 3
---

# Chapter 2: Cognitive Planning using LLMs

## Learning Objectives

- Understand how Large Language Models (LLMs) can be used for cognitive planning
- Implement cognitive planning systems for humanoid robots
- Integrate LLM-based planning with action execution
- Learn about task decomposition and hierarchical planning
- Create robust planning systems that handle ambiguity and errors

## Introduction to Cognitive Planning

Cognitive planning in robotics refers to the high-level decision-making process that determines what actions a robot should take to achieve its goals. For humanoid robots, this involves understanding natural language commands, decomposing complex tasks into simpler ones, and adapting to dynamic environments.

### The Role of LLMs in Cognitive Planning

Large Language Models have revolutionized cognitive planning by:

1. **Natural Language Understanding**: Converting human commands to robot actions
2. **Task Decomposition**: Breaking complex tasks into executable sequences
3. **Context Management**: Maintaining understanding of the environment and goals
4. **Adaptation**: Adjusting plans based on new information or obstacles

### Cognitive Planning vs. Low-level Planning

- **Cognitive Planning**: High-level goal-directed behavior, task decomposition, handling ambiguous commands
- **Low-level Planning**: Path planning, trajectory generation, motion control

## Architecture of LLM-Based Cognitive Planning

The cognitive planning system typically follows this architecture:

```
[Human Command] → [NLU with LLM] → [Task Decomposition] → [Plan Refinement] → [Action Execution] → [Feedback Loop]
```

### Natural Language Understanding with LLMs

LLMs can understand complex, natural language commands:

```python
import openai

class CognitivePlanner:
    def __init__(self, openai_api_key):
        openai.api_key = openai_api_key
        self.robot_capabilities = [
            "move_forward", "turn_left", "turn_right", "stop",
            "pick_up_object", "place_object", "speak_text",
            "wave_gesture", "navigate_to", "find_object"
        ]
        
    def parse_command(self, command_text):
        """Convert natural language to structured action plan"""
        
        prompt = f"""
        You are a cognitive planning system for a humanoid robot.
        Convert the following human command to a sequence of robot actions.
        Select from the following capabilities: {self.robot_capabilities}
        
        Human Command: "{command_text}"
        
        Return the plan as a JSON array of actions, where each action has:
        - action: name of the action (from the capabilities list)
        - parameters: object with required parameters for the action
        
        Example response format:
        [
            {{"action": "navigate_to", "parameters": {{"location": "kitchen"}}}},
            {{"action": "find_object", "parameters": {{"object": "bottle"}}}},
            {{"action": "pick_up_object", "parameters": {{"object": "bottle"}}}},
            {{"action": "navigate_to", "parameters": {{"location": "table"}}}},
            {{"action": "place_object", "parameters": {{"object": "bottle", "location": "table"}}}}
        ]
        
        Command:
        """
        
        response = openai.ChatCompletion.create(
            model="gpt-3.5-turbo",
            messages=[{"role": "user", "content": prompt}],
            temperature=0.1,  # Low temperature for consistent outputs
            max_tokens=500
        )
        
        import json
        try:
            # Extract JSON from the response
            content = response.choices[0].message.content
            # Find JSON in the response
            import re
            json_match = re.search(r'\[.*\]', content, re.DOTALL)
            if json_match:
                plan = json.loads(json_match.group())
                return plan
        except (json.JSONDecodeError, AttributeError):
            # If parsing fails, return a default plan
            return [{"action": "speak_text", "parameters": {"text": f"I don't understand the command: {command_text}"}}]
        
        return [{"action": "speak_text", "parameters": {"text": f"I don't understand the command: {command_text}"}}]
```

## Task Decomposition and Hierarchical Planning

Complex commands need to be broken down into simpler, executable actions:

### Example: "Bring me a drink from the kitchen"

This high-level command decomposes into:

1. **Goal**: Deliver a drink to the user
2. **Subgoals**:
   - Navigate to the kitchen
   - Identify a drink
   - Pick up the drink
   - Navigate back to the user
   - Present the drink

### Implementation of Hierarchical Planner

```python
class HierarchicalPlanner:
    def __init__(self, llm_planner):
        self.llm_planner = llm_planner
        self.known_locations = {
            "kitchen": [1.0, 2.0, 0.0],
            "living_room": [0.0, 0.0, 0.0],
            "bedroom": [-2.0, 1.0, 0.0],
            "dining_room": [-1.0, -1.0, 0.0]
        }
        
        self.known_objects = {
            "drink": ["water_bottle", "soda_can", "juice_box"],
            "snack": ["cookies", "apple", "chips"],
            "tool": ["screwdriver", "wrench", "hammer"]
        }
    
    def create_plan(self, high_level_command):
        """Create a plan for a high-level command using LLM and domain knowledge"""
        
        # First, use LLM to get a general plan structure
        general_plan = self.llm_planner.parse_command(high_level_command)
        
        # Then, refine the plan using domain knowledge
        refined_plan = self.refine_plan(general_plan, high_level_command)
        
        return refined_plan
    
    def refine_plan(self, plan, command):
        """Refine the plan using domain knowledge and context"""
        refined = []
        
        for action in plan:
            if action["action"] == "navigate_to" and "location" in action["parameters"]:
                location = action["parameters"]["location"]
                
                # Resolve location to coordinates
                if location in self.known_locations:
                    coords = self.known_locations[location]
                    refined.append({
                        "action": "navigate_to_coordinates",
                        "parameters": {"x": coords[0], "y": coords[1], "theta": coords[2]}
                    })
                else:
                    # If location not known, add a search step
                    refined.append({
                        "action": "search_for_location",
                        "parameters": {"location": location}
                    })
                    
            elif action["action"] == "find_object" and "object" in action["parameters"]:
                obj_type = action["parameters"]["object"]
                
                # Expand object type to specific objects
                if obj_type in self.known_objects:
                    possible_objects = self.known_objects[obj_type]
                    refined.append({
                        "action": "search_for_objects",
                        "parameters": {"objects": possible_objects}
                    })
                else:
                    refined.append(action)  # Keep original action
                    
            else:
                refined.append(action)
        
        return refined
```

## Context Management and Memory

Cognitive planning systems need to maintain context across multiple interactions:

### Memory Systems

```python
import datetime
from typing import List, Dict, Any

class ContextManager:
    def __init__(self):
        self.episodic_memory = []  # Recent interactions
        self.semantic_memory = {}  # General knowledge about the world
        self.procedural_memory = {}  # How to perform tasks
        
    def update_context(self, event: Dict[str, Any]):
        """Update the context with a new event"""
        event_with_timestamp = {
            "timestamp": datetime.datetime.now(),
            "event": event
        }
        self.episodic_memory.append(event_with_timestamp)
        
        # Keep only recent events (last 100)
        if len(self.episodic_memory) > 100:
            self.episodic_memory = self.episodic_memory[-100:]
    
    def get_recent_context(self, timeframe_minutes=30):
        """Get context from the last timeframe_minutes"""
        cutoff_time = datetime.datetime.now() - datetime.timedelta(minutes=timeframe_minutes)
        recent_events = [
            event for event in self.episodic_memory 
            if event["timestamp"] > cutoff_time
        ]
        return recent_events
    
    def infer_state(self):
        """Infer the current state of the world from context"""
        recent_events = self.get_recent_context()
        
        # Example: Infer robot location from navigation events
        current_location = "unknown"
        for event in reversed(recent_events):
            if event["event"]["type"] == "navigation" and event["event"]["status"] == "completed":
                current_location = event["event"]["destination"]
                break
        
        # Example: Infer task progress
        current_task = "idle"
        for event in reversed(recent_events):
            if event["event"]["type"] == "task":
                current_task = event["event"]["task_name"]
                break
        
        return {
            "location": current_location,
            "task": current_task,
            "recent_events": recent_events[-10:]  # Last 10 events
        }
```

## Handling Ambiguity and Clarification

Real-world commands are often ambiguous and need clarification:

```python
class AmbiguityResolver:
    def __init__(self, context_manager):
        self.context_manager = context_manager
        
    def resolve_ambiguity(self, command):
        """Determine if command is ambiguous and what clarification is needed"""
        context = self.context_manager.infer_state()
        
        # Example ambiguities to check
        if "it" in command.lower() or "that" in command.lower():
            # Check if "it" or "that" refers to something in context
            if not self.resolve_pronoun(command, context):
                return {
                    "ambiguous": True,
                    "clarification_needed": "What does 'it' or 'that' refer to?",
                    "options": self.get_possible_referents(context)
                }
        
        if "there" in command.lower():
            # Unclear location reference
            return {
                "ambiguous": True,
                "clarification_needed": "Where specifically do you mean?",
                "options": ["kitchen", "living room", "bedroom", "dining room"]
            }
        
        # Check for ambiguous object references
        import re
        object_patterns = re.findall(r'the (\w+)', command.lower())
        for obj in object_patterns:
            if self.is_ambiguous_object(obj, context):
                return {
                    "ambiguous": True,
                    "clarification_needed": f"Which {obj} do you mean?",
                    "options": self.get_specific_objects(obj, context)
                }
        
        return {"ambiguous": False}
    
    def resolve_pronoun(self, command, context):
        """Try to resolve pronouns like 'it' or 'that' using context"""
        # Implementation would use context to resolve pronouns
        # For simplicity, return False to trigger clarification
        return False
    
    def is_ambiguous_object(self, obj, context):
        """Check if an object reference is ambiguous"""
        # Implementation would check if multiple instances exist
        return obj in ["object", "item", "thing", "one"]
    
    def get_specific_objects(self, obj, context):
        """Get specific instances of an object type"""
        return [f"{obj}_1", f"{obj}_2", f"{obj}_3"]
    
    def get_possible_referents(self, context):
        """Get possible referents for pronouns"""
        return ["the bottle", "the chair", "the table", "the person"]
```

## Plan Execution and Monitoring

Once a plan is created, it needs to be executed and monitored:

```python
class PlanExecutor:
    def __init__(self, robot_interface, context_manager):
        self.robot_interface = robot_interface
        self.context_manager = context_manager
        self.current_plan = None
        self.current_step = 0
        
    def execute_plan(self, plan):
        """Execute a plan step by step"""
        self.current_plan = plan
        self.current_step = 0
        
        for i, action in enumerate(plan):
            self.current_step = i
            success = self.execute_action(action)
            
            if not success:
                return self.handle_failure(action, i)
        
        return True
    
    def execute_action(self, action):
        """Execute a single action"""
        action_type = action["action"]
        parameters = action.get("parameters", {})
        
        try:
            if action_type == "navigate_to_coordinates":
                return self.robot_interface.navigate_to(
                    parameters["x"], 
                    parameters["y"], 
                    parameters.get("theta", 0.0)
                )
            elif action_type == "pick_up_object":
                return self.robot_interface.pick_up_object(
                    parameters["object"]
                )
            elif action_type == "place_object":
                return self.robot_interface.place_object(
                    parameters["object"], 
                    parameters["location"]
                )
            elif action_type == "speak_text":
                return self.robot_interface.speak_text(
                    parameters["text"]
                )
            else:
                self.robot_interface.speak_text(f"Unknown action: {action_type}")
                return False
                
        except Exception as e:
            self.robot_interface.speak_text(f"Error executing action: {str(e)}")
            return False
    
    def handle_failure(self, failed_action, step_index):
        """Handle plan execution failure"""
        self.context_manager.update_context({
            "type": "failure",
            "action": failed_action,
            "step": step_index,
            "reason": "action_execution_failed"
        })
        
        # For this implementation, return False to indicate failure
        # A more sophisticated system might have recovery actions
        return False
```

## Integration with ROS 2

Integrating cognitive planning with ROS 2 requires proper message passing:

```python
import rclpy
from rclpy.node import Node
from std_msgs.msg import String
from geometry_msgs.msg import Pose
from cognitive_planning_msgs.msg import Plan, PlanStep  # Custom message

class CognitivePlannerNode(Node):
    def __init__(self):
        super().__init__('cognitive_planner_node')
        
        # Publishers
        self.plan_publisher = self.create_publisher(Plan, 'robot_plan', 10)
        self.feedback_publisher = self.create_publisher(String, 'planner_feedback', 10)
        
        # Subscribers
        self.command_subscriber = self.create_subscription(
            String,
            'high_level_command',
            self.command_callback,
            10
        )
        
        # Initialize planner components
        self.context_manager = ContextManager()
        self.ambiguity_resolver = AmbiguityResolver(self.context_manager)
        self.hierarchical_planner = HierarchicalPlanner(CognitivePlanner(openai_api_key="YOUR_KEY"))
        self.plan_executor = PlanExecutor(RobotInterface(), self.context_manager)
        
        self.get_logger().info('Cognitive Planner Node initialized')
    
    def command_callback(self, msg):
        """Process a high-level command"""
        command_text = msg.data
        self.get_logger().info(f'Received command: {command_text}')
        
        # Check for ambiguity
        ambiguity_check = self.ambiguity_resolver.resolve_ambiguity(command_text)
        if ambiguity_check["ambiguous"]:
            feedback_msg = String()
            feedback_msg.data = f"Clarification needed: {ambiguity_check['clarification_needed']}"
            self.feedback_publisher.publish(feedback_msg)
            return
        
        # Create and execute plan
        plan = self.hierarchical_planner.create_plan(command_text)
        
        # Publish the plan
        plan_msg = self.convert_to_ros_plan(plan)
        self.plan_publisher.publish(plan_msg)
        
        # Execute the plan
        success = self.plan_executor.execute_plan(plan)
        
        # Report results
        result_msg = String()
        if success:
            result_msg.data = f"Successfully executed command: {command_text}"
        else:
            result_msg.data = f"Failed to execute command: {command_text}"
        
        self.feedback_publisher.publish(result_msg)
        
        # Update context
        self.context_manager.update_context({
            "type": "command_execution",
            "command": command_text,
            "result": "success" if success else "failure"
        })
    
    def convert_to_ros_plan(self, plan):
        """Convert internal plan representation to ROS message"""
        plan_msg = Plan()
        
        for i, step in enumerate(plan):
            step_msg = PlanStep()
            step_msg.id = i
            step_msg.action = step["action"]
            
            # Convert parameters to string format for simplicity
            import json
            step_msg.parameters = json.dumps(step.get("parameters", {}))
            
            plan_msg.steps.append(step_msg)
        
        return plan_msg

# Example RobotInterface class (simplified)
class RobotInterface:
    def __init__(self):
        pass
    
    def navigate_to(self, x, y, theta):
        # Implementation would send navigation goals to Nav2
        print(f"Navigating to ({x}, {y}, {theta})")
        return True  # Simulated success
    
    def pick_up_object(self, obj_name):
        # Implementation would control manipulator
        print(f"Attempting to pick up {obj_name}")
        return True  # Simulated success
    
    def place_object(self, obj_name, location):
        # Implementation would control manipulator
        print(f"Placing {obj_name} at {location}")
        return True  # Simulated success
    
    def speak_text(self, text):
        # Implementation would use text-to-speech
        print(f"Robot says: {text}")
        return True  # Simulated success

def main(args=None):
    rclpy.init(args=args)
    planner_node = CognitivePlannerNode()
    
    try:
        rclpy.spin(planner_node)
    except KeyboardInterrupt:
        pass
    finally:
        planner_node.destroy_node()
        rclpy.shutdown()

if __name__ == '__main__':
    main()
```

## Error Handling and Recovery

Robust cognitive planning systems need to handle errors gracefully:

```python
class RecoverySystem:
    def __init__(self):
        self.recovery_strategies = {
            "navigation_failure": [
                "try_alternative_path",
                "request_human_help",
                "wait_and_retry"
            ],
            "object_not_found": [
                "expand_search_area",
                "ask_for_help",
                "substitute_alternative"
            ],
            "grasp_failure": [
                "adjust_grasp_approach",
                "request_assistance",
                "try_different_object"
            ]
        }
    
    def suggest_recovery(self, failure_type):
        """Suggest recovery strategies for a given failure type"""
        if failure_type in self.recovery_strategies:
            return self.recovery_strategies[failure_type]
        else:
            return ["request_human_help"]
    
    def execute_recovery(self, strategy, context):
        """Execute a recovery strategy"""
        if strategy == "try_alternative_path":
            # Implementation would involve replanning with Nav2
            print("Attempting alternative navigation path...")
            return True
        elif strategy == "request_human_help":
            # Ask human for assistance
            print("Requesting human assistance...")
            return False  # Need human intervention
        elif strategy == "expand_search_area":
            # Increase the area to search for an object
            print("Expanding search area...")
            return True
        else:
            print(f"Unknown recovery strategy: {strategy}")
            return False
```

## Summary

Cognitive planning with LLMs enables humanoid robots to understand and execute complex, natural language commands. By combining LLMs for natural language understanding and task decomposition with robust execution monitoring and error handling, we can create sophisticated robotic systems that interact naturally with humans. The key components include natural language understanding, hierarchical planning, context management, ambiguity resolution, and error recovery.

## Exercises

1. Implement a simple cognitive planner that can handle basic commands
2. Add context management to track the robot's state across interactions
3. Create a system that asks for clarification when commands are ambiguous

## Next Steps

In the next chapter, we'll explore how to integrate vision, language, and action systems into a complete pipeline, bringing together all the components learned in this course into a cohesive system for humanoid robots.