Mathmatics in Hal File

More
30 Mar 2018 19:16 #108120 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

Please Log in or Create an account to join the conversation.

More
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

Please Log in or Create an account to join the conversation.

More
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()
The following user(s) said Thank You: MatthiasF1210

Please Log in or Create an account to join the conversation.

More
31 Mar 2018 21:49 #108188 by MatthiasF1210
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.
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.094 seconds
Powered by Kunena Forum