Advertisement
serafim7

Universal drone remote control (tablet) [OpenComputers]

Jun 17th, 2017
447
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.54 KB | None | 0 0
  1. --Universal drone remote control(tablet) version: 1.0 by Litvinov
  2. --http://computercraft.ru/topic/1950-universal-drone-remote-control-distantcionnoe-upravlenie-dronom/
  3. --https://pastebin.com/EYkxNTss
  4. local com = require('component')
  5. local event = require('event')
  6. local keyboard = require('keyboard')
  7. local term = require('term')
  8.  
  9. local gpu = com.gpu
  10. local nav
  11. local m
  12.  
  13. local receivePort = 1011
  14. local sendPort = 1010
  15. local wRes, hRes = gpu.maxResolution()
  16. local step = 1
  17. local acceleration
  18. local sideInt = 0
  19. local navigationSides = false
  20.  
  21. local colors = {
  22.   white = 0xFFFFFF,
  23.   black = 0x000000,
  24.   gray = 0x2D2D2D,
  25.   green = 0x00FF00,
  26.   yellow = 0xFFFF00
  27. }
  28.  
  29. local navSides = {
  30.   ['forward'] = {
  31.     [2] = {['z'] = '-'},
  32.     [3] = {['z'] = ''},
  33.     [4] = {['x'] = '-'},
  34.     [5] = {['x'] = ''}
  35.   },
  36.   ['back'] = {
  37.     [2] = {['z'] = ''},
  38.     [3] = {['z'] = '-'},
  39.     [4] = {['x'] = ''},
  40.     [5] = {['x'] = '-'}
  41.   },
  42.   ['right'] = {
  43.     [2] = {['x'] = ''},
  44.     [3] = {['x'] = '-'},
  45.     [4] = {['z'] = '-'},
  46.     [5] = {['z'] = ''}
  47.   },
  48.   ['left'] = {
  49.     [2] = {['x'] = '-'},
  50.     [3] = {['x'] = ''},
  51.     [4] = {['z'] = ''},
  52.     [5] = {['z'] = '-'}
  53.   }
  54. }
  55.  
  56. local answers = {
  57.   ['ping'] = function(_, _, _, _, distance)
  58.     gpu.fill(1, 12, wRes, 1, ' ')
  59.     gpu.set(1, 12, 'Расстояние к дрону: '..math.floor(distance))
  60.   end,
  61.   ['cAcceleration'] = function(_, _, _, _, _, _, cAcceleration)
  62.     gpu.setForeground(colors.green)
  63.     gpu.fill(13, 5, 4, 1, ' ')
  64.     gpu.set(13, 5, ''..cAcceleration)
  65.     gpu.setForeground(colors.white)
  66.     acceleration = cAcceleration
  67.   end,
  68.   ['cSelectSlot'] = function(_, _, _, _, _, _, cSelectSlot, selectCount)
  69.     gpu.setForeground(colors.green)
  70.     gpu.set(23, 7, ''..cSelectSlot)
  71.     gpu.fill(41, 8, 2, 1, ' ')
  72.     gpu.set(41, 8, ''..selectCount)
  73.     gpu.setForeground(colors.white)
  74.   end,
  75. }
  76.  
  77. local function droneMsg(...)
  78.   local msg = {...}
  79.   for answer in pairs(answers) do
  80.     if answer == msg[6] then
  81.       answers[msg[6]](...)
  82.       break
  83.     end
  84.   end
  85. end
  86.  
  87. local function send(...)
  88.   m.broadcast(sendPort, ...)
  89. end
  90.  
  91. local function programExit()
  92.   gpu.setResolution(wRes, hRes)
  93.   term.clear()
  94.   m.close(receivePort)
  95.   event.ignore('modem_message', droneMsg)
  96.   os.exit()
  97. end
  98.  
  99. local function infoInit()
  100.   gpu.fill(1, 2, wRes, hRes - 2, ' ')
  101.   gpu.setForeground(colors.gray)
  102.   gpu.fill(1, 3, wRes, 1, '-')
  103.   gpu.fill(1, 10, wRes, 1, '-')
  104.   gpu.setForeground(colors.white)
  105.   gpu.set(1, 4, 'Шаг - ')
  106.   gpu.set(1, 5, 'Ускорение - ')
  107.   gpu.set(1, 6, 'Сторона взаимодействия - ')
  108.   gpu.set(1, 7, 'Активный слот дрона - ')
  109.   gpu.set(1, 8, 'Количество предметов в активном слоте - ')
  110.   gpu.set(1, 9, 'Управления с учётом направления взгляда - ')
  111.   gpu.setForeground(colors.green)
  112.   gpu.set(7, 4, ''..step)
  113.   send('getAcceleration')
  114.   gpu.set(26, 6, ''..sideInt)
  115.   send('select', 1)
  116.   gpu.set(43, 9, ''..tostring(navigationSides))
  117.   gpu.setForeground(colors.white)
  118. end
  119.  
  120. local function initInterface()
  121.   gpu.setResolution(wRes, hRes)
  122.   gpu.setBackground(colors.black)
  123.   term.clear()
  124.   gpu.setBackground(colors.gray)
  125.   gpu.fill(1, 1, wRes, 1, ' ')
  126.   gpu.fill(1, hRes, wRes, 1, ' ')
  127.   gpu.setForeground(colors.yellow)
  128.   gpu.set((wRes - 30) / 2, 1, 'Universal drone remote control')
  129.   gpu.setForeground(colors.green)
  130.   gpu.set(1, hRes, 'Q')
  131.   gpu.set(24, hRes, 'M')
  132.   gpu.setForeground(colors.white)
  133.   gpu.set(3, hRes, '- закрыть программу;')
  134.   gpu.set(26, hRes, '- показать инструкцию;')
  135.   gpu.setBackground(colors.black)
  136.   infoInit()
  137. end
  138.  
  139. local function initialization()
  140.   if com.isAvailable('modem') then
  141.     m = com.modem
  142.     m.open(receivePort)
  143.     event.listen('modem_message', droneMsg)
  144.     if com.isAvailable('navigation') then
  145.       nav = com.navigation
  146.     end
  147.     initInterface()
  148.   else
  149.     io.stderr:write('Плата беспроводной сети не обнаружена!')
  150.     os.exit()
  151.   end
  152. end
  153.  
  154. local function manual()
  155.   if gpu.getResolution() == gpu.maxResolution() then
  156.     gpu.fill(1, 2, wRes, hRes - 2, ' ')
  157.     gpu.setForeground(colors.green)
  158.     gpu.set(1, 3, 'WASD')
  159.     gpu.set(1, 4, 'LShift/LCtrl')
  160.     gpu.set(1, 5, 'R/F')
  161.     gpu.set(1, 6, 'T/G')
  162.     gpu.set(1, 7, 'Y/H')
  163.     gpu.set(1, 8, 'C/X')
  164.     gpu.set(1, 9, 'U/J')
  165.     gpu.set(1, 10, 'I/K')
  166.     gpu.set(1, 11, 'O/L')
  167.     gpu.set(1, 12, '1-8')
  168.     gpu.set(1, 13, 'B')
  169.     gpu.set(1, 14, 'N')
  170.     gpu.set(1, 15, 'P')
  171.     gpu.set(1, 16, 'V')
  172.     gpu.set(1, 17, 'Z')
  173.     gpu.set(1, 18, 'E')
  174.     gpu.setForeground(colors.white)
  175.     gpu.set(6, 3, '- горизонтальное перемещение')
  176.     gpu.set(14, 4, '- вверх/вниз')
  177.     gpu.set(5, 5, '- сломать/установить блок')
  178.     gpu.set(5, 6, '- захватить/выбросить предмет')
  179.     gpu.set(5, 7, '- захватить все возможные предметы/выбросить все возможные предметы')
  180.     gpu.set(5, 8, '- попытаться захватить со всех сторон существо поводком/сбросить')
  181.     gpu.set(5, 9, '- увеличть/уменишить шаг')
  182.     gpu.set(5, 10, '- увеличть/уменишить ускорение')
  183.     gpu.set(5, 11, '- изменить сторону взаимодействия')
  184.     gpu.set(5, 12, '- сделать активным слот')
  185.     gpu.set(3, 13, '- перемещение на координаты')
  186.     gpu.set(3, 14, '- вкл/выкл управление с учётом направления взгляда')
  187.     gpu.set(3, 15, '- показать расстояние к дрону')
  188.     gpu.set(3, 16, '- установить случайный цвет дрона')
  189.     gpu.set(3, 17, '- выкл/вкл дрон')
  190.     gpu.set(3, 18, '- уменьшить/увеличить экран')
  191.     gpu.set(1, 20, 'Нажмите любую клавишу что бы скрыть инструкцию...')
  192.     event.pull('key_down')
  193.     infoInit()
  194.   end
  195. end
  196.  
  197. local function changeResolution()
  198.   if gpu.getResolution() == gpu.maxResolution() then
  199.     gpu.setResolution(2, 2)
  200.   else
  201.     initInterface()
  202.   end
  203. end
  204.  
  205. local function changeStep(plus)
  206.   if plus then
  207.     if step < 1 then
  208.       step = step + 0.1
  209.     elseif step >= 1 and step < 10 then
  210.       step = step + 1
  211.     elseif step >= 10 and step < 100 then
  212.       step = step + 10
  213.     elseif step >= 100 and step < 1000 then
  214.       step = step + 100
  215.     end
  216.   else
  217.     if step > 0.3 and step <= 1 then
  218.       step = step - 0.1
  219.     elseif step > 1 and step <= 10 then
  220.       step = step - 1
  221.     elseif step > 10 and step <= 100 then
  222.       step = step - 10
  223.     elseif step > 100 and step <= 1000 then
  224.       step = step - 100
  225.     end
  226.   end
  227.   gpu.fill(7, 4, 4, 1, ' ')
  228.   gpu.setForeground(colors.green)
  229.   gpu.set(7, 4, ''..step)
  230.   gpu.setForeground(colors.white)
  231. end
  232.  
  233. local function changeAcceleration(plus)
  234.   if plus then
  235.     if acceleration < 2 then
  236.       acceleration = acceleration + 0.5
  237.       send('setAcceleration', acceleration)
  238.     end
  239.   else
  240.     if acceleration > 0.5 then
  241.       acceleration = acceleration - 0.5
  242.       send('setAcceleration', acceleration)
  243.     end
  244.   end
  245. end
  246.  
  247. local function changeSideInt(plus)
  248.   if plus then
  249.     if sideInt < 5 then
  250.       sideInt = sideInt + 1
  251.     end
  252.   else
  253.     if sideInt > 0 then
  254.       sideInt = sideInt - 1
  255.     end
  256.   end
  257.   gpu.setForeground(colors.green)
  258.   gpu.set(26, 6, ''..sideInt)
  259.   gpu.setForeground(colors.white)
  260. end
  261.  
  262. local function changeNavigation()
  263.   if navigationSides then
  264.     navigationSides = false
  265.   else
  266.     if nav then
  267.       navigationSides = true
  268.     else
  269.       term.setCursor(1, 12)
  270.       io.stderr:write('Улучшение "Навигация" отсуствует')
  271.     end
  272.   end
  273.   gpu.fill(43, 9, 5, 1, ' ')
  274.   gpu.setForeground(colors.green)
  275.   gpu.set(43, 9, ''..tostring(navigationSides))
  276.   gpu.setForeground(colors.white)
  277. end
  278.  
  279. local function goTo()
  280.   term.setCursor(1, 12)
  281.   io.write('Введите координаты дрона:\n')
  282.   io.write('Позиция X = ')
  283.   local posX = tonumber(io.read()) or 0
  284.   io.write('Позиция Z = ')
  285.   local posZ = tonumber(io.read()) or 0
  286.   io.write('Введите координаты точки назначения:\n')
  287.   io.write('Позиция X = ')
  288.   local goX = tonumber(io.read()) or 0
  289.   io.write('Позиция Z = ')
  290.   local goZ = tonumber(io.read()) or 0
  291.   io.write('Введите высоту полёта на которой нет препятсвий для робота:\n')
  292.   io.write('Высота Y = ')
  293.   local goY = tonumber(io.read()) or 0
  294.   io.write('\nПеремещение на высоту: Y = '..goY..';')
  295.   send('move', 0, goY, 0)
  296.   os.sleep(goY / 10)
  297.   io.write('\nПеремещение на координаты: X = '..goX..'; Z = '..goZ..';')
  298.   send('move', goX - posX, 0, goZ - posZ)
  299.   os.sleep(5)
  300.   gpu.fill(1, 12, wRes, 11, ' ')
  301. end
  302.  
  303. local function droneMove(side)
  304.   local playerDir = nav.getFacing()
  305.   local x = tonumber(navSides[side][playerDir]['x'] and navSides[side][playerDir]['x']..step or 0)
  306.   local z = tonumber(navSides[side][playerDir]['z'] and navSides[side][playerDir]['z']..step or 0)
  307.   send('move', x, 0, z)
  308. end
  309.  
  310. local commands = {
  311.   ['w'] = function() if navigationSides then droneMove('forward') else send('move', step, 0, 0) end end,
  312.   ['s'] = function() if navigationSides then droneMove('back') else send('move', -step, 0, 0) end end,
  313.   ['a'] = function() if navigationSides then droneMove('left') else send('move', 0, 0, -step) end end,
  314.   ['d'] = function() if navigationSides then droneMove('right') else send('move', 0, 0, step) end end,
  315.   ['lshift'] = function() send('move', 0, step, 0) end,
  316.   ['lcontrol'] = function() send('move', 0, -step, 0) end,
  317.   ['r'] = function() send('swing', sideInt) end,
  318.   ['f'] = function() send('place', sideInt) end,
  319.   ['t'] = function() send('suck', sideInt) end,
  320.   ['g'] = function() send('drop', sideInt) end,
  321.   ['y'] = function() send('suckAll', sideInt) end,
  322.   ['h'] = function() send('dropAll', sideInt) end,
  323.   ['c'] = function() send('leash') end,
  324.   ['x'] = function() send('unleash') end,
  325.   ['u'] = function() changeStep(true) end,
  326.   ['j'] = function() changeStep() end,
  327.   ['o'] = function() changeSideInt(true) end,
  328.   ['l'] = function() changeSideInt() end,
  329.   ['i'] = function() changeAcceleration(true) end,
  330.   ['k'] = function() changeAcceleration() end,
  331.   ['1'] = function() send('select', 1) end,
  332.   ['2'] = function() send('select', 2) end,
  333.   ['3'] = function() send('select', 3) end,
  334.   ['4'] = function() send('select', 4) end,
  335.   ['5'] = function() send('select', 5) end,
  336.   ['6'] = function() send('select', 6) end,
  337.   ['7'] = function() send('select', 7) end,
  338.   ['8'] = function() send('select', 8) end,
  339.   ['b'] = function() goTo() end,
  340.   ['n'] = function() changeNavigation() end,
  341.   ['p'] = function() send('getPing') end,
  342.   ['v'] = function() send('changeColor', math.random(0x0, 0xFFFFFF)) end,
  343.   ['z'] = function() send('droneSwitch') end,
  344.   ['e'] = function() changeResolution() end,
  345.   ['m'] = function() manual() end,
  346.   ['q'] = function() programExit() end
  347. }
  348.  
  349. local function start()
  350.   initialization()
  351.   while true do
  352.     local event, _, _, code = event.pull('key_down')
  353.     local key = keyboard.keys[code]
  354.     for command in pairs(commands) do
  355.       if command == key then
  356.           commands[key]()
  357.         break
  358.       end
  359.     end
  360.   end
  361. end
  362.  
  363. start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement