2016-05-17 11:37:54 +00:00
|
|
|
component lin2rev "Calculate motor angle from position in machine units";
|
|
|
|
pin in float scale;
|
2016-06-16 22:37:30 +00:00
|
|
|
|
|
|
|
pin in float cmd_in;
|
|
|
|
pin out float cmd_out;
|
|
|
|
|
|
|
|
pin in float fb_in;
|
|
|
|
pin out float fb_out;
|
|
|
|
|
|
|
|
variable double pi = 3.141592654;
|
2016-05-17 11:37:54 +00:00
|
|
|
variable double s;
|
2016-06-16 22:37:30 +00:00
|
|
|
variable int lastq = 0;
|
|
|
|
variable int q = 0;
|
2016-06-16 23:29:49 +00:00
|
|
|
variable int rev = 0;
|
2016-06-16 22:37:30 +00:00
|
|
|
|
2016-05-17 11:37:54 +00:00
|
|
|
function _;
|
|
|
|
license "GPL";
|
|
|
|
;;
|
2016-06-16 22:37:30 +00:00
|
|
|
|
2016-05-17 11:37:54 +00:00
|
|
|
s = scale;
|
|
|
|
if(s == 0){
|
|
|
|
s = 1;
|
|
|
|
}
|
2016-06-16 22:37:30 +00:00
|
|
|
|
2016-06-16 23:25:12 +00:00
|
|
|
cmd_out = (cmd_in/s)*2.0*pi;
|
2016-06-16 22:37:30 +00:00
|
|
|
|
|
|
|
while(cmd_out < -pi){
|
|
|
|
cmd_out += 2.0 * pi;
|
2016-05-17 11:37:54 +00:00
|
|
|
}
|
2016-06-16 22:37:30 +00:00
|
|
|
|
|
|
|
while(cmd_out > pi){
|
|
|
|
cmd_out -= 2.0 * pi;
|
|
|
|
}
|
|
|
|
|
|
|
|
q = 0;
|
|
|
|
if(fb_in > pi/2.0){
|
|
|
|
q = 2;
|
|
|
|
}
|
|
|
|
if(fb_in < -pi/2.0){
|
|
|
|
q = 3;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(q != 0 && q == 3 && lastq == 2){
|
|
|
|
rev++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(q != 0 && q == 2 && lastq == 3){
|
|
|
|
rev--;
|
|
|
|
}
|
|
|
|
|
|
|
|
lastq = q;
|
|
|
|
|
2016-06-16 23:25:12 +00:00
|
|
|
fb_out = ((fb_in+rev*pi*2.0)*s)/(2.0*pi);
|