WUT Velma robot API
test_velma_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 VelmaWorkspace
47 
49  m_pub = MarkerPublisher('velma_workspace')
50  js_pub = rospy.Publisher('/joint_states', JointState, queue_size=1000)
51  rospy.sleep(0.5)
52 
53  rospack = rospkg.RosPack()
54  ws_data_path = rospack.get_path('velma_kinematics') + '/data/workspace/'
55  ws_param_filename = ws_data_path + 'velma_ws_param.npy'
56  ws_filename = ws_data_path + 'velma_ws.npy'
57 
58  arm_name = 'right'
59  js_msg = JointState()
60  for i in range(7):
61  js_msg.name.append('{}_arm_{}_joint'.format(arm_name, i))
62  js_msg.position.append(0.0)
63  js_msg.name.append('torso_0_joint')
64  js_msg.position.append(0.0)
65 
66  generate = False
67  if generate:
68  ws = VelmaWorkspace.generate()
69  ws.save(ws_param_filename, ws_filename)
70  else:
71  ws = VelmaWorkspace.load(ws_param_filename, ws_filename)
72  #ws = LWRWorkspace.load()
73 
74  x_range = list(range(ws.getCellsX()))
75  y_range = list(range(ws.getCellsY()))
76  z_range = list(range(ws.getCellsZ()))
77 
78  slice_coord = 'y'
79 
80  if slice_coord == 'x':
81  ic_cells = ws.getCellsX()
82  elif slice_coord == 'y':
83  ic_cells = ws.getCellsY()
84  elif slice_coord == 'z':
85  ic_cells = ws.getCellsZ()
86  ic = 0
87 
88  while not rospy.is_shutdown():
89  m_id = 0
90  if slice_coord == 'x':
91  x_range = [ic]
92  elif slice_coord == 'y':
93  y_range = [ic]
94  elif slice_coord == 'z':
95  z_range = [ic]
96 
97  for ix in x_range:
98  for iy in y_range:
99  for iz in z_range:
100  pt = PyKDL.Vector( ws.getCellCenterX(ix), ws.getCellCenterY(iy),
101  ws.getCellCenterZ(iz))
102  col = ws.getCellValue(ix, iy, iz)
103  #col = math.sqrt( ws.getCellValue(ix, iy, iz) )
104  size = ws.getCellSize()
105  m_id = m_pub.addSinglePointMarker(pt, m_id, r=col, g=col, b=col, a=1,
106  namespace='default', frame_id='torso_base',
107  m_type=Marker.CUBE, scale=Vector3(size, size, size), T=None)
108 
109  m_pub.publishAll()
110  ic = (ic+1)%ic_cells
111 
112  js_msg.header.stamp = rospy.Time.now()
113  js_pub.publish(js_msg)
114 
115  rospy.sleep(0.5)
116 
117  return 0
118 
119 def main():
120  rospy.init_node('test_velma_workspace', anonymous=False)
121  return testVelmaWorkspace()
122 
123 if __name__ == "__main__":
124  exit(main())
This class is an interface to ROS markers publisher.