Advertisement
Guest User

Untitled

a guest
Mar 28th, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.92 KB | None | 0 0
  1. import serial #Allows you to talk to the Arduino board
  2. import time #Allows you to tell the code to wait
  3. import Tkinter
  4. import tkMessageBox
  5. from Tkinter import *
  6.  
  7. #Connect to COM3, your USB port may be different so check which COM port your
  8. #Arduino board is connected to
  9. usbport = 'COM9'
  10.  
  11. #Don't change this line of code
  12. ser = serial.Serial(usbport, 9600, timeout=1)
  13. mainWindow = Tkinter.Tk()
  14. currPositions = [90,90,90,35]
  15.  
  16. #Middle
  17. middleLabel = Tkinter.Label(None, text='Middle', font=('Times', '18'),fg='blue')
  18. middleValue = Tkinter.Label(None, text=currPositions[0], font=('Times', '18'),fg='blue')
  19. middleButtonPlus = Tkinter.Button(mainWindow, text ="+", command = lambda: moveIncrement(0, 1))
  20. middleButtonMinus = Tkinter.Button(mainWindow, text ="-", command = lambda: moveIncrement(0, 0))
  21.  
  22. middleLabel.grid(row=0, column=0)
  23. middleButtonPlus.grid(row=0, column=1)
  24. middleButtonMinus.grid(row=0, column=2)
  25. middleValue.grid(row=0, column=3)
  26.  
  27.  
  28. #Left
  29. leftLabel = Tkinter.Label(None, text='Left', font=('Times', '18'),fg='blue')
  30. leftValue = Tkinter.Label(None, text=currPositions[1], font=('Times', '18'),fg='blue')
  31. leftButtonPlus = Tkinter.Button(mainWindow, text ="+", command = lambda: moveIncrement(1, 1))
  32. leftButtonMinus = Tkinter.Button(mainWindow, text ="-", command = lambda: moveIncrement(1, 0))
  33.  
  34. leftLabel.grid(row=1, column=0)
  35. leftButtonPlus.grid(row=1, column=1)
  36. leftButtonMinus.grid(row=1, column=2)
  37. leftValue.grid(row=1, column=3)
  38.  
  39.  
  40. #right
  41. rightLabel = Tkinter.Label(None, text='Right', font=('Times', '18'),fg='blue')
  42. rightValue = Tkinter.Label(None, text=currPositions[2], font=('Times', '18'),fg='blue')
  43. rightButtonPlus = Tkinter.Button(mainWindow, text ="+", command = lambda: moveIncrement(2, 1))
  44. rightButtonMinus = Tkinter.Button(mainWindow, text ="-", command = lambda: moveIncrement(2, 0))
  45.  
  46. rightLabel.grid(row=2, column=0)
  47. rightButtonPlus.grid(row=2, column=1)
  48. rightButtonMinus.grid(row=2, column=2)
  49. rightValue.grid(row=2, column=3)
  50.  
  51. #Claw
  52. clawLabel = Tkinter.Label(None, text='Claw', font=('Times', '18'),fg='blue')
  53. clawValue = Tkinter.Label(None, text=currPositions[3], font=('Times', '18'),fg='blue')
  54. clawButtonPlus = Tkinter.Button(mainWindow, text ="+", command = lambda: moveIncrement(3, 1))
  55. clawButtonMinus = Tkinter.Button(mainWindow, text ="-", command = lambda: moveIncrement(3, 0))
  56.  
  57. clawLabel.grid(row=3, column=0)
  58. clawButtonPlus.grid(row=3, column=1)
  59. clawButtonMinus.grid(row=3, column=2)
  60. clawValue.grid(row=3, column=3)
  61.  
  62. #Button and text box to enter angle for Middle servo
  63. E1 = Entry(mainWindow, bd =5, width = 7)
  64. E1.insert(END, "90")
  65. B1 = Tkinter.Button(mainWindow, text ="Middle", command = lambda: moveButton(E1, 0))
  66.  
  67. #Button and text box to enter angle for Left servo
  68. E2 = Entry(mainWindow, bd =5, width = 7)
  69. E2.insert(END, "90")
  70. B2 = Tkinter.Button(mainWindow, text ="Left", command = lambda: moveButton(E2, 1))
  71.  
  72. #Button and text box to enter angle for Right servo
  73. E3 = Entry(mainWindow, bd =5, width = 7)
  74. E3.insert(END, "90")
  75. B3 = Tkinter.Button(mainWindow, text ="Right", command = lambda: moveButton(E3, 2))
  76.  
  77.  
  78. #Button and text box to enter angle for Claw servo
  79. E4 = Entry(mainWindow, bd =5, width = 7)
  80. E4.insert(END, "35")
  81. B4 = Tkinter.Button(mainWindow, text ="Claw", command = lambda: moveButton(E4, 3))
  82.  
  83. #Entry locations
  84. E1.grid(row=4, column=1)
  85. E2.grid(row=5, column=1)
  86. E3.grid(row=6, column=1)
  87. E4.grid(row=7, column=1)
  88.  
  89. #Button locations
  90. B1.grid(row=4, column=0)
  91. B2.grid(row=5, column=0)
  92. B3.grid(row=6, column=0)
  93. B4.grid(row=7, column=0)
  94.  
  95. #Function to move the arm, you should not need to change this code
  96. def move(servo, angle):
  97. if (0 <= angle <= 180):
  98. ser.write(chr(servo))
  99. ser.write(chr(angle))
  100. currPositions[servo] = angle
  101. refreshLabels()
  102. else:
  103. print(angle, ": Servo angle must be an integer between 0 and 180.\n")
  104.  
  105. def moveButton(E, servo):
  106. move(servo, int(E.get()))
  107.  
  108. def moveIncrement(servo, direction):
  109. angle = 0
  110. if direction == 1:
  111. angle = currPositions[servo]+1
  112. else:
  113. angle = currPositions[servo]-1
  114. move(servo, angle)
  115.  
  116. #This function will move 1 servo slowly. You need to specify which motor to move (servo),
  117. #the position the motor is currently at (currAngle), the angle you wish it to move to (angle),
  118. #the number of degrees it will move at a time (increment) and how long it will wait after
  119. #each movement
  120. def moveSlow(servo, angle, increment, delay):
  121. if currPositions[servo] > angle:
  122. increment = increment*-1
  123. for i in range (currPositions[servo], angle, increment):
  124. move(servo, i)
  125. time.sleep(delay)
  126. move(servo, angle)
  127.  
  128. #This function will move multiple servos slowly. You need to specify which motors to move (servo),
  129. #the positions the motors are currently at (currAngle), the angles you wish it to move the motors to (angle),
  130. #the number of degrees it will move at a time (increment) and how long it will wait after
  131. #each movement
  132. def moveMultipleSlow(servo, angle, increment, delay):
  133. inPosition = 0
  134. while(inPosition < len(servo)):
  135. inPosition = 0
  136. inc = 0
  137. for i in range(0, len(servo)):
  138. if currPositions[servo[i]] > angle[i]:
  139. inc = increment*-1
  140. else:
  141. inc = increment
  142. if currPositions[servo[i]] != angle[i]:
  143. currPositions[servo[i]] = currPositions[servo[i]]+inc
  144. move(servo[i], currPositions[servo[i]])
  145. time.sleep(delay)
  146. if(abs(currPositions[servo[i]] - angle[i]) < abs(increment)):
  147. move(servo[i], angle[i])
  148. time.sleep(delay)
  149. inPosition = inPosition + 1
  150.  
  151. def refreshLabels():
  152. middleValue.config(text=currPositions[0])
  153. leftValue.config(text=currPositions[1])
  154. rightValue.config(text=currPositions[2])
  155. clawValue.config(text=currPositions[3])
  156.  
  157.  
  158.  
  159. time.sleep(2)
  160.  
  161. mainWindow.mainloop()
  162.  
  163. ser.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement