multi_robot.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 # ##
4 # @brief [py demo] muliti robot sync test
5 # @author Jin Hyuk Gong (jinhyuk.gong@doosan.com)
6 
7 import rospy
8 import os
9 import threading, time
10 import sys
11 sys.dont_write_bytecode = True
12 sys.path.append( os.path.abspath(os.path.join(os.path.dirname(__file__),"../../../../common/imp")) ) # get import pass : DSR_ROBOT.py
13 
14 
15 # for single robot
16 #import DR_init
17 #DR_init.__dsr__id = "dsr01"
18 #DR_init.__dsr__model = "m1013"
19 #from DSR_ROBOT import *
20 
21 # for mulit robot
22 ########from DSR_ROBOT_MULTI import *
23 from DSR_ROBOT import *
24 
25 NUM_ROBOT = 2
26 
27 ############################################################################################
28 class CRobotSync:
29  def __init__(self, r):
30  self.description = "Sync for Multiple Robos"
31  self.author = "Doosan Robotics"
32  self.nRobot = r
33  self.nIsRun = True
34 
35  self.nWaitBit = 0
36  self.nCurBit = 0
37 
38  self.bIsWait = list()
39  self.lock = list()
40 
41  for i in range(r):
42  self.lock.append( threading.Lock() )
43  self.bIsWait.append(False)
44  self.nWaitBit |= 0x1<<i
45 
46  def CleanUp(self):
47  if True == self.nIsRun:
48  self.nIsRun = False
49  print("~CleanUp()")
50 
51  def Wait(self, r):
52  self.bIsWait[r] = True
53  self.lock[r].acquire()
54  self.bIsWait[r] = False
55  return 0
56 
57  def WakeUp(self, r):
58  while self.nIsRun:
59  if(True == self.bIsWait[r]):
60  self.lock[r].release()
61  break
62  else:
63  time.sleep(0.01)
64  return 0
65 
66  def WakeUpAll(self):
67  self.nCurBit = 0
68  while self.nIsRun:
69  for i in range(self.nRobot):
70  if(True == self.bIsWait[i]):
71  self.nCurBit |= 0x1<<i;
72  if(self.nWaitBit == self.nCurBit):
73  break;
74  for i in range(self.nRobot):
75  self.lock[i].release()
76  return 0
77 
78 RobotSync = CRobotSync(NUM_ROBOT)
79 
80 #######################################################################################
81 
82 def shutdown():
83 
84  print "shutdown time!"
85  print "shutdown time!"
86  print "shutdown time!"
87 
88  pub_stop_r1.publish(stop_mode=STOP_TYPE_QUICK)
89  pub_stop_r2.publish(stop_mode=STOP_TYPE_QUICK)
90 
91  return 0
92 
94  msgRobotState_cb_r1.count += 1
95 
96  if (0==(msgRobotState_cb_r1.count % 100)):
97  rospy.loginfo("________ ROBOT[1] STATUS ________")
98  print(" robot_state : %d" % (msg.robot_state))
99  print(" robot_state_str : %s" % (msg.robot_state_str))
100  print(" current_posj : %7.3f %7.3f %7.3f %7.3f %7.3f %7.3f" % (msg.current_posj[0],msg.current_posj[1],msg.current_posj[2],msg.current_posj[3],msg.current_posj[4],msg.current_posj[5]))
101 
102  #print(" io_control_box : %d" % (msg.io_control_box))
103  ##print(" io_modbus : %d" % (msg.io_modbus))
104  ##print(" error : %d" % (msg.error))
105  #print(" access_control : %d" % (msg.access_control))
106  #print(" homming_completed : %d" % (msg.homming_completed))
107  #print(" tp_initialized : %d" % (msg.tp_initialized))
108  print(" speed : %d" % (msg.speed))
109  #print(" mastering_need : %d" % (msg.mastering_need))
110  #print(" drl_stopped : %d" % (msg.drl_stopped))
111  #print(" disconnected : %d" % (msg.disconnected))
112 msgRobotState_cb_r1.count = 0
113 
115  msgRobotState_cb_r2.count += 1
116 
117  if (0==(msgRobotState_cb_r2.count % 100)):
118  rospy.loginfo("________ ROBOT[2] STATUS ________")
119  print(" robot_state : %d" % (msg.robot_state))
120  print(" robot_state_str : %s" % (msg.robot_state_str))
121  print(" current_posj : %7.3f %7.3f %7.3f %7.3f %7.3f %7.3f" % (msg.current_posj[0],msg.current_posj[1],msg.current_posj[2],msg.current_posj[3],msg.current_posj[4],msg.current_posj[5]))
122 
123  #print(" io_control_box : %d" % (msg.io_control_box))
124  ##print(" io_modbus : %d" % (msg.io_modbus))
125  ##print(" error : %d" % (msg.error))
126  #print(" access_control : %d" % (msg.access_control))
127  #print(" homming_completed : %d" % (msg.homming_completed))
128  #print(" tp_initialized : %d" % (msg.tp_initialized))
129  print(" speed : %d" % (msg.speed))
130  #print(" mastering_need : %d" % (msg.mastering_need))
131  #print(" drl_stopped : %d" % (msg.drl_stopped))
132  #print(" disconnected : %d" % (msg.disconnected))
133 msgRobotState_cb_r2.count = 0
134 
135 def thread_subscriber_r1(robot_id, robot_model):
136  rospy.Subscriber('/'+ robot_id + robot_model +'/state', RobotState, msgRobotState_cb_r1)
137  rospy.spin()
138  #rospy.spinner(2)
139 
140 def thread_subscriber_r2(robot_id, robot_model):
141  rospy.Subscriber('/'+ robot_id + robot_model +'/state', RobotState, msgRobotState_cb_r2)
142  rospy.spin()
143  #rospy.spinner(2)
144 
145 
146 #----------------------------------------------------------------------
147 JReady = posj(0, -20, 110, 0, 60, 0)
148 J00 = posj(-180, 0, -145, 0, -35, 0)
149 J01r = posj(-180.0, 71.4, -145.0, 0.0, -9.7, 0.0)
150 J02r = posj(-180.0, 67.7, -144.0, 0.0, 76.3, 0.0)
151 J03r = posj(-180.0, 0.0, 0.0, 0.0, 0.0, 0.0)
152 J04r = posj(-90.0, 0.0, 0.0, 0.0, 0.0, 0.0)
153 J04r1 = posj(-90.0, 30.0, -60.0, 0.0, 30.0, -0.0)
154 J04r2 = posj(-90.0, -45.0, 90.0, 0.0, -45.0, -0.0)
155 J04r3 = posj(-90.0, 60.0, -120.0, 0.0, 60.0, -0.0)
156 J04r4 = posj(-90.0, 0.0, -0.0, 0.0, 0.0, -0.0)
157 J05r = posj(-144.0, -4.0, -84.8, -90.9, 54.0, -1.1)
158 J07r = posj(-152.4, 12.4, -78.6, 18.7, -68.3, -37.7)
159 J08r = posj(-90.0, 30.0, -120.0, -90.0, -90.0, 0.0)
160 JEnd = posj(0.0, -12.6, 101.1, 0.0, 91.5, -0.0)
161 dREL1 = posx(0, 0, 350, 0, 0, 0)
162 dREL2 = posx(0, 0, -350, 0, 0, 0)
163 velx = [0, 0]
164 accx = [0, 0]
165 vel_spi = [400, 400]
166 acc_spi = [150, 150]
167 J1 = posj(81.2, 20.8, 127.8, 162.5, 56.1, -37.1)
168 X0 = posx(-88.7, 799.0, 182.3, 95.7, 93.7, 133.9)
169 X1 = posx(304.2, 871.8, 141.5, 99.5, 84.9, 133.4)
170 X2 = posx(437.1, 876.9, 362.1, 99.6, 84.0, 132.1)
171 X3 = posx(-57.9, 782.4, 478.4, 99.6, 84.0, 132.1)
172 amp = [0, 0, 0, 30, 30, 0]
173 period = [0, 0, 0, 3, 6, 0]
174 x01 = [423.6, 334.5, 651.2, 84.7, -180.0, 84.7]
175 x02 = [423.6, 34.5, 951.2, 68.2, -180.0, 68.2]
176 x03 = [423.6, -265.5, 651.2, 76.1, -180.0, 76.1]
177 x04 = [423.6, 34.5, 351.2, 81.3, -180.0, 81.3]
178 
179 
180 def thread_robot1(robot_id, robot_model):
181  try:
182  nRobotID = 0
183  r = CDsrRobot(robot_id, robot_model)
184 
185  while not rospy.is_shutdown():
186 
187  RobotSync.Wait(nRobotID)
188  r.movej(JReady, 20, 20)
189 
190  #RobotSync.Wait(nRobotID)
191  #r.config_create_tcp("TCP_ZERO", TCP_POS)
192 
193  #RobotSync.Wait(nRobotID)
194  #r.set_current_tcp("TCP_ZERO")
195 
196  RobotSync.Wait(nRobotID)
197  r.movej(J1, 20, 20, 0)
198 
199  RobotSync.Wait(nRobotID)
200  r.movel(X3, velx, accx, 2.5)
201 
202  except Exception as err:
203  RobotSync.CleanUp()
204  rospy.loginfo("Runtime Exception : %s" % err)
205  return 0
206 
207 def thread_robot2(robot_id, robot_model):
208  try:
209  nRobotID = 1
210  r = CDsrRobot(robot_id, robot_model)
211 
212  while not rospy.is_shutdown():
213 
214  RobotSync.Wait(nRobotID)
215  r.movej(JReady, 20, 20)
216 
217  #RobotSync.Wait(nRobotID)
218  #r.config_create_tcp("TCP_ZERO", TCP_POS)
219 
220  #RobotSync.Wait(nRobotID)
221  #r.set_current_tcp("TCP_ZERO")
222 
223  RobotSync.Wait(nRobotID)
224  r.movej(J1, 20, 20, 0)
225 
226  RobotSync.Wait(nRobotID)
227  r.movel(X3, velx, accx, 2.5)
228 
229  except Exception as err:
230  RobotSync.CleanUp()
231  rospy.loginfo("Runtime Exception : %s" % err)
232  return 0
233 
234 if __name__ == "__main__":
235  rospy.init_node('m1013x2_sync_py')
236  rospy.on_shutdown(shutdown)
237 
238  robot_id1 = "dsr01"; robot_model1 = "m1013"
239  robot_id2 = "dsr02"; robot_model2 = "m1013"
240 
241  pub_stop_r1 = rospy.Publisher('/'+ robot_id1 + robot_model1 +'/stop', RobotStop, queue_size=10)
242  pub_stop_r2 = rospy.Publisher('/'+ robot_id2 + robot_model2 +'/stop', RobotStop, queue_size=10)
243 
244  '''
245  t1 = threading.Thread(target=thread_subscriber_r1, args=(robot_id1, robot_model1))
246  t1.daemon = True
247  t1.start()
248 
249  t2 = threading.Thread(target=thread_subscriber_r2, args=(robot_id2, robot_model2))
250  t2.daemon = True
251  t2.start()
252  '''
253  #RobotSync = CRobotSync(NUM_ROBOT)
254 
255  t1 = threading.Thread(target=thread_robot1, args=(robot_id1, robot_model1))
256  t1.daemon = True
257  t1.start()
258 
259  t2 = threading.Thread(target=thread_robot2, args=(robot_id2, robot_model2))
260  t2.daemon = True
261  t2.start()
262 
263  time.sleep(1)
264 
265  while not rospy.is_shutdown():
266  time.sleep(0.01)
267  RobotSync.WakeUpAll()
268 
269  #----------------------------------------------------------------------
270 
271  print 'good bye!'
def WakeUp(self, r)
Definition: multi_robot.py:57
def thread_robot2(robot_id, robot_model)
Definition: multi_robot.py:207
def thread_subscriber_r1(robot_id, robot_model)
Definition: multi_robot.py:135
def msgRobotState_cb_r2(msg)
Definition: multi_robot.py:114
def thread_robot1(robot_id, robot_model)
Definition: multi_robot.py:180
def __init__(self, r)
Definition: multi_robot.py:29
def thread_subscriber_r2(robot_id, robot_model)
Definition: multi_robot.py:140
def shutdown()
Definition: multi_robot.py:82
def msgRobotState_cb_r1(msg)
Definition: multi_robot.py:93


py
Author(s):
autogenerated on Sat May 18 2019 02:32:56