PageRenderTime 51ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/WireBot/Wirebot.py

https://github.com/kvoisine/Make-Projects
Python | 238 lines | 215 code | 8 blank | 15 comment | 5 complexity | 584412a853edbfa1465928b0beb043b7 MD5 | raw file
  1. #!/usr/bin/python
  2. #Riley Porter
  3. #Synthetos.com
  4. """Please read this file from top to bottom.. There are places where you will need to edit this file to match YOUR Wirebot configuration.
  5. The variables you need to set are:
  6. PORT, V1, V2, V3
  7. Once you have this file all setup. (Your vertices all set) then it will open the serial port that you specified and connect to the grblShield.
  8. Once this is connected the instructions being sent to your wirebot motors are a "configuration loop".
  9. This means that all motors move your center object. Ours was a ping pong ball up and down.. Forever... This is done to make sure
  10. that your machine is behaving correctly before trying any of the other pre-defined shapes. Once you know your machine is functioning
  11. correctly (moving up and down) you can comment out the code that calls the configurationLoop() function. You can read more about
  12. how to do this at the bottom of the source code.
  13. Lastly, should you want further explanation about how / why this code is the way it is be sure to check out the full technical write up at:
  14. https://www.synthetos.com/blog/grblshield-wirebot/
  15. """
  16. try:
  17. import serial, sys
  18. except:
  19. print "Error Importing the Serial Module"
  20. print "You can download the serial port module here"
  21. print "\thttp://pypi.python.org/pypi/pyserial"
  22. from math import *
  23. from time import sleep
  24. move = 0, 0, 2
  25. class wirebot(object):
  26. def __init__(self):
  27. #Machine Specific
  28. """ATTENTION SET THE PORT SETTING TO YOUR COMPUTERS PORT"""
  29. PORT = "COM30" #Set your serial port here.
  30. BAUD = 9600 #This should be 9600 unless you are using a custom grbl build.
  31. """THIS IS WHERE YOU SET THE VERTICE'S VALUES"""
  32. #V1 or Vertice 1 information
  33. self.x1 = -12
  34. self.y1 = -12
  35. self.z1 = 12
  36. #V2 or Vertice 2 information
  37. self.x2 = 12
  38. self.y2 = -12
  39. self.z2 = 12
  40. #V3 or Vertice 3 information
  41. self.x3 = 0
  42. self.y3 = 12
  43. self.z3 = 12
  44. """LEAVE THIS ALONE SET THE VERTICIE'S ABOVE"""
  45. self.v1 = (self.x1, self.y1, self.z1) #Maps to Motor Port X on grblShield
  46. self.v2 = (self.x2, self.y2, self.z2) #Maps to Motor Port Y on grblShield
  47. self.v3 = (self.x3, self.y3, self.z3) #Maps to Motor Port Z on grblShield
  48. self.ZER0 = (0,0,0)
  49. """SETTINGS
  50. This is where you need to setup the settings for your machine.
  51. This example is a X,Y,Z (24" x 24" x 24")
  52. - X +
  53. ---------------- +
  54. | V3 |
  55. | /\ |
  56. | / \ | Y Note: The center of the Triangle is (0,0,0)
  57. | / \ |
  58. | / \ |
  59. | V1--------V2 |
  60. ---------------- -
  61. UP = + for Z
  62. DOWN = - for Z
  63. We use a 2'x 2' frame for this project. So Roughly 24/2 = 12. But we use 11.5
  64. X Y Z
  65. ---------------------------------
  66. V1 = (-12, -12, 12) #Left Vertex
  67. V2 = ( 12, -12, 12) #Right Vertex
  68. V3 = ( 0, 12, 12) #Rear Vertex
  69. The above example assumes the vertices are all at the same height, but they don’t have to be.
  70. Just enter the actual height from the origin (Which is 0,0,0 Or the center of the triangle). The example also assumes that the rear vertex is
  71. half way between the left and right in X, but it doesn’t have to be. The positions of the vertices
  72. are somewhat arbitrary, assuming you don’t reduce the useful working volume too much.
  73. What does that mean? It does not have to be a perfect equilateral triangle. Or that each vertex does not need
  74. to be at the same height as the others. However each "offset" will cut into your working volume area.
  75. """
  76. #This will setup your serial port connection
  77. try:
  78. self.s = serial.Serial(PORT, BAUD)
  79. except:
  80. print "Error Opening Serial Port"
  81. print "Make sure you have set this correctly in the code."
  82. print "If you are using windows the format is like this: COM3, etc"
  83. print "If you are on *nix its /dev/sttyUSB or something similar"
  84. print "Program Exiting...."
  85. sys.exit()
  86. """This might look confusing but these are just pre-defined shapes broken down into coodinates.
  87. Meaning CUBE will draw a cube with your center "ball" or whatever in 3d space. In my example machine I used
  88. a 2' x 2' Cube. This is a very small working area. However should you have a larger area and you want to draw a bigger
  89. cube then set the v value to something larger than 8."""
  90. #3d Cube
  91. self.CUBE = [ (-v,-v,v),
  92. (v,-v,v),
  93. (v,v,v),
  94. (-v,v,v),
  95. (-v,v,-v),
  96. (v,v,-v),
  97. (v,-v,-v),
  98. (-v,-v,-v),
  99. (0,0,0) ]
  100. #Not sure if this one works
  101. self.LETTER_M = [(0,0,v),
  102. (2,0,0),
  103. (4,0,8),
  104. (4,0,0)]
  105. #This one draws an inverted pyramid
  106. self.INVERSE_TRIANGLE = [(-v,-v,v),
  107. (0,0,0),
  108. (v,-v,v),
  109. (0,0,0),
  110. (v,v,v),
  111. (0,0,0),
  112. (-v,v,v),
  113. (0,0,0)]
  114. #This SHOULD draw a 2d square
  115. self.VERT_SQUARE = [(-v,-v,v),
  116. (-v,-v,-v),
  117. (v,-v,-v),
  118. (v,-v,v),
  119. (-v,-v,v) ]
  120. def ConfigureLoop(self):
  121. """This method is used to make sure your motors are all hooked up and working correctly.
  122. When you run this loop your center object (in our example was a ping pong ball) should be moving
  123. up then pausing for 2 seconds then moving back down. Over and over. Once you see this is the
  124. case then your machine is at least working correctly a little bit. :) """
  125. MOVENMENT_LENGTH = 6
  126. DELAY = 2
  127. print "In Configuration Loop... Hit CTRL-C to break this loop..."
  128. while(1):
  129. value = self.baseEQ((0,0,MOVENMENT_LENGTH))
  130. print ("Sending: G0 X%s Y%s Z%s\n" % (value[0],value[1],value[2]))
  131. self.s.writelines("G0 X%s Y%s Z%s\n" % (value[0],value[1],value[2]))
  132. sleep(DELAY)
  133. value = self.baseEQ((0,0,0))
  134. print ("Sending: G0 X%s Y%s Z%s\n" % (value[0],value[1],value[2]))
  135. self.s.writelines("G0 X%s Y%s Z%s\n" % (value[0],value[1],value[2]))
  136. sleep(DELAY)
  137. print "In Configuration Loop... Hit CTRL-C to break this loop..."
  138. def Zero(self):
  139. """This is the zeroing function. This function
  140. once ran, should return the length of line to (0,0,0) from each vertex"""
  141. print("Zeroing Function Called")
  142. L1, L2, L3 = self.baseEQ(self.ZER0)
  143. print("G92 X%s Y%s Z%s\n" % (L1, L2, L3))
  144. self.s.writelines("G92 X%s Y%s Z%s\n" % (L1, L2, L3))
  145. def playShape(self, shape):
  146. """This takes a pattern and will play it on wirebot rig"""
  147. for coord in shape:
  148. value = self.baseEQ(coord)
  149. print ("Sending: G0 X%s Y%s Z%s\n" % (value[0],value[1],value[2]))
  150. self.s.writelines("G0 X%s Y%s Z%s\n" % (value[0],value[1],value[2]))
  151. sleep(3)
  152. def baseEQ(self, value):
  153. """This is the master equation to translate normal 3 axis gcode
  154. into the 3 motor kinetic model"""
  155. x1,y1,z1 = self.v1
  156. x2,y2,z2 = self.v2
  157. x3,y3,z3 = self.v3
  158. """This is ugly. Dont kiss you mother with this code."""
  159. lvalue = []
  160. lvalue.append(value[0])
  161. lvalue.append(value[1])
  162. lvalue.append(value[2])
  163. x,y,z = lvalue[0],lvalue[1],lvalue[2]
  164. L1 = sqrt((x1-x)**2 + (y1-y)**2 + (z1-z)**2)
  165. L2 = sqrt((x2-x)**2 + (y2-y)**2 + (z2-z)**2)
  166. L3 = sqrt((x3-x)**2 + (y3-y)**2 + (z3-z)**2)
  167. #print ("Sent: (%s,%s,%s)\nReturned: (%2.2f,%2.2f,%2.2f)" % (x,y,z,L1,L2,L3))
  168. val = ("%2.2f,%2.2f,%2.2f") % (L1,L2,L3) #This will break down long gcode values into 2 decimal points
  169. return val.split(",")
  170. if __name__ == "__main__":
  171. print "[*]Starting the Wirebot Controller:"
  172. wb = wirebot() #Creates a witebot object
  173. wb.Zero() #This will "zero" your wirebot machine. Make sure your center object is in your desired (0,0,0) or origin.
  174. wb.ConfigureLoop() #Runs the configuration loop, Comment this out by placing a # in front of it once you determine your machine is behaving correctly
  175. """Un-Comment this if you would like to continue to draw inverse triangles"""
  176. #while(1):
  177. # wb.playShape(wb.INVERSE_TRIANGLE) #You can change wb.INVERSE_TRIANGLE with any of the other shapes listed above.