Advertisement
DeaD_EyE

Weifang2::Shuttle

Oct 29th, 2019
369
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.48 KB | None | 0 0
  1. from __future__ import print_function
  2. from __future__ import unicode_literals
  3.  
  4. import csv
  5. from io import StringIO
  6. from itertools import cycle
  7. from collections import namedtuple
  8.  
  9. from vcScript import *
  10. import vcVector
  11. import vcMatrix
  12.  
  13.  
  14. INIT = False
  15.  
  16. def printVec(vec):
  17.   """
  18.  Usefull utility function for printing matrix value
  19.  """
  20.   fmt = "{:<6.3f}\t{:<6.3f}\t{:<6.3f}\t{:<6.3f}"
  21.   print(fmt.format(vec.X, vec.Y, vec.Z, vec.W))
  22.  
  23.  
  24. def printMatrix(mat):    
  25.   """
  26.  Usefull utility function for printing matrix value
  27.  """
  28.   print('  X\t  Y\t  Z\t  W')
  29.   for Vec in [mat.N, mat.O, mat.A, mat.P]:
  30.     printVec(Vec)
  31.  
  32.  
  33. class Action(object):
  34.     wp = 'WP'
  35.     stop = 'STOP'
  36.  
  37.  
  38. class vcShuttle(object):
  39.  
  40.   def __init__(self):
  41.     global INIT
  42.     vehicle = getComponent().findBehaviour('Vehicle')
  43.     vehicle.clearMove()
  44.     vehicle.detachAllWagons()
  45.     vehicle.Acceleration = 1000
  46.     vehicle.Deceleration = 1000
  47.     vehicle.MaxSpeed = 2000
  48.     vehicle.Interpolation = 0.13
  49.     if not INIT:
  50.       INIT = True
  51.       vehicle.OnFinalDestinationReached = self.target_reached
  52.     self.vehicle = vehicle
  53.     self.trigger = None
  54.  
  55.   def add(self, position):
  56.     self.vehicle.addControlPoint(position)
  57.  
  58.   def clear(self):
  59.     self.vehicle.clearMove()
  60.  
  61.   def target_reached(self, comp, duration):
  62.     print(comp.Name, 'reached in', round(duration, 1), 's')
  63.     if self.trigger:
  64.       print('Trigger:', self.trigger.Name)
  65.  
  66.   def trigger_target(self, comp_name, frame_name):
  67.     app = getApplication()
  68.     comp = app.findComponent(comp_name)
  69.     frame = comp.findFeature(frame_name)
  70.     station = app.findComponent(frame.Name)
  71.     self.trigger = station
  72.  
  73.  
  74. class Shuttle(object):
  75.   sid = 0
  76.  
  77.   @classmethod
  78.   def _enumerate(cls):
  79.     cls.sid += 10
  80.     return cls.sid
  81.  
  82.   def __init__(self, paths, sid=None):
  83.     self.sid = sid or self._enumerate()
  84.     self.paths = paths
  85.     self.vcShuttle = vcShuttle()
  86.     self.trigger()
  87.  
  88.   def trigger(self, *args):
  89.     self.vcShuttle.clear()
  90.     for position in self.paths:
  91.       self.vcShuttle.add(position)
  92.     last = self.paths.last
  93.     self.vcShuttle.trigger_target(*last)
  94.  
  95.  
  96. class Path(object):
  97.   def __init__(self, comp, frame, action):
  98.     self.comp_name = comp
  99.     self.frame_name = frame
  100.    
  101.     comp = getApplication().findComponent(comp)
  102.     frame = comp.getFeature(frame)
  103.     npm = frame.NodePositionMatrix
  104.     self.frame = frame
  105.     #self.position = wpm * frame.PositionMatrix.P
  106.     self.position = npm.P
  107.     self.action = action
  108.  
  109.  
  110. class Paths(object):
  111.   def __init__(self, paths, initial=0):
  112.     if not any([p.action == Action.stop for p in paths]):
  113.       last_path = paths[-1]
  114.       last_path.action = Action.stop
  115.       paths[-1] = last_path
  116.        
  117.     self._paths = iter(cycle(paths))
  118.     self._last = None
  119.     [next(self._paths) for _ in range(initial)]
  120.  
  121.   @classmethod
  122.   def from_csv(cls, input_string):
  123.     fake_fd = StringIO(input_string)
  124.     reader = csv.reader(fake_fd)
  125.     next(reader)
  126.     positions = []
  127.     for comp, frame, action in reader:
  128.       path = Path(comp, frame, action)
  129.       positions.append(path)
  130.     return cls(positions)
  131.  
  132.   def __iter__(self):
  133.     for path in self._paths:
  134.       yield path.position
  135.       self._last = (path.comp_name, path.frame_name)
  136.       if path.action == Action.stop:
  137.         break
  138.  
  139.   @property
  140.   def last(self):
  141.     return self._last
  142.  
  143.  
  144. def OnSignal(signal):
  145.   print('Got signal', signal.Name if signal else None)
  146.   if signal and signal.Value:
  147.     global_context['Shuttle'].trigger()
  148.  
  149.  
  150. def OnRun():
  151.   paths = Paths.from_csv(csv_data)
  152.   shuttle = Shuttle(paths)
  153.   global_context['Shuttle'] = shuttle
  154.   comp = getComponent()
  155.   start_button = comp.getProperty('Start')
  156.   start_signal = comp.getBehaviour('Start')
  157.   start_button.OnChanged = start_signal.signal
  158.  
  159.  
  160. global_context = {'Shuttle': None}
  161.  
  162. csv_data = """
  163. comp,frame,action
  164. Transfersystem,B60,WP
  165. Transfersystem,B61,WP
  166. Transfersystem,B62,WP
  167. Transfersystem,Station 10,STOP
  168. Transfersystem,Station 20,STOP
  169. Transfersystem,B1,WP
  170. Transfersystem,E1,WP
  171. Transfersystem,B2,WP
  172. Transfersystem,G1,WP
  173. Transfersystem,B3,WP
  174. Transfersystem,E2,WP
  175. Transfersystem,B4,WP
  176. Transfersystem,Station 30,STOP
  177. Transfersystem,Station 40,STOP
  178. Transfersystem,B5,WP
  179. Transfersystem,E3,WP
  180. Transfersystem,B6,WP
  181. Transfersystem,B7,WP
  182. Transfersystem,E4,WP
  183. Transfersystem,B8,WP
  184. Transfersystem,Station 50,STOP
  185. Transfersystem,Station 60,STOP
  186. Transfersystem,B9,WP
  187. Transfersystem,E5,WP
  188. Transfersystem,B51,WP
  189. Transfersystem,B52,WP
  190. Transfersystem,E6,WP
  191. Transfersystem,B10,WP
  192. Transfersystem,Station 70,WP
  193. Transfersystem,Station 80,WP
  194. Transfersystem,Station 80,WP
  195. Transfersystem,B20,WP
  196. Transfersystem,B21,WP
  197. Transfersystem,B22,WP
  198. Transfersystem,B23,WP
  199. Transfersystem,B24,WP
  200. Transfersystem,B25,WP
  201. Transfersystem,Station 90,STOP
  202. Transfersystem,B30,WP
  203. Transfersystem,B31,WP
  204. Transfersystem,B32,WP
  205. Transfersystem,B33,WP
  206. Transfersystem,B34,WP
  207. Transfersystem,B35,WP
  208. Transfersystem,Hochregallager 1,STOP
  209. Transfersystem,Hochregallager 2,STOP
  210. Transfersystem,B40,WP
  211. Transfersystem,B41,WP
  212. Transfersystem,B42,WP
  213. """.strip()
  214.  
  215. #def getPath(comp_name, owp_name):
  216. #  comp = getApplication().findComponent(comp_name)
  217. #  if comp:
  218. #    owp = comp.findBehaviour(owp_name)
  219. #    if owp:
  220. #      return owp.Path
  221. #  return []
  222.  
  223. #def getPositions(comp_name, owp_name):
  224. #  return [pos.PositionMatrix.P for pos in getPath(comp_name, owp_name)]
  225.  
  226. #pos = getPositions('Transfersystem', 'OneWayPath')
  227. #print(pos)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement