Skip to content

Conversation

devin-ai-integration[bot]
Copy link

Fix ladder diagram execution order in generated ST code

Summary

Fixes a critical bug where ladder diagram rungs were being executed out of order in generated Structured Text programs. The issue affected projects where blocks without explicit executionOrderId were sorted by position, causing incorrect top-to-bottom execution sequence.

Root cause: Two bugs in the sorting logic:

  1. SortInstances() used operator.eq() which returns booleans, but cmp_to_key() requires integer comparisons (-1/0/1)
  2. ComputeProgram() sorted outVariables&coils and blocks separately then concatenated them, always placing coils before blocks regardless of Y-position

Fix: Changed to use integer subtraction for proper comparison and combine all unordered instances before sorting by Y-coordinate.

Impact: Blocks now execute in correct top-to-bottom order based on their visual position in the ladder diagram.

Review & Testing Checklist for Human

  • Test with multiple PLCopen XML projects - especially ones with complex ladder diagrams, mixed block types, and varying Y-coordinates to ensure no regressions
  • Verify Y-coordinate sorting logic - confirm the 10-pixel tolerance for "same rung" detection works correctly with your typical ladder diagram layouts
  • Test mixed executionOrderId scenarios - projects with some blocks having explicit executionOrderId > 0 and others relying on position-based sorting
  • End-to-end execution testing - deploy generated ST code to actual PLC hardware/simulator to verify execution order is correct and no functional regressions occurred

Notes

  • Only tested with the specific plc.xml file provided by the user - broader testing needed to catch edge cases
  • This change affects ALL ladder diagram and function block diagram projects, so thorough regression testing is critical
  • The fix maintains existing behavior for SFC (Sequential Function Chart) and explicit executionOrderId cases

Link to Devin run: https://app.devin.ai/sessions/c3ee655cdb0f4f7ab869c1554723a288
Requested by: Thiago Alves (@thiagoralves)

…stance sorting

Two bugs were causing incorrect execution order in generated ST code:

1. SortInstances function used operator.eq() which returns booleans (True/False),
   but cmp_to_key requires comparison functions returning integers (-1, 0, 1).
   Changed to integer subtraction (ay - by) and (ax - bx) for proper sorting.

2. ComputeProgram concatenated separately-sorted lists (outVariables&coils + blocks),
   which always placed all coils before all blocks regardless of Y-position.
   Changed to combine all unordered instances first, then sort them together.

This ensures correct top-to-bottom execution order for ladder diagram rungs.

Fixes execution order for LD/FBD blocks without explicit executionOrderId.

Co-Authored-By: Thiago Alves <thiagoralves@gmail.com>
Copy link
Author

🤖 Devin AI Engineer

I'll be helping with this pull request! Here's what you should know:

✅ I will automatically:

  • Address comments on this PR. Add '(aside)' to your comment to have me ignore it.
  • Look at CI failures and help fix them

Note: I can only respond to comments from users who have write access to this repository.

⚙️ Control Options:

  • Disable automatic comment and CI monitoring

Copy link

coderabbitai bot commented Oct 3, 2025

Important

Review skipped

Auto reviews are disabled on this repository.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.


Comment @coderabbitai help to get the list of available commands and usage tips.

@thiagoralves thiagoralves requested a review from Copilot October 3, 2025 16:03
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR fixes a critical bug in the PLCopen compiler where ladder diagram rungs were executing out of order in generated Structured Text code. The issue stemmed from incorrect comparison functions and improper sorting of different instance types.

Key changes:

  • Fixed comparison function to return integers instead of booleans for proper sorting
  • Combined all unordered instances before sorting to maintain correct Y-coordinate order
  • Removed unused operator import

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@thiagoralves thiagoralves merged commit 07b273c into development Oct 3, 2025
7 checks passed
@thiagoralves thiagoralves deleted the devin/1759506016-fix-ladder-rung-execution-order branch October 3, 2025 16:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant