From 307c513a9b72df99a184981a5c167ae646a69392 Mon Sep 17 00:00:00 2001 From: osmarks Date: Sun, 5 Feb 2023 21:59:30 +0000 Subject: [PATCH] changes to things --- aidans_bad_code.py | 13 +++++ isqgrav.html | 67 ++++++++++++++++++++++++++ pid-controller.py | 43 +++++++++++++++++ square-law gravity - physics test.html | 22 ++++++--- 4 files changed, 139 insertions(+), 6 deletions(-) create mode 100644 aidans_bad_code.py create mode 100644 isqgrav.html create mode 100644 pid-controller.py diff --git a/aidans_bad_code.py b/aidans_bad_code.py new file mode 100644 index 0000000..bd84774 --- /dev/null +++ b/aidans_bad_code.py @@ -0,0 +1,13 @@ +class Primes: + def __init__(self, max): + self.internal = range(2,max+1) + + def __next__(self): + i = self.internal.__iter__().__next__() + self.internal = filter(lambda n : n % i != 0, self.internal) + return i + + def __iter__(self): return self + +for i in Primes(100): + print(i) \ No newline at end of file diff --git a/isqgrav.html b/isqgrav.html new file mode 100644 index 0000000..3c72c6a --- /dev/null +++ b/isqgrav.html @@ -0,0 +1,67 @@ + + + +
+ \ No newline at end of file diff --git a/pid-controller.py b/pid-controller.py new file mode 100644 index 0000000..9148785 --- /dev/null +++ b/pid-controller.py @@ -0,0 +1,43 @@ +import time +import collections + +PIDState = collections.namedtuple("PIDState", ["Kp", "Ki", "Kd", "last_time", "integral", "last_error"]) + +def init(Kp, Ki, Kd): return PIDState(Kp, Ki, Kd, None, 0, None) +def step(state, error, deriv, ts=None): + ts = ts if ts is not None else time.time() + integral = state.integral + if state.last_time: + tdiff = ts - state.last_time + integral += 0.5 * (state.last_error + error) * tdiff # approximate actually integrating using a trapzeium + output = state.Kp * error + state.Ki * integral + state.Kd * deriv + return PIDState(Kp=state.Kp, Ki=state.Ki, Kd=state.Kd, last_time=ts, integral=integral, last_error=error), output + +if __name__ == "__main__": + import matplotlib.pyplot as plt, numpy as np + def extract_series(l, ix): return list(map(lambda x: x[ix], l)) + values = [(10, -10, 0, 0, 0)] + state = init(10, 4, -0.3) + setpoint = -5 + max_time = 2 + timestep = 0.05 + times = np.arange(0, max_time, timestep) + for t in times: + current_pv = values[-1][0] + error = setpoint - current_pv + deriv = (error - (state.last_error or 0)) / timestep + state, output = step(state, error, deriv, ts=t) + output = max(min(output, 10), -10) + print(output, current_pv, error) + new_pv = current_pv + (output + 1) * 0.05 + values.append((new_pv, error, output, state.integral, deriv)) + #print(values) + values = values[1:] + plt.axis([0, max_time, -10, 10]) + plt.plot(times, extract_series(values, 0), label="PV") + plt.plot(times, extract_series(values, 1), label="error") + plt.plot(times, extract_series(values, 2), label="output") + plt.plot(times, extract_series(values, 3), label="integ") + plt.plot(times, extract_series(values, 4), label="deriv") + plt.legend() + plt.show() \ No newline at end of file diff --git a/square-law gravity - physics test.html b/square-law gravity - physics test.html index 211ab2f..6c66504 100644 --- a/square-law gravity - physics test.html +++ b/square-law gravity - physics test.html @@ -9,6 +9,7 @@ const centerY = canv.height / 2 const scale = 0.5 const G = 0.1 + const stepsPerSecond = 1000 const vzero = [0, 0] const vadd = ([a, b], [c, d]) => [a + c, b + d] @@ -35,18 +36,27 @@ if (!timestamp) { return } - const timestep = (timestamp - previousPreviousTimestamp) / 1000 + const deltaT = (timestamp - previousPreviousTimestamp) / 1000 + const stepCount = Math.min(Math.ceil(deltaT * stepsPerSecond), 20) + const timestep = deltaT / stepCount + console.log(stepCount, deltaT) + + for (let j = 0; j < stepCount; j++) { + for (const object of objects) { + object.x = vadd(object.x, vscale(timestep, object.v)) + const F = vsum(objects.filter(x => x !== object).map(x => + vscale(G * (object.m * x.m) * (vmag(vsub(object.x, x.x)) ** 2), vnorm(vsub(object.x, x.x))) + )) + object.v = vadd(object.v, vscale(timestep, vscale(1 / object.m, F))) + //console.log(F, object.x, object.v) + } + } ctx.fillStyle = "black" ctx.fillRect(0, 0, canv.width, canv.height) var i = 0 for (const object of objects) { - object.x = vadd(object.x, vscale(timestep, object.v)) - const F = vsum(objects.filter(x => x !== object).map(x => - vscale(G * (object.m * x.m) * (vmag(vsub(object.x, x.x)) ** 2), vnorm(vsub(object.x, x.x))) - )) - object.v = vadd(object.v, vscale(timestep, vscale(1 / object.m, F))) //console.log(F, object.x, object.v) ctx.beginPath() const disp = vscale(scale, object.x)