Advertisement
Guest User

TSA Program

a guest
Jan 22nd, 2019
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.92 KB | None | 0 0
  1. --variable storage / setup
  2. tFile = {}
  3. hFile = {}
  4. gFile = {}
  5. eFile = {}
  6. bFile = {}
  7. pFile = {}
  8. aFile = {}
  9. mFile = {}
  10. cFile = {}
  11. spoolLeft = 5000
  12. sPos = 1
  13. --file
  14. local function loadTheFile() --Hello! load is already a lua function, so I'm calling this loadTheFile instead.
  15. tFile = {"G01 X0.0 Y0.0 Z0.0","G01 X2.0 Y2.0","G01 Z1.0","M30"}
  16. end
  17. --chop
  18. local function bananaSplit()
  19. for k,v in pairs(tFile) do
  20. cFile[k] = v
  21. if v == "M30" then
  22. cFile[k] = "G01 X0.0 Y0.0 Z0.0"
  23. break
  24. end
  25. end
  26. --split into metatables
  27. local tCon = ""
  28. for i = 1,#cFile do
  29. local mCon = {}
  30. mFile[i] = {}
  31. tCon = string.sub(cFile[i],5)
  32. for m in string.gmatch(tCon,"%S+") do
  33. mCon[#mCon+1] = m
  34. end
  35. mFile[i][1] = mCon[1]
  36. if mCon[2] then
  37. mFile[i][2] = mCon[2]
  38. end
  39. if mCon[3] then
  40. mFile[i][3] = mCon[3]
  41. end
  42. end
  43. --arrange by value and inherit
  44. local vCon = ""
  45. local rCon = ""
  46. for i = 1,#mFile do
  47. aFile[i] = {}
  48. for o = 1,#mFile[i] do
  49. vCon = string.sub(mFile[i][o],1,1)
  50. rCon = string.sub(mFile[i][o],2)
  51. if vCon == "X" then
  52. aFile[i][1] = rCon
  53. elseif vCon == "Y" then
  54. aFile[i][2] = rCon
  55. elseif vCon == "Z" then
  56. aFile[i][3] = rCon
  57. end
  58. end
  59. for o = 1,3 do
  60. if aFile[i][o] == nil then
  61. aFile[i][o] = aFile[i-1][o]
  62. end
  63. end
  64. end
  65. end
  66. local function distanceBetween()
  67. --multiply by 100 as done by example --WORKS HERE!
  68. for i = 1,#aFile do
  69. pFile[i] = {}
  70. for o = 1,3 do
  71. pFile[i][o] = tonumber(aFile[i][o])*100
  72. end
  73. end
  74. --calculate between
  75. pFile[0] = {}
  76. pFile[0][1],pFile[0][2],pFile[0][3] = 0,0,0
  77. for i = 1,#pFile do
  78. bFile[#bFile+1] = math.sqrt((math.abs(pFile[i][1]-pFile[i-1][1])^2)+(math.abs((pFile[i][2]-pFile[i-1][2]))^2))
  79. bFile[#bFile] = math.sqrt((bFile[#bFile]^2)+(math.abs(pFile[i][3]-pFile[i-1][3])^2))
  80. if bFile[#bFile] == 0 then
  81. table.remove(bFile,#bFile)
  82. end
  83. end
  84. --calculate time each
  85. for i = 1,#bFile do
  86. eFile[i] = bFile[i]/20
  87. end
  88. --calculate time left at each spot
  89. local gCon = 0
  90. for i = 1,#eFile do
  91. gCon = 0
  92. for o = i,#eFile do
  93. gCon = gCon + eFile[o]
  94. end
  95. gFile[i] = gCon
  96. end
  97. --format time left at each spot, for printing
  98. local hFirst = 0
  99. local hLast = 0
  100. for i = 1,#gFile do
  101. hLast = math.floor(gFile[i]%60)
  102. hFirst = math.floor((gFile[i]-hLast)/60)
  103. hFile[i] = "Time Remaining: "..hFirst..":"..hLast
  104. end
  105. end
  106. --step
  107. local function step(sPos)
  108. if sPos < #pFile then
  109. return sPos+1 --Tells the program where the coordinates are
  110. else
  111. return "break"
  112. end
  113. end
  114. local function printStatus(sPos)
  115. print("Position: "..pFile[sPos][1]..", "..pFile[sPos][2]..", "..pFile[sPos][3])
  116. if hFile[sPos] then
  117. print(hFile[sPos])
  118. end
  119. end
  120. --[[
  121. /*
  122. Code time
  123. */
  124. ]]--
  125. loadTheFile()
  126. bananaSplit()
  127. distanceBetween()
  128. while true do
  129. if bFile[sPos] then
  130. spoolLeft = spoolLeft - bFile[sPos]
  131. end
  132. if spoolLeft <= 0 then
  133. print("Spool is empty; please replace. (press enter)")
  134. read()
  135. spoolLeft = 5000
  136. end
  137. printStatus(sPos)
  138. continue = step(sPos)
  139. if continue ~= "break" then
  140. sPos = continue
  141. else
  142. break
  143. end
  144. end
  145. print("Program Complete")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement