Skip to content

Commit ed61346

Browse files
committed
Add socket control
1 parent 5ee2bd8 commit ed61346

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

pymycobot/mycobotsocket.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# coding=utf-8
2+
3+
from __future__ import division
4+
import socket
5+
import time
6+
import sys
7+
# import threading
8+
9+
10+
class MyCobotSocket:
11+
"""MyCobot Python API Socket communication class."""
12+
13+
def __init__(self, ip, port, baudrate="115200", timeout=0.1, debug=False):
14+
"""
15+
Args:
16+
port : port string
17+
baudrate : baud rate string, default '115200'
18+
timeout : default 0.1
19+
debug : whether show debug info
20+
"""
21+
self.SERVER_IP = ip # 输入正确的目标ip地址,请查看树莓派ip
22+
self.SERVER_PORT = 9000
23+
self.sock = self.connect()
24+
self.send_command(port)
25+
self.send_command(baudrate)
26+
27+
def connect(self):
28+
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
29+
sock.connect((self.SERVER_IP, self.SERVER_PORT)) # 打开链接
30+
return sock
31+
32+
def send_command(self, command):
33+
# 发送数据
34+
self.sock.sendall((command+"~").encode())
35+
# 接收数据
36+
data = self.sock.recv(1024)
37+
if data.decode() == "None":
38+
return ""
39+
return data.decode()

tests/test_socket.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from pymycobot.mycobotsocket import MyCobotSocket
2+
import time
3+
m = MyCobotSocket("192.168.10.115", "/dev/ttyAMA0", "1000000")
4+
# m.send_command("send_angles([0,0,0,0,0,0],40)")
5+
# m.send_command("power_on")
6+
# print(m.send_command("is_power_on"))
7+
# m.send_command("release_all_servos")
8+
# print(m.send_command("is_controller_connected"))
9+
# m.send_command("send_angles([0,0,0,0,0,0],30)")
10+
# m.send_command("send_angle(1,100,30)")
11+
print(m.send_command("get_coords"))
12+
# print(m.send_command("get_coords"))
13+
# m.send_command("send_coords([59.2,-52.4,231.5,169.03,15.64,-91.2],20,1)")
14+
# print(m.send_command(
15+
# "is_in_position([59.2,-52.4,231.5,169.03,15.64,-91.2],1)"))
16+
# m.send_command("jog_coord(1,0,20)")
17+
# print(m.send_command("release_servo(2)"))
18+
# m.send_command("sync_send_angles([0,0,0,0,0,0],30)")
19+
# m.send_command("sync_send_angles([100,0,0,0,0,0],30)")
20+
# m.send_command(
21+
# "sync_send_coords([59.2,-52.4,231.5,169.03,15.64,-91.2],30,1)")
22+
# m.send_command(
23+
# "sync_send_coords([53,53,412,-90.7,-1.5,12.67],30,1)")
24+
# print(m.send_command("is_in_position([53,53,412,-90.7,-1.5,12.67],1)"))
25+
26+
# m.send_command("jog_angle(1,1,20)")
27+
# time.sleep(2)
28+
# m.send_command("jog_stop")

0 commit comments

Comments
 (0)