-
Notifications
You must be signed in to change notification settings - Fork 0
/
engine.lua
102 lines (82 loc) · 3.19 KB
/
engine.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
local BATTERY_HEATING_FACTOR = 45
local BATTERY_HEAT_STEP = 0.0016
local BATTERY_COOL_STEP = 0.0009
local BATTERY_TEMP_THRESHOLD = 0.5
local IAS_COOLING_FACTOR = 0.0045
local MOTOR_HEATING_FACTOR = 50
local MOTOR_HEAT_STEP = 0.002
local MOTOR_COOL_STEP = 0.0045
local MOTOR_TEMP_THRESHOLD = 0.1
local MOTOR_MAX_TEMP = 50
local ESC_HEATING_FACTOR = 50
local ESC_HEAT_STEP = 0.001
local ESC_COOL_STEP = 0.008
local ESC_TEMP_THRESHOLD = 0.1
local ESC_MAX_TEMP = 40
local engine = {
state = {
temp = {
esc = 0,
motor = 0,
battery = 0,
}
}
}
engine.init = function(datarefs)
engine.state.temp.battery = datarefs.battery
engine.state.temp.motor = datarefs.motor
engine.state.temp.esc = datarefs.esc
end
engine.update = function(datarefs)
-- datarefs = {
-- oat,
-- battery_on
-- throttle
-- engineOpen
-- ias
-- }
------ Battery -----
local batteryTemp = datarefs.oat + datarefs.battery_on * datarefs.engineOpen * datarefs.throttle * BATTERY_HEATING_FACTOR
local batteryTempDiff = math.abs(batteryTemp - engine.state.temp.battery)
-- Don't change temp if it's near final temp
if (engine.state.temp.battery > batteryTemp - BATTERY_TEMP_THRESHOLD) and (engine.state.temp.battery < batteryTemp + BATTERY_TEMP_THRESHOLD) then
engine.state.temp.battery = batteryTemp
return
end
if (batteryTemp > engine.state.temp.battery) then
engine.state.temp.battery = engine.state.temp.battery + (batteryTempDiff * BATTERY_HEAT_STEP)
else
engine.state.temp.battery = engine.state.temp.battery - (batteryTempDiff * BATTERY_COOL_STEP) - datarefs.engineOpen * 0.05
end
------ Motor -----
local motorTemp = datarefs.oat + datarefs.battery_on * datarefs.throttle * MOTOR_HEATING_FACTOR - (IAS_COOLING_FACTOR * datarefs.ias)
local motorTempDiff = math.abs(motorTemp - engine.state.temp.motor)
-- Don't change temp if it's near final temp
if (engine.state.temp.motor > motorTemp - MOTOR_TEMP_THRESHOLD) and (engine.state.temp.motor < motorTemp + MOTOR_TEMP_THRESHOLD) then
engine.state.temp.motor = motorTemp
return
end
if (motorTemp > engine.state.temp.motor) then
engine.state.temp.motor = engine.state.temp.motor + (motorTempDiff * MOTOR_HEAT_STEP)
else
engine.state.temp.motor = engine.state.temp.motor - (motorTempDiff * MOTOR_COOL_STEP) - datarefs.engineOpen * 0.25
end
-- Cap at max temp
if (engine.state.temp.motor > MOTOR_MAX_TEMP + datarefs.oat) then engine.state.temp.motor = MOTOR_MAX_TEMP + datarefs.oat end
------ Electronic Speed Controller -----
local escTemp = datarefs.oat + datarefs.battery_on * datarefs.throttle * ESC_HEATING_FACTOR
local escTempDiff = math.abs(escTemp - engine.state.temp.esc)
-- Don't change temp if it's near final temp
if (engine.state.temp.esc > escTemp - ESC_TEMP_THRESHOLD) and (engine.state.temp.esc < escTemp + ESC_TEMP_THRESHOLD) then
engine.state.temp.esc = escTemp
return
end
if (escTemp > engine.state.temp.esc) then
engine.state.temp.esc = engine.state.temp.esc + (escTempDiff * ESC_HEAT_STEP)
else
engine.state.temp.esc = engine.state.temp.esc - (escTempDiff * ESC_COOL_STEP)
end
-- Cap at max temp
if (engine.state.temp.esc > ESC_MAX_TEMP + datarefs.oat) then engine.state.temp.esc = ESC_MAX_TEMP + datarefs.oat end
end
return engine