image

Lesson 2: Copilot Fundamentals, Debugging & Refactoring for C++

Session Duration: 2 hours 25 minutes (three sections)
Audience: Embedded/C++ Developers, Firmware Engineers, QA Engineers
Environment: Windows, VS Code
Extensions: GitHub Copilot
Source Control: GitHub/Bitbucket


Overview

This lesson covers three progressive sections: workspace setup, core Copilot features tailored for C++ development, and practical debugging/refactoring workflows. You’ll learn to use Copilot with real embedded C++ code — templates, macros, pointers, compiler errors, and runtime bugs.

What You’ll Learn:

  • Inline code suggestions for C++ (comment-driven completions, templates, ISR code)
  • Using Copilot to understand unfamiliar C++ — templates, macros, pointers
  • Navigating large C++ repos with #codebase and #file: references
  • /explain and /fix with embedded C++ examples
  • Diagnosing compiler errors and linker errors with Copilot
  • Debugging runtime issues — null pointers, memory bugs, undefined behavior
  • Using @terminal for build commands and error analysis

Key Concepts:

ConceptDescription
Ask ModeQuestion-and-answer about code, architecture, debugging
Edit ModeCode generation, refactoring, documentation via Inline Chat
Agent ModeAutonomous multi-step tasks across files with planning
Chat Participants@terminal, @vscode, @github for domain-specific assistance
Slash Commands/explain, /fix, /doc, /tests for common operations
#codebaseSemantic search across the entire project

Table of Contents


Prerequisites

Before starting this session, ensure you have:

  • Visual Studio Code installed (latest version recommended)
  • GitHub Copilot subscription (or Copilot Free plan with monthly limits)
  • ✅ Active internet connection for AI model access
  • ✅ A GitHub account for authentication

Copilot Feature Availability Across IDEs

GitHub Copilot features vary by IDE. This workshop focuses on VS Code, which has the most comprehensive feature set.

[Image: Copilot Features by IDE]

FeatureVS CodeVisual StudioJetBrainsEclipseXcodeNeovimGitHub.com
Code completion
Copilot Chat
Model picker
Agent mode
Edit mode
Extensions
Code review
Custom instructions
MCP Support
PR summaries
Text completions
Copilot Spaces
Copilot Coding Agent

Note: VS Code and Visual Studio offer the richest Copilot experience with Edit mode, Code review, and Custom instructions support.


Verify Your Setup

  1. Check VS Code installation:

    • Open VS Code
    • Press Ctrl+Shift+P and type “About” to verify version
  2. Verify Copilot subscription:

  3. Install GitHub Copilot extension:

    • Open VS Code Extensions (Ctrl+Shift+X)
    • Search for “GitHub Copilot”
    • Install both “GitHub Copilot” and “GitHub Copilot Chat” extensions

Why Copilot Features Matter

Understanding the full range of Copilot features enables you to:

Benefits of Mastering Copilot Features

  1. Right Tool for the Job

    • Ask Mode for exploration and understanding
    • Edit Mode for targeted code changes
    • Agent Mode for complex multi-file tasks
  2. Faster Code Comprehension

    • Use #codebase to search project-wide for patterns and definitions
    • Use /explain to decode unfamiliar templates, macros, and pointer semantics
    • Use #file: references for context-aware answers
  3. Accelerated Development

    • Generate tests with /tests
    • Fix bugs with /fix
    • Document code with /doc
  4. Seamless IDE Integration

    • Terminal assistance with @terminal
    • VS Code settings help with @vscode
    • GitHub integration with @github

Learning Path

This lesson is divided into three progressive sections:

SectionTopicsTime
1. Welcome & SetupWorkshop objectives, Copilot overview, workspace setup, high-level architecture40 min
2. Copilot FundamentalsInline suggestions, understanding C++ (templates, macros, pointers), navigating large repos, /explain, /fix45 min
3. Debugging & RefactoringCompiler errors, runtime issues (null pointers, memory), @terminal + /fix workflow60 min
Total2h 25m

Agenda

Section 1 — Welcome & Setup (40 min)

Sub-TopicFocusTime
Workshop ObjectivesGoals, expected outcomes, logistics5 min
GitHub Copilot OverviewWhat it is, how it works (high level)10 min
Workspace Setup for C++VS Code, extensions, open ODrive repo, verify Copilot15 min
High-Level Copilot ArchitectureLLM, context window, prompt construction (keep it high level)10 min

Section 2 — Copilot Fundamentals for C++ Development (45 min)

Sub-TopicFocusTime
Inline Code SuggestionsComment-driven completions, Tab/Escape, C++ specifics10 min
Understanding Unfamiliar C++Ask Mode + /explain: templates, macros, pointers10 min
Navigating Large C++ Repos#codebase, #file: references, tracing code paths10 min
/explain and /fix with C++Slash commands on real firmware code10 min
Hands-On Exercises 1–4Practice fundamentals5 min

Section 3 — Debugging and Refactoring C++ with Copilot (60 min)

Sub-TopicFocusTime
Diagnosing Compiler ErrorsPaste errors into Chat, template errors, linker errors15 min
Runtime IssuesNull pointers, memory bugs, undefined behavior, ISR safety15 min
@terminal and /fix WorkflowBuild → analyze errors → fix → rebuild cycle15 min
Hands-On Exercises 5–7Practice debugging15 min

1. Get Started with Chat in VS Code

Chat in Visual Studio Code enables you to use natural language for AI-powered coding assistance. Ask questions about your code, get help understanding complex logic, generate new features, fix bugs, and more - all through a conversational interface.

Access Chat in VS Code

🎯 Copilot Mode: All Modes

VS Code provides three ways to start an AI chat conversation, each optimized for different workflows and tasks:

1. Chat View

Press Ctrl+Alt+I to open the Chat view in a dedicated side panel. If you prefer a larger workspace for chat, you can open it as an editor tab by selecting New Chat Editor from the chat menu or as a separate window by selecting New Chat Window.

Use the Chat view for:

  • Ongoing, multi-turn chat conversations
  • Switching between different agents to ask questions, make code edits across files, or start autonomous coding workflows
  • Working on features that span multiple files
  • Planning and implementing complex changes

2. Inline Chat

Press Ctrl+I to start a chat conversation directly in your editor or terminal.

Use inline chat for:

  • Getting suggestions inline, right where you’re working
  • Understanding code in your current context
  • Getting help with terminal commands and output

3. Quick Chat

Press Ctrl+Shift+Alt+L to open a lightweight chat overlay.

Use quick chat for:

  • Quick questions that don’t require extended conversation
  • Getting answers without changing your current view
  • Looking up information while maintaining focus on your work

Essential Keyboard Shortcuts

ShortcutAction
Ctrl+Alt+IOpen the Chat view
Ctrl+IEnter voice chat prompt in Chat view / Start inline chat in editor or terminal
Ctrl+I (hold)Start inline voice chat
Ctrl+NStart a new chat session in Chat view
Ctrl+Shift+ISwitch to using agents in Chat view
Ctrl+Shift+Alt+LOpen Quick Chat
Ctrl+Alt+.Show the model picker to select a different AI model
TabAccept inline suggestion or navigate to next edit suggestion
EscapeDismiss inline suggestion
F2Get AI-powered suggestions when renaming symbols

2. Copilot Chat Modes

Ask Mode

🎯 Purpose: Questions, exploration, understanding

Use the Chat panel to ask questions about the codebase, architecture, or debug issues.

Example Prompts:

Explain this project in simple, layman's terms. What is the high-level 
functionality? Provide a tour of the folder structure and explain the 
key external dependencies.
Explain the high-level architecture of Firmware/MotorControl. How do 
the Axis, Motor, and Encoder classes interact during the control loop?
How is the custom fibre protocol implemented in #file:fibre.cpp? 
Explain how an object method on the firmware side is exposed to the 
Python client.
I am seeing ERROR_OVERSPEED in my logs. Look at #file:axis.cpp and 
#file:motor.cpp. What specific conditions trigger this error and how 
can I disable it safely?
What is the pin mapping for the SPI interface in #file:board.cpp? 
Can you list the GPIO pins used for the DRV8301?
How does the build system work in this repo? Detailed check of 
#file:Tupfile.lua and explain how the firmware binary is generated 
from the source files.

