def Socket_command():
    global Socket, arm_mode_flag, data

    if arm_mode_flag == True:
        arm_mode_flag = False
        for case in switch(socket_cmd.action):
            #-------PtP Mode--------
            if case(Taskcmd.Action_Type.PtoP):
                for case in switch(socket_cmd.setboth):
                    if case(Taskcmd.Ctrl_Mode.CTRL_POS):
                        data = TCP.SetPtoP(socket_cmd.grip, Taskcmd.RA.ABS,
                                           Taskcmd.Ctrl_Mode.CTRL_POS, pos.x,
                                           pos.y, pos.z, pos.pitch, pos.roll,
                                           pos.yaw, socket_cmd.setvel)
                        break
                    if case(Taskcmd.Ctrl_Mode.CTRL_EULER):
                        data = TCP.SetPtoP(socket_cmd.grip, Taskcmd.RA.ABS,
                                           Taskcmd.Ctrl_Mode.CTRL_EULER, pos.x,
                                           pos.y, pos.z, pos.pitch, pos.roll,
                                           pos.yaw, socket_cmd.setvel)
                        break
                    if case(Taskcmd.Ctrl_Mode.CTRL_BOTH):
                        data = TCP.SetPtoP(socket_cmd.grip, Taskcmd.RA.ABS,
                                           Taskcmd.Ctrl_Mode.CTRL_BOTH, pos.x,
                                           pos.y, pos.z, pos.pitch, pos.roll,
                                           pos.yaw, socket_cmd.setvel)
                        break
                break
            #-------Line Mode--------
            if case(Taskcmd.Action_Type.Line):
                for case in switch(socket_cmd.setboth):
                    if case(Taskcmd.Ctrl_Mode.CTRL_POS):
                        data = TCP.SetLine(socket_cmd.grip, Taskcmd.RA.ABS,
                                           Taskcmd.Ctrl_Mode.CTRL_POS, pos.x,
                                           pos.y, pos.z, pos.pitch, pos.roll,
                                           pos.yaw, socket_cmd.setvel)
                        break
                    if case(Taskcmd.Ctrl_Mode.CTRL_EULER):
                        data = TCP.SetLine(socket_cmd.grip, Taskcmd.RA.ABS,
                                           Taskcmd.Ctrl_Mode.CTRL_EULER, pos.x,
                                           pos.y, pos.z, pos.pitch, pos.roll,
                                           pos.yaw, socket_cmd.setvel)
                        break
                    if case(Taskcmd.Ctrl_Mode.CTRL_BOTH):
                        data = TCP.SetLine(socket_cmd.grip, Taskcmd.RA.ABS,
                                           Taskcmd.Ctrl_Mode.CTRL_BOTH, pos.x,
                                           pos.y, pos.z, pos.pitch, pos.roll,
                                           pos.yaw, socket_cmd.setvel)
                        break
                break
            #-------設定手臂速度--------
            if case(Taskcmd.Action_Type.SetVel):
                data = TCP.SetVel(socket_cmd.grip, socket_cmd.setvel)
                break
            #-------設定手臂Delay時間--------
            if case(Taskcmd.Action_Type.Delay):
                data = TCP.SetDelay(socket_cmd.grip, 0)
                break
            #-------設定手臂急速&安全模式--------
            if case(Taskcmd.Action_Type.Mode):
                data = TCP.Set_SpeedMode(socket_cmd.grip, socket_cmd.Speedmode)
                break
        socket_cmd.action = 6  ##切換初始mode狀態
        print(data)
        print("Socket:", Socket)
        Socket.send(data.encode('utf-8'))  #socket傳送for python to translate str
