Helper Functions for checking logged creation errors, starting the network, etc.
import os
import platform
from pathlib import Path
import sys
RMP_PATH = ""
NODE_NAME = ""
NIC_PRIMARY = ""
CPU_AFFINITY = 0
def find_rapid_code_directory(start_directory=None):
"""
Attempts to find the install directory of RapidCode.
"""
start_directory = start_directory or os.path.dirname(os.path.abspath(__file__))
start_path = Path(start_directory)
search_paths = [
start_path,
start_path.parent.parent.absolute(),
start_path.parent.parent.parent.absolute() / "Release",
Path("/rsi")
]
file_name = "RapidCodePython.py"
for path in search_paths:
if path.is_dir() and file_name in os.listdir(path):
return str(path)
raise FileNotFoundError(
"Could not find RapidCode Directory. "
"Try entering the path manually, likely C:/RSI/X.X.X"
)
if platform.system() == "Windows":
os.add_dll_directory("c:\\Program Files (x86)\\INtime\\bin")
if RMP_PATH == "":
rapidcode_dir = find_rapid_code_directory()
else:
rapidcode_dir = RMP_PATH
sys.path.append(rapidcode_dir)
import RapidCodePython as RapidCode
def get_creation_parameters():
"""
Create a motion controller and return it. If any errors are found, raise an exception with the error log as the message.
"""
creation_params:RapidCode.CreationParameters = RapidCode.CreationParameters()
creation_params.RmpPath = rapidcode_dir
creation_params.NicPrimary = NIC_PRIMARY
if platform.system() == "Windows":
creation_params.NodeName = NODE_NAME
elif platform.system() == "Linux":
creation_params.CpuAffinity = CPU_AFFINITY
else:
raise Exception("Unsupported platform")
return creation_params
def check_errors(rsi_object):
"""
Check for errors in the given rsi_object and print any errors that are found. If the error log contains any errors (not just warnings), raises an exception with the error log as the message.
Returns a tuple containing a boolean indicating whether the error log contained any errors and the error log string.
"""
error_string_builder = ""
i = rsi_object.ErrorLogCountGet()
while rsi_object.ErrorLogCountGet() > 0:
error:RapidCode.RsiError = rsi_object.ErrorLogGet()
error_type = "WARNING" if error.isWarning else "ERROR"
error_string_builder += f"{error_type}: {error.text}\n"
if len(error_string_builder) > 0:
print(error_string_builder)
if "ERROR" in error_string_builder:
raise Exception(error_string_builder)
return "ERROR" in error_string_builder, error_string_builder
def start_the_network(controller):
"""
Attempts to start the network using the given MotionController object. If the network fails to start, it reads and prints any log messages that may be helpful in determining the cause of the problem, and then raises an RsiError exception.
"""
if controller.network_state_get() != RapidCode.RSINetworkState.RSINetworkStateOPERATIONAL:
print("Starting Network..")
controller.network_start()
if controller.network_state_get() != RapidCode.RSINetworkState.RSINetworkStateOPERATIONAL:
messages_to_read = controller.network_log_message_count_get()
for i in range(messages_to_read):
print(controller.network_log_message_get(i))
print("Expected OPERATIONAL state but the network did not get there.")
else:
print("Network Started")