APIs, concepts, guides, and more
_helpers.py
1""" Helper functions for RapidCode Python samples.
2"""
3
4from _imports import RapidCode, RAPIDCODE_DIR, constants, platform
5
6def get_creation_parameters():
7 # create a motion controller and return it.
8 # If any errors are found, raise an exception with the error log as the message.
9 creation_params: RapidCode.CreationParameters = RapidCode.CreationParameters()
10 creation_params.RmpPath = RAPIDCODE_DIR
11 creation_params.NicPrimary = constants.RMP_NIC_PRIMARY
12
13 if platform.system() == "Windows":
14 creation_params.NodeName = constants.RMP_NODE_NAME
15 elif platform.system() == "Linux":
16 creation_params.CpuAffinity = constants.RMP_CPU_AFFINITY
17 else:
18 raise Exception("Unsupported platform")
19
20 return creation_params
21
22def check_errors(rsi_object):
23 # check for errors in the given rsi_object and print any errors that are found.
24 # If the error log contains any errors (not just warnings), raises an exception with the error log as the message.
25 # returns a tuple containing a boolean indicating whether the error log contained any errors and the error log string.
26 error_string_builder = ""
27 i = rsi_object.ErrorLogCountGet()
28 while rsi_object.ErrorLogCountGet() > 0:
29 error:RapidCode.RsiError = rsi_object.ErrorLogGet()
30 error_type = "WARNING" if error.isWarning else "ERROR"
31 error_string_builder += f"{error_type}: {error.text}\n"
32 if len(error_string_builder) > 0:
33 print(error_string_builder)
34 if "ERROR" in error_string_builder:
35 raise Exception(error_string_builder)
36 return "ERROR" in error_string_builder, error_string_builder
37
38def start_the_network(controller):
39 # attempts to start the network using the given MotionController object.
40 # If the network fails to start, it reads and prints any log messages that may be helpful
41 # in determining the cause of the problem, and then raises an RsiError exception.
42 if controller.NetworkStateGet() != RapidCode.RSINetworkState_RSINetworkStateOPERATIONAL: # Check if network is started already.
43 print("Starting Network..")
44 controller.NetworkStart() # If not. Initialize The Network. (This can also be done from RapidSetup Tool)
45
46 if controller.NetworkStateGet() != RapidCode.RSINetworkState_RSINetworkStateOPERATIONAL: # Check if network is started again.
47 messages_to_read = controller.NetworkLogMessageCountGet() # Some kind of error starting the network, read the network log messages
48
49 for i in range(messages_to_read):
50 print(controller.NetworkLogMessageGet(i)) # Print all the messages to help figure out the problem
51 print("Expected OPERATIONAL state but the network did not get there.")
52 # 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)
53 else: # Else, of network is operational.
54 print("Network Started")