PageRenderTime 219ms CodeModel.GetById 37ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/xrhex.py

https://bitbucket.org/dexteris/forma
Python | 164 lines | 111 code | 14 blank | 39 comment | 8 complexity | 9555190872fddf0f0d6bedb11714fa38 MD5 | raw file
  1. # Copyright (c) 2010 Galen Clark Haynes
  2. # 2010 Dexteris Robotics, LLC
  3. # All rights reserved.
  4. #
  5. # Redistribution and use in source and binary forms, with or without
  6. # modification, are permitted provided that the following conditions are met:
  7. #
  8. # * Redistributions of source code must retain the above copyright
  9. # notice, this list of conditions and the following disclaimer.
  10. # * Redistributions in binary form must reproduce the above copyright
  11. # notice, this list of conditions and the following disclaimer in the
  12. # documentation and/or other materials provided with the distribution.
  13. # * Neither the name of Dexteris Robotics, LLC nor the names of its
  14. # contributors may be used to endorse or promote products derived from
  15. # this software without specific prior written permission.
  16. #
  17. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  18. # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  21. # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  22. # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  23. # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  24. # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  25. # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  26. # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  27. # POSSIBILITY OF SUCH DAMAGE.
  28. import copy
  29. from math import pi
  30. import forma
  31. import forma.gl
  32. class RHexLeg(forma.Forma):
  33. def __init__(self,**kwargs):
  34. # initialize
  35. self.length = 0.175
  36. self.width = 0.03
  37. self.color = [0.8,0.7,0.4]
  38. super(type(self),self).__init__(kwargs)
  39. # design leg
  40. r1 = forma.Rotation(y=pi/2)
  41. self.rotation = forma.Rotation(z=0)
  42. basecyl = forma.SolidCylinder(radius=self.width/3,height=self.width)
  43. t = forma.Translation(x=self.length/2)
  44. c = forma.Cylinder(arc=pi,radius=self.length/2,height=self.width,color=self.color)
  45. self.attach(r1.attach(self.rotation.attach(basecyl.attach(t.attach(c)))))
  46. def set_angle(self,a):
  47. self.rotation.z = a
  48. def get_angle(self):
  49. return self.rotation.z
  50. angle = property(get_angle,set_angle)
  51. class RHex(forma.Forma):
  52. def __init__(self,**kwargs):
  53. # initialize
  54. self.width = 0.3
  55. self.height = 0.08
  56. self.length = 0.57
  57. self.color = [0.1,0.1,0.1]
  58. super(type(self),self).__init__(kwargs)
  59. boxlength = self.length - self.height
  60. # design body
  61. self.attach(forma.Box(width=self.width,height=self.height,length=boxlength,color=self.color))
  62. c_front = forma.SolidCylinder(radius=self.height/2,height=self.width,arc=pi,color=self.color)
  63. c_back = copy.copy(c_front)
  64. self.attach(forma.Translation(y=boxlength/2).attach(forma.Rotation(y=pi/2).attach(c_front)))
  65. self.attach(forma.Translation(y=-boxlength/2).attach(forma.Rotation(y=pi/2,z=pi).attach(c_back)))
  66. # add legs
  67. w = self.width/2
  68. l = boxlength/2
  69. self.leg = []
  70. for s in [w,-w]:
  71. for p in [-l,0,l]:
  72. t = forma.Translation(x=s+0.02*s/abs(s),y=p)
  73. self.attach(t)
  74. if p == 0:
  75. offset = 0.04 * s / abs(s)
  76. c = forma.Box(width=offset,length=offset,height=offset,color=self.color)
  77. #c = forma.Cube(size=offset)
  78. t.attach(c)
  79. newt = forma.Translation(x=offset)
  80. t.attach(newt)
  81. t = newt
  82. ll = RHexLeg()
  83. self.leg.append(ll)
  84. t.attach(ll)
  85. class EduBot(forma.Forma):
  86. def __init__(self,**kwargs):
  87. self.width = 0.25
  88. self.height = 0.108
  89. self.length = 0.36
  90. self.color = [0.4,0.4,0.4]
  91. self.shell_color = [0.1,0.1,0.1]
  92. super(type(self),self).__init__(kwargs)
  93. boxlength = self.length - self.height
  94. # front to back base piece
  95. base = forma.Box(height=0.01,width=self.width*0.5,length=self.length,color=self.color)
  96. self.attach(base)
  97. # middle pieces
  98. middle = forma.Box(height=0.01,width=self.width+0.04,length=self.length/5,color=self.color)
  99. c = forma.SolidCylinder(height=self.width+0.03,radius=self.length/10,arc=pi,color=self.shell_color)
  100. r = forma.Rotation(y=pi/2)
  101. self.attach(middle)
  102. self.attach(r.attach(forma.Rotation(z=pi/2).attach(c)))
  103. # front piece
  104. ends = forma.Box(height=0.01,width=self.width,length=self.length/5,color=self.color)
  105. t = forma.Translation(y=boxlength/2)
  106. c = forma.SolidCylinder(height=self.width-0.01,radius=self.length/10,arc=pi,color=self.shell_color)
  107. r = forma.Rotation(y=pi/2)
  108. self.attach(t.attach(r.attach(forma.Rotation(z=pi/2).attach(c))))
  109. self.attach(t.attach(ends))
  110. # back piece
  111. t = forma.Translation(y=-boxlength/2)
  112. self.attach(t.attach(r.attach(forma.Rotation(z=pi/2).attach(copy.copy(c)))))
  113. self.attach(t.attach(copy.copy(ends)))
  114. r = forma.Rotation(x=pi/2)
  115. c = forma.SolidCylinder(height=self.length,radius = self.width*0.25,arc=pi,color=self.shell_color)
  116. self.attach(r.attach(c))
  117. w = self.width/2
  118. l = boxlength/2
  119. self.leg = []
  120. for s in [-w,w]:
  121. for p in [-l,0,l]:
  122. t = forma.Translation(x=s+0.02*s/abs(s),y=p)
  123. self.attach(t)
  124. if p == 0:
  125. offset = 0.02 * s / abs(s)
  126. newt = forma.Translation(x=offset)
  127. t.attach(newt)
  128. t = newt
  129. ll = RHexLeg(length=0.117,width=0.02,color=[0.1,0.1,0.1])
  130. t.attach(ll)
  131. self.leg.append(ll)
  132. if __name__ == '__main__':
  133. s = forma.Scene()
  134. #xrhex = RHex(color=[0.8,0.7,0.4])
  135. xrhex = RHex()
  136. s.attach(xrhex)
  137. t = forma.Translation(x=0.5)
  138. rhex = RHex(length=0.543,width=0.335,height=0.139,color=[0.0,0.1,0.05])
  139. #rhex = RHex(height=0.15,width=0.33,length=0.53,color=[0.7,0.7,0.7])
  140. s.attach(t.attach(rhex))
  141. t = forma.Translation(x=-0.5)
  142. edubot = EduBot()
  143. s.attach(t.attach(edubot))
  144. for i in [1,3,5]:
  145. xrhex.leg[i].angle = 6*pi/5
  146. rhex.leg[i].angle = 6*pi/5
  147. edubot.leg[i].angle = 6*pi/5
  148. win = forma.gl.GLUTWindow()
  149. win.scene = s
  150. win.run()