コード例 #2
0
def socket_client():
    global Arm_feedback, data
    try:
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        s.connect(('192.168.0.1', 8080))  #iclab 5 & iclab hiwin
        #s.connect(('192.168.1.102', 8080))#iclab computerx
    except socket.error as msg:
        print(msg)
        sys.exit(1)
    print('Connection has been successful')
    print(s.recv(1024))
    start_input = int(input('開始傳輸請按1,離開請按3 : '))  #輸入開始指令
    #start_input = 1
    if start_input == 1:
        while 1:
            ##---------------socket 傳輸手臂命令-----------------
            #-------選擇模式--------
            for case in switch(socket_cmd.action):
                #-------PtP Mode--------
                if case(Taskcmd.Action_Type.PtoP):
                    for case in switch(socket_cmd.setboth):
                        if case(Taskcmd.Ctrl_Mode.CTRL_POS):
                            data = TCP.SetPtoP(socket_cmd.grip, Taskcmd.RA.ABS,
                                               Taskcmd.Ctrl_Mode.CTRL_POS,
                                               pos.x, pos.y, pos.z, pos.pitch,
                                               pos.roll, pos.yaw,
                                               socket_cmd.setvel)
                            break
                        if case(Taskcmd.Ctrl_Mode.CTRL_EULER):
                            data = TCP.SetPtoP(socket_cmd.grip, Taskcmd.RA.ABS,
                                               Taskcmd.Ctrl_Mode.CTRL_EULER,
                                               pos.x, pos.y, pos.z, pos.pitch,
                                               pos.roll, pos.yaw,
                                               socket_cmd.setvel)
                            break
                        if case(Taskcmd.Ctrl_Mode.CTRL_BOTH):
                            data = TCP.SetPtoP(socket_cmd.grip, Taskcmd.RA.ABS,
                                               Taskcmd.Ctrl_Mode.CTRL_BOTH,
                                               pos.x, pos.y, pos.z, pos.pitch,
                                               pos.roll, pos.yaw,
                                               socket_cmd.setvel)
                            break
                    break
                #-------Line Mode--------
                if case(Taskcmd.Action_Type.Line):
                    for case in switch(socket_cmd.setboth):
                        if case(Taskcmd.Ctrl_Mode.CTRL_POS):
                            data = TCP.SetLine(socket_cmd.grip, Taskcmd.RA.ABS,
                                               Taskcmd.Ctrl_Mode.CTRL_POS,
                                               pos.x, pos.y, pos.z, pos.pitch,
                                               pos.roll, pos.yaw,
                                               socket_cmd.setvel)
                            break
                        if case(Taskcmd.Ctrl_Mode.CTRL_EULER):
                            data = TCP.SetLine(socket_cmd.grip, Taskcmd.RA.ABS,
                                               Taskcmd.Ctrl_Mode.CTRL_EULER,
                                               pos.x, pos.y, pos.z, pos.pitch,
                                               pos.roll, pos.yaw,
                                               socket_cmd.setvel)
                            break
                        if case(Taskcmd.Ctrl_Mode.CTRL_BOTH):
                            data = TCP.SetLine(socket_cmd.grip, Taskcmd.RA.ABS,
                                               Taskcmd.Ctrl_Mode.CTRL_BOTH,
                                               pos.x, pos.y, pos.z, pos.pitch,
                                               pos.roll, pos.yaw,
                                               socket_cmd.setvel)
                            break
                    break
                #-------設定手臂速度--------
                if case(Taskcmd.Action_Type.SetVel):
                    data = TCP.SetVel(socket_cmd.grip, socket_cmd.setvel)
                    break
                #-------設定手臂Delay時間--------
                if case(Taskcmd.Action_Type.Delay):
                    data = TCP.SetDelay(socket_cmd.grip, 0)
                    break
                #-------設定手臂急速&安全模式--------
                if case(Taskcmd.Action_Type.Mode):
                    data = TCP.SetMode(socket_cmd.grip, 0)
                    break
            socket_cmd.action = 5  ##切換初始mode狀態
            s.send(data.encode('utf-8'))  #socket傳送for python to translate str
            feedback_str = s.recv(1024)
            #手臂端傳送手臂狀態
            if str(feedback_str[2]) == '70':  # F 手臂為Ready狀態準備接收下一個運動指令
                feedback = 0
                socket_client_arm_state(feedback)
                print("isbusy false")
            if str(feedback_str[2]) == '84':  # T 手臂為忙碌狀態無法執行下一個運動指令
                feedback = 1
                socket_client_arm_state(feedback)
                print("isbusy true")
            if str(feedback_str[2]) == '54':  # 6 策略完成
                feedback = 6
                socket_client_arm_state(feedback)
                print("shutdown")
            #Arm_feedback = TCP.Is_busy(feedback)
        ##---------------socket 傳輸手臂命令 end-----------------
            if Arm_feedback == Taskcmd.Arm_feedback_Type.shutdown:
                rospy.on_shutdown(myhook)
                break
    if start_input == 3:
        pass
    s.close()
コード例 #3
0
def socket_command():
    while 1:
        ##---------------socket 傳輸手臂命令-----------------
        #-------選擇模式--------
        for case in switch(socket_cmd.action):
            #-------PtP Mode--------
            if case(Taskcmd.Action_Type.PtoP):
                for case in switch(socket_cmd.setboth):
                    if case(Taskcmd.Ctrl_Mode.CTRL_POS):
                        data = TCP.SetPtoP(socket_cmd.grip, Taskcmd.RA.ABS,
                                           Taskcmd.Ctrl_Mode.CTRL_POS, pos.x,
                                           pos.y, pos.z, pos.pitch, pos.roll,
                                           pos.yaw, socket_cmd.setvel)
                        break
                    if case(Taskcmd.Ctrl_Mode.CTRL_EULER):
                        data = TCP.SetPtoP(socket_cmd.grip, Taskcmd.RA.ABS,
                                           Taskcmd.Ctrl_Mode.CTRL_EULER, pos.x,
                                           pos.y, pos.z, pos.pitch, pos.roll,
                                           pos.yaw, socket_cmd.setvel)
                        break
                    if case(Taskcmd.Ctrl_Mode.CTRL_BOTH):
                        data = TCP.SetPtoP(socket_cmd.grip, Taskcmd.RA.ABS,
                                           Taskcmd.Ctrl_Mode.CTRL_BOTH, pos.x,
                                           pos.y, pos.z, pos.pitch, pos.roll,
                                           pos.yaw, socket_cmd.setvel)
                        break
                break
            #-------Line Mode--------
            if case(Taskcmd.Action_Type.Line):
                for case in switch(socket_cmd.setboth):
                    if case(Taskcmd.Ctrl_Mode.CTRL_POS):
                        data = TCP.SetLine(socket_cmd.grip, Taskcmd.RA.ABS,
                                           Taskcmd.Ctrl_Mode.CTRL_POS, pos.x,
                                           pos.y, pos.z, pos.pitch, pos.roll,
                                           pos.yaw, socket_cmd.setvel)
                        break
                    if case(Taskcmd.Ctrl_Mode.CTRL_EULER):
                        data = TCP.SetLine(socket_cmd.grip, Taskcmd.RA.ABS,
                                           Taskcmd.Ctrl_Mode.CTRL_EULER, pos.x,
                                           pos.y, pos.z, pos.pitch, pos.roll,
                                           pos.yaw, socket_cmd.setvel)
                        break
                    if case(Taskcmd.Ctrl_Mode.CTRL_BOTH):
                        data = TCP.SetLine(socket_cmd.grip, Taskcmd.RA.ABS,
                                           Taskcmd.Ctrl_Mode.CTRL_BOTH, pos.x,
                                           pos.y, pos.z, pos.pitch, pos.roll,
                                           pos.yaw, socket_cmd.setvel)
                        break
                break
            #-------設定手臂速度--------
            if case(Taskcmd.Action_Type.SetVel):
                data = TCP.SetVel(socket_cmd.grip, socket_cmd.setvel)
                break
            #-------設定手臂Delay時間--------
            if case(Taskcmd.Action_Type.Delay):
                data = TCP.SetDelay(socket_cmd.grip, 0)
                break
            #-------設定手臂急速&安全模式--------
            if case(Taskcmd.Action_Type.Mode):
                data = TCP.Set_SpeedMode(socket_cmd.grip, socket_cmd.Speedmode)
                break
        socket_cmd.action = 5  ##切換初始mode狀態
        s.send(data.encode('utf-8'))  #socket傳送for python to translate str
        feedback_str = s.recv(1024)
        #手臂端傳送手臂狀態
        if str(feedback_str[2]) == '70':  # F 手臂為Ready狀態準備接收下一個運動指令
            Arm_feedback = 0
            socket_client_arm_state(Arm_feedback)
            #print("isbusy false")
        if str(feedback_str[2]) == '84':  # T 手臂為忙碌狀態無法執行下一個運動指令
            Arm_feedback = 1
            socket_client_arm_state(Arm_feedback)
            #print("isbusy true")
        if str(feedback_str[2]) == '54':  # 6 策略完成
            Arm_feedback = 6
            socket_client_arm_state(Arm_feedback)
            print("shutdown")
    ##---------------socket 傳輸手臂命令 end-----------------
        if Arm_feedback == Taskcmd.Arm_feedback_Type.shutdown:
            rospy.on_shutdown(myhook)
            break
