Skip to main content

Best Practices: Control Loop Patterns

Pattern A: Periodic Control + Cached State

Best for high-frequency control loops where fresh state is not critical every iteration.
When to use:
  • High-frequency loops (>50Hz)
  • State freshness is not critical
  • Minimal latency required

Pattern B: Request-Then-Read

Forces fresh feedback before reading state.
When to use:
  • State-based control decisions
  • Closed-loop control requiring fresh feedback
  • Lower frequency loops (<50Hz)

Pattern C: Multi-Motor Batch Query

Efficient pattern for multiple motors.
When to use:
  • Multiple motors on same bus
  • Coordinated control
  • Efficient CAN bus utilization

Pattern D: State Machine Control

For complex control sequences.

Pattern E: Graceful Degradation

Continue operation despite errors when safe.

Bus Load Management

Avoiding CAN Buffer Overflow (OS Error 105)

If you see socketcan write failed: No buffer space available (os error 105): 1. Increase control period:
2. Increase TX queue length:
3. Avoid multiple senders:
4. Reduce unnecessary feedback requests:

CAN Bandwidth Estimation

Timing Best Practices

Measure and Adapt

Use High-Resolution Timer

Safety Patterns

Watchdog Pattern

Temperature Protection

Next Steps