-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsocketClient.py
More file actions
49 lines (42 loc) · 1.75 KB
/
Copy pathsocketClient.py
File metadata and controls
49 lines (42 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
from socketio import Client
import json
# Class which acts as an abstraction of the robot on the server
class RobotClient():
# Method to initialize the client
def __init__(self, current_joint1=0, current_joint2=0, verbose=False):
self.sio = Client()
# here we keep track of the current target positions of the robot
self.jointAngles = [current_joint1, current_joint2, 0]
self.verbose = verbose
# here we listen to a specific response from the server, in this case jointUpdate
@self.sio.on('jointUpdate')
def on_message(data):
# so in this case we just print that the joint was updated
if self.verbose:
print('Joint updated: ', data)
print('>', end="")
# when we get a jointStart, we update the jointAngles
@self.sio.on('jointStart')
def on_start(data):
self.jointAngles = json.loads(data)
if self.verbose:
print('All joints updated: ', self.jointAngles)
print('>', end="")
# Here we connect to the server
self.sio.connect('http://hannesarni.com:3100')
# we poll the server for the current angle positions
self.sio.emit('start', '')
def move_joint1(self, degrees, velocity=500):
self.jointAngles[0] = degrees
self.sio.emit('jointUpdate', json.dumps({
"jointIndex": 0,
"degs": degrees,
"vel": velocity
}))
def move_joint2(self, degrees, velocity=500):
self.jointAngles[1] = degrees
self.sio.emit('jointUpdate', json.dumps({
"jointIndex": 1,
"degs": degrees,
"vel": velocity
}))