Guides

Best Practices

Guidelines for AI-assisted PLC development

Best Practices

AI is a powerful tool for PLC development, but it requires thoughtful use. These best practices will help you maintain code quality and safety while leveraging AI assistance.

Always Review AI Suggestions

Never accept AI-generated code without reviewing it first. AI can make mistakes, and in industrial automation, mistakes can have serious consequences.

Review Checklist

Before accepting any AI-generated code, verify:

  • Logic is correct for all conditions
  • Edge cases are handled
  • Data types are appropriate
  • Variable names follow your conventions
  • No infinite loops or blocking operations
  • Safety interlocks are preserved
  • Error handling is adequate

Pay Special Attention To

  • Safety-critical code - Emergency stops, interlocks, limit switches
  • Timing operations - Delays, timeouts, watchdogs
  • State machines - Transitions, unreachable states
  • Mathematical operations - Division by zero, overflow

Test Generated Code

AI-generated code should be tested as rigorously as hand-written code.

Testing Approach

  1. Unit test individual function blocks
  2. Integration test combinations of blocks
  3. Simulation test with simulated I/O
  4. Hardware test on actual equipment (with safety measures)

Test Edge Cases

AI might not consider all edge cases. Test:

  • Boundary values (0, max, negative)
  • Rapid input changes
  • Simultaneous inputs
  • Power cycle recovery
  • Error conditions

Maintain Coding Standards

Naming Conventions

Be consistent with variable naming. Tell the AI your conventions:

"Use these naming conventions:
- BOOL inputs: b prefix (bStart, bStop)
- REAL values: r prefix (rSpeed, rTemperature)
- TIME values: t prefix (tDelay, tTimeout)
- Function blocks: FB_ prefix
- Structures: ST_ prefix"

Code Structure

Request consistent structure:

"Structure function blocks with:
1. Input processing first
2. Main logic second
3. Output assignment last
4. Error handling throughout"

Comments

Ask for appropriate comments:

"Add comments explaining:
- The purpose of each section
- Any non-obvious logic
- Safety-related code"

Safety Considerations

Never Remove Safety Code

When modifying existing code:

"Modify the motor control but:
- Keep all emergency stop logic
- Preserve the interlock with bGuardClosed
- Don't change the timeout watchdog"

Review Safety Logic Carefully

AI may not fully understand safety requirements:

  • Verify emergency stops work in all states
  • Check that interlocks cannot be bypassed
  • Ensure fail-safe behavior

Use Explicit Safety Requirements

"Add safety requirements:
- Motor must stop within 100ms of emergency stop
- Cannot start if guard is open
- Must have watchdog timeout of 5 seconds"

Version Control

Commit Frequently

Make small, focused commits:

git commit -m "Add FB_Motor basic structure"
git commit -m "Add speed control to FB_Motor"
git commit -m "Add error handling to FB_Motor"

Write Clear Commit Messages

Document AI-assisted changes:

git commit -m "Add pump control (AI-assisted, manually reviewed)"

Review Before Committing

Don't commit code you haven't fully reviewed and tested.

Documentation

Document AI Interactions

Keep notes on significant AI interactions:

  • What you asked for
  • What the AI generated
  • What modifications you made
  • Why you made them

Update Project Documentation

When AI adds features, update:

  • Function block descriptions
  • Interface documentation
  • System documentation

Working with Existing Code

Understand Before Modifying

Before asking AI to modify code:

  1. Read and understand the existing code
  2. Identify dependencies
  3. Note any safety-critical sections

Be Explicit About What to Preserve

"Modify the state machine but:
- Keep states 0-5 unchanged
- Only add new states 10-15
- Preserve all existing transitions"

Test Regression

After modifying existing code:

  • Test that existing functionality still works
  • Verify no unintended side effects

Know the Limitations

AI Cannot

  • Access real-time PLC data
  • Understand your specific hardware
  • Know your safety requirements unless told
  • Guarantee bug-free code

You Must

  • Verify all generated code
  • Understand the code you accept
  • Test thoroughly
  • Take responsibility for deployed code

Summary

DoDon't
Review all AI suggestionsAccept code blindly
Test generated code thoroughlySkip testing
Be specific in promptsBe vague
Maintain coding standardsLet conventions slip
Preserve safety codeRemove interlocks
Commit frequentlyMake huge changes
Document changesLeave code undocumented

Next Steps

On this page