View difference between Paste ID: qxpwJfn5 and YU3xb5VY
SHOW: | | - or go back to the newest paste.
1-
--Automated Cruise Control v2 by Gazer29
1+
--Engine Control v2 by Gazer29
2-
--Uses Automation mod version: Beta 1
2+
--Uses Automation mod
3
 
4
local component = require("component")
5
local term = require ("term")
6
local entity = component.entity_link.getAPI("immersiverailroading:locomotivediesel")
7
local thread = require("thread")
8
require("PIDController")
9
local serial = require("serialization")
10
local fs = require("filesystem")
11-
local glassesTerminal = require("component").glasses
11+
12
local modem = component.modem
13
14
local ATC = false
15
local RUNNING = true
16
local MODE = 3 -- 0 SET, 
17-
local MODE = 0 -- 0 SET, 1 RAMP,
17+
18
local display = false
19
local iteration = 1
20
local timer = nil
21
22
local CONFIG_FILE = "/home/CMD.dat"
23
local CONFIG = nil
24
local DEFAULT_CONFIG = {}
25
local data = nil
26
27-
glassesTerminal.removeAll()
27+
28-
atext = glassesTerminal.addText2D()
28+
29-
atext.setText("ATC: OFF") -- no modifier!
29+
30-
atext.setFontSize(42)
30+
31-
atext.addColor(1, 0, 0, 1)
31+
32-
atext.setCondition(atext.addColor(0, 1, 0, 1), "OVERLAY_ACTIVE", true)
32+
33
end
34-
btext = glassesTerminal.addText2D()
34+
35-
btext.setText("+10")
35+
36-
btext.setFontSize(60)
36+
37-
btext.addColor(1,0,0,1)
37+
38-
btext.addTranslation(0, 10, 0)
38+
39-
btext.setCondition(btext.addColor(0, 1, 0, 1), "OVERLAY_ACTIVE", true)
39+
40
	local config = serial.unserialize(f:read("*a"))
41-
ctext = glassesTerminal.addText2D()
41+
42-
ctext.setText("-10")
42+
43-
ctext.setFontSize(60)
43+
44-
ctext.addColor(1,0,0,1)
44+
45-
ctext.addTranslation(0,18,0)
45+
46-
ctext.setCondition(ctext.addColor(0, 1, 0, 1), "OVERLAY_ACTIVE", true)
46+
47
	positions = entity.getLocation() 
48
	xpos = positions.getX()
49
	ypos = positions.getY()
50
	zpos = positions.getZ()
51
	print("X: 	", xpos)
52
	print("Z: 	", zpos)
53
end
54
55
function GetTrainSpeed()
56
	speed = entity.getCurrentSpeed()
57
	return speed
58
end
59-
	--print("Could not open " .. CONFIG_FILE .. ".")
59+
60-
	--print("Loading default config")
60+
61-
	--saveConfig(CONFIG_FILE, DEFAULT_CONFIG)
61+
62
	return uuid
63
end
64
65
function setThrottle(x)
66
	if 0 >= x then x = 0 end
67
	if 1 <= x then x = 1 end
68
	entity.setThrottleLevel(x)
69
	if display then
70-
function helpdisplay()
70+
71-
	print("AutomaticTrainControl v1")
71+
72-
	print("Commands:")
72+
73-
	print("'Any number'    - Changes set speed")
73+
74-
	print("'+' 		- Displays additional information")
74+
75-
	print("'-' 		- Hides additional information")
75+
76-
	print("'ramp'		- Changes to ramp mode, requires further input")
76+
77-
	print("'idle'		- Pauses train control")
77+
78-
	print("'auto'		- Automatic control")
78+
79-
	print("'stop' 		- Ends program")
79+
80
		end
81
end
82
83
local function main()
84
	while RUNNING do
85
        os.sleep(sleep)
86
		input = GetTrainSpeed()
87
        if data ~= nil then
88
            if data[uuid] ~= nil then
89
                setpoint = data[uuid].setspeed
90
                print(setpoint)
91
                PID:SetPoint(setpoint)
92
                end
93
            end
94
        PID:SetInput(input)
95
        PID:Compute()
96
        output = PID:GetOutput()
97
        BrakeOut = output * -1
98
        setThrottle(output)
99
        setBrake(BrakeOut)
100
		end
101
  end
102
  
103
function stop()
104
  RUNNING = false
105
end
106
   
107
local function handleMessage(host, msg)
108
  local packet = serial.unserialize(msg)
109
  if packet == nil then
110
    print("nil packet")
111
    return
112
  end
113
  if packet.t == "STATUS" then
114
        --print(packet.v)
115
    	data = packet.v
116
  else
117
    print("unknown command")
118
  end
119-
local function reads()
119+
120-
  while RUNNING do
120+
121-
    user = nil
121+
122-
    user = io.read()
122+
123-
	if user == "stop" then	
123+
124-
		print("STOPPING...")
124+
125-
		os.sleep(1)
125+
126-
		stop()
126+
        --print("recv")
127
        handleMessage(p2, p5)
128-
	if user == "." then
128+
129-
		term.clear()
129+
130
    modem.close(54421)
131-
	if user == "idle" then
131+
132-
		MODE = 2
132+
133
----------- Main loop ------------	
134-
    if user == "auto" then
134+
135-
		MODE = 3
135+
136
PID:new(0.01, 0.5, 0.0001, 0)
137-
	if user == "ramp" then
137+
138-
		print("Start Speed (type 'x' for current setpoint):")
138+
139-
		c = io.read()
139+
140-
		if c == "x" then
140+
141-
			c = setpoint
141+
142-
			end
142+
143-
		print("End Speed:")
143+
144-
		rampStart = c
144+
--main()
145-
		c = io.read()
145+
146-
		print("Ramp Time (seconds):")
146+
local ta = thread.create(main)
147-
		rampEnd = c
147+
148-
		c = io.read()
148+
149-
		rampTime = c
149+