Learn how to create a MotionController, set an AxisCount and get Axis status in Python.
- Warning
- This is a sample program to assist in the integration of the RMP motion controller with your application. It may not contain all of the logic and safety features that your application requires. We recommend that you wire an external hardware emergency stop (e-stop) button for safety when using our code sample apps. Doing so will help ensure the safety of you and those around you and will prevent potential injury or damage.
The sample apps assume that the system (network, axes, I/O) are configured prior to running the code featured in the sample app. See the Configuration page for more information.
- Precondition
- Python has the library
grpcio-tools
, which can be installed using the Pip package manager. The command to install looks like pip3 install grpcio-tools
. The grpcio-tools
package also provides command line utilities to build gRPC code from a proto file. The Python script requires the following imports (some of which are scripts generated by grpcio-tools
) at the top:
import grpc
import rapidgrpc_pb2 as rapidgrpc
import rapidgrpc_pb2_grpc as rapidgrpc_stubs
import rapidcode_pb2 as rapidcode
import rapidcode_pb2_grpc as rapidcode_stubs
import rsienums_pb2 as rsienums
The following helper function is used to check each response message for errors:
def do_rpc(rpc, request):
response = rpc(request)
num_errors = len(response.header.errors)
if (num_errors > 0):
raise Exception(f"Last RPC had {num_errors} errors! {response.header.errors}")
return response
📜
Stub Creation
Create a stub to use RMPService's RPCs. grpc.insecure_channel
is a class from the Python grpcio-tools
package that establishes a connection to the server listening at the specified IP and port.
channel = grpc.insecure_channel(format_address(ip_address, port))
rmp_stub = rapidcode_stubs.RMPServiceStub(channel)
📜 Server Info
Get some information about the server.
server_stub = rapidgrpc_stubs.ServerControlServiceStub(channel)
info_request = rapidgrpc.ServerGetInfoRequest()
info_response = do_rpc(server_stub.GetInfo, info_request)
print(f"Received GetInfoResponse from server...\n"
f"ID: {info_response.id:x}\n"
f"Name: {info_response.name}\n"
f"Version: {info_response.version}")
📜 Create a Motion Controller
Create a motion controller on the server with a specified number of axes.
motioncontroller_request = rapidcode.MotionControllerRequest()
motioncontroller_request.action.create.node_name = RMP_NODE_NAME
motioncontroller_request.config.axis_count = AXIS_COUNT
motioncontroller_response = do_rpc(rmp_stub.MotionController, motioncontroller_request)
📜 Check the status of an Axis
Check the current state within the status message of an Axis.
axis_request = rapidcode.AxisRequest()
axis_request.index = 0
axis_status_response = do_rpc(rmp_stub.Axis, axis_request)
response_state = axis_status_response.status.state
print(f"Axis state: {rsienums.RSIState.Name(response_state)}")