I'm using a Texas Instruments DRV2605 haptic controller in my project to drive a LRA vibration motor. I would like to calculate the LRA rated voltage during run time using the formula below as provided in the datasheet:
I'm successfully calculating the rated voltage, but the calculation requires floating point calculations and I would like to avoid that because I'm low on available flash space on my ATTiny85 micro controller. Here is how I'm currently calculating the rated voltage value:
uint8_t LRA_closed_loop_rated_voltage( uint16_t voltage ) {
float RatedVoltage;
uint16_t SampleTime = 300;
uint16_t frequency = 175;
RatedVoltage = ( (float) voltage ) * sqrt( 1 - ( ( 4 * SampleTime + 300 ) * 0.000001 ) * frequency );
RatedVoltage = ( RatedVoltage * 255 ) / 5300;
return RatedVoltage;
}
The function above is just a simplified version showing the calculation and the SampleTime and frequency variables are set to their defaults values here. The voltage argument is the actuator's rated voltage in millivolt.
EDIT: Even though SampleTime and frequency have fixed values in the function above, their values will be determined by reading the relevant registers in the final implementation. SampleTime may have one of the following values: 150, 200, 250, 300 and the frequency will depend on the manufacturers specifications for the actuator.
It was easy to overcome the floating point issue on the other calculations by simply scaling up the values by 1000 (fixed point arithmetic):
ODClamp = ( Vpeak * 255) / 5.6 /* Vpeak in volts */
TO:
ODClamp = ( Vpeak * 255) / 5600 /* Vpeak in millivolts */
How can I do the calculation in question without having to use floating point?
sampleTime
andfrequency
are close to their default values, a Taylor expansion around those values might be good enough. E.g. at order 2 you have f(x) ≈ 0.99801 − x (0.47861 + x 0.19736). – Edgar Bonet Sep 19 '16 at 14:57