Advertisement
Guest User

Untitled

a guest
Oct 1st, 2016
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.04 KB | None | 0 0
  1. #!/usr/bin/env python
  2. import roslib
  3. import rospy
  4. import math
  5. import sys, select, termios, tty
  6. import time
  7.  
  8. from std_msgs.msg import Int32
  9. from phidgets.msg import motor_encoder
  10.  
  11. millis = lambda: int(round(time.time() * 1000))
  12.  
  13. R = 0.0365
  14. D = 0.274
  15. N = 360.0
  16.  
  17. class EncoderDeadRec:
  18.     def __init__(self):
  19.  
  20.         self.firstrun = True
  21.         self.left_count_init = 0 # initial count of left wheel encoder
  22.         self.right_count_init = 0 # initial count of right wheel encoder
  23.  
  24.         self.left_count = 0
  25.         self.left_count_change = 0
  26.         self.right_count = 0
  27.         self.right_count_change = 0
  28.  
  29.         rospy.Subscriber("/left/encoder", motor_encoder, self.sub1callback)
  30.         rospy.Subscriber("/right/encoder", motor_encoder, self.sub2callback)
  31.  
  32.         self.calculate()
  33.  
  34.     def sub1callback(self, ros_data):
  35.         self.left_count = ros_data.count
  36.         self.left_count_change = ros_data.count_change
  37.  
  38.     def sub2callback(self, ros_data):
  39.         self.right_count = ros_data.count
  40.         self.right_count_change = ros_data.count_change
  41.  
  42.     def calculate(self):
  43.         rate = rospy.Rate(10) # 10hz
  44.  
  45.         while not rospy.is_shutdown():
  46.            
  47.             if self.firstrun == True:
  48.                 self.left_count_init = self.left_count
  49.                 self.right_count_init = self.right_count
  50.             self.firstrun == False
  51.             self.left_count -= self.left_count_init
  52.             self.right_count -= self.right_count_init
  53.  
  54.             eta_k_l = (self.left_count*2.0*math.pi/N)
  55.             eta_k_r = (self.right_count*2.0*math.pi/N)
  56.             a_k_l = R*eta_k_l
  57.             a_k_r = R*eta_k_r
  58.             a_k = (a_k_l + a_k_r)/2.0
  59.             DeltPhi_k = (a_k_l + a_k_r)/D
  60.             r_k = a_k/DeltPhi_k
  61.             taylor = 0.0
  62.             for i in range(2): # set depth of taylor expansion here
  63.                 taylor+= ( (-1**(i+2))*(DeltPhi_k**(2*i))/math.factorial(2*(i+1)) )
  64.             DeltLambda = math.sqrt((2*a_k**2)*taylor)
  65.  
  66.             print "\n"
  67.             print self.left_count
  68.             print self.left_count_change
  69.             print self.right_count
  70.             print self.right_count_change
  71.             print eta_k_r  
  72.  
  73.         rate.sleep()
  74.  
  75. if __name__=="__main__":
  76.     rospy.init_node('EncoderDeadRec')
  77.    
  78.  
  79.     try:
  80.         est = EncoderDeadRec()
  81.     except rospy.ROSInterruptException: pass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement