kopia lustrzana https://github.com/jamesgao/kiln_controller
Attempting to fix #1
rodzic
ea40bee4af
commit
e441599847
|
@ -3,6 +3,13 @@ import time
|
||||||
import random
|
import random
|
||||||
import warnings
|
import warnings
|
||||||
|
|
||||||
|
class StateMachine(object):
|
||||||
|
def __init__(self):
|
||||||
|
import stepper
|
||||||
|
import thermo
|
||||||
|
self.monitor = thermo.Monitor()
|
||||||
|
self.regulator = stepper.Regulator()
|
||||||
|
|
||||||
class KilnController(object):
|
class KilnController(object):
|
||||||
def __init__(self, schedule, monitor, interval=5, start_time=None, Kp=.025, Ki=.01, Kd=.001, simulate=True):
|
def __init__(self, schedule, monitor, interval=5, start_time=None, Kp=.025, Ki=.01, Kd=.001, simulate=True):
|
||||||
self.schedule = schedule
|
self.schedule = schedule
|
||||||
|
|
|
@ -47,14 +47,14 @@ class WebApp(object):
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
try:
|
try:
|
||||||
import thermo
|
import thermo
|
||||||
monitor = thermo.Monitor()
|
monitor = thermo.Monitor()
|
||||||
|
|
||||||
app = WebApp(monitor)
|
app = WebApp(monitor)
|
||||||
def send_temp(time, temp):
|
def send_temp(time, temp):
|
||||||
app.send(dict(time=time, temp=temp))
|
app.send(dict(time=time, temp=temp))
|
||||||
monitor.callback = send_temp
|
monitor.callback = send_temp
|
||||||
monitor.start()
|
monitor.start()
|
||||||
app.run()
|
app.run()
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
monitor.stop()
|
monitor.stop()
|
||||||
|
|
|
@ -5,33 +5,46 @@ var tempgraph = (function(module) {
|
||||||
this.scalefunc = module.temp_to_F;
|
this.scalefunc = module.temp_to_F;
|
||||||
this.temp_suffix = "°F"
|
this.temp_suffix = "°F"
|
||||||
this.temp_prefix = ""
|
this.temp_prefix = ""
|
||||||
this._mapped = this.temperature.map(this._map_temp.bind(this));
|
|
||||||
|
|
||||||
this.graph = new tempgraph.Graph();
|
this.graph = new tempgraph.Graph();
|
||||||
this.graph.plot(initial.map(this._map_temp.bind(this)), "temperature", false);
|
this._mapped = this.temperature.map(this._map_temp.bind(this));
|
||||||
|
this.graph.plot(this._mapped, "temperature", false);
|
||||||
|
|
||||||
var latest = this.temperature[this.temperature.length-1];
|
this.update_temp(this.last());
|
||||||
this.update_temp({time:latest.x/1000., temp:latest.y});
|
|
||||||
this._bindUI();
|
this._bindUI();
|
||||||
}
|
}
|
||||||
module.Monitor.prototype.update_temp = function(data) {
|
module.Monitor.prototype.update_temp = function(data) {
|
||||||
var now = new Date(data.time*1000.);
|
var now = new Date(data.time*1000.);
|
||||||
var nowstr = now.getHours() % 12 + ":" + now.getMinutes() + (now.getHours() > 12 ? " pm" : " am");
|
|
||||||
var temp = this.scalefunc(data.temp);
|
var temp = this.scalefunc(data.temp);
|
||||||
|
|
||||||
|
var minstr = now.getMinutes();
|
||||||
|
minstr = minstr.length < 2 ? "0"+minstr : minstr;
|
||||||
|
var nowstr = now.getHours() % 12 + ":" + minstr + (now.getHours() > 12 ? " pm" : " am");
|
||||||
$("#current_time").text(nowstr);
|
$("#current_time").text(nowstr);
|
||||||
$("#current_temp").text(this.temp_prefix+temp+this.temp_suffix);
|
$("#current_temp").text(this.temp_prefix+temp+this.temp_suffix);
|
||||||
if (now > this.temperature[this.temperature.length-1].x) {
|
|
||||||
this.temperature.push({x:now, y:+data.temp});
|
//Adjust x and ylims
|
||||||
|
if (now > this.last().time) {
|
||||||
|
this.temperature.push(data);
|
||||||
this._mapped.push({x:now, y:temp});
|
this._mapped.push({x:now, y:temp});
|
||||||
|
|
||||||
var lims = this.graph.xlim();
|
var lims = this.graph.x.domain();
|
||||||
|
//incoming sample needs to shift xlim
|
||||||
if (now > lims[1]) {
|
if (now > lims[1]) {
|
||||||
var start = new Date(now.getTime() - lims[1].getTime() + lims[0].getTime());
|
var start = new Date(now.getTime() - lims[1].getTime() + lims[0].getTime());
|
||||||
this.graph.xlim(start, now);
|
this.graph.x.domain([start, now]);
|
||||||
|
//If incoming sample is higher or lower than the ylims, expand that as well
|
||||||
|
var ylims = this.graph.y.domain(), range = 2*(ylims[1] - ylims[0]);
|
||||||
|
if (temp >= ylims[1]) {
|
||||||
|
this.graph.y.domain([ylims[0], ylims[0]+range]);
|
||||||
|
} else if (temp <= ylims[0]) {
|
||||||
|
this.graph.y.domain([ylims[1]-range, ylims[1]]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
this.graph.update("temperature", this._mapped);
|
this.graph.update("temperature", this._mapped);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//update the output slider and text, if necessary
|
||||||
if (data.output !== undefined) {
|
if (data.output !== undefined) {
|
||||||
$("#current_output_text").text(data.output*100+"%");
|
$("#current_output_text").text(data.output*100+"%");
|
||||||
$("#current_output").val(data.output*1000);
|
$("#current_output").val(data.output*1000);
|
||||||
|
@ -42,6 +55,9 @@ var tempgraph = (function(module) {
|
||||||
}
|
}
|
||||||
module.Monitor.prototype.setProfile = function(profile) {
|
module.Monitor.prototype.setProfile = function(profile) {
|
||||||
}
|
}
|
||||||
|
module.Monitor.prototype.last = function() {
|
||||||
|
return this.temperature[this.temperature.length-1];
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
module.Monitor.prototype.setScale = function(scale) {
|
module.Monitor.prototype.setScale = function(scale) {
|
||||||
|
@ -69,21 +85,23 @@ var tempgraph = (function(module) {
|
||||||
}
|
}
|
||||||
this._mapped = this.temperature.map(this._map_temp.bind(this));
|
this._mapped = this.temperature.map(this._map_temp.bind(this));
|
||||||
this.graph.y.domain(d3.extent(this._mapped, function(d) { return d.y; }));
|
this.graph.y.domain(d3.extent(this._mapped, function(d) { return d.y; }));
|
||||||
var latest = this.temperature[this.temperature.length-1];
|
|
||||||
this.update_temp({time:latest.x/1000., temp:latest.y});
|
this.update_temp(this.last());
|
||||||
this.graph.update("temperature", this._mapped);
|
this.graph.update("temperature", this._mapped);
|
||||||
}
|
}
|
||||||
|
|
||||||
module.Monitor.prototype._map_temp = function(d) {
|
module.Monitor.prototype._map_temp = function(d) {
|
||||||
return {x:d.x, y:this.scalefunc(d.y)};
|
return {x:new Date(d.time*1000), y:this.scalefunc(d.temp)};
|
||||||
}
|
}
|
||||||
module.Monitor.prototype._bindUI = function() {
|
module.Monitor.prototype._bindUI = function() {
|
||||||
var sock = new WebSocket("ws://"+window.location.hostname+":"+window.location.port+"/ws/", "protocolOne");
|
try {
|
||||||
|
var sock = new WebSocket("ws://"+window.location.hostname+":"+window.location.port+"/ws/", "protocolOne");
|
||||||
|
|
||||||
sock.onmessage = function(event) {
|
sock.onmessage = function(event) {
|
||||||
var data = JSON.parse(event.data);
|
var data = JSON.parse(event.data);
|
||||||
this.update_temp(data);
|
this.update_temp(data);
|
||||||
}.bind(this);
|
}.bind(this);
|
||||||
|
} catch (e) {}
|
||||||
|
|
||||||
$("#temp_scale_C").click(function() { this.setScale("C");}.bind(this));
|
$("#temp_scale_C").click(function() { this.setScale("C");}.bind(this));
|
||||||
$("#temp_scale_F").click(function() { this.setScale("F");}.bind(this));
|
$("#temp_scale_F").click(function() { this.setScale("F");}.bind(this));
|
||||||
|
@ -103,12 +121,6 @@ var tempgraph = (function(module) {
|
||||||
}(tempgraph || {}));
|
}(tempgraph || {}));
|
||||||
|
|
||||||
d3.json("data.json", function(error, data) {
|
d3.json("data.json", function(error, data) {
|
||||||
var newdata = [], d;
|
monitor = new tempgraph.Monitor(data);
|
||||||
for (var i = 0; i < data.length; i+=4) {
|
|
||||||
d = data[i];
|
|
||||||
newdata.push({x:new Date(d.time*1000), y:+d.temp});
|
|
||||||
}
|
|
||||||
|
|
||||||
monitor = new tempgraph.Monitor(newdata);
|
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
Ładowanie…
Reference in New Issue