Advertisement
130s

sample_ROS_PolygonStamped.py

Jun 11th, 2012
393
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.51 KB | None | 0 0
  1. #!/usr/bin/env python
  2. '''
  3. ROS' test code to see how geometry_msgs/PolygonStamped works. See http://answers.ros.org/question/36009/better-msg-implementation-for-random-complicated/ for the related discussion.
  4. @author: Isaac Saito <130s _a_t_ lateeye.net>
  5.  
  6. Btw, this code doesn't work as I intended; I want it to draw a square...instead, it shows skewed 2D polygon. Ironically this defect fulfils my purpose (i.e. to mismatch the start & end point 8) ).
  7. '''
  8.  
  9. import roslib; roslib.load_manifest('ros_provingground')
  10. import rospy
  11. from geometry_msgs.msg import Point32
  12. from geometry_msgs.msg import PolygonStamped
  13.  
  14. def pubPolygon():
  15. pub = rospy.Publisher('dotOnLine', PolygonStamped)
  16. rospy.init_node('polygonTalker')
  17. size_pts = 100
  18. #size_pts = 20
  19. i = j = 0
  20. init_x = 10.0
  21. init_y = 10.0
  22. init_z = 2.0
  23.  
  24. inc_x = 0.001
  25. #inc_y = 0.005
  26. inc_y = 0.001
  27. x_curr = init_x
  28. y_curr = init_y
  29.  
  30. while not rospy.is_shutdown():
  31. polst = PolygonStamped()
  32. polst.header.frame_id = '/frame_polygonTalker'
  33. #print 'polst len=', len(polst.polygon.points)
  34. pArr = []
  35. for i in range(size_pts):
  36. p = Point32()
  37. if i < (size_pts * 1/4):
  38. p.x = x_curr + inc_x * i
  39. p.y = y_curr + inc_y * i
  40. elif (size_pts * 1/4) <= i and i < (size_pts * 2/4):
  41. p.x = x_curr + inc_x * i
  42. p.y = y_curr - inc_y * i
  43. elif (size_pts * 2/4) <= i and i < (size_pts * 3/4):
  44. p.x = x_curr - inc_x * i
  45. p.y = y_curr - inc_y * i
  46. elif (size_pts * 3/4) <= i and i < size_pts:
  47. # p.x = x_curr - inc_x * (i - 0.5) # Trying to mismatch the ending point with starting point.
  48. # p.y = y_curr + inc_y * (i - 0.5) # Trying to mismatch the ending point with starting point.
  49. p.x = x_curr - inc_x * i
  50. p.y = y_curr + inc_y * i
  51. x_curr = p.x
  52. y_curr = p.y
  53. p.z = init_z
  54. pArr.append(p)
  55. i = i + 1
  56. print 'j=', j, 'i = ', i, ' x=', p.x, ' y=', p.y
  57. #str = "hello world %s"%rospy.get_time()
  58. #rospy.loginfo(str)
  59. polst.polygon.points = pArr
  60. pub.publish(polst)
  61. rospy.sleep(1.0)
  62. j = j + 1
  63. x_curr = x_curr + 0.001
  64. y_curr = y_curr + 0.001
  65.  
  66. if __name__ == '__main__':
  67. try:
  68. pubPolygon()
  69. except rospy.ROSInterruptException: pass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement