APIs, concepts, guides, and more
RapidCodeHelpers.py
1
16
17
18import os
19import platform
20from pathlib import Path
21import sys
22
23# Configuration constants
24# These constants are used to configure the creation of the motion controller.
25# They are intended to be modified by the user to match their specific setup.
26RMP_PATH = "" # The path to the RapidCode install directory. If left empty, the script will attempt to find it automatically.
27
28# Windows
29NODE_NAME = "" # The INtime node name (default is NodeA)
30
31# Linux
32NIC_PRIMARY = "" # The primary NIC to use for EtherCAT communication
33CPU_AFFINITY = 0 # The CPU core to use for the RMP firmware
34
35def find_rapid_code_directory(start_directory=None):
36 """
37 Attempts to find the install directory of RapidCode.
38 """
39 start_directory = start_directory or os.path.dirname(os.path.abspath(__file__))
40 start_path = Path(start_directory)
41
42 search_paths = [
43 start_path,
44 start_path.parent.parent.absolute(),
45 start_path.parent.parent.parent.absolute() / "Release",
46 Path("/rsi")
47 ]
48
49 file_name = "RapidCodePython.py"
50
51 for path in search_paths:
52 if path.is_dir() and file_name in os.listdir(path):
53 return str(path)
54
55 raise FileNotFoundError(
56 "Could not find RapidCode Directory. "
57 "Try entering the path manually, likely C:/RSI/X.X.X"
58 )
59
60if platform.system() == "Windows":
61 os.add_dll_directory("c:\\Program Files (x86)\\INtime\\bin") #ntx.dll
62
63if RMP_PATH == "":
64 rapidcode_dir = find_rapid_code_directory()
65else:
66 rapidcode_dir = RMP_PATH
67
68sys.path.append(rapidcode_dir)
69import RapidCodePython as RapidCode
70
71def get_creation_parameters():
72 """
73 Create a motion controller and return it. If any errors are found, raise an exception with the error log as the message.
74 """
75 creation_params:RapidCode.CreationParameters = RapidCode.CreationParameters()
76 creation_params.RmpPath = rapidcode_dir
77 creation_params.NicPrimary = NIC_PRIMARY
78
79 if platform.system() == "Windows":
80 creation_params.NodeName = NODE_NAME
81 elif platform.system() == "Linux":
82 creation_params.CpuAffinity = CPU_AFFINITY
83 else:
84 raise Exception("Unsupported platform")
85
86 return creation_params
87
88def check_errors(rsi_object):
89 """
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.
92 """
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
104
105def start_the_network(controller):
106 """
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.
108 """
109 if controller.network_state_get() != RapidCode.RSINetworkState.RSINetworkStateOPERATIONAL: # Check if network is started already.
110 print("Starting Network..")
111 controller.network_start() # If not. Initialize The Network. (This can also be done from RapidSetup Tool)
112
113 if controller.network_state_get() != RapidCode.RSINetworkState.RSINetworkStateOPERATIONAL: # Check if network is started again.
114 messages_to_read = controller.network_log_message_count_get() # Some kind of error starting the network, read the network log messages
115
116 for i in range(messages_to_read):
117 print(controller.network_log_message_get(i)) # Print all the messages to help figure out the problem
118 print("Expected OPERATIONAL state but the network did not get there.")
119 # raise Exception(Expected OPERATIONAL state but the network did not get there.)# Uncomment if you want your application to exit when the network isn't operational. (Comment when using phantom axis)
120 else: # Else, of network is operational.
121 print("Network Started")
122
123