Attempting to fix #1

master
James Gao 2014-10-19 21:34:17 -07:00
rodzic ea40bee4af
commit e441599847
3 zmienionych plików z 51 dodań i 32 usunięć

Wyświetl plik

@ -3,6 +3,13 @@ import time
import random
import warnings
class StateMachine(object):
def __init__(self):
import stepper
import thermo
self.monitor = thermo.Monitor()
self.regulator = stepper.Regulator()
class KilnController(object):
def __init__(self, schedule, monitor, interval=5, start_time=None, Kp=.025, Ki=.01, Kd=.001, simulate=True):
self.schedule = schedule

Wyświetl plik

@ -47,14 +47,14 @@ class WebApp(object):
if __name__ == "__main__":
try:
import thermo
monitor = thermo.Monitor()
import thermo
monitor = thermo.Monitor()
app = WebApp(monitor)
def send_temp(time, temp):
app.send(dict(time=time, temp=temp))
monitor.callback = send_temp
monitor.start()
app.run()
app = WebApp(monitor)
def send_temp(time, temp):
app.send(dict(time=time, temp=temp))
monitor.callback = send_temp
monitor.start()
app.run()
except KeyboardInterrupt:
monitor.stop()

Wyświetl plik

@ -5,33 +5,46 @@ var tempgraph = (function(module) {
this.scalefunc = module.temp_to_F;
this.temp_suffix = "°F"
this.temp_prefix = ""
this._mapped = this.temperature.map(this._map_temp.bind(this));
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({time:latest.x/1000., temp:latest.y});
this.update_temp(this.last());
this._bindUI();
}
module.Monitor.prototype.update_temp = function(data) {
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 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_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});
var lims = this.graph.xlim();
var lims = this.graph.x.domain();
//incoming sample needs to shift xlim
if (now > lims[1]) {
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);
}
//update the output slider and text, if necessary
if (data.output !== undefined) {
$("#current_output_text").text(data.output*100+"%");
$("#current_output").val(data.output*1000);
@ -42,6 +55,9 @@ var tempgraph = (function(module) {
}
module.Monitor.prototype.setProfile = function(profile) {
}
module.Monitor.prototype.last = function() {
return this.temperature[this.temperature.length-1];
}
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.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);
}
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() {
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) {
var data = JSON.parse(event.data);
this.update_temp(data);
}.bind(this);
sock.onmessage = function(event) {
var data = JSON.parse(event.data);
this.update_temp(data);
}.bind(this);
} catch (e) {}
$("#temp_scale_C").click(function() { this.setScale("C");}.bind(this));
$("#temp_scale_F").click(function() { this.setScale("F");}.bind(this));
@ -103,12 +121,6 @@ var tempgraph = (function(module) {
}(tempgraph || {}));
d3.json("data.json", function(error, data) {
var newdata = [], d;
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);
monitor = new tempgraph.Monitor(data);
});