Advertisement
Guest User

Untitled

a guest
Nov 15th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.26 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3.  
  4. import rospy
  5. from std_msgs.msg import String
  6. from std_msgs.msg import Float64
  7. from sensor_msgs.msg import LaserScan
  8. import numpy
  9. import time
  10. import math
  11.  
  12. # Used for localization
  13. import tf
  14. from tf import transformations
  15.  
  16. class DistanceTracker():
  17. def __init__(self):
  18. self.transformationListener = tf.TransformListener()
  19. self.transformationListener.waitForTransform("/odom", "/base_link", rospy.Time(), rospy.Duration(4.0))
  20. self.r = rospy.Rate(250) # 250hz
  21. self.startTime = time.time()
  22. self.maxDistance = 0
  23. self.maxPos = [0, 0]
  24. (self.startTrans, self.startRot) = self.getTranslation()
  25. self.start()
  26.  
  27. def getTranslation(self):
  28. (currentTranslation, currentRotation) = self.transformationListener.lookupTransform("/odom", "/base_link", rospy.Time(0))
  29. return (currentTranslation, currentRotation)
  30.  
  31. def start(self):
  32. print(self.startTrans, self.startRot)
  33. while not rospy.is_shutdown():
  34. try:
  35. pub = rospy.Publisher('/dist', Float64, queue_size=10) # Make new /dist topic to publish to
  36. curTime = time.time()
  37. elapsed = curTime - self.startTime
  38.  
  39. (curTrans, curRot) = self.getTranslation()
  40. dx = curTrans[0] - self.startTrans[0]
  41. dy = curTrans[1] - self.startTrans[1]
  42. distanceTraveled = math.sqrt(dx * dx + dy * dy)
  43.  
  44.  
  45. if (distanceTraveled > self.maxDistance):
  46. self.maxDistance = distanceTraveled
  47. self.maxPos = [curTrans[0], curTrans[1]]
  48.  
  49. pub.publish(distanceTraveled) # publish to the /dist topic which the /freeforce node will subscribe to
  50. rospy.loginfo("Time: %s, Distance traveled: %s, (%s, %s)", str(elapsed), str(self.maxDistance), str(self.maxPos[0]), str(self.maxPos[1]))
  51.  
  52. except(tf.LookupException, tf.ConnectivityException):
  53. continue
  54. rospy.sleep(0.1)
  55.  
  56. def main():
  57. rospy.init_node('DistanceTracker')
  58. try:
  59. DistanceTracker()
  60. except rospy.ROSInterruptException:
  61. pass
  62.  
  63. if __name__ == '__main__':
  64. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement