Skip to main content

Slave groups

Slave grouping lets you assign slaves to different groups (1-7; group=0 means unassigned, folded into group 1 before mapping), and every group can be configured with its own PDO cycle divider so that different groups exchange data at different rates.

Typical use cases (main cycle 31 µs, the smallest cycle the real-time driver supports):

  • Servo drives (group 1): the default group, sent every cycle, about 32KHz at 31 µs
  • IO modules (group 2): sent once every 4 cycles, 125 µs 8000Hz
  • Sensors (group 3): sent once every 255 cycles (the divider limit is 255), about 8ms 125Hz
Key simplification
  • No manual group enabling — on entering SafeOp the system enables the groups that have slaves and disables empty groups
  • Property-style divider — set it directly through master.groups[g].divider
  • Each group sends and receives independently — every group has its own PDO data frame

Feature overview​

CategoryAPITypeR/WDescription
Group assignmentslave.groupintRead-writeGroup membership of the slave (0-7; 0 = unassigned, folded into group 1 before mapping); must be set before SAFE_OP
Group accessmaster.groups_GroupAccessorRead-onlyGroup indexer
master.groups[g]_GroupViewRead-onlyGroup view (index 0-7; group 0 is the unassigned marker and does not send or receive on its own)
master.groups[g][i]SlaveRead-onlySlave inside the group (0-based)
len(master.groups[g])intRead-onlyNumber of slaves in the group
Group dividermaster.groups[g].dividerintRead-writePDO cycle divider (1-255, group numbers 1-7)
Group diagnosticsmaster.active_group_countintRead-onlyNumber of active groups (empty groups do not count)

Quick start​

from darra_ethercat import EtherCATMaster, EcState

with EtherCATMaster() as master:
master.set_eni(r"C:\config.deni")
master.set_network(r"\Device\NPF_{GUID}")

# 1. assign slaves to groups (must happen before SAFE_OP)
# master[1], master[2] are left unset and keep group=0 (unassigned; folded into group 1 before mapping, sent every cycle)
master[3].group = 2 # IO modules → group 2 (1-based index)
master[4].group = 2
master[5].group = 3 # sensors → group 3

# 2. set the group cycle dividers (group-level configuration accepts only group numbers 1-7)
master.groups[2].divider = 4 # group 2: sent every 4 cycles
master.groups[3].divider = 10 # group 3: sent every 10 cycles
# group 1 (the default group) has divider=1 by default; no extra setting needed

# 3. switch to OP — the system enables the groups that have slaves
master.set_state(EcState.OP)
master.start()

# 4. access slaves through the group indexer
io = master.groups[2][0] # the first slave of group 2
sensor = master.groups[3][0] # the first slave of group 3
print(f"组2: {len(master.groups[2])} 个从站") # Group 2: {len(master.groups[2])} slaves
print(f"组3: {len(master.groups[3])} 个从站") # Group 3: {len(master.groups[3])} slaves

Group semantics​

GroupMeaning
0Unassigned marker — stays 0 when not set; folded into group 1 before the SAFE_OP mapping, and does not send or receive on its own
1-7Valid PDO groups — assigned by the user; group numbers need not be consecutive; slaves that are not explicitly assigned end up in group 1
note
  • The valid group range is 1-7, and every group sends and receives its own PDO data
  • slave.group accepts 0-7 on write; group-level configuration and queries (divider, set_group_cycle_divider(), set_group_enabled(), per-group frame-loss statistics) accept only 1-7, and setting group 0 has no effect (master.groups[g] raises IndexError when the group number is outside 0-7)
  • Group membership must be set before entering SAFE_OP; afterwards an assignment to slave.group raises RuntimeError (the runtime rejects it instead of silently pretending success); the divider and the enabled state take effect per group immediately
  • Group numbers need not be consecutive (for example 1, 3, 5); the system skips empty groups
  • On entering SafeOp the groups that have slaves are enabled automatically and empty groups are disabled

Slave group assignment​

The group membership of a slave is set through the slave.group property.

slave.group​

@property
def group(self) -> int

@group.setter
def group(self, value: int) -> None

Gets or sets the group membership of the slave (0-7; 0 = unassigned, folded into group 1 before mapping). It must be set before SAFE_OP; afterwards the runtime rejects an assignment and the setter raises RuntimeError.

Example:

# assign slaves to groups
master[1].group = 1 # explicitly assigned to group 1
master[2].group = 2 # explicitly assigned to group 2
# master[3].group is left unset and stays 0 (unassigned; folded into group 1 before mapping)

# read the group the slave belongs to
group = master[1].group # 1

master.groups[g][i]​

@property
def groups(self)

Group indexer (advanced accessor): master.groups[组号] returns the view of that group, and master.groups[组号][索引] returns the given slave inside the group.

Example:

# get all slaves of group 1
group1_slaves = master.groups[1]

# iterate the slaves in the group
for slave in master.groups[1]:
print(slave.name)

# access by index
first = master.groups[1][0]

# number of slaves in the group
count = len(master.groups[1])

active_group_count​

@property
def active_group_count(self) -> int

Gets the number of active groups (empty groups without slaves do not count).

Group cycle divider​

The divider is set through master.groups[g].

groups[g].divider​

@property
def divider(self) -> int

@divider.setter
def divider(self, value: int) -> None

Gets or sets the PDO cycle divider of a group (1-255). Group numbers accept only 1-7 (group 0 is the unassigned marker, so writing groups[0].divider has no effect); the divider of every group defaults to 1, writing a value <= 0 raises ValueError, and a failed read falls back to returning 1.

  • 1 — sent every cycle (1000Hz with a 1ms main cycle)
  • 2 — sent every 2 cycles (500Hz)
  • 4 — sent every 4 cycles (250Hz)
  • 10 — sent every 10 cycles (100Hz)

Example:

# set the dividers (group numbers 1-7)
master.groups[2].divider = 4 # group 2: every 4 cycles
master.groups[3].divider = 10 # group 3: every 10 cycles
# group 1 (the default group) has divider=1 by default (every cycle); no setting needed

# read the divider
div = master.groups[3].divider # 10

Group properties:

CategoryPropertyTypeR/WDescription
Group dividergroups[g].dividerintRead-writePDO cycle divider of the group (1-255)
Group viewlen(groups[g])intRead-onlyNumber of slaves in the group

Automatic enable/disable​

On entering the SafeOp state the system scans every group (1-7) for slaves:

  • A group with slaves → enabled automatically, with the divider forced to at least 1
  • An empty group → disabled automatically, and skipped in PDO sending and receiving

You do not need to call set_group_enabled() manually, and group numbers need not be consecutive.

Group expected WKC​

def get_group_expected_wkc(self, group: int) -> int

Reads the expected WKC of the given group. Every group has its own frame and its own WKC; once the expected value has been determined at configuration time / on entering OP it is a fixed true value.

for g in range(1, 8):   # valid group numbers 1-7
print(f"组 {g} 期望 WKC: {master.get_group_expected_wkc(g)}") # Group {g} expected WKC: {master.get_group_expected_wkc(g)}
Group-level measured WKC

The measured WKC, the consecutive WKC-shortfall count, the cumulative frame count and other run-time fields of a group are in group_config_raw; use the low-level static helper methods EcGroupConfigHelper.get_group_actual_wkc() / get_group_consecutive_miss() and so on to parse them (see darra_ethercat.data.structures).

Group diagnostics​

Every group tracks its own PDO frame-loss statistics.

Frame loss per group​

# query the frame-loss statistics of the given group
stats = master.diagnostics_info.pdo.get_frame_loss_stats(1) # group 1
print(f"组1: 累计丢帧={stats.total_lost}, 连续={stats.consecutive_lost}") # Group 1: total frame loss={stats.total_lost}, consecutive={stats.consecutive_lost}

stats2 = master.diagnostics_info.pdo.get_frame_loss_stats(2) # group 2
print(f"组2: 累计丢帧={stats2.total_lost}") # Group 2: total frame loss={stats2.total_lost}

Aggregate all groups​

# no argument: returns the aggregate statistics of all groups
total = master.diagnostics_info.pdo.get_frame_loss_stats()
print(f"总丢帧: {total.total_lost}") # Total frame loss: {total.total_lost}

PDOFrameLoss event​

The group argument of the PDO consecutive-frame-loss event identifies which group lost frames:

def on_loss(mi, group, consecutive, total):
print(f"组 {group} 丢帧: 连续={consecutive}, 累计={total}") # Group {group} frame loss: consecutive={consecutive}, total={total}

if group == 1:
# the servo group lost frames: handle it immediately
print("警告: 伺服组通信异常!") # Warning: servo group communication fault!

master.on_pdo_frame_loss(on_loss)

Configuration sequence​

INIT → PRE_OP → SAFE_OP → OP
| | |
| | └─ Group configuration locked; PDO sent per group
| |
| └─ Set slave group assignment: slave.group = N
| Set group divider: master.groups[N].divider = divider
| (groups that contain slaves are enabled automatically on entering SAFE_OP)
|
└─ Initialize the master
caution
  • slave.group takes effect when the master enters SAFE_OP
  • Set the group dividers after initialization and before switching to OP
  • The group configuration locks automatically once SAFE_OP is entered

Full example​

from darra_ethercat import EtherCATMaster, EcState

with EtherCATMaster() as master:
master.set_eni(r"C:\config.deni")
master.set_network(r"\Device\NPF_{GUID}")

# 1. assign slaves to groups
# master[1] ~ master[3] are left unset and keep group=0 (servo drives, unassigned, folded into group 1 before mapping)
for i in range(4, 6):
master[i].group = 2 # IO modules -> group 2 (1-based index)
# the remaining slaves keep group=0 (folded into group 1 before mapping, sent every cycle)

# 2. set the group cycle dividers (group numbers 1-7)
master.groups[2].divider = 4 # group 2: 250Hz
# group 1 (the default group) has divider=1 by default (1000Hz); no setting needed

# 3. set the cycle time
master.config.loop_cycle = 1_000_000 # 1ms

# 4. PDO frame-loss monitoring (independent per group)
master.on_pdo_frame_loss(lambda mi, group, consecutive, total:
print(f"组 {group} 丢帧: 连续={consecutive}")) # Group {group} frame loss: consecutive={consecutive}

# 5. start — the groups that have slaves are enabled automatically
master.set_state(EcState.OP)
master.start()

# 6. inspect the group state
print(f"活跃组: {master.active_group_count}") # Active groups: {master.active_group_count}
print(f"组1: {len(master.groups[1])} 个从站") # Group 1: {len(master.groups[1])} slaves
print(f"组2: {len(master.groups[2])} 个从站") # Group 2: {len(master.groups[2])} slaves

# 7. handle per group in the cycle callback (read the process image; do not do SDO in the callback)
def on_pdo(mi):
for servo in master.groups[1]:
raw = servo.pdo.inputs
if raw and len(raw) >= 2:
status = int.from_bytes(raw[:2], 'little')

master.on_pdo_cycle(on_pdo)