RSI-PI/templates/krl/state_machine.src
Adam 6e0b87b945 Implement Phase 3: KRL Coordination
Complete implementation of Python-KRL coordination features for seamless
bidirectional communication between RSIPI and KUKA KRL programs.

## IOAPI Enhancements

Added high-level I/O control methods for convenient digital I/O manipulation:

- **set_output(channel, value, group='Digout')** - Set digital output by channel number
- **get_input(channel, group='Digin')** - Read digital input by channel number
- **pulse(channel, duration=0.1, group='Digout')** - Generate timed pulse on output

Benefits:
- Simpler channel-based addressing (channel 1 instead of 'Digout.o1')
- Automatic channel name formatting
- Built-in pulse generation for pneumatic actuators and signaling
- Consistent error handling

## KRLAPI Enhancements

Added coordination helper methods for Python-KRL synchronization:

- **wait_for_signal(channel, timeout=5.0)** - Block until KRL sets I/O signal
- **signal_complete(channel)** - Signal KRL that Python operation is complete
- **write_param(slot, value)** - Write to Tech.C variables (Python → KRL)
- **read_param(slot)** - Read from Tech.T variables (KRL → Python)

Features:
- Configurable timeouts with proper error handling
- Flexible slot addressing (11, 'C11', 'c11' all work)
- Slot validation (enforces 11-199 range)
- Comprehensive logging for debugging
- Clear docstrings with KRL code examples

## KRL Template Library

Created comprehensive KRL templates demonstrating coordination patterns:

**templates/krl/basic_handshake.src**
- Simple I/O handshaking (KRL signals → Python waits → Python signals back)
- Timeout handling and error recovery
- Complete Python code examples in comments

**templates/krl/parameter_passing.src**
- Bidirectional Tech variable communication
- KRL writes position to Tech.T, Python reads
- Python calculates target, writes to Tech.C, KRL reads
- Demonstrates full parameter exchange workflow

**templates/krl/state_machine.src**
- Multi-state coordination workflow
- States: IDLE, CALIBRATING, READY, EXECUTING, COMPLETE, ERROR
- Combines I/O signals and Tech variables
- Error handling and timeout mechanisms
- Demonstrates complex production-ready pattern

**templates/krl/README.md**
- Comprehensive coordination patterns documentation
- Tech variable mapping conventions (C vs T variables)
- I/O signal mapping standards
- Timing best practices
- Troubleshooting guide

## Python Coordination Examples

Created production-ready Python examples demonstrating all coordination methods:

**examples/coordination/01_basic_handshake.py**
- Simple I/O handshake demonstration
- Matches basic_handshake.src template
- Command-line interface with argparse
- Comprehensive logging and error handling

**examples/coordination/02_parameter_passing.py**
- Parameter exchange workflow
- Reads position from KRL (Tech.T)
- Calculates target position
- Writes target to KRL (Tech.C)
- Matches parameter_passing.src template

**examples/coordination/03_state_machine.py**
- Complex multi-state coordination
- State monitoring loop with enum
- Calibration routine with offset calculation
- Error detection and signaling
- Matches state_machine.src template

**examples/coordination/README.md**
- Complete usage instructions
- Configuration requirements
- Troubleshooting section
- Customization examples
- Advanced usage patterns

## Modified Files

src/RSIPI/io_api.py:
- Added time import
- Implemented set_output() method
- Implemented get_input() method with navigation of receive_variables
- Implemented pulse() method with blocking time.sleep()
- Comprehensive docstrings with examples

src/RSIPI/krl_api.py:
- Added time import
- Implemented wait_for_signal() with configurable polling
- Implemented signal_complete() method
- Implemented write_param() with slot normalization and validation
- Implemented read_param() with slot normalization and validation
- KRL code examples in all docstrings

## New Directories

templates/krl/
- 3 KRL program templates
- Comprehensive README with patterns and conventions

examples/coordination/
- 3 Python example scripts
- Complete usage documentation

## Design Decisions

**I/O Channel Numbering**: 1-based to match KUKA conventions
**Tech Variable Slots**: Validated 11-199 range (KUKA reserves 1-10)
**Blocking Operations**: wait_for_signal() and pulse() block with configurable timeouts
**Error Handling**: Proper exceptions with clear messages
**Logging**: Debug/Info/Warning levels for all operations
**Documentation**: Every method includes KRL code examples

## Phase 3 Status:  COMPLETE

All planned features implemented:
-  High-level Digital I/O API
-  KRL state coordination helpers
-  Parameter passing via Tech variables
-  KRL code templates
-  Python coordination examples
-  Comprehensive documentation

Next: Phase 4 (Advanced Motion Control)
2026-01-17 00:38:32 +00:00

190 lines
5.0 KiB
Plaintext

