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;
ENDIF
UNDO
ENDPROC
ENDMODULE
MODULE ROS_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;
ERROR
RAISE; ! raise errors to calling code
ENDPROC
PROC ROS_wait_for_client(VAR socketdev server_socket, VAR socketdev client_socket, \num wait_time)
VAR string client_ip;
VAR num time_val := WAIT_MAX; ! default to wait-forever
IF Present(wait_time) time_val := wait_time;
IF (SocketGetStatus(client_socket) <> SOCKET_CLOSED) SocketClose client_socket;
WaitUntil (SocketGetStatus(client_socket) = SOCKET_CLOSED);
SocketAccept server_socket, client_socket, \ClientAddress:=client_ip, \Time:=time_val;
TPWrite "Client at "+client_ip+" connected.";
ERROR
RAISE; ! raise errors to calling code
! 所有的错误处理使用Raise,即调用上层统一进行错误处理
ENDPROC
ENDMODULE