klampt.control.networkrobotinterface module
Classes for building networked RIL servers and clients.
Added in version 0.9.
Classes:
Base class for a robot interface that communicates with a server. |
|
|
Base class for an asynchronous robot interface that serves clients. |
|
A XMLRPC-based server for a RIL robot controller. |
|
An XMLRPC-based client that connects to an XMLRPCRobotInterfaceServer. |
- class klampt.control.networkrobotinterface.ClientRobotInterfaceBase[source]
Bases:
_RobotInterfaceStatefulBase
Base class for a robot interface that communicates with a server. Users will not use this class directly.
For subclass implementers: begin/endStep() will need to be called repeatedly. On endStep() all the communication is performed. Implement connect() disconnect(), getInitialData() and updateSettingsAndCommands(),
Methods:
connect
()updateSettingsAndCommands
(settings, commands)partInterface
(part[, joint_idx])Returns a RobotInterfaceBase that allows control of the given part/joint.
Tries to connect to the robot.
close
()Cleanly shuts down any resources acquired using initialize().
This is called before the commands sent at each time step
endStep
()This is called after the commands sent at each time step
reset
()If the robot has a non-normal status code, attempt to reset it to normal operation.
- getInitialData()[source]
- Return type:
Tuple
[Dict
,_RobotInterfaceStructure
,_RobotInterfaceSettings
,_RobotInterfaceState
]
- partInterface(part, joint_idx=None)[source]
Returns a RobotInterfaceBase that allows control of the given part/joint. If no such controller exists, raises a NotImplementedError.
The part/joint controller should operate on exactly the DOFs specified by self.indices(part,joint_idx).
- class klampt.control.networkrobotinterface.ServerRobotInterfaceBase(interface)[source]
Bases:
_RobotInterfaceStatefulBase
Base class for an asynchronous robot interface that serves clients. End users will not use this class, but implementers will need to subclass it.
Subclass implementers will need to start the controller thread, and then start a thread that receives RPC calls and call getInitialData_impl and updateSettingsAndCommands_impl in response.
You’ll need to pair your server with a client that implements the appropriate protocols, e.g.,
XMLRPCRobotInterfaceServer
/XMLRPCRobotInterfaceClient
Implementation sketch (omitting error handling):
#TODO: define MyServer as subclass of ServerRobotInterfaceBase server = MyServer(addr,interface) if not server.initialize(): print("Error initializing") exit(1) server.startController() #serve the client quit = False client = None while not quit: if client is None: client = server.accept() else: func, args = server.read_rpc() if func == 'getInitialData': res = server.getInitialData_impl() server.respond_rpc(res) elif func == 'updateSettingsAndCommands': res = server.updateSettingsAndCommands_impl(*args) server.respond_rpc(res) else: raise RuntimeError("Invalid RPC call?") time.sleep(0.01) #some poll rate server.stop() server.close()
Methods:
Tries to connect to the robot.
stop
()Asks to stop motion -- perhaps call this when all clients disconnect
close
()Subclasses might want to override this to close clients as well.
- getInitialData_impl()[source]
- Return type:
Tuple
[Dict
,_RobotInterfaceStructure
,_RobotInterfaceSettings
,_RobotInterfaceState
]
- class klampt.control.networkrobotinterface.XMLRPCRobotInterfaceServer(interface, ip, port=7881)[source]
Bases:
ServerRobotInterfaceBase
A XMLRPC-based server for a RIL robot controller.
Usage:
raw_interface = MyRobotsInterface(...) interface = RobotInterfaceCompleter(raw_interface) server = XMLRPCRobotInterfaceServer(interface, 'localhost') server.serve() #will not terminate until Ctrl+C is pressed
- Parameters:
interface (RobotInterfaceBase) – the interface to serve
ip (str) – the IP address (usually ‘127.0.0.1’ or ‘localhost’)
port (int) – the port of the listening socket. 7881 is used by default.
Methods:
log
(err)Logs an error.
serve
()
- class klampt.control.networkrobotinterface.XMLRPCRobotInterfaceClient(addr='http://127.0.0.1:7881')[source]
Bases:
ClientRobotInterfaceBase
An XMLRPC-based client that connects to an XMLRPCRobotInterfaceServer.
No need to call connect, disconnect, etc. Once this is initialized, just treat it like a normal RIL interface.
Usage:
interface = XMLRPCRobotInterfaceClient() if not interface.initialize(): print("Couldn't connect to localhost:7881") exit(1) #now can treat like a standard RobotInterfaceBase qsns = interface.sensedPosition() vsns = interface.sensedVelocity()
- Parameters:
addr (str) – the IP address of the server, including port. Localhost port 7881 is used by default. (note that on Windows, using localhost is extremely slow, so we use 127.0.0.1 instead)
Methods:
connect
()updateSettingsAndCommands
(delta_settings, ...)