Edit Mode

🎯 Purpose: Code generation, refactoring, documentation

Use Inline Chat (Ctrl+I) to generate/refactor code, add documentation, or create scripts/tests.

Example Prompts:

Add a new configuration variable `float soft_stop_rate` to the 
ControllerConfig struct. Then, update the update() method in 
controller.cpp to use this rate to decelerate the velocity setpoint 
to zero when the axis state changes to IDLE.
Create a new test case using the doctest framework that verifies 
the SVM utility function in #file:utils.cpp handles all six sextants 
correctly with edge-case inputs.
Add Doxygen-style comments to the on_measurement function in 
#file:foc.cpp, explaining the Clarke transform and the current 
measurement processing.
Write a Python script using odrive.enums that connects to a generic 
ODrive, configures the plotter, and graphs 
axis0.motor.current_control.Iq_measured and 
axis0.motor.current_control.Iq_setpoint in real-time.
Optimize the SVM function in #file:utils.cpp using SIMD intrinsics 
or faster math approximations if possible, while maintaining 
readability and accuracy.
Rename the function delay_us to delay_ust in #file:utils.hpp and 
update all files that call this function to use the new name.

Agent Mode

🎯 Purpose: Multi-step, cross-file, autonomous tasks

Use for complex tasks where Copilot can plan, research, and execute changes.

Example Prompts:

Build the ODrive firmware by running the tup build command in the 
terminal. If there are compilation errors, analyze the error output, 
identify which files are causing issues, examine the problematic code, 
suggest fixes, apply them, and then rebuild to verify the fixes work.
Implement a 'Stall Detection' feature. Research how other motor 
controllers detect stalls. Modifications should likely involve 
#file:motor.cpp to monitor back-EMF vs current. Create a plan, 
then add the config flag and the detection logic.
Create a new test plan in tools/integration_tests/ that uses the 
odrivetool library. It should sequence a full calibration, enter 
closed loop control, perform a sinusoidal position move, and verify 
the final position error is within tolerance.
I want to port the Arduino/ArduinoI2C library to work with an 
STM32-based Arduino core. Analyze #file:ArduinoI2C.ino and identify 
which hardware-specific AVR registers or libraries need to be abstracted.
Find all instances of raw printf debug statements across the Firmware/ 
directory. Replace them with a new macro ODRIVE_LOG() that respects 
a compile-time log level defined in #file:freertos_vars.h.
Read #file:docs/error-codes.rst and compare it against the actual 
error enums defined in #file:odrive-interface.yaml. List any 
discrepancies where the documentation is missing new error codes 
added to the code.

3. Chat Participants & Slash Commands

Chat Participants (@-mentions)

Chat participants handle domain-specific requests. VS Code provides built-in participants:

ParticipantDescriptionExample
@workspaceProject-wide context and code search@workspace how is authentication implemented?
@terminalShell commands and terminal help@terminal list the 5 largest files
@vscodeVS Code features and settings@vscode how to enable word wrapping?
@githubGitHub repos, issues, and PRs@github What are all open PRs assigned to me?

@workspace Examples:

@workspace Trace where ERROR_PHASE_RESISTANCE_OUT_OF_RANGE is defined 
in #file:Firmware/odrive-interface.yaml and find all occurrences where 
it is raised in the C++ firmware files.
@workspace Explain the odrive_firmware_pkg definition in 
#file:Firmware/Tupfile.lua. How does it gather the source files from 
MotorControl/ and communication/?
@workspace how is authentication implemented?

@terminal Examples:

@terminal What is the specific tup command to build only the firmware 
for the v3.6 board variant? Explain the flags.
@terminal scan tools/setup.py and generate the pip command to install 
all development dependencies for the Python tools.
@terminal list the 5 largest files in this workspace

@vscode Examples:

@vscode How do I configure launch.json to attach the Cortex-Debug 
extension to an ST-Link OpenOCD server for this project?
@vscode Create a tasks.json entry to run the tup build command 
when I press Ctrl+Shift+B.
@vscode how to enable word wrapping?

Slash Commands (/-commands)

Slash commands are shortcuts to specific functionality:

Slash CommandDescription
/docGenerate code documentation comments
/explainExplain a code block, file, or concept
/fixFix a code block or resolve errors
/testsGenerate tests for selected methods
/setupTestsGet help setting up a testing framework
/clearStart a new chat session
/newScaffold a new workspace or file
/newNotebookScaffold a new Jupyter notebook
/searchGenerate a search query for the Search view
/startDebuggingGenerate launch.json and start debugging

Chat Tools (#-mentions)

Use tools in chat to accomplish specialized tasks:

Chat ToolDescription
#fileReference a specific file as context
#codebasePerform a code search in the workspace
#selectionGet the current editor selection
#terminalSelectionGet the current terminal selection
#changesList of source control changes
#fetchFetch content from a web page
#githubRepoPerform a code search in a GitHub repo
#extensionsSearch for and ask about VS Code extensions

4. Inline Chat & Copilot Edits

Inline Suggestions

Start typing and Copilot provides inline suggestions. Press Tab to accept.

Tips for better inline suggestions:

  • Write descriptive comments before the code
  • Use meaningful variable and function names
  • Provide context with comments like // Generate SPI driver for...

Inline Chat (Ctrl+I)

Select code and press Ctrl+I to open inline chat for:

  • Adding documentation
  • Refactoring code
  • Fixing errors
  • Generating tests

Multi-File Editing (Agent Mode)

Agent Mode can make coordinated changes across multiple files:

  • Header and source file updates
  • Interface changes with all implementations
  • Refactoring with test updates

Hands-On Exercises

For detailed hands-on exercises with step-by-step instructions, see the separate exercises document:

📋 Hands-On Exercises (30 min)

The exercises are organized by lesson section:

Section 2 — Copilot Fundamentals:

  • Exercise 1: Inline Code Suggestions for C++
  • Exercise 2: Understand C++ Templates, Macros & Pointers
  • Exercise 3: Navigate a Large C++ Repo with #codebase
  • Exercise 4: /explain and /fix with C++ Examples

Section 3 — Debugging & Refactoring:

  • Exercise 5: Diagnose Compiler Errors
  • Exercise 6: Debug Runtime Issues (null pointers, memory)
  • Exercise 7: @terminal and /fix Workflow

💡 Tip: Do Exercises 1–4 after Section 2 and Exercises 5–7 after Section 3.


Quick Reference: GitHub Copilot Chat

Essential Commands

Command/PromptWhat It Does
Ctrl+Alt+IOpen the Chat view in the Secondary Side Bar
Ctrl+IStart inline chat in editor or terminal
Ctrl+Shift+Alt+LOpen Quick Chat
Ctrl+NStart a new chat session in Chat view
Ctrl+Alt+.Show model picker to select different AI model

Modes at a Glance

ModeShortcutBest For
AskChat viewQuestions, exploration, understanding
EditCtrl+ICode changes, refactoring, documentation
AgentChat with complex taskMulti-file, autonomous, planning

Chat Participants

ParticipantDescriptionExample

| @terminal | Shell commands and terminal help | @terminal list the 5 largest files | | @vscode | VS Code features and settings | @vscode how to enable word wrapping? | | @github | GitHub repos, issues, and PRs | @github What are all open PRs assigned to me? |

Slash Commands

CommandDescription
/docGenerate code documentation
/explainExplain code or concepts
/fixFix code errors
/testsGenerate unit tests
/setupTestsSetup testing framework
/newScaffold new workspace/file
/newNotebookCreate Jupyter notebook
/startDebuggingGenerate debug config

Chat Tools (# commands)

ToolDescription
#fileReference a specific file
#codebaseSearch the workspace
#selectionCurrent editor selection
#terminalSelectionCurrent terminal selection
#changesSource control changes
#fetchFetch web page content
#githubRepoSearch GitHub repository
#extensionsSearch VS Code extensions

Prompt Tips

  1. Add context with #file: - Reference specific files
  2. Be specific - Clear requirements get better results
  3. Iterate - Refine responses with follow-up questions
  4. Use constraints - Specify patterns, standards, limitations

Inline Suggestions

ActionDescription
Start typingGet inline suggestions as you code
TabAccept suggestion
EscapeDismiss suggestion
Code commentsProvide prompt via comments (e.g., # write a calculator class)

Editor AI Features

ActionDescription
Ctrl+IStart inline chat in editor
F2AI-powered symbol renaming
Right-click → Generate CodeAccess common AI actions (explain, fix, test, review)
Lightbulb (Code Actions)Fix linting/compiler errors

Source Control & Issues

FeatureDescription
Generate commit messageAI-generated commit messages based on changes
PR descriptionGenerate pull request titles and descriptions
@github participantQuery issues, PRs, and commits

Tips for Effective Prompts

  1. Add context with #-mentions: Reference specific files (#file), your codebase (#codebase), or terminal output (#terminalSelection)

  2. Use / commands: Type / to access common commands like /new, /explain, or create your own custom prompts

  3. Reference tools: Type # followed by a tool name to extend chat capabilities

  4. Be specific and iterative:

    • Provide clear requirements
    • Ask follow-up questions to refine results
    • Keep prompts simple and focused
  5. Choose the right agent:

    • Ask: Questions and exploration
    • Edit: Code modifications across files
    • Agent: Complex, multi-step autonomous tasks

Explore Different Language Models

VS Code offers different language models optimized for different tasks:

  • Some models are designed for fast coding tasks
  • Others excel at complex reasoning and planning

To change the language model:

  • Use the model picker in the chat input field (Ctrl+Alt+.)
  • Select the model that best fits your needs

Customize Your Chat Experience

Customize your chat experience to generate responses that match your coding style, tools, and developer workflow:

Custom Instructions

Define common guidelines for tasks like generating code, performing code reviews, or generating commit messages. Custom instructions describe the conditions in which the AI should operate (how a task should be done).

Reusable Prompt Files

Define reusable prompts for common tasks. Prompt files are standalone prompts that you can run directly in chat by typing /prompt-name.

Custom Agents

Define how chat operates, which tools it can use, and how it interacts with the codebase. Each chat prompt runs within the boundaries of the agent.

MCP Servers

Extend chat with custom capabilities by integrating external tools and services through the Model Context Protocol.

Tips:

  • Define language-specific instructions to get more accurate generated code
  • Store your instructions in your workspace to easily share them with your team
  • Define reusable prompt files for common tasks to save time

Troubleshooting

IssueSolution
Copilot not respondingRestart VS Code; verify Copilot extension is installed and enabled
Chat view not openingVerify GitHub Copilot Chat extension is installed and enabled
Authentication failsVerify Copilot subscription at https://github.com/settings/copilot
”Not authenticated” errorSign in to GitHub through VS Code (File → Preferences → Settings → GitHub)
Directory not trustedApprove when prompted, or mark folder as trusted in workspace settings
Slow responsesUse /compact to reduce context size, or switch to a faster language model
Wrong file being modifiedReview carefully before accepting; use full path with #file:
Inline suggestions not appearingCheck that GitHub Copilot extension is enabled and you’re signed in
PowerShell version issuesUpdate to PowerShell 7+: winget install Microsoft.PowerShell

Getting Help


Additional Resources

Microsoft Learn

Official Documentation


Frequently Asked Questions

Q: When should I use Ask Mode vs Edit Mode vs Agent Mode?

Short Answer: Ask for questions, Edit for changes, Agent for complex multi-step tasks.

Detailed Explanation:

  • Ask Mode: Use when you need to understand code, explore architecture, or get explanations. The AI reads but doesn’t modify files.
  • Edit Mode: Use when you know exactly what file/function to change. Select code, press Ctrl+I, describe the change.
  • Agent Mode: Use when the task requires research, planning, and changes across multiple files. The AI can autonomously explore, plan, and execute.

Q: How do I search across my project in Chat?

Short Answer: Use #codebase to perform a semantic search across your workspace.

Detailed Explanation:

  • #codebase: Performs semantic search across your code. Good for finding specific patterns, tracing definitions, and understanding how things connect.
  • #file: References a specific file for context-aware questions.

Use #codebase for “find all instances of X” and “how does X work?” queries. Use #file: when you know the specific file you want to discuss.


Q: How do I get better inline suggestions?

Short Answer: Provide more context through comments and meaningful names.

Detailed Explanation:

  1. Write a descriptive comment before the code you want generated
  2. Use meaningful function and variable names
  3. Have relevant files open in the editor
  4. Start typing the function signature to guide Copilot
  5. If suggestions are wrong, add more specific comments

Example:

// Calculate motor torque from current using Kt constant
// Input: phase_current_amps (float)
// Output: torque in Nm (float)
// Constraint: Must handle division by zero
float calculate_torque(

Q: Can Copilot work with my company’s private code?

Short Answer: Yes, Copilot can work with any code in your VS Code workspace.

Detailed Explanation:

  • Copilot analyzes code in your current workspace to provide context-aware suggestions
  • For business/enterprise plans, code is not used to train models
  • You can configure what data is sent to GitHub via VS Code settings
  • Check your organization’s Copilot policy for specific guidelines

Q: How do I control which AI model Copilot uses?

Short Answer: Use Ctrl+Alt+. to open the model picker.

Detailed Explanation: Different models have different strengths:

  • Claude Sonnet 4: Great for complex reasoning and planning
  • GPT-4o: Strong general-purpose model
  • Faster models: Lower latency for quick suggestions

You can change models per-conversation or set a default in VS Code settings.


Q: What if Copilot generates incorrect or insecure code?

Short Answer: Always review generated code; use /fix or ask for improvements.

Detailed Explanation:

  1. Review before accepting: Never blindly accept suggestions
  2. Request improvements: “Make this thread-safe” or “Add error handling”
  3. Specify constraints: Include security requirements in your prompts
  4. Use validation: Ask Copilot to review its own output for issues
  5. Test thoroughly: Generated code should go through normal testing

Example prompt for secure code:

Generate this function with:
- Input validation for all parameters
- Bounds checking on buffer access
- No dynamic allocation
- Error codes for failure cases

Summary: Key Takeaways

1. Three Modes for Three Purposes

  • Ask Mode: Questions and exploration (Chat view)
  • Edit Mode: Targeted code changes (Ctrl+I)
  • Agent Mode: Complex autonomous tasks

2. Navigating Large C++ Codebases

  • #codebase for project-wide semantic search
  • #file: for referencing specific files
  • /explain to decode templates, macros, and pointer semantics

3. Slash Commands Accelerate Common Tasks

  • /doc - Generate documentation
  • /explain - Understand code
  • /fix - Resolve errors
  • /tests - Create unit tests

4. Debugging Workflow

  • Paste compiler errors into Chat for diagnosis
  • Use /fix for targeted corrections
  • Use @terminal for build commands and output analysis
  • Chain: build → diagnose → fix → rebuild

5. Context is King

  • Use #file: to reference specific files
  • Provide constraints and requirements
  • Iterate with follow-up questions

6. Practice Makes Perfect

  • Start with Ask Mode to explore
  • Progress to Edit Mode for changes
  • Graduate to Agent Mode for complex tasks

Next Steps

After completing this session:

  1. Practice with your own projects - Try using different modes on your actual codebase
  2. Experiment with custom instructions - Define your coding standards and preferences
  3. Create reusable prompts - Build a library of common tasks for your team
  4. Explore advanced features - Try agents, custom tools, and MCP servers
  5. Share knowledge - Help your team adopt best practices

Lesson 2: Basic Feature Overview - GitHub Copilot in VS Code
Last Updated: January 2026