def socket_client():
    global Arm_feedback,data,point_data_flag,arm_mode_flag,speed_mode_flag,Socket_sent_flag
    try:
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        s.connect(('192.168.0.1', 8080))#iclab 5 & iclab hiwin
        #s.connect(('192.168.1.102', 8080))#iclab computerx
    except socket.error as msg:
        print(msg)
        sys.exit(1)
    print('Connection has been successful')
    print(s.recv(1024))
    #start_input=int(input('開始傳輸請按1,離開請按3 : ')) #輸入開始指令
    start_input = 1
    if start_input==1:
        while 1:
        ##---------------socket 傳輸手臂命令-----------------
            #if Arm_feedback == 0:
            if arm_mode_flag == True:
                arm_mode_flag = False
                #-------選擇模式--------
                for case in switch(socket_cmd.action):
                    #-------PtP Mode--------
                    if case(Taskcmd.Action_Type.PtoP):
                        for case in switch(socket_cmd.setboth):
                            if case(Taskcmd.Ctrl_Mode.CTRL_POS):
                                data = TCP.SetPtoP(socket_cmd.grip,Taskcmd.RA.ABS,Taskcmd.Ctrl_Mode.CTRL_POS,pos.x,pos.y,pos.z,pos.pitch,pos.roll,pos.yaw,socket_cmd.setvel)
                                break
                            if case(Taskcmd.Ctrl_Mode.CTRL_EULER):
                                data = TCP.SetPtoP(socket_cmd.grip,Taskcmd.RA.ABS,Taskcmd.Ctrl_Mode.CTRL_EULER,pos.x,pos.y,pos.z,pos.pitch,pos.roll,pos.yaw,socket_cmd.setvel)
                                break
                            if case(Taskcmd.Ctrl_Mode.CTRL_BOTH):
                                data = TCP.SetPtoP(socket_cmd.grip,Taskcmd.RA.ABS,Taskcmd.Ctrl_Mode.CTRL_BOTH,pos.x,pos.y,pos.z,pos.pitch,pos.roll,pos.yaw,socket_cmd.setvel)
                                break
                        break
                    #-------Line Mode--------
                    if case(Taskcmd.Action_Type.Line):
                        for case in switch(socket_cmd.setboth):
                            if case(Taskcmd.Ctrl_Mode.CTRL_POS):
                                data = TCP.SetLine(socket_cmd.grip,Taskcmd.RA.ABS,Taskcmd.Ctrl_Mode.CTRL_POS,pos.x,pos.y,pos.z,pos.pitch,pos.roll,pos.yaw,socket_cmd.setvel)
                                break
                            if case(Taskcmd.Ctrl_Mode.CTRL_EULER):
                                data = TCP.SetLine(socket_cmd.grip,Taskcmd.RA.ABS,Taskcmd.Ctrl_Mode.CTRL_EULER,pos.x,pos.y,pos.z,pos.pitch,pos.roll,pos.yaw,socket_cmd.setvel )
                                break
                            if case(Taskcmd.Ctrl_Mode.CTRL_BOTH):
                                data = TCP.SetLine(socket_cmd.grip,Taskcmd.RA.ABS,Taskcmd.Ctrl_Mode.CTRL_BOTH,pos.x,pos.y,pos.z,pos.pitch,pos.roll,pos.yaw,socket_cmd.setvel )
                                break
                        break
                    #-------設定手臂速度--------
                    if case(Taskcmd.Action_Type.SetVel):
                        data = TCP.SetVel(socket_cmd.grip, socket_cmd.setvel)
                        break
                    #-------設定手臂Delay時間--------
                    if case(Taskcmd.Action_Type.Delay):
                        data = TCP.SetDelay(socket_cmd.grip,0)
                        break
                    #-------設定手臂急速&安全模式--------
                    if case(Taskcmd.Action_Type.Mode):
                        data = TCP.Set_SpeedMode(socket_cmd.grip,socket_cmd.Speedmode)
                        break
                socket_cmd.action= 5 ##切換初始mode狀態
                s.send(data.encode('utf-8'))#socket傳送for python to translate str

                Socket_sent_flag = True
                socket_client_sent_flag(Socket_sent_flag)
            feedback_str = s.recv(1024)
            #手臂端傳送手臂狀態
            if str(feedback_str[2]) == '70':# F 手臂為Ready狀態準備接收下一個運動指令
                Arm_feedback = 0
                socket_client_arm_state(Arm_feedback)
                #print("isbusy false")
            if str(feedback_str[2]) == '84':# T 手臂為忙碌狀態無法執行下一個運動指令
                Arm_feedback = 1
                socket_client_arm_state(Arm_feedback)
                #print("isbusy true")
            if str(feedback_str[2]) == '54':# 6 策略完成
                Arm_feedback = 6
                socket_client_arm_state(Arm_feedback)
                print("shutdown")
            #確認傳送旗標
            if str(feedback_str[4]) == '48':#回傳0 false

            if str(feedback_str[4]) == '49':#回傳1 true
                
        ##---------------socket 傳輸手臂命令 end-----------------
            if Arm_feedback == Taskcmd.Arm_feedback_Type.shutdown:
                rospy.on_shutdown(myhook)
                break
    if start_input == 3:
        pass
    s.close()
