Advertisement
softwarety6

Untitled

Dec 24th, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.06 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. import rospy
  4. from geometry_msgs.msg import Twist
  5. import math
  6.  
  7. def foo(new_vel, old_vel, step):
  8.     if new_vel > old_vel + step:
  9.     return old_vel + step
  10.     elif new_vel < old_vel - step:
  11.     return old_vel - step
  12.     else:
  13.     return new_vel
  14.  
  15. class Smoother():
  16.     def __init__(self, lin_step = 0.01, ang_step = 0.1):
  17.     rospy.init_node('vel_smoother', anonymous=True)
  18.  
  19.     self.last_twist = Twist()
  20.     self.last_twist.linear.y = 0.0
  21.     self.last_twist.linear.z = 0.0
  22.     self.last_twist.angular.x = 0.0
  23.     self.last_twist.angular.y = 0.0
  24.  
  25.     self.LIN_STEP = lin_step
  26.     self.ANG_STEP = ang_step
  27.  
  28.     rospy.Subscriber("cmd_vel", Twist, self.smooth)
  29.     self.pub = rospy.Publisher("cmd_vel_sm", Twist, queue_size=100)
  30.  
  31.     while not rospy.is_shutdown():
  32.         rospy.spin()
  33.  
  34.     def smooth(self, data):
  35.     self.last_twist.linear.x = foo(data.linear.x, self.last_twist.linear.x, self.LIN_STEP)
  36.     self.last_twist.angular.z = foo(data.angular.z, self.last_twist.angular.z, self.ANG_STEP)
  37.  
  38.     self.pub.publish(self.last_twist)
  39.  
  40. if __name__ == '__main__':
  41.     s = Smoother()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement