WUT Velma robot API
test_lwr_workspace.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 # Inverse kinematics for KUKA LWR 4+ robot
4 # Copyright (c) 2021, Robot Control and Pattern Recognition Group,
5 # Institute of Control and Computation Engineering
6 # Warsaw University of Technology
7 #
8 # All rights reserved.
9 #
10 # Redistribution and use in source and binary forms, with or without
11 # modification, are permitted provided that the following conditions are met:
12 # * Redistributions of source code must retain the above copyright
13 # notice, this list of conditions and the following disclaimer.
14 # * Redistributions in binary form must reproduce the above copyright
15 # notice, this list of conditions and the following disclaimer in the
16 # documentation and/or other materials provided with the distribution.
17 # * Neither the name of the Warsaw University of Technology nor the
18 # names of its contributors may be used to endorse or promote products
19 # derived from this software without specific prior written permission.
20 #
21 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
22 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 # DISCLAIMED. IN NO EVENT SHALL <COPYright HOLDER> BE LIABLE FOR ANY
25 # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
28 # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30 # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #
32 # Author: Dawid Seredynski
33 #
34 
35 import rospy
36 import rospkg
37 import copy
38 import PyKDL
39 import math
40 import numpy as np
41 
42 from visualization_msgs.msg import *
43 from sensor_msgs.msg import JointState
44 
46 from velma_kinematics.velma_workspace import LWRWorkspace
47 
49  arm_name = 'right'
50  arm_name = 'left'
51 
52  m_pub = MarkerPublisher('lwr_workspace')
53  js_pub = rospy.Publisher('/joint_states', JointState, queue_size=1000)
54  rospy.sleep(0.5)
55 
56  rospack = rospkg.RosPack()
57  ws_data_path = rospack.get_path('velma_kinematics') + '/data/workspace/'
58  ws_param_filename = ws_data_path + 'lwr_ws_param.npy'
59  ws_filename = ws_data_path + 'lwr_ws.npy'
60 
61  js_msg = JointState()
62  for i in range(7):
63  js_msg.name.append('{}_arm_{}_joint'.format(arm_name, i))
64  js_msg.position.append(0.0)
65  js_msg.name.append('torso_0_joint')
66  js_msg.position.append(0.0)
67 
68  generate = False
69  if generate:
70  ws = LWRWorkspace.generate()
71  ws.save(ws_param_filename, ws_filename)
72  else:
73  ws = LWRWorkspace.load(ws_param_filename, ws_filename)
74  #ws = LWRWorkspace.load()
75 
76  x_range = list(range(ws.getCellsX()))
77  y_range = list(range(ws.getCellsY()))
78  z_range = list(range(ws.getCellsZ()))
79 
80  slice_coord = 'y'
81 
82  if slice_coord == 'x':
83  ic_cells = ws.getCellsX()
84  elif slice_coord == 'y':
85  ic_cells = ws.getCellsY()
86  elif slice_coord == 'z':
87  ic_cells = ws.getCellsZ()
88  ic = 0
89 
90  while not rospy.is_shutdown():
91  m_id = 0
92  if slice_coord == 'x':
93  x_range = [ic]
94  elif slice_coord == 'y':
95  y_range = [ic]
96  elif slice_coord == 'z':
97  z_range = [ic]
98 
99  for ix in x_range:
100  for iy in y_range:
101  for iz in z_range:
102  pt = PyKDL.Vector( ws.getCellCenterX(ix), ws.getCellCenterY(iy),
103  ws.getCellCenterZ(iz))
104  col = ws.getCellValue(ix, iy, iz)
105  #col = math.sqrt( ws.getCellValue(ix, iy, iz) )
106  size = ws.getCellSize()
107  m_id = m_pub.addSinglePointMarker(pt, m_id, r=col, g=col, b=col, a=1,
108  namespace='default', frame_id='calib_{}_arm_base_link'.format(arm_name),
109  m_type=Marker.CUBE, scale=Vector3(size, size, size), T=None)
110 
111  m_pub.publishAll()
112  ic = (ic+1)%ic_cells
113 
114  js_msg.header.stamp = rospy.Time.now()
115  js_pub.publish(js_msg)
116 
117  rospy.sleep(0.5)
118 
119  return 0
120 
121 def main():
122  rospy.init_node('test_lwr_workspace', anonymous=False)
123  return testLWRWorkspace()
124 
125 if __name__ == "__main__":
126  exit(main())
This class is an interface to ROS markers publisher.