Advertisement
chrisCNG

robotArmInvKin

Nov 11th, 2019
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.60 KB | None | 0 0
  1. #Inverse Kinematics v3 for 2 joint robot arm by chris garrard
  2. #arm lengths a1, a2
  3. #takes x,y coordinates of destination object
  4. #and incrementally changes the arm angles to reach it
  5. # outputs the arm angle values for the way points
  6.  
  7. #import libraries
  8. import math
  9. import time
  10. import pygame
  11. pygame.init()
  12.  
  13. # define event handlers for simulation
  14. def draw(mainDisplay):
  15.     mainDisplay.fill(blue)
  16.     global x1, y1, xE, yE
  17.     pygame.draw.circle(mainDisplay, ball_color, ball_pos, Ball_Radius)
  18.     pygame.draw.line(mainDisplay, green, (xO,yO), (x1,y1), 10)
  19.     pygame.draw.line(mainDisplay, red, (x1,y1), (xE,yE), 10)
  20.  
  21. def plot(q1, q2):
  22.  
  23.     global x1, y1, xE, yE
  24.     x1 = a1 * math.cos(q1)
  25.     y1 = a1 * math.sin(q1)
  26.     theta = q2 + q1
  27.     xE = x1 + a2 *  math.cos(theta) + xO
  28.     yE = y1 + a2 * math.sin(theta) + yO
  29.     x1 = x1 + xO
  30.     y1 = y1 + yO
  31.  
  32. # Intitialize Global Variables
  33. Width = 500
  34. Height = 500
  35.  
  36. white = (240,240,255)
  37. black = (40,40,40)
  38. red = (212,64,28)
  39. green = (40,181,73)
  40. blue = (21,59,176)
  41. xEnd = 0
  42. yEnd = 0
  43. xO = 250
  44. yO = 250
  45. a1 = 80
  46. a2 = 80
  47. wayPt1 = []
  48. wayPt2 = []
  49. Ball_Radius = 5
  50. ball_color = white
  51. q1 = 0
  52. q2 = 0
  53. q3 = 0
  54. q4 = 0
  55. xE = 0
  56. yE = 0
  57. theta = 0
  58. q2Start = math.pi / 2
  59. q1Start = 0
  60.  
  61. # input destination with reach checking
  62. while (xEnd**2 + yEnd**2) > (a1 + a2)**2 or (xEnd**2 + yEnd**2) == 0:
  63.     xEnd = int(input("enter xEnd: "))
  64.     yEnd = int(input("enter yEnd: "))
  65. ball_pos = [xEnd + xO, yEnd + yO]
  66.  
  67. # setup display
  68. # Set up display area
  69. mainDisplay = pygame.display.set_mode((Width,Height))
  70.  
  71. mainDisplay.fill(blue)
  72. pygame.display.set_caption('inverseKinematics 2joint')
  73.  
  74.  
  75.  
  76. #use math.atan2(y, x) to calculate arm angles for end point
  77.  
  78. q2 = math.acos((xEnd**2 + yEnd**2 - a1*a1 - a2*a2)/(2*a1*a2))
  79.  
  80. q1 = (math.atan2((yEnd),(xEnd))) - (math.atan2((a2*math.sin(q2)),(a1 + (a2*math.cos(q2)))))
  81.  
  82. q3 = math.degrees(q2)
  83. q4 = math.degrees(q1)
  84.  
  85. q2End = q2
  86. q1End = q1
  87. # calculate arm increments
  88. delq2 = ((q2End - q2Start) / 100)
  89. delq1 = ((q1End - q1Start) / 100)
  90.  
  91. print(int(math.degrees(delq2)))
  92. print(int(math.degrees(delq1)))
  93.  
  94.  
  95. print(int(math.degrees(q2End)))
  96. print(int(math.degrees(q1End)))
  97.  
  98. # starting pose
  99. q1 = q1Start
  100. q2 = q2Start
  101.  
  102. # Main Loop
  103. for i in range(0, 101):
  104.     for event in pygame.event.get():
  105.         if event.type == pygame.QUIT:
  106.             pygame.quit()
  107.             quit()
  108.  
  109.     plot(q1, q2)
  110.     q1 += delq1
  111.     q2 += delq2
  112.     wayPt1.append(int(math.degrees(q1)))
  113.     wayPt2.append(int(math.degrees(q2)))
  114.  
  115.     draw(mainDisplay)
  116.     pygame.time.delay(20)
  117.  
  118.     pygame.display.update()
  119. print(wayPt2, wayPt1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement