Replace PID.prototype.millis with PID.prototype.millis = function() { return getTime()*1000;}
Just use it like in the example (without the require):
var ctr = new PID(50, 66, 10,2,1, PID.DIRECT )
ctr.setMode(ctr.AUTOMATIC);
ctr.setTunings(10,2,1);
ctr.setOutputLimits(0,100);
ctr.setInput(temp);
If it works well, I guess we could add the module to Espruino?
Have you done much work in other Object oriented languages? It's probably wrong but I tend to think of the prototype system as follows (and it works for me :) :
function Foo() { ... } // the constructor (but also the name of the class)
Foo.prototype // a list of the class methods and 'static' properties
Foo.prototype.bar = function() { ... } // define one of the Class's methods
Foo.prototype.baz = 42; // define a value that is shared between all instances of the class. In C++/Java this would be called 'static'
var a = new Foo(); // An object of class Foo
var b = new Foo();
a.x = "Test";
console.log(b.x); // prints undefined - x wasn't in the prototype so is unique to the 'a' Object
console.log(a.baz); // prints 42 - from the prototype
a.baz = "Hello";
console.log(b.baz); // prints 'Hello' - baz was in the prototype, so it's shared between instances of Foo
Foo.prototype.hello = function() {
console.log(this.x); // access the Object with 'this'
}
a.hello(); // prints "Test". In the 'hello' function, 'this' is the same value as 'a'.
It all stems from some pretty simple rules in the interpreter. Each object is nothing more than a list of key/value pairs, and:
When you say foo = new Foo(), it effectively just does foo = Foo(); foo.__proto__ = Foo.prototype.
Then, if you access foo.bar:
foo is searched for something named bar. If that exists, it's returned.
Otherwise, if foo has a field named __proto__, that is searched for bar and it's returned if found
Otherwise, if we found __proto__ in the last step, see if there's a __proto__ inside that, and so on.
Or if nothing was found, simply return undefined (but if you're doing foo.bar= ..., it'll simply directly set bar on foo).
So it's actually a really simple way of getting something like an Object Oriented language. I always find if I want to know what's going to happen, I can basically walk through the steps above.
Espruino is a JavaScript interpreter for low-power Microcontrollers. This site is both a support community for Espruino and a place to share what you are working on.
I haven't tried it, but it should work with minor tweaks. I guess the first things to try would be:
PID.prototype.millis
withPID.prototype.millis = function() { return getTime()*1000;}
Just use it like in the example (without the require):
If it works well, I guess we could add the module to Espruino?
Have you done much work in other Object oriented languages? It's probably wrong but I tend to think of the prototype system as follows (and it works for me :) :
It all stems from some pretty simple rules in the interpreter. Each object is nothing more than a list of key/value pairs, and:
When you say
foo = new Foo()
, it effectively just doesfoo = Foo(); foo.__proto__ = Foo.prototype
.Then, if you access
foo.bar
:foo
is searched for something namedbar
. If that exists, it's returned.foo
has a field named__proto__
, that is searched forbar
and it's returned if found__proto__
in the last step, see if there's a__proto__
inside that, and so on.foo.bar= ...
, it'll simply directly setbar
onfoo
).So it's actually a really simple way of getting something like an Object Oriented language. I always find if I want to know what's going to happen, I can basically walk through the steps above.
Does that help explain it at all?