APIs, concepts, guides, and more
RapidCodeHelpers.py
1
16
17
18import os
19from pathlib import Path
20import sys
21
22#rapidcode_dir = find_rapid_code_directory()
23#sys.path.append(rapidcode_dir)
24#import RapidCode
25
26
27def check_errors(rsi_object):
28 """
29 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.
30 Returns a tuple containing a boolean indicating whether the error log contained any errors and the error log string.
31 """
32 error_string_builder = ""
33 i = rsi_object.ErrorLogCountGet()
34 while rsi_object.ErrorLogCountGet() > 0:
35 error:RapidCode.RsiError = rsi_object.ErrorLogGet()
36 error_type = "WARNING" if error.isWarning else "ERROR"
37 error_string_builder += f"{error_type}: {error.text}\n"
38 if len(error_string_builder) > 0:
39 print(error_string_builder)
40 if "ERROR" in error_string_builder:
41 raise Exception(error_string_builder)
42 return "ERROR" in error_string_builder, error_string_builder
43
44def start_the_network(controller):
45 """
46 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.
47 """
48 if controller.network_state_get() != RapidCode.RSINetworkState.RSINetworkStateOPERATIONAL: # Check if network is started already.
49 print("Starting Network..")
50 controller.network_start() # If not. Initialize The Network. (This can also be done from RapidSetup Tool)
51
52 if controller.network_state_get() != RapidCode.RSINetworkState.RSINetworkStateOPERATIONAL: # Check if network is started again.
53 messages_to_read = controller.network_log_message_count_get() # Some kind of error starting the network, read the network log messages
54
55 for i in range(messages_to_read):
56 print(controller.network_log_message_get(i)) # Print all the messages to help figure out the problem
57 print("Expected OPERATIONAL state but the network did not get there.")
58 # 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)
59 else: # Else, of network is operational.
60 print("Network Started")
61
62def find_rapid_code_directory(start_directory=os.path.dirname(os.path.abspath(__file__))):
63 """
64 Attempts find the install directory of RapidCode.
65 """
66 start_path=Path(start_directory)
67 likely_rapidcode_path_1 = start_path #for running directly from the RapidCode directory (release/debug/or RSI/X.X.X)
68 likely_rapidcode_path_2 = start_path.parent.parent.absolute() #for running from examples subfolder in public release
69 likely_rapidcode_path_3 = start_path.parent.parent.parent.absolute() / "Release"#Path for RSi developers
70 likely_rapidcode_path_4 = start_path.parent.parent.absolute()
71
72
73 #this_dir = Path(__file__).absolute()
74 try:
75 file_name = "RapidCode.py"#the file we need to import RapidCode
76 if file_name in os.listdir(likely_rapidcode_path_1):
77 rapidcode_dir = likely_rapidcode_path_1
78 elif(file_name in os.listdir(likely_rapidcode_path_2)):
79 rapidcode_dir=likely_rapidcode_path_2
80 elif(file_name in os.listdir(likely_rapidcode_path_3)):
81 rapidcode_dir=likely_rapidcode_path_3
82 elif(file_name in os.listdir(likely_rapidcode_path_4)):
83 rapidcode_dir=likely_rapidcode_path_4
84 else:
85 raise Exception("RapidCode search path exhausted.")
86 except:
87 raise Exception("Could not find RapidCode Directory. Try entering the path manually likely C:/RSI/X.X.X")
88
89 return str(rapidcode_dir)
90
91
92