&ACCESS RVP
&REL 1
&PARAM TEMPLATE = C:\KRC\Roboter\Template\vorgabe
&PARAM EDITMASK = *
DEF state_machine()
; ================================================================
; Multi-State Coordination Pattern
; ================================================================
; Demonstrates a state machine for complex Python-KRL workflows.
; Uses both I/O signals and Tech variables for robust coordination.
;
; State Definitions:
; State 0: IDLE - Waiting to start
; State 1: CALIBRATING - Python performing calibration
; State 2: READY - Calibration complete, ready for motion
; State 3: EXECUTING - Robot executing motion task
; State 4: COMPLETE - Task finished
; State 9: ERROR - Error condition
;
; I/O Mapping:
; $OUT[1]: State signal to Python (pulse indicates state change)
; $IN[1]: Python acknowledgement
; $IN[2]: Python error signal
;
; Tech Variable Mapping:
; Tech.T[11]: Current state (KRL → Python)
; Tech.C[11]: Python command (0=continue, 1=pause, 2=abort)
; Tech.C[12-14]: Calibration offset (X, Y, Z)
;
; Python Code Example:
; api = RSIAPI('RSI_EthernetConfig.xml')
; api.start()
;
; # Monitor state transitions
; while True:
; state = api.krl.read_param('T11')
;
; if state == 1: # CALIBRATING
; print("Performing calibration...")
; # Calibration logic here
; offset_x = 5.0
; offset_y = -2.0
; offset_z = 0.5
;
; # Write calibration results
; api.krl.write_param('C12', offset_x)
; api.krl.write_param('C13', offset_y)
; api.krl.write_param('C14', offset_z)
;
; # Acknowledge state completion
; api.krl.signal_complete(1)
;
; elif state == 3: # EXECUTING
; print("Robot executing motion...")
; # Monitor execution, send corrections if needed
; api.motion.update_cartesian(X=offset_x, Y=offset_y)
;
; elif state == 4: # COMPLETE
; print("Task complete!")
; break
;
; elif state == 9: # ERROR
; print("Error detected, aborting!")
; break
;
; time.sleep(0.1)
;
; api.stop()
; ================================================================
; Variable declarations
INT current_state, python_cmd
REAL offset_x, offset_y, offset_z
E6POS target_pos
BOOL ack_received
INT timeout_counter
BAS(#INITMOV, 0)
; Initialize state machine
current_state = 0 ; IDLE
$TECH.T[11] = current_state
; ============================================================
; STATE 0: IDLE
; ============================================================
current_state = 0
$TECH.T[11] = current_state
WAIT SEC 1.0
; ============================================================
; STATE 1: CALIBRATING
; ============================================================
current_state = 1
$TECH.T[11] = current_state
; Signal Python to start calibration
$OUT[1] = TRUE
WAIT SEC 0.1
$OUT[1] = FALSE
; Wait for Python calibration completion
ack_received = FALSE
timeout_counter = 0
WHILE (ack_received == FALSE) AND (timeout_counter < 200)
; Check for error signal
IF $IN[2] == TRUE THEN
current_state = 9 ; ERROR
$TECH.T[11] = current_state
HALT
ENDIF
; Check for completion
IF $IN[1] == TRUE THEN
ack_received = TRUE
ELSE
WAIT SEC 0.1
timeout_counter = timeout_counter + 1
ENDIF
ENDWHILE
IF ack_received == FALSE THEN
; Timeout error
current_state = 9
$TECH.T[11] = current_state
HALT
ENDIF
; ============================================================
; STATE 2: READY
; ============================================================
; Read calibration offsets from Python
offset_x = $TECH.C[12]
offset_y = $TECH.C[13]
offset_z = $TECH.C[14]
current_state = 2
$TECH.T[11] = current_state
; Signal ready
$OUT[1] = TRUE
WAIT SEC 0.1
$OUT[1] = FALSE
WAIT SEC 0.5
; ============================================================
; STATE 3: EXECUTING
; ============================================================
current_state = 3
$TECH.T[11] = current_state
; Calculate target with calibration offset
target_pos = $POS_ACT
target_pos.X = target_pos.X + offset_x
target_pos.Y = target_pos.Y + offset_y
target_pos.Z = target_pos.Z + offset_z
; Execute motion (Python can send RSI corrections during this)
LIN target_pos Vel=0.3 m/s CPDAT1 Tool[1] Base[0]
; Check for pause/abort commands
python_cmd = $TECH.C[11]
IF python_cmd == 2 THEN
; Abort requested
current_state = 9
$TECH.T[11] = current_state
HALT
ENDIF
; ============================================================
; STATE 4: COMPLETE
; ============================================================
current_state = 4
$TECH.T[11] = current_state
; Signal completion
$OUT[1] = TRUE
WAIT SEC 0.2
$OUT[1] = FALSE
; Return to home
PTP HOME Vel=100 % DEFAULT
; Final state update
current_state = 0 ; Back to IDLE
$TECH.T[11] = current_state
END