Advertisement
Guest User

Untitled

a guest
Mar 6th, 2015
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.82 KB | None | 0 0
  1. import RoboTX
  2. import time
  3.  
  4. # Edit this line to use the correct COM port
  5. RoboTX.Initialize(4)
  6. RoboTX.LampOn(8,256)
  7. loc = [(0,75), (3150,80), (3150, 990), (3150, 1950)]
  8. anti = [(0,-75), (-3150,-80), (-3150, -990), (-3150, -1950)]
  9.  
  10. def MoveIn():
  11. while RoboTX.GetSwitchInput(2) == 0:
  12. RoboTX.StartMotor(2, RoboTX.Right)
  13. RoboTX.StopMotor(2)
  14.  
  15. def MoveOut():
  16. while RoboTX.GetSwitchInput(4) == 0:
  17. RoboTX.StartMotor(2, RoboTX.Left)
  18. RoboTX.StopMotor(2)
  19.  
  20. def MoveBottom():
  21. MoveIn()
  22. while RoboTX.GetSwitchInput(3) == 0:
  23. RoboTX.StartMotor(3, RoboTX.Right)
  24. RoboTX.StopMotor(3)
  25.  
  26. def MoveNearEnd():
  27. MoveIn()
  28. while RoboTX.GetSwitchInput(1) == 0:
  29. RoboTX.StartMotor(1, RoboTX.Left)
  30. RoboTX.StopMotor(1)
  31.  
  32. def MoveV(d):
  33. RoboTX.ResetCounter(3)
  34. time.sleep(0.25)
  35. Direction = RoboTX.Left
  36. if d < 0:
  37. d = -d
  38. Direction = RoboTX.Right
  39. while RoboTX.GetCounter(3) < d:
  40. RoboTX.StartMotor(3, Direction)
  41. RoboTX.StopMotor(3)
  42.  
  43. def MoveH(d):
  44. RoboTX.ResetCounter(1)
  45. time.sleep(0.25)
  46. Direction = RoboTX.Right
  47. if d < 0:
  48. d = -d
  49. Direction = RoboTX.Left
  50. while RoboTX.GetCounter(1) < d:
  51. RoboTX.StartMotor(1, Direction)
  52. RoboTX.StopMotor(1)
  53.  
  54. def MoveHome():
  55. MoveBottom()
  56. MoveNearEnd()
  57.  
  58. def Grab():
  59. MoveOut()
  60. MoveV(150)
  61. MoveIn()
  62. MoveV(-150)
  63.  
  64. def Drop():
  65. MoveV(150)
  66. MoveOut()
  67. MoveV(-150)
  68. MoveIn()
  69.  
  70. def MovePoint(l):
  71. MoveV(l[1])
  72. MoveH(l[0])
  73.  
  74. def ShelveObject(i):
  75. Grab()
  76. MovePoint(anti[0])
  77. MovePoint(loc[i])
  78. Drop()
  79. MovePoint(anti[i])
  80. MovePoint(loc[0])
  81.  
  82. def ObjectIsPresent():
  83. RoboTX.LampOn(8, 512)
  84. a = not bool(RoboTX.GetSwitchInput(6))
  85. RoboTX.LampOff(8)
  86. print a
  87.  
  88. def Color():
  89. a = RoboTX.GetColorSensor(5)
  90.  
  91. if (a > 50 and a < 300):
  92. return 2#yellow
  93. elif (a > 900 and a < 2000):
  94. return 3#blue
  95. elif (a > 400 and a < 800):
  96. return 1#red
  97. else:
  98. return "ERROR"
  99.  
  100. def SortObjects():
  101. MoveHome()
  102. MovePoint(loc[0])
  103.  
  104. for i in range(3):
  105. print "You have 2 second to place the next block"
  106. time.sleep(2)
  107.  
  108. a = Avg(100)
  109.  
  110. if (a == "ERROR"):
  111. print "Oops. Please restart"
  112. time.sleep(10)
  113. else:
  114. ShelveObject(a)
  115.  
  116. def Avg(f):
  117. r = 0
  118. b = 0
  119. y = 0
  120. for i in range(f):
  121. a = Color()
  122.  
  123. if a == 1:
  124. r = r + 1
  125. elif a == 2:
  126. y = y + 1
  127. elif a == 3:
  128. b = b + 1
  129. g = [r, y, b]
  130. g.sort()
  131.  
  132. if (g[2] == r):
  133. return 1
  134. elif (g[2] == b):
  135. return 3
  136. else:
  137. return 2
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement