Herowaree

conv

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