Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions lessons/06-debugging/demo-script.md
Original file line number Diff line number Diff line change
Expand Up @@ -226,9 +226,9 @@ and the call from motor.cpp.

**Step 1: Open the demo file**

Open `src-ODrive/Firmware/MotorControl/demo_buggy.cpp` and scroll to the `FaultLogger` class (around line 23).
Open `src-ODrive/Firmware/MotorControl/demo_buggy.cpp` and scroll to the `FaultLogger` class (around line 24).

**The buggy code (lines 29-37):**
**The buggy code (lines 31-38):**
```cpp
// BUG: Can you spot the off-by-one error?
void log_fault(uint32_t error_code) {
Expand All @@ -253,7 +253,7 @@ void log_fault(uint32_t error_code) {

**Step 2: Use /fix in Chat**

1. **Select lines 29-37** (the entire `log_fault` function)
1. **Select lines 31-38** (the entire `log_fault` function)
2. Open **Inline Chat** panel (`Ctrl+I`) first
3. Type:
```
Expand Down Expand Up @@ -314,9 +314,9 @@ Explanation:

**Step 4: Show threading bug**

**In the same file (`demo_buggy.cpp`), scroll to the `Encoder` class (around line 52).**
**In the same file (`demo_buggy.cpp`), scroll to the `Encoder` class (around line 55).**

**The buggy code (lines 63-76):**
**The buggy code (lines 65-77):**
```cpp
// Called from 8 kHz interrupt
void update_isr() {
Expand All @@ -342,7 +342,7 @@ float get_position() {

**Step 5: Ask /fix for solutions**

1. **Select lines 63-80** (both `update_isr()` and `get_position()` functions)
1. **Select lines 65-81** (both `update_isr()` and `get_position()`/`get_velocity()` functions)
2. Open **Copilot Chat** panel (`Ctrl+Alt+I`)
3. Type:
```
Expand Down Expand Up @@ -410,7 +410,7 @@ Recommendation for 8 kHz ISR: Option 1 (interrupt disable) - only ~10 cycles.
## Demo 3: Real-World Debug Session (9 min)

### Setup
- Continue using `demo_buggy.cpp` - scroll to the `SpeedCalculator` class (around line 85)
- Continue using `demo_buggy.cpp` - scroll to the `SpeedCalculator` class (around line 92)
- This demonstrates the full /explain → /fix → test workflow

### Demo Flow
Expand All @@ -422,7 +422,7 @@ Recommendation for 8 kHz ISR: Option 1 (interrupt disable) - only ~10 cycles.

**Step 1: Show the buggy code**

**In `demo_buggy.cpp`, scroll to the `SpeedCalculator` class (line 85). The buggy code is lines 96-106:**
**In `demo_buggy.cpp`, scroll to the `SpeedCalculator` class (line 92). The buggy code is lines 104-111:**

```cpp
// BUG: Integer overflow at high speeds!
Expand All @@ -446,7 +446,7 @@ float calculate_rpm() {

**Step 2: Ask for explanation**

1. **Select lines 96-106** (the `calculate_rpm` function)
1. **Select lines 104-111** (the `calculate_rpm` function)
2. Open **Copilot Chat** panel (`Ctrl+Alt+I`)
3. Type:
```
Expand Down Expand Up @@ -523,7 +523,7 @@ First select @ODrive-QA in chat.
Generate a unit test for calculate_rpm that tests high speed values to catch integer overflow
```

> **Note:** Using `@ODrive-QA` invokes the `odrive-qa-assistant` skill which is specialized for test generation.
> **Note:** Using `@ODrive-QA` invokes the `cpp-testing` skill which is specialized for test generation.

**Expected AI Response:**
```cpp
Expand Down Expand Up @@ -559,7 +559,7 @@ TEST_CASE("Encoder RPM calculation at high speed") {
```

**Presenter Says:**
> "Excellent! `@ODrive-QA` generated a unit test that specifically checks high-speed values where overflow would occur. This ensures the bug stays fixed. The agent invoked the `odrive-qa-assistant` skill to create comprehensive tests."
> "Excellent! `@ODrive-QA` generated a unit test that specifically checks high-speed values where overflow would occur. This ensures the bug stays fixed. The agent invoked the `cpp-testing` skill to create comprehensive tests."

---

Expand Down
30 changes: 15 additions & 15 deletions lessons/06-debugging/hands-on-exercises.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ Open this file in VS Code before starting.
### Bug Locations in demo_buggy.cpp
| Bug | Class | Lines |
|-----|-------|-------|
| 1. Circular Buffer | `FaultLogger` | 29-37 |
| 2. Race Condition | `Encoder` | 63-80 |
| 3. Integer Overflow | `SpeedCalculator` | 96-106 |
| 1. Circular Buffer | `FaultLogger` | 31-38 |
| 2. Race Condition | `Encoder` | 65-81 |
| 3. Integer Overflow | `SpeedCalculator` | 104-111 |

### If demo_buggy.cpp Doesn't Exist

Expand Down Expand Up @@ -197,7 +197,7 @@ The motor controller logs the last 10 faults in a circular buffer. Users report

### Buggy Code

**File:** `src-ODrive/Firmware/MotorControl/demo_buggy.cpp` — `FaultLogger` class (lines 23-48)
**File:** `src-ODrive/Firmware/MotorControl/demo_buggy.cpp` — `FaultLogger` class (lines 24-48)

```cpp
#define FAULT_HISTORY_SIZE 10
Expand Down Expand Up @@ -225,8 +225,8 @@ public:

**Step 1: Identify the Bug (2 min)**

1. Open `demo_buggy.cpp` and scroll to the `FaultLogger` class (line 23)
2. **Select lines 29-37** (the `log_fault` function)
1. Open `demo_buggy.cpp` and scroll to the `FaultLogger` class (line 24)
2. **Select lines 31-38** (the `log_fault` function)
3. Open **Copilot Chat** panel (`Ctrl+Alt+I`)
4. Type:
```
Expand All @@ -251,7 +251,7 @@ Ask Copilot to generate a test:
@ODrive-QA Generate a unit test that logs 15 faults and verifies no crash
```

> **Note:** Using `@ODrive-QA` invokes the `odrive-qa-assistant` skill for test generation.
> **Note:** Using `@ODrive-QA` invokes the `cpp-testing` skill for test generation.

Run the test mentally or with a simple main():
```cpp
Expand Down Expand Up @@ -304,7 +304,7 @@ The encoder updates position estimates in an 8 kHz interrupt. The main control l

### Buggy Code

**File:** `src-ODrive/Firmware/MotorControl/demo_buggy.cpp` — `Encoder` class (lines 52-81)
**File:** `src-ODrive/Firmware/MotorControl/demo_buggy.cpp` — `Encoder` class (lines 65-81)

```cpp
class Encoder {
Expand Down Expand Up @@ -342,8 +342,8 @@ public:

**Step 1: Identify the Race (2 min)**

1. In `demo_buggy.cpp`, scroll to the `Encoder` class (line 52)
2. **Select lines 63-80** (both `update_isr()` and `get_position()` functions)
1. In `demo_buggy.cpp`, scroll to the `Encoder` class (line 55)
2. **Select lines 65-81** (both `update_isr()` and `get_position()`/`get_velocity()` functions)
3. Open **Copilot Chat** panel (`Ctrl+Alt+I`)
4. Type:
```
Expand Down Expand Up @@ -463,7 +463,7 @@ The encoder calculates motor RPM from tick counts. At low speeds it works fine,

### Buggy Code

**File:** `src-ODrive/Firmware/MotorControl/demo_buggy.cpp` — `SpeedCalculator` class (lines 85-109)
**File:** `src-ODrive/Firmware/MotorControl/demo_buggy.cpp` — `SpeedCalculator` class (lines 104-111)

```cpp
class SpeedCalculator {
Expand Down Expand Up @@ -497,8 +497,8 @@ public:

**Step 1: Understand the Bug (2 min)**

1. In `demo_buggy.cpp`, scroll to the `SpeedCalculator` class (line 85)
2. **Select lines 96-106** (the `calculate_rpm` function)
1. In `demo_buggy.cpp`, scroll to the `SpeedCalculator` class (line 92)
2. **Select lines 104-111** (the `calculate_rpm` function)
3. Open **Copilot Chat** panel (`Ctrl+Alt+I`)
4. Type:
```
Expand Down Expand Up @@ -535,7 +535,7 @@ Ask:
@ODrive-QA Generate a unit test for calculate_rpm that tests speeds from 100 to 50,000 RPM
```

> **Note:** `@ODrive-QA` will invoke the `odrive-qa-assistant` skill to create comprehensive test cases.
> **Note:** `@ODrive-QA` will invoke the `cpp-testing` skill to create comprehensive test cases.

### Success Criteria
- ✅ Understand intermediate overflow concept
Expand Down Expand Up @@ -676,7 +676,7 @@ Context: [files involved, expected vs actual behavior]
@ODrive-QA Verify the fix compiles correctly
```

> **Note:** `@ODrive-QA` invokes the `odrive-qa-assistant` skill for builds and tests.
> **Note:** `@ODrive-QA` invokes the `cpp-testing` and `odrive-toolchain` skills for builds and tests.

### Context is Key

Expand Down
67 changes: 59 additions & 8 deletions lessons/06-debugging/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ Debugging is where developers spend 30-50% of their time. This lesson teaches yo

- [Overview](#overview)
- [Prerequisites](#prerequisites)
- [Available Agents and Skills](#available-agents-and-skills)
- [Why Debugging with Copilot Matters](#why-debugging-with-copilot-matters)
- [Learning Path](#learning-path)
- [@terminal for Build Errors](#1-terminal-for-build-errors-10-min)
Expand Down Expand Up @@ -75,6 +76,56 @@ Before starting this session, ensure you have:

---

## Available Agents and Skills

This repository includes several custom agents and skills that enhance debugging capabilities. Understanding when to use each agent is key to efficient debugging.

### Custom Agents

| Agent | Invocation | Purpose | Best For |
|-------|------------|---------|----------|
| **ODrive Engineer** | `@ODrive-Engineer` | Primary orchestrator for development tasks | Multi-file bugs, firmware development, complex analysis |
| **ODrive QA** | `@ODrive-QA` | Testing & QA orchestrator | Test generation, build verification, coverage analysis |
| **ODrive Reviewer** | `@ODrive-Reviewer` | Code review specialist | Style, safety, embedded best practices |
| **ODrive Toolchain** | `@ODrive-Toolchain` | Build & test operations | Building firmware, running tests, symbol search |
| **ODrive Ops** | `@ODrive-Ops` | CI/CD workflows | Release management, GitHub Actions |
| **Ada to C++ Migrator** | `@ada-to-cpp-migrator` | Language migration specialist | Migrating Ada code to Modern C++ |

### Available Skills

Skills are specialized capabilities that agents can invoke:

| Skill | Description | Status |
|-------|-------------|--------|
| **cpp-testing** | Unit test generation with doctest framework | ✅ Production |
| **odrive-toolchain** | Build firmware, run tests, symbol search | ✅ Production |
| **odrive-ops** | CI/CD workflows, releases, deployment | ✅ Production |
| **control-algorithms** | PID controllers, observers, control transformations | 🚧 Planned |
| **foc-tuning** | Automated FOC parameter tuning | 🚧 Planned |
| **sensorless-control** | Sliding mode observers, PLL, back-EMF estimation | 🚧 Planned |
| **pcb-review** | PCB schematic/layout review | 🚧 Planned |
| **signal-integrity** | Impedance calculation, EMI analysis | 🚧 Planned |
| **ada-cpp-migration** | Ada to Modern C++ migration patterns | ✅ Production |

### When to Use Which Agent

```
Build/compile error? ──→ @terminal (quick fix)
└─→ @ODrive-Toolchain (complex build issues)

Single-file logic bug? ──→ /fix (inline correction)

Multi-file bug? ──→ @ODrive-Engineer (orchestrated analysis)

Need tests? ──→ @ODrive-QA (test generation)

Code review? ──→ @ODrive-Reviewer (style & safety check)

CI/CD issue? ──→ @ODrive-Ops (workflow management)
```

---

## Why Debugging with Copilot Matters

Debugging is where developers spend 30-50% of their time. AI-assisted debugging can dramatically reduce this overhead.
Expand Down Expand Up @@ -389,7 +440,7 @@ The `/fix` command is a **code-focused debugging assistant** that:

#### 1. **Off-by-One Errors**

**File:** `demo_buggy.cpp` — FaultLogger class (line 29-37)
**File:** `demo_buggy.cpp` — FaultLogger class (lines 31-38)

**Buggy Code:**
```cpp
Expand Down Expand Up @@ -452,7 +503,7 @@ Or initialize watchdog_timer_ in constructor to avoid nullptr.

#### 3. **Race Conditions (Thread Safety)**

**File:** `demo_buggy.cpp` — Encoder class (lines 63-80)
**File:** `demo_buggy.cpp` — Encoder class (lines 65-81)

**Buggy Code:**
```cpp
Expand Down Expand Up @@ -537,7 +588,7 @@ If heap is required:

#### 5. **Integer Overflow**

**File:** `demo_buggy.cpp` — SpeedCalculator class (lines 96-106)
**File:** `demo_buggy.cpp` — SpeedCalculator class (lines 104-111)

**Buggy Code:**
```cpp
Expand Down Expand Up @@ -762,7 +813,7 @@ while (dma1->SR & DMA_BUSY) {
### Debugging Strategy with Copilot
**🎯 Copilot Modes: Chat + Agent**

> **Tip:** For complex debugging involving multiple files or domain expertise, use `@ODrive-Engineer` for firmware issues or `@ODrive-QA` for test-related debugging. The agents can invoke specialized skills like `odrive-qa-assistant` to build and verify fixes.
> **Tip:** For complex debugging involving multiple files or domain expertise, use `@ODrive-Engineer` for firmware issues or `@ODrive-QA` for test-related debugging. The agents can invoke specialized skills like `cpp-testing` to build and verify fixes.

**Step 1: Reproduce the Bug**
- Can you trigger it consistently?
Expand Down Expand Up @@ -865,7 +916,7 @@ if (fault_write_idx_ >= FAULT_HISTORY_SIZE) {

### Bug 2: Race Condition in Position Estimate (5 min)

**File:** `demo_buggy.cpp` — Encoder class (lines 52-81)
**File:** `demo_buggy.cpp` — Encoder class (lines 65-81)

**Buggy Code:**
```cpp
Expand Down Expand Up @@ -937,7 +988,7 @@ void Encoder::get_estimates(float* pos, float* vel) {

### Bug 3: Integer Overflow in Speed Calculation (5 min)

**File:** `demo_buggy.cpp` — SpeedCalculator class (lines 96-106)
**File:** `demo_buggy.cpp` — SpeedCalculator class (lines 104-111)

**Buggy Code:**
```cpp
Expand All @@ -956,8 +1007,8 @@ float SpeedCalculator::calculate_rpm() {
**Your Task:**

**Step 1:** Use Copilot Chat to detect overflow
1. Open `demo_buggy.cpp`, scroll to SpeedCalculator class (line 85)
2. Select lines 96-106 (the `calculate_rpm` function)
1. Open `demo_buggy.cpp`, scroll to SpeedCalculator class (line 92)
2. Select lines 104-111 (the `calculate_rpm` function)
3. Open Chat (`Ctrl+Alt+I`) and type:
```
/explain why this gives wrong values at high speeds
Expand Down
6 changes: 3 additions & 3 deletions lessons/06-debugging/slides.md
Original file line number Diff line number Diff line change
Expand Up @@ -548,15 +548,15 @@ float get_position() const { // Now const-correct
**Fix 3 bugs in `demo_buggy.cpp` in 15 minutes:**

1. **⭐⭐ Circular Buffer (5 min)** — FaultLogger class
- Off-by-one boundary check (lines 29-37)
- Off-by-one boundary check (lines 31-38)
- Use /fix in Chat mode

2. **⭐⭐⭐ Race Condition (5 min)** — Encoder class
- ISR writes, main loop reads (lines 63-80)
- ISR writes, main loop reads (lines 65-81)
- Evaluate multiple solutions

3. **⭐⭐ Integer Overflow (5 min)** — SpeedCalculator class
- RPM calculation overflow (lines 96-106)
- RPM calculation overflow (lines 104-111)
- Use /explain then /fix

**Files:** `demo_buggy.cpp` + `hands-on-exercises.md`
Expand Down