Herowaree

turtle_math

Dec 10th, 2019
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.48 KB | None | 0 0
  1. #! /usr/bin/env python
  2.  
  3. import math
  4.  
  5. # Module turtle_math
  6.  
  7. # Convert a theta value in radians to degrees
  8. def thetaRad2Deg(theta_rad):
  9. if theta_rad < 0.0:
  10. theta_deg = 180.0 + 180.0 * (math.pi + theta_rad) / math.pi
  11. else:
  12. theta_deg = 180.0 * theta_rad / math.pi
  13.  
  14. return theta_deg
  15.  
  16. # Convert a theta value in degrees to radians
  17. def thetaDeg2Rad(theta_deg):
  18. if theta_deg > 180.0:
  19. theta_rad = (theta_deg - 180.0) * math.pi / 180.0 - math.pi
  20. else:
  21. theta_rad = theta_deg * math.pi / 180.0
  22.  
  23. return theta_rad
  24.  
  25. # Sum between two theta values in radians
  26. def getThetaSum(theta1, theta2):
  27. theta = (thetaRad2Deg(theta1) + thetaRad2Deg(theta2)) % 360
  28.  
  29. return thetaDeg2Rad(theta)
  30.  
  31. def getThetaDegDiff(thetaRbt, thetaGoal):
  32. d = abs(thetaRbt - thetaGoal) % 360
  33. if d > 180:
  34. r = 360 - d
  35. else:
  36. r = d
  37.  
  38. #calculate sign
  39. if (thetaRbt - thetaGoal >= 0 and thetaRbt - thetaGoal <= 180) or (thetaRbt - thetaGoal <=-180 and thetaRbt - thetaGoal >= -360):
  40. sign = 1
  41. else:
  42. sign = -1
  43.  
  44. r *= sign
  45.  
  46. return r
  47.  
  48. def getDiffThetaObj2Robot(rbt_x, rbt_y, rbt_theta, obj_x, obj_y, obj_z):
  49. if obj_z != -1:
  50. x = obj_x - rbt_x
  51. y = obj_y - rbt_y
  52.  
  53. thetaObj = math.atan2(y, x)
  54.  
  55. diff = getThetaDegDiff(thetaRad2Deg(rbt_theta), thetaRad2Deg(thetaObj))
  56.  
  57. return diff
  58. else:
  59. return 0.0
Advertisement
Add Comment
Please, Sign In to add comment