ad
ad

What I Learned from Letting AI Write My Microcontroller Code

Education


Introduction

As I've been writing firmware for my surface grinder project, I decided to try out some AI coding tools to see if they could enhance my software development experience. With over three decades in the field, I often wonder if these advancements make my life easier or if they signal the onset of a robot apocalypse—or perhaps both. In this exploration, I will share the insights I've gained while integrating these AI tools into my microcontroller coding tasks.

The Automation Project Journey

This article is part of my surface grinder automation project. Thus far, I have scanned a 3D model of the surface grinder, optimized the mechanical systems, constructed a control panel with both physical controls and a touchscreen, and am currently focused on the firmware that binds it all together. Firmware development is complex and critical, either transforming my project into an intuitive system or dragging me into a mire of bugs and confusion.

Using AI to Aid Development

To address some of my architectural questions, I turned to ChatGPT. I found it to be a helpful resource for brainstorming ideas and sorting through technical problems. For example, when I asked about tracking machine position for a CNC controller, it recommended retaining position information using steps for precision and converting to inches or millimeters only for user interaction. This kind of back-and-forth dialogue opened up avenues of thought that I hadn’t considered initially.

Establishing an Architecture

When considering unit systems, I followed an approach of tracking machine positions in encoder counts or steps, even if the motor step resolution varied across the axes. Internally storing positions in units smaller than the required precision (like nanometers) helped maintain accuracy without sacrificing clarity in user interface (UI) design.

The Model-View-Controller (MVC) design pattern emerged as a fitting structure for my UI. The primary logic controlling machine protocols can remain distinct from visibility elements, allowing for better testing and flexibility.

Getting Help from GitHub Copilot

Switching gears, I explored using GitHub Copilot, a coding assistant integrated into Visual Studio. It uses contextual information from my project to improve its autocomplete capabilities and can generate code snippets based on user input. For instance, when I sought to define enumerated types in C++, Copilot quickly provided code that emphasized type safety.

By employing Copilot's capabilities for existing code manipulation, I inserted new method declarations and implementations with minimal fuss. I was impressed by how it could quickly adapt while maintaining context across files.

Challenges with AI Tools

However, Copilot is not perfect. While it performed admirably in many areas, it sometimes generated incorrect function calls or incomplete implementations. Sometimes, I found it easier to patch up its suggestions than to rely on them entirely, as was the case when implementing methods for handling analog inputs.

When attempting to create a new class to read encoder inputs, the AI’s generated solutions sometimes strayed into nonsensical territory, showing the challenges still inherent with AI-generated code. It did manage adequately when prompted to refine functionality but required frequent manual adjustments.

Conclusion: The Mixed Blessing of AI Tools

In summary, AI tools can significantly enhance productivity for seasoned developers, allowing them to save time and effort but should still be employed with caution. Experienced programmers are likely to find these tools useful, though not infallible. Newer developers might face challenges if they lean too heavily on AI without foundational knowledge. In essence, while these AI advancements are exciting, a careful approach is crucial to effectively integrate them into your coding practices.


Keywords

AI tools, microcontroller, firmware, software development, ChatGPT, GitHub Copilot, CNC controller, MVC design, code generation, debugging.


FAQ

Q: Can AI coding tools replace experienced programmers?
A: While AI tools can greatly assist with coding tasks, they are best used as resources for experienced developers rather than replacements. They lack the nuanced understanding that a seasoned programmer possesses.

Q: Are AI-generated code snippets always correct?
A: No, AI-generated code often requires careful review. Mistakes and incorrect function calls can occur frequently.

Q: What coding tasks are best suited for AI tools?
A: AI tools work well for repetitive tasks, code generation from templates, and suggesting solutions to common programming problems.

Q: How do AI tools help with debugging?
A: AI tools can offer suggestions on fixing errors based on common practices, but they are not a substitute for thorough code reviews.

Q: Are there specific environments where AI tools excel?
A: AI tools, particularly GitHub Copilot, excel in well-structured environments like integrated development environments (IDEs) where context is provided through existing code.