Skip to content

Commit d08a300

Browse files
committed
Fixed various bugs in TC sim
1 parent 59403d7 commit d08a300

1 file changed

Lines changed: 25 additions & 20 deletions

File tree

  • src/Theories/Unofficial-CTs

src/Theories/Unofficial-CTs/TC.ts

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ class tcSim extends theoryClass<theory> {
113113
this.kp = pidSettings[0];
114114
this.ki = pidSettings[1];
115115
this.kd = pidSettings[2];
116-
this.T = 100;
116+
this.T = 30;
117117
this.setPoint = pidSettings[3];
118118

119119
this.achievementMulti = this.lastPub >= 750 ? 30 : this.lastPub >= 600 ? 10 : 1;
@@ -136,7 +136,7 @@ class tcSim extends theoryClass<theory> {
136136
this.forcedPubConditions.push(() => this.pubRho >= this.lastPub);
137137
this.simEndConditions.push(() => this.curMult > 15);
138138
for (let i=7; i<11; i++) {
139-
while (this.lastPub <= this.variables[i].cost && this.variableAvailability[i]()) {
139+
while (this.variables[i].cost <= this.lastPub && this.variableAvailability[i]()) {
140140
this.variables[i].buy();
141141
}
142142
}
@@ -149,7 +149,7 @@ class tcSim extends theoryClass<theory> {
149149
if (!global.simulating) break;
150150
this.tick();
151151
this.updateSimStatus();
152-
if (this.lastPub < 500) this.updateMilestones();
152+
this.updateMilestones();
153153
this.buyVariables();
154154
}
155155
this.trimBoughtVars();
@@ -164,11 +164,11 @@ class tcSim extends theoryClass<theory> {
164164
const area = 0.024; // area of element (m^2)
165165
const mass = 10; // grams
166166
const Tc = 30;
167-
if (this.milestones[1] >= 1)
168-
this.ki = 0;
169-
if (this.milestones[2] >= 3)
170-
this.kd = 0;
167+
168+
const vki = this.milestones[1] > 0 ? this.ki : 0;
169+
const vkd = this.milestones[2] > 0 ? this.kd : 0;
171170
this.timer += this.systemDt;
171+
172172
if (this.timer > this.frequency) {
173173
this.T = this.amplitude;
174174
this.timer = 0;
@@ -182,31 +182,36 @@ class tcSim extends theoryClass<theory> {
182182
// Anti-windup scheme
183183
if (this.integral > 100) this.integral = 100;
184184
if (this.integral < -100) this.integral = -100;
185-
let output = Math.round(Math.max(0, Math.min(this.kp * this.error[0] + this.ki * this.integral + this.kd * derivative, 512))) / 512; // range 0-512
185+
const output = Math.round(Math.max(0, Math.min(this.kp * this.error[0] + vki * this.integral + vkd * derivative, 512))); // range 0-512
186186

187187
// Heating simulation
188-
const dT = Math.abs(1 / mass / Cp * (Q * output - (this.T - Tc) * h * area));
189-
const exponentialTerm = (Q * output - h * area * (this.T - Tc)) * Math.pow(Math.E, -1 * this.systemDt / mass / Cp);
190-
this.T = Tc + (Q * output - exponentialTerm) / (h * area);
188+
const suppliedHeat = Q * output / 512;
189+
const dT = Math.abs(1 / mass / Cp * (suppliedHeat - (this.T - Tc) * h * area));
190+
const exponentialTerm = (suppliedHeat - h * area * (this.T - Tc)) * Math.pow(Math.E, -1 * this.systemDt / mass / Cp);
191+
this.T = Tc + (suppliedHeat - exponentialTerm) / (h * area);
191192

192193
// Variable calculation
193-
const c1exp = this.variables[7].value;
194-
const r1exp = this.variables[8].value;
195-
const r2exp = this.variables[9].value;;
196-
const dr =
197-
this.variables[1].value * r1exp +
198-
this.variables[2].value * r2exp -
199-
l10(1 + l10(1 + Math.abs(this.error[0])));
200-
194+
// P Update
201195
if (this.achievementMulti == 30) {
202196
let dP =
203197
this.variables[5].value +
204198
this.variables[6].value +
205-
Math.E * (-0.01 * Math.pow(0.8, this.milestones[4]) * Math.abs(this.T - 100));
199+
Math.LOG10E * (-0.01 * Math.pow(0.8, this.milestones[4])) +
200+
l10(Math.abs(this.T - 100));
206201
this.P = add(this.P, dP + l10(this.dt));
207202
}
208203

204+
// R Update
205+
const r1exp = this.variables[8].value;
206+
const r2exp = this.variables[9].value;
207+
const dr =
208+
this.variables[1].value * r1exp +
209+
this.variables[2].value * r2exp -
210+
l10(1 + l10(1 + Math.abs(this.error[0])));
209211
this.r = add(this.r, dr + l10(this.dt));
212+
213+
// rho update
214+
const c1exp = this.variables[7].value;
210215
const vc1 = this.variables[0].value * c1exp;
211216
const vc2 = this.milestones[4] > 0 ? this.variables[3].value : 0;
212217
const mrexp = this.milestones[3];

0 commit comments

Comments
 (0)