Mathmatics in Hal File
- MatthiasF1210
- Offline
- Junior Member
Less
More
- Posts: 37
- Thank you received: 0
30 Mar 2018 19:16 #108120
by MatthiasF1210
Mathmatics in Hal File was created by MatthiasF1210
Hi,
I would like to use something like "round" in my Hal File, but could'nt find anything in the doku.
Is there a possibility to round float values?
Thanks
Matthias
I would like to use something like "round" in my Hal File, but could'nt find anything in the doku.
Is there a possibility to round float values?
Thanks
Matthias
Please Log in or Create an account to join the conversation.
30 Mar 2018 20:30 #108133
by andypugh
Replied by andypugh on topic Mathmatics in Hal File
You could convert to integer and then back again. But that's not very nice.
If you are doing something complicated enough to need a "round" function then I think you are doing something complicated enough to warrant a custom comp.
See this sine / cosine example to see how simple a custom HAL component can be:
linuxcnc.org/docs/2.7/html/hal/comp.html#_sincos
If you are doing something complicated enough to need a "round" function then I think you are doing something complicated enough to warrant a custom comp.
See this sine / cosine example to see how simple a custom HAL component can be:
linuxcnc.org/docs/2.7/html/hal/comp.html#_sincos
Please Log in or Create an account to join the conversation.
30 Mar 2018 20:32 #108134
by andypugh
Replied by andypugh on topic Mathmatics in Hal File
Addendum: You can only use mathematical functions included in rtapi_math.h
github.com/LinuxCNC/linuxcnc/blob/master/src/rtapi/rtapi_math.h
But that does include round(), ceil() and floor()
github.com/LinuxCNC/linuxcnc/blob/master/src/rtapi/rtapi_math.h
But that does include round(), ceil() and floor()
The following user(s) said Thank You: MatthiasF1210
Please Log in or Create an account to join the conversation.
- MatthiasF1210
- Offline
- Junior Member
Less
More
- Posts: 37
- Thank you received: 0
31 Mar 2018 21:49 #108188
by MatthiasF1210
Replied by MatthiasF1210 on topic Mathmatics in Hal File
Hi Andypugh,
thanks for the info. I never tried to compile my own component so far, but you are right, it's quite simple...
For some reason (i don't understand why) the round function of math wasn't working, so i great a small workaround.
Thanks, I enjoy the linuxcnc more and more each day
Matthias
thanks for the info. I never tried to compile my own component so far, but you are right, it's quite simple...
For some reason (i don't understand why) the round function of math wasn't working, so i great a small workaround.
component roundx "round the input value to a specified number of decimal places";
pin out float out "input rounded for X decimal places" ;
pin in float in;
param rw unsigned decimal = 1 "number of decimal places";
function _;
license "GPL"; // indicates GPL v2 or later
;;
#include <rtapi_math.h>
FUNCTION(_) {
float tmp = 0;
float rest = 0;
unsigned exp_ = 0;
exp_ = pow(10,decimal);
tmp = exp_ * in;
rest = tmp - floor(tmp);
if(rest >= 0.5){
out = (floor(tmp) + 1) / exp_;}
else{
out = floor(tmp) / exp_;}
}
Thanks, I enjoy the linuxcnc more and more each day
Matthias
Please Log in or Create an account to join the conversation.
Time to create page: 0.062 seconds