sittner / linuxcnc-ethercat

LinuxCNC EtherCAT HAL driver

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Problems with float. Anybody else?

sirop opened this issue · comments

Hallo.

I have problems with floats - they are simpy too big.
So I looked into the source code.
The lines https://github.com/sittner/linuxcnc-ethercat/blob/master/src/lcec_generic.c#L132-L137

 if (hal_data->subType == lcecPdoEntTypeFloatUnsigned) {
          fval = lcec_generic_read_u32(pd, hal_data);
        } else {
          fval = lcec_generic_read_s32(pd, hal_data);
        }
}

seem to be strange as fval is of type hal_float_t , but lcec_generic_read_u32(pd, hal_data)
and lcec_generic_read_s32(pd, hal_data) return (un)signed integers. Thus we lose information when casting integers to float.

Shouldn't we define an exra function for floats?
Something like:

double lcec_generic_read_float(uint8_t *pd, lcec_generic_pin_t *hal_data) {

    if (hal_data->subType==lcecPdoEntTypeFloat32) { // for REAL data type in EtherCAT standard
      uint32_t temp = EC_READ_U32(&pd[hal_data->pdo_os]);
      float ret;
      memcpy(&ret,&temp,4);
      return ret;
  }
  else { //for 64 bit -  for LREAL data type in EtherCAT standard
       uint64_t temp = EC_READ_U64(&pd[hal_data->pdo_os]);
       double ret;
       memcpy(&ret,&temp,8);
       return ret;
  }

}

Ok, just forgotten about

hal_float_t floatScale;
hal_float_t floatOffset;

Closed.