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)
205 lines
7.2 KiB
Python
205 lines
7.2 KiB
Python
"""Digital I/O API namespace for RSIPI."""
|
|
|
|
import logging
|
|
import time
|
|
from typing import Union, Optional, TYPE_CHECKING
|
|
|
|
if TYPE_CHECKING:
|
|
from .rsi_client import RSIClient
|
|
|
|
|
|
class IOAPI:
|
|
"""
|
|
Digital I/O control interface for KUKA RSI robot control.
|
|
|
|
Manages digital input/output signals for coordinating with external systems,
|
|
controlling pneumatic tools, and synchronizing with sensors.
|
|
"""
|
|
|
|
def __init__(self, client: 'RSIClient') -> None:
|
|
"""
|
|
Initialize IOAPI namespace.
|
|
|
|
Args:
|
|
client: RSIClient instance for variable access
|
|
"""
|
|
self.client = client
|
|
|
|
def toggle(self, group: str, name: str, state: Union[bool, int]) -> str:
|
|
"""
|
|
Set a digital I/O variable to the specified state.
|
|
|
|
Args:
|
|
group: Parent I/O variable group (e.g., 'Digout', 'DiO', 'DiL')
|
|
name: I/O channel name or number within the group (e.g., 'o1', '1')
|
|
state: Desired state (True/False or 1/0)
|
|
|
|
Returns:
|
|
Status message indicating success or failure
|
|
|
|
Raises:
|
|
RSIVariableError: If the specified I/O group or channel doesn't exist
|
|
RSISafetyViolation: If safety checks prevent the operation
|
|
|
|
Example:
|
|
>>> api.io.toggle('Digout', 'o1', True) # Turn on output 1
|
|
'Updated Digout.o1 to 1'
|
|
>>> api.io.toggle('DiL', '5', False) # Turn off input latch 5
|
|
'Updated DiL.5 to 0'
|
|
|
|
Note:
|
|
This method goes through the full safety validation chain. I/O
|
|
variables can have safety limits configured just like motion axes.
|
|
"""
|
|
var_name = f"{group}.{name}"
|
|
state_value = int(bool(state)) # Ensure binary 0 or 1
|
|
|
|
# Import here to avoid circular dependency
|
|
from .tools_api import ToolsAPI
|
|
|
|
tools = ToolsAPI(self.client)
|
|
result = tools.update_variable(var_name, state_value)
|
|
logging.debug(f"I/O {var_name} set to {state_value}")
|
|
return result
|
|
|
|
def set_output(self, channel: int, value: bool, group: str = 'Digout') -> str:
|
|
"""
|
|
Set digital output by channel number.
|
|
|
|
High-level wrapper for setting digital outputs. More convenient than
|
|
toggle() when working with standard digital output channels.
|
|
|
|
Args:
|
|
channel: Output channel number (1-based, e.g., 1 for o1)
|
|
value: Desired state (True = ON, False = OFF)
|
|
group: I/O group name (default: 'Digout')
|
|
|
|
Returns:
|
|
Status message indicating success
|
|
|
|
Raises:
|
|
RSIVariableError: If the output channel doesn't exist
|
|
RSISafetyViolation: If safety checks prevent the operation
|
|
|
|
Example:
|
|
>>> api.io.set_output(1, True) # Turn ON output 1
|
|
'Updated Digout.o1 to 1'
|
|
>>> api.io.set_output(3, False) # Turn OFF output 3
|
|
'Updated Digout.o3 to 0'
|
|
>>> api.io.set_output(5, True, group='DiO') # Custom group
|
|
'Updated DiO.5 to 1'
|
|
|
|
Note:
|
|
The default group 'Digout' corresponds to standard KUKA digital
|
|
outputs configured in RSI. Channel numbering starts at 1 to match
|
|
KUKA controller conventions.
|
|
"""
|
|
channel_name = f"o{channel}"
|
|
return self.toggle(group, channel_name, value)
|
|
|
|
def get_input(self, channel: int, group: str = 'Digin') -> bool:
|
|
"""
|
|
Read digital input by channel number.
|
|
|
|
High-level wrapper for reading digital input states from the robot
|
|
controller. Returns current state as boolean.
|
|
|
|
Args:
|
|
channel: Input channel number (1-based, e.g., 1 for i1)
|
|
group: I/O group name (default: 'Digin')
|
|
|
|
Returns:
|
|
True if input is HIGH/ON, False if LOW/OFF
|
|
|
|
Raises:
|
|
RSIVariableError: If the input channel doesn't exist in receive_variables
|
|
|
|
Example:
|
|
>>> # Check if input 1 is active
|
|
>>> if api.io.get_input(1):
|
|
... print("Sensor triggered!")
|
|
Sensor triggered!
|
|
|
|
>>> # Read from custom group
|
|
>>> state = api.io.get_input(5, group='DiI')
|
|
>>> print(f"Input 5 state: {state}")
|
|
Input 5 state: True
|
|
|
|
Note:
|
|
This reads from receive_variables, which contains the robot
|
|
controller's current I/O state. Values are updated every RSI
|
|
cycle (~4ms).
|
|
"""
|
|
from .exceptions import RSIVariableError
|
|
|
|
channel_name = f"i{channel}"
|
|
var_name = f"{group}.{channel_name}"
|
|
|
|
# Navigate nested receive_variables structure
|
|
if group in self.client.receive_variables:
|
|
group_dict = self.client.receive_variables.get(group, {})
|
|
if isinstance(group_dict, dict) and channel_name in group_dict:
|
|
value = group_dict[channel_name]
|
|
return bool(value)
|
|
else:
|
|
raise RSIVariableError(f"Input channel '{channel_name}' not found in group '{group}'")
|
|
else:
|
|
raise RSIVariableError(f"Input group '{group}' not found in receive_variables")
|
|
|
|
def pulse(self, channel: int, duration: float = 0.1, group: str = 'Digout') -> str:
|
|
"""
|
|
Generate a timed pulse on the specified output channel.
|
|
|
|
Turns the output ON, waits for the specified duration, then turns it OFF.
|
|
Useful for triggering pneumatic actuators, solenoids, or signaling events.
|
|
|
|
Args:
|
|
channel: Output channel number (1-based)
|
|
duration: Pulse duration in seconds (default: 0.1 = 100ms)
|
|
group: I/O group name (default: 'Digout')
|
|
|
|
Returns:
|
|
Status message indicating completion
|
|
|
|
Raises:
|
|
RSIVariableError: If the output channel doesn't exist
|
|
RSISafetyViolation: If safety checks prevent the operation
|
|
|
|
Example:
|
|
>>> # 100ms pulse on output 2
|
|
>>> api.io.pulse(2)
|
|
'Pulse completed on Digout.o2 (duration: 0.1s)'
|
|
|
|
>>> # 500ms pulse on output 5
|
|
>>> api.io.pulse(5, duration=0.5)
|
|
'Pulse completed on Digout.o5 (duration: 0.5s)'
|
|
|
|
>>> # Trigger pneumatic gripper on custom channel
|
|
>>> api.io.pulse(3, duration=0.2, group='DiO')
|
|
'Pulse completed on DiO.o3 (duration: 0.2s)'
|
|
|
|
Warning:
|
|
This method blocks for the duration of the pulse. For non-blocking
|
|
pulses, consider using threading or async I/O patterns.
|
|
|
|
Note:
|
|
Pulse timing accuracy depends on system load and RSI cycle time.
|
|
For critical timing requirements, consider hardware-timed outputs
|
|
or KRL-based pulse generation.
|
|
"""
|
|
channel_name = f"o{channel}"
|
|
var_name = f"{group}.{channel_name}"
|
|
|
|
# Turn ON
|
|
self.set_output(channel, True, group=group)
|
|
logging.debug(f"Pulse started on {var_name}")
|
|
|
|
# Wait for duration
|
|
time.sleep(duration)
|
|
|
|
# Turn OFF
|
|
self.set_output(channel, False, group=group)
|
|
logging.info(f"Pulse completed on {var_name} (duration: {duration}s)")
|
|
|
|
return f"Pulse completed on {var_name} (duration: {duration}s)"
|