20from pathlib
import Path
35def find_rapid_code_directory(start_directory=None):
37 Attempts to find the install directory of RapidCode.
39 start_directory = start_directory
or os.path.dirname(os.path.abspath(__file__))
40 start_path = Path(start_directory)
44 start_path.parent.parent.absolute(),
45 start_path.parent.parent.parent.absolute() /
"Release",
49 file_name =
"RapidCodePython.py"
51 for path
in search_paths:
52 if path.is_dir()
and file_name
in os.listdir(path):
55 raise FileNotFoundError(
56 "Could not find RapidCode Directory. "
57 "Try entering the path manually, likely C:/RSI/X.X.X"
60if platform.system() ==
"Windows":
61 os.add_dll_directory(
"c:\\Program Files (x86)\\INtime\\bin")
64 rapidcode_dir = find_rapid_code_directory()
66 rapidcode_dir = RMP_PATH
68sys.path.append(rapidcode_dir)
69import RapidCodePython
as RapidCode
71def get_creation_parameters():
73 Create a motion controller and return it. If any errors are found, raise an exception with the error log as the message.
75 creation_params:RapidCode.CreationParameters = RapidCode.CreationParameters()
76 creation_params.RmpPath = rapidcode_dir
77 creation_params.NicPrimary = NIC_PRIMARY
79 if platform.system() ==
"Windows":
80 creation_params.NodeName = NODE_NAME
81 elif platform.system() ==
"Linux":
82 creation_params.CpuAffinity = CPU_AFFINITY
84 raise Exception(
"Unsupported platform")
86 return creation_params
88def check_errors(rsi_object):
90 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.
91 Returns a tuple containing a boolean indicating whether the error log contained any errors and the error log string.
93 error_string_builder =
""
94 i = rsi_object.ErrorLogCountGet()
95 while rsi_object.ErrorLogCountGet() > 0:
96 error:RapidCode.RsiError = rsi_object.ErrorLogGet()
97 error_type =
"WARNING" if error.isWarning
else "ERROR"
98 error_string_builder += f
"{error_type}: {error.text}\n"
99 if len(error_string_builder) > 0:
100 print(error_string_builder)
101 if "ERROR" in error_string_builder:
102 raise Exception(error_string_builder)
103 return "ERROR" in error_string_builder, error_string_builder
105def start_the_network(controller):
107 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.
109 if controller.network_state_get() != RapidCode.RSINetworkState.RSINetworkStateOPERATIONAL:
110 print(
"Starting Network..")
111 controller.network_start()
113 if controller.network_state_get() != RapidCode.RSINetworkState.RSINetworkStateOPERATIONAL:
114 messages_to_read = controller.network_log_message_count_get()
116 for i
in range(messages_to_read):
117 print(controller.network_log_message_get(i))
118 print(
"Expected OPERATIONAL state but the network did not get there.")
121 print(
"Network Started")