##-----------socket client end--------
##-------------socket 封包傳輸 end--------------##
## 多執行緒
def thread_test():
    socket_client()
## 多執行序 end
def myhook():
    print ("shutdown time!")
if __name__ == '__main__':
    socket_cmd.action = 5##切換初始mode狀態
    t = threading.Thread(target=thread_test)
    t.start() # 開啟多執行緒
    socket_server()
    t.join()

    # Ctrl+K Ctrl+C	添加行注释 Add line comment
    # Ctrl+K Ctrl+U	删除行注释 Remove line comment
    #Ctrl+] / [	缩进/缩进行 Indent/outdent line
コード例 #5
0
def socket_command():
    while 1:
        ##---------------socket 傳輸手臂命令-----------------
        #-------選擇模式--------
        for case in switch(socket_cmd.action):
            #-------PtP Mode--------
            if case(Taskcmd.Action_Type.PtoP):
                for case in switch(socket_cmd.setboth):
                    if case(Taskcmd.Ctrl_Mode.CTRL_POS):
                        data = TCP.SetPtoP(socket_cmd.grip, Taskcmd.RA.ABS,
                                           Taskcmd.Ctrl_Mode.CTRL_POS, pos.x,
                                           pos.y, pos.z, pos.pitch, pos.roll,
                                           pos.yaw, socket_cmd.setvel)
                        break
                    if case(Taskcmd.Ctrl_Mode.CTRL_EULER):
                        data = TCP.SetPtoP(socket_cmd.grip, Taskcmd.RA.ABS,
                                           Taskcmd.Ctrl_Mode.CTRL_EULER, pos.x,
                                           pos.y, pos.z, pos.pitch, pos.roll,
                                           pos.yaw, socket_cmd.setvel)
                        break
                    if case(Taskcmd.Ctrl_Mode.CTRL_BOTH):
                        data = TCP.SetPtoP(socket_cmd.grip, Taskcmd.RA.ABS,
                                           Taskcmd.Ctrl_Mode.CTRL_BOTH, pos.x,
                                           pos.y, pos.z, pos.pitch, pos.roll,
                                           pos.yaw, socket_cmd.setvel)
                        break
                break
            #-------Line Mode--------
            if case(Taskcmd.Action_Type.Line):
                for case in switch(socket_cmd.setboth):
                    if case(Taskcmd.Ctrl_Mode.CTRL_POS):
                        data = TCP.SetLine(socket_cmd.grip, Taskcmd.RA.ABS,
                                           Taskcmd.Ctrl_Mode.CTRL_POS, pos.x,
                                           pos.y, pos.z, pos.pitch, pos.roll,
                                           pos.yaw, socket_cmd.setvel)
                        break
                    if case(Taskcmd.Ctrl_Mode.CTRL_EULER):
                        data = TCP.SetLine(socket_cmd.grip, Taskcmd.RA.ABS,
                                           Taskcmd.Ctrl_Mode.CTRL_EULER, pos.x,
                                           pos.y, pos.z, pos.pitch, pos.roll,
                                           pos.yaw, socket_cmd.setvel)
                        break
                    if case(Taskcmd.Ctrl_Mode.CTRL_BOTH):
                        data = TCP.SetLine(socket_cmd.grip, Taskcmd.RA.ABS,
                                           Taskcmd.Ctrl_Mode.CTRL_BOTH, pos.x,
                                           pos.y, pos.z, pos.pitch, pos.roll,
                                           pos.yaw, socket_cmd.setvel)
                        break
                break
            #-------設定手臂速度--------
            if case(Taskcmd.Action_Type.SetVel):
                data = TCP.SetVel(socket_cmd.grip, socket_cmd.setvel)
                break
            #-------設定手臂Delay時間--------
            if case(Taskcmd.Action_Type.Delay):
                data = TCP.SetDelay(socket_cmd.grip, 0)
                break
            #-------設定手臂急速&安全模式--------
            if case(Taskcmd.Action_Type.Mode):
                data = TCP.Set_SpeedMode(socket_cmd.grip, socket_cmd.Speedmode)
                break
            return (data)