1.ABB机器人进行socket通讯,支持机器人做socket的client连接其他设备,也支持机器人作为socket的server,被其他设备进行连接。
2.作为server时,需要考虑其他客户端连接机器人时断开后的机器人异常处理。同时,机器人作为socket 的server,通常编写测试完成后,对应的task会被设为semi static(即开机自启动,且前台无法停止程序,除非程序出错或者再次进入controller的task设定,将task类型设为normal)
3.此处推荐Ros编写的机器人作为socket server的代码,以供参考。相关代码可以从https://github.com/ros-industrial/abb_driver/tree/kinetic-devel/rapid 下载
主程序如下:
MODULE ROS_stateServer !程序运行模式设定为循环 LOCAL CONST num server_port := 11002; LOCAL CONST num update_rate := 0.10; ! broadcast rate (sec)
LOCAL VAR socketdev server_socket; LOCAL VAR socketdev client_socket;
PROC main() TPWrite "StateServer: Waiting for connection."; ROS_init_socket server_socket, server_port; ROS_wait_for_client server_socket, client_socket;
WHILE (TRUE) DO send_joints; send_status; WaitTime update_rate; ENDWHILE
ERROR (ERR_SOCK_TIMEOUT, ERR_SOCK_CLOSED) IF (ERRNO=ERR_SOCK_TIMEOUT) OR (ERRNO=ERR_SOCK_CLOSED) THEN SkipWarn; ! TBD: include this error data in the message logged below? ErrWrite \W, "ROS StateServer disconnect", "Connection lost. Waiting for new connection."; ExitCycle; ! restart program !程序运行模式设定为循环,这样ExitCycle可以重新从main第一行开始运行 ELSE TRYNEXT; ENDIFUNDOENDPROCENDMODULE
MODULEROS_socket(SYSMODULE)PROC ROS_init_socket(VAR socketdev server_socket, num port)IF(SocketGetStatus(server_socket) = SOCKET_CLOSED) SocketCreate server_socket;IF(SocketGetStatus(server_socket) = SOCKET_CREATED) SocketBind server_socket, GetSysInfo(\LanIp), port;IF(SocketGetStatus(server_socket) = SOCKET_BOUND) SocketListen server_socket;
ERRORRAISE;! raise errors to calling codeENDPROC
PROCROS_wait_for_client(VAR socketdev server_socket, VAR socketdev client_socket, \num wait_time)VARstring client_ip;VARnum time_val := WAIT_MAX; ! default to wait-forever
IFPresent(wait_time) time_val := wait_time;
IF(SocketGetStatus(client_socket) <> SOCKET_CLOSED) SocketClose client_socket;WaitUntil(SocketGetStatus(client_socket) = SOCKET_CLOSED);
SocketAcceptserver_socket, client_socket, \ClientAddress:=client_ip, \Time:=time_val;TPWrite"Client at "+client_ip+" connected.";
ERRORRAISE;! raise errors to calling code ! 所有的错误处理使用Raise,即调用上层统一进行错误处理ENDPROCENDMODULE |