赞
踩
Socket又称"套接字",应用程序通常通过"套接字"向网络发出请求或者应答网络请求,使主机间或者一台计算机上的进程间可以通讯。
建立Socket连接至少需要一对套接字,其中一个运行于客户端,称为ClientSocket ,另一个运行于服务器端,称为ServerSocket 。
每台电脑对应唯一ip地址,电脑上每一个进程对应一个端口(一个进程可以绑定多个端口号,但一个端口只能绑定一个进程)。此外,0~1023为知名端口号,一般不要去设置这些,1024~65535则可以自己去设置。比如9999。
先运行服务端,再运行客户端
厂家已经把代码封装好了,机器人只要启动,服务端默认就打开了的(也就是roboflow打开就行,不需要上电操作),启动roboflow之后,服务器端就开始运行。
可以打开roboflow,查看配置的ip以及端口是什么,剩下的操作都很简单,调用接口就行
- def get_angles(self):
- '''获取当前六个关节角度(°)'''
- message = "get_angles()".encode()
- self.sock.sendall(message)
- angles_str = self.sock.recv(1024)
- # while not angles_str.startswith('get_angles'):
- # self.sock.sendall(message)
- # angles_str = self.sock.recv(1024)
- # str to list[float]
- angles = [float(p) for p in angles_str[12:-1].decode().split(',')]
- return angles
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- # 大象机器人Socket控制工具包
-
- import socket
- import time
-
- class elephant_command():
- def __init__(self):
- '''初始化,连接机械臂'''
- self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- self.server_address = ('192.168.2.8', 5001) # 机械臂服务器的IP地址和端口
- print("start connect")
- self.sock.connect(self.server_address)
- print("connect success")
-
- def get_angles(self):
- '''获取当前六个关节角度(°)'''
- message = "get_angles()"
- self.sock.sendall(message)
- angles_str = self.sock.recv(1024)
- while not angles_str.startswith('get_angles'):
- self.sock.sendall(message)
- angles_str = self.sock.recv(1024)
- # str to list[float]
- angles = [float(p) for p in angles_str[12:-1].split(',')]
- return angles
-
- def set_angles(self, angles_array, speed):
- '''设定六个关节的角度(°)和速度'''
- ang_msg = "set_angles({},{})".format(','.join(['{:.3f}'.format(x) for x in angles_array]), speed)
- self.sock.sendall(ang_msg)
- back_msg = self.sock.recv(1024)
- print(back_msg)
-
- def set_angle(self, joint, angle, speed):
- '''设定单个关节(joint,1~6)的角度(°)和速度(°/min)'''
- ang_msg = "set_angle(J{},{},{})".format(joint, angle, speed)
- self.sock.sendall(ang_msg)
- back_msg = self.sock.recv(1024)
- print(back_msg)
-
- def get_coords(self):
- '''获取当前末端位姿(mm)'''
- message = "get_coords()".encode()
- self.sock.sendall(message)
- coords_str = self.sock.recv(1024)
- while not coords_str.startswith(b'get_coords'):
- self.sock.sendall(message)
- coords_str = self.sock.recv(1024)
- # str to list[float]
- coords = [float(p) for p in coords_str[12:-1].split(b',')]
- return coords
-
- def set_coords(self, coords_array, speed):
- '''设定机械臂目标位姿(mm)和运动速度(mm/min)'''
- coords_msg = "set_coords({},{})".format(','.join(['{:.3f}'.format(x) for x in coords_array]), speed)
- # print(coords_msg)
- self.sock.sendall(coords_msg.encode())
- back_msg = self.sock.recv(1024)
- print(back_msg)
-
- def set_coord(self, axis, coord, speed):
- '''设定x,y,z,rx,ry,rz某一方向的坐标(mm)和速度(mm/min)'''
- coord_msg = "set_coord({},{:.3f},{})".format(axis, coord, speed)
- self.sock.sendall(coord_msg)
- back_msg = self.sock.recv(1024)
- print(back_msg)
-
- def jog_coord(self, axis, dirc, speed):
- '''让机械臂沿一轴(axis, x,y,z)方向(dirc, -1负方向,0停止,1正方向)以匀速(mm/min)运动'''
- coord_msg = "jog_coord({},{},{})".format(axis, dirc, speed)
- self.sock.sendall(coord_msg.encode())
- back_msg = self.sock.recv(1024)
- print(back_msg)
-
- def jog_stop(self, axis):
- '''让机械臂沿一轴(axis, x,y,z,rx,ry,rz,j1~j6)运动停止'''
- coord_msg = "jog_stop({})".format(axis)
- self.sock.sendall(coord_msg.encode())
- back_msg = self.sock.recv(1024)
- print(back_msg)
-
- def jog_angle(self, joint, dirc, speed):
- '''让机械臂某一关节(joint, 1~6)匀速( / )转动(dirc, -1负方向,0停止,1正方向)'''
- coord_msg = "jog_angle(J{},{},{})".format(joint, dirc, speed)
- self.sock.sendall(coord_msg.encode())
- back_msg = self.sock.recv(1024)
- print(back_msg)
-
- def task_stop(self):
- '''停止当前任务'''
- message = "task_stop()"
- self.sock.sendall(message.encode())
- back_msg = self.sock.recv(1024)
- print(back_msg)
-
- def wait(self, seconds):
- '''设定机械臂等待时间(s)'''
- message = "wait({})".format(seconds)
- self.sock.sendall(message)
- back_msg = self.sock.recv(1024)
- print(back_msg)
-
- def power_on(self):
- '''给机械臂上电?'''
- message = "power_on()"
- self.sock.sendall(message)
- time.sleep(20)
- back_msg = self.sock.recv(1024)
- print(back_msg)
-
- def power_off(self):
- '''给机械臂断电?'''
- message = "power_off()"
- self.sock.sendall(message)
- back_msg = self.sock.recv(1024)
- print(back_msg)
-
- def get_speed(self):
- '''获取机械臂(末端)速度(mm/s)'''
- message = "get_speed()"
- self.sock.sendall(message)
- speed = self.sock.recv(1024)
- return speed
-
- def state_check(self):
- '''检查机械臂状态(1正常,0不正常)'''
- message = b"state_check()"
- self.sock.sendall(message)
- state = self.sock.recv(1024)
- return state
-
- def check_running(self):
- '''检查机械臂是否运行(1正在运行,0不在运行)'''
- message = b"check_running()"
- self.sock.sendall(message)
- running_state = self.sock.recv(1024)
- if running_state == 'check_running:1':
- return True
- else:
- return False
-
- def set_torque_limit(self, axis, torque):
- '''设置机械臂在x,y,z某一方向上的力矩限制(N)'''
- torque_limit = "set_torque_limit({},{})".format(axis, torque)
- self.sock.sendall(torque_limit)
- back_msg = self.sock.recv(1024)
- print(back_msg)
-
- def set_payload(self, payload):
- '''设置机械臂负载(kg)'''
- message = "set_payload({})".format(payload)
- self.sock.sendall(message)
- back_msg = self.sock.recv(1024)
- print(back_msg)
-
- def set_acceleration(self, acc):
- '''设置机械臂(末端)加速度(整数,mm/s^2)'''
- message = "set_acceleration({})".format(acc)
- self.sock.sendall(message)
- back_msg = self.sock.recv(1024)
- print(back_msg)
-
- def get_acceleration(self):
- '''获取机械臂(末端)加速度(mm/s^2)'''
- message = "get_acceleration()"
- self.sock.sendall(message)
- acc = self.sock.recv(1024)
- return acc
-
- def wait_command_done(self):
- '''等待命令执行完毕'''
- message = "wait_command_done()"
- self.sock.sendall(message)
- back_msg = self.sock.recv(1024)
- print(back_msg)
-
- def pause_program(self):
- '''暂停进程'''
- message = "pause_program()"
- self.sock.sendall(message)
- back_msg = self.sock.recv(1024)
- print(back_msg)
-
- def resume_program(self):
- '''重启已暂停的进程'''
- message = "resume_program()"
- self.sock.sendall(message)
- back_msg = self.sock.recv(1024)
- print(back_msg)
-
- def state_on(self):
- '''机器人使能(使可控)'''
- message = "state_on()"
- self.sock.sendall(message)
- time.sleep(5)
- back_msg = self.sock.recv(1024)
- print(back_msg)
-
- def state_off(self):
- '''机器人去使能(使不可控)'''
- message = "state_off()"
- self.sock.sendall(message)
- time.sleep(5)
- back_msg = self.sock.recv(1024)
- print(back_msg)
-
- def set_digital_out(self, pin_number, signal):
- """ 设定数字输出端口电平,pin_number:0~15, signal:0低1高"""
- digital_signal = 'set_digital_out({},{})'.format(pin_number, signal)
- self.sock.sendall(digital_signal.encode())
- back_msg = self.sock.recv(1024)
- print(back_msg)
-
简单测试
- from elephant import elephant_command
- import time
- import random
-
- erobot = elephant_command()
- # 笛卡尔空间
- cur_pose = erobot.get_coords()
- print(cur_pose)
尽量多看官方的github代码,里面有最详细的资源github
要用rviz控制机械,肯定就需要安装大象机器人的库
先安装依赖,命令行执行下面语句:(此外ROS以及moveit都已经安装)
pip install pymycobot --upgrade
git 库并且编译(删除src中的mycobot320这个库,这个文件用不到,并且有问题)
- cd ~/catkin_ws/src
- git clone https://github.com/elephantrobotics/mycobot_ros.git
- cd ..
- catkin build
- source /devel/setup.bash
最后一部环境变量也可以设置为永久环境变量的更改,参考之前的博客
实现与真实机械臂的通信需要修改端口波特率等,在py文件中。pro600默认使用python3(每一代机器人的版本不同,我需要看py文件去确定)
启动launch,打开rviz。官方已经给了配置以及模型文件了。拖动划块可以移动。
roslaunch mycobot_600 mycobot_600_slider.launch
如果要实现实时控制机械臂,那么还需要启动python文件,新开一个终端,用rosrun启动(可能会手动把文件的权限设置为可执行文件)
rosrun mycobot_600 slider_600.py
特别注意:不要拖动过快以及造成碰撞!!不要拖动过快以及造成碰撞!!不要拖动过快以及造成碰撞!!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。