Advertisement
aferral

ApiMov

Jan 16th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.13 KB | None | 0 0
  1. --- Apis para movimiento de turtles
  2.  
  3.  
  4. -- VARIABLE GLOBALES
  5.  
  6. global_debug = 0
  7.  
  8. global_deltah=0
  9. global_deltaw=0
  10. global_deltad=0
  11.  
  12. global_orientacion = 0 -- Guarda global_orientacion respecto a origen. 0: frente, 1: derecha, 2: atras, 3: izquierda
  13.  
  14.  
  15. -- Utilidad para printear con formato estilo C
  16. printf = function(s,...)
  17. return io.write(s:format(...))
  18. end -- function
  19.  
  20.  
  21.  
  22. -- FUNCIONES NAVEGACION
  23.  
  24.  
  25. function tLeft()
  26. turtle.turnLeft()
  27. global_orientacion = (global_orientacion-1) % 4
  28. end
  29. function tRight()
  30. turtle.turnRight()
  31. global_orientacion = (global_orientacion+1) % 4
  32. end
  33.  
  34. function back()
  35. if global_debug == 1 then print("b") end
  36.  
  37. if (not turtle.back()) then
  38. error("NO se logro romper bloque")
  39. end
  40.  
  41. if (global_orientacion == 0) then
  42. global_deltad=global_deltad-1
  43. end
  44. if (global_orientacion == 1) then
  45. global_deltaw=global_deltaw-1
  46. end
  47. if (global_orientacion == 2) then
  48. global_deltad=global_deltad+1
  49. end
  50. if (global_orientacion == 3) then
  51. global_deltaw=global_deltaw+1
  52. end
  53. end
  54.  
  55. function forw()
  56. if global_debug == 1 then print("f") end
  57.  
  58. turtle.dig()
  59. if (not turtle.forward()) then
  60. os.sleep(0.2)
  61. turtle.dig()
  62. if (not turtle.forward()) then
  63. error("NO se logro romper bloque")
  64. end
  65. end
  66.  
  67. if (global_orientacion == 0) then
  68. global_deltad=global_deltad+1
  69. end
  70. if (global_orientacion == 1) then
  71. global_deltaw=global_deltaw+1
  72. end
  73. if (global_orientacion == 2) then
  74. global_deltad=global_deltad-1
  75. end
  76. if (global_orientacion == 3) then
  77. global_deltaw=global_deltaw-1
  78. end
  79.  
  80. end
  81.  
  82. function up()
  83. if global_debug == 1 then print("up") end
  84.  
  85. turtle.digUp()
  86. if (not turtle.up()) then
  87. error("NO se logro romper bloque")
  88. end
  89.  
  90. global_deltah=global_deltah+1
  91.  
  92. end
  93.  
  94. function down()
  95. if global_debug == 1 then print("down") end
  96.  
  97. turtle.digDown()
  98. if (not turtle.down()) then
  99. error("NO se logro romper bloque")
  100. end
  101.  
  102. global_deltah=global_deltah-1
  103.  
  104. end
  105.  
  106.  
  107. -- Funcion que retorna a origen dado desplazamiento global
  108.  
  109. function retorno()
  110.  
  111. printf("El delta de posicion global_orientacion es \n")
  112. printf("DH: %d DW: %d DD: %d global_orientacion: %d \n",global_deltah,global_deltaw,global_deltad,global_orientacion)
  113.  
  114. -- Ajusta global_orientacion
  115. local dh = global_deltah
  116. local dw = global_deltaw
  117. local dd = global_deltad
  118. -- Primero ajusta dH
  119. for i=1,math.abs(dh) do
  120. if (dh > 0) then
  121. down()
  122. else
  123. up()
  124. end
  125. end
  126.  
  127.  
  128. --Ajusta DW
  129. while (global_orientacion ~= 1) do
  130. tLeft()
  131. end
  132.  
  133. for i=1,math.abs(dw) do
  134. if (dw > 0) then
  135. back()
  136. else
  137. forward()
  138. end
  139. end
  140.  
  141. -- Ajusta dD
  142. while (global_orientacion ~= 0) do
  143. tLeft()
  144. end
  145. for i=1,math.abs(dd) do
  146. if (dd > 0) then
  147. back()
  148. else
  149. forward()
  150. end
  151. end
  152.  
  153.  
  154. printf("Posterior a relocacion GLOBALES \n")
  155. printf("DH: %d DW: %d DD: %d global_orientacion: %d \n",global_deltah,global_deltaw,global_deltad,global_orientacion)
  156.  
  157. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement