APIs, concepts, guides, and more
Helper Functions

Helper Functions for checking logged creation errors, starting the network, etc.


📜 Helper Functions Python

import os
from pathlib import Path
import sys
#rapidcode_dir = find_rapid_code_directory()
#sys.path.append(rapidcode_dir)
#import RapidCode
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: # Check if network is started already.
print("Starting Network..")
controller.network_start() # If not. Initialize The Network. (This can also be done from RapidSetup Tool)
if controller.network_state_get() != RapidCode.RSINetworkState.RSINetworkStateOPERATIONAL: # Check if network is started again.
messages_to_read = controller.network_log_message_count_get() # Some kind of error starting the network, read the network log messages
for i in range(messages_to_read):
print(controller.network_log_message_get(i)) # Print all the messages to help figure out the problem
print("Expected OPERATIONAL state but the network did not get there.")
# 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)
else: # Else, of network is operational.
print("Network Started")
def find_rapid_code_directory(start_directory=os.path.dirname(os.path.abspath(__file__))):
"""
Attempts find the install directory of RapidCode.
"""
start_path=Path(start_directory)
likely_rapidcode_path_1 = start_path #for running directly from the RapidCode directory (release/debug/or RSI/X.X.X)
likely_rapidcode_path_2 = start_path.parent.parent.absolute() #for running from examples subfolder in public release
likely_rapidcode_path_3 = start_path.parent.parent.parent.absolute() / "Release"#Path for RSi developers
likely_rapidcode_path_4 = start_path.parent.parent.absolute()
#this_dir = Path(__file__).absolute()
try:
file_name = "RapidCode.py"#the file we need to import RapidCode
if file_name in os.listdir(likely_rapidcode_path_1):
rapidcode_dir = likely_rapidcode_path_1
elif(file_name in os.listdir(likely_rapidcode_path_2)):
rapidcode_dir=likely_rapidcode_path_2
elif(file_name in os.listdir(likely_rapidcode_path_3)):
rapidcode_dir=likely_rapidcode_path_3
elif(file_name in os.listdir(likely_rapidcode_path_4)):
rapidcode_dir=likely_rapidcode_path_4
else:
raise Exception("RapidCode search path exhausted.")
except:
raise Exception("Could not find RapidCode Directory. Try entering the path manually likely C:/RSI/X.X.X")
return str(rapidcode_dir)

Learn more in topic page.