Skip to content

Commit 88a1211

Browse files
committed
Merge branch 'swift'
2 parents 168756d + bb62d04 commit 88a1211

File tree

4 files changed

+155
-36
lines changed

4 files changed

+155
-36
lines changed

examples/mmc.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import numpy as np
99
import qpsolvers as qp
1010

11+
import time
12+
1113
# Launch the simulator Swift
1214
env = rtb.backend.Swift()
1315
env.launch()

examples/swifty.py

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
2+
#!/usr/bin/env python
3+
"""
4+
@author Jesse Haviland
5+
"""
6+
7+
import roboticstoolbox as rtb
8+
import spatialmath as sm
9+
import numpy as np
10+
import qpsolvers as qp
11+
12+
# Launch the simulator Swift
13+
env = rtb.backend.Swift()
14+
env.launch()
15+
16+
# Create a Panda robot object
17+
panda = rtb.models.Panda()
18+
19+
# Set joint angles to ready configuration
20+
panda.q = panda.qr
21+
22+
# Add the Panda to the simulator
23+
env.add(panda)
24+
25+
26+
while 1:
27+
pass
28+
29+
30+
31+
32+
33+
34+
35+
36+
37+
38+
# import webbrowser as wb
39+
# import asyncio
40+
# import datetime
41+
# import random
42+
# import websockets
43+
# import threading
44+
# import time
45+
# from queue import Queue
46+
47+
48+
# class Socket:
49+
50+
# def __init__(self):
51+
# print('Server Started')
52+
# self.loop = asyncio.new_event_loop()
53+
# asyncio.set_event_loop(self.loop)
54+
55+
# start_server = websockets.serve(self.serve, "localhost", 8997)
56+
57+
# self.loop.run_until_complete(start_server)
58+
# self.loop.run_forever()
59+
60+
# async def serve(self, websocket, path):
61+
# # while True:
62+
# # message = await self.producer()
63+
# await websocket.send('51111')
64+
# self.loop.stop()
65+
66+
# # async def producer(self):
67+
# # data = self.q.get()
68+
# # # await asyncio.sleep(1)
69+
# # # now = datetime.datetime.utcnow().isoformat() + "Z"
70+
71+
# # return data
72+
73+
74+
# # q = Queue()
75+
# # # x = threading.Thread(target=Socket, args=(q, ), daemon=True)
76+
# # # x.start()
77+
# Socket()
78+
79+
80+
# while 1:
81+
# time.sleep(1)
82+
# print('hello')
83+
# # q.put('hi')
84+
85+
86+
# wb.open_new_tab('http://www.google.com')
87+
88+
# def send(data):
89+
90+
91+
92+
# panda = rp.models.Panda()
93+
# panda.q = panda.qr
94+
95+
# Tep = panda.fkine() * sm.SE3.Tx(-0.2) * sm.SE3.Ty(0.2) * sm.SE3.Tz(0.2)
96+
97+
# arrived = False
98+
# # env.add(panda)
99+
100+
# dt = 0.05
101+
102+
# while not arrived:
103+
104+
# v, arrived = rp.p_servo(panda.fkine(), Tep, 1)
105+
# panda.qd = np.linalg.pinv(panda.jacobe()) @ v
106+
# time.sleep(1)
107+
108+
# env.step(50)

roboticstoolbox/backend/Swift/Swift.py

Lines changed: 41 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,13 @@
33
@author Jesse Haviland
44
"""
55

6-
import os
7-
from subprocess import call, Popen
86
from roboticstoolbox.backend.Connector import Connector
9-
import zerorpc
107
import roboticstoolbox as rp
118
import numpy as np
129
import spatialmath as sm
1310
import time
11+
from queue import Queue
12+
from swift import start_servers
1413

1514

1615
class Swift(Connector): # pragma nocover
@@ -43,7 +42,10 @@ class Swift(Connector): # pragma nocover
4342
def __init__(self):
4443
super(Swift, self).__init__()
4544

46-
# Popen(['npm', 'start', '--prefix', os.environ['SIM_ROOT']])
45+
self.robots = []
46+
self.shapes = []
47+
self.outq = Queue()
48+
self.inq = Queue()
4749

4850
#
4951
# Basic methods to do with the state of the external program
@@ -60,11 +62,7 @@ def launch(self):
6062

6163
super().launch()
6264

63-
self.robots = []
64-
self.shapes = []
65-
66-
self.swift = zerorpc.Client()
67-
self.swift.connect("tcp://127.0.0.1:4242")
65+
start_servers(self.outq, self.inq)
6866

6967
def step(self, dt=50):
7068
"""
@@ -81,9 +79,9 @@ def step(self, dt=50):
8179
- Each robot in the scene is updated based on
8280
their control type (position, velocity, acceleration, or torque).
8381
- Upon acting, the other three of the four control types will be
84-
updated in the internal state of the robot object.
85-
- The control type is defined by the robot object, and not all robot
86-
objects support all control types.
82+
updated in the internal state of the robot object.
83+
- The control type is defined by the robot object, and not all
84+
robot objects support all control types.
8785
- Execution is blocked for the specified interval
8886
8987
"""
@@ -149,17 +147,18 @@ def add(self, ob, show_robot=True, show_collision=False):
149147
:return: object id within visualizer
150148
:rtype: int
151149
152-
``id = env.add(robot)`` adds the ``robot`` to the graphical environment.
150+
``id = env.add(robot)`` adds the ``robot`` to the graphical
151+
environment.
153152
154153
.. note::
155154
156155
- Adds the robot object to a list of robots which will be updated
157156
when the ``step()`` method is called.
158157
159158
"""
160-
# id = add(robot) adds the robot to the external environment. robot must
161-
# be of an appropriate class. This adds a robot object to a list of
162-
# robots which will act upon the step() method being called.
159+
# id = add(robot) adds the robot to the external environment. robot
160+
# must be of an appropriate class. This adds a robot object to a
161+
# list of robots which will act upon the step() method being called.
163162

164163
# TODO can add more than a robot right?
165164

@@ -169,26 +168,28 @@ def add(self, ob, show_robot=True, show_collision=False):
169168
robot = ob.to_dict()
170169
robot['show_robot'] = show_robot
171170
robot['show_collision'] = show_collision
172-
id = self.swift.robot(robot)
171+
id = self._send_socket('robot', robot)
173172

174-
loaded = False
175-
while not loaded:
176-
loaded = self.swift.is_loaded(id)
173+
loaded = 0
174+
while loaded == 0:
175+
loaded = int(self._send_socket('is_loaded', id))
177176
time.sleep(0.1)
178177

179178
self.robots.append(ob)
180179
return id
181-
elif isinstance(ob, rp.Shape):
182-
shape = ob.to_dict()
183-
id = self.swift.shape(shape)
184-
self.shapes.append(ob)
185-
return id
180+
# elif isinstance(ob, rp.Shape):
181+
# shape = ob.to_dict()
182+
# id = self.swift.shape(shape)
183+
# id = self._send_socket('shape', shape)
184+
# self.shapes.append(ob)
185+
# return id
186186

187187
def remove(self):
188188
"""
189189
Remove a robot to the graphical scene
190190
191-
``env.remove(robot)`` removes the ``robot`` from the graphical environment.
191+
``env.remove(robot)`` removes the ``robot`` from the graphical
192+
environment.
192193
"""
193194

194195
# TODO - shouldn't this have an id argument? which robot does it remove
@@ -239,13 +240,21 @@ def _draw_all(self):
239240

240241
for i in range(len(self.robots)):
241242
self.robots[i].fkine_all()
242-
self.swift.robot_poses([i, self.robots[i].fk_dict()])
243+
# self.swift.robot_poses([i, self.robots[i].fk_dict()])
244+
self._send_socket('robot_poses', [i, self.robots[i].fk_dict()])
243245

244246
for i in range(len(self.shapes)):
245-
self.swift.shape_poses([i, self.shapes[i].fk_dict()])
247+
# self.swift.shape_poses([i, self.shapes[i].fk_dict()])
248+
self._send_socket('shape_poses', [i, self.shapes[i].fk_dict()])
249+
250+
# def record_start(self, file):
251+
# self.swift.record_start(file)
252+
253+
# def record_stop(self):
254+
# self.swift.record_stop(1)
246255

247-
def record_start(self, file):
248-
self.swift.record_start(file)
256+
def _send_socket(self, code, data):
257+
msg = [code, data]
249258

250-
def record_stop(self):
251-
self.swift.record_stop(1)
259+
self.outq.put(msg)
260+
return self.inq.get()

roboticstoolbox/backend/__init__.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
from roboticstoolbox.backend.PyPlot import *
22
from roboticstoolbox.backend.urdf import *
33

4-
try:
5-
from roboticstoolbox.backend.Swift import *
6-
except ImportError:
7-
pass
4+
# try:
5+
from roboticstoolbox.backend.Swift import *
6+
# except ImportError:
7+
# pass
88

99
try:
1010
from roboticstoolbox.backend.VPython import *

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy