motionbad4.lua

by 6_4
550 days ago
COPY
1
local peripheralCallNative = select(2, debug.getupvalue(peripheral.call, 2)).call
2
local ni = peripheral.wrap('back')
3
if not fs.exists("event.lua") then shell.run("wget https://raw.githubusercontent.com/kepler155c/opus/develop-1.8/sys/modules/opus/event.lua") end
4
local Event = require("/event")
5
local sensitivity = .05
6
local top = .2 -- tolerance?
7
-- 1.0175
8
local gravAdjust = (1.0175/0.98)
9
local yScPower = 0.5    -- Plethora config
10
local mcGrav = 0.08     -- Minecraft gravity
11
local mcGravAdj = 0.98  -- Minecraft gravity
12
-- Computed Values:
13
local myAdjust = mcGrav * mcGravAdj
14
local grav = myAdjust * gravAdjust
15

16
local wantedPosition = -30
17

18
local function queueCmd(results, fn, ...)
19
	local argz = {...}
20
	if not results then
21
		Event.addRoutine(function()
22
			peripheralCallNative("back", fn, unpack(argz))
23
		end):terminate()
24
	else
25
		Event.addRoutine(function()
26
			results(peripheralCallNative("back", fn, unpack(argz)))
27
		end)
28
	end
29
end
30

31
local lastQueuedPower = 0
32
local lastCompleted = os.clock()
33

34
local function launch(yPower)
35
	lastQueuedPower = yPower
36

37
	yPower = yPower * (1/yScPower)
38
	local aM = math.min(math.abs(yPower), 4)
39

40
	local pitch = (yPower >= 0) and -90 or 90
41

42
	queueCmd(function()
43
		lastCompleted = os.clock()
44
	end, 'launch', 0, pitch, aM)
45
end
46

47
local function correctMY(my)
48
	my = my - myAdjust
49
	local isTrue = lastCompleted == os.clock()
50
	return isTrue and my or my + (lastQueuedPower)
51
end
52

53
local totalDeviation = 0
54
local wantedTarget = 10
55
local lastTime = os.epoch("utc")
56
local lastTime2 = os.epoch("utc")
57
Event.onInterval(0.05, function()
58
	local thisTime2 = os.epoch("utc")
59
	local tt2 = thisTime2 - lastTime2
60
	lastTime2 = os.epoch("utc")
61
	-- Queue happens instantly
62
	queueCmd(function(r)
63
		local thisTime = os.epoch("utc")
64
		local ogmy = r.motionY
65
		local my = correctMY(r.motionY)
66
		local dpy = r.deltaPosY
67
		totalDeviation = totalDeviation + dpy
68

69
		local errorP = wantedTarget - totalDeviation
70
		local wantedVel = math.abs(errorP) < top and 0 or errorP * sensitivity
71

72
		local errorV = (wantedVel - my)
73
		local power = grav + errorV
74

75
		launch(power)
76
		print(("%.3f %.3f %.3f %.3f %.3f %d %d"):format(wantedVel, my, power, dpy, totalDeviation, thisTime - lastTime, tt2))
77
		lastTime = thisTime
78
	end, 'getMetaOwner')
79
end)
80

81
Event.pullEvents()
82