Created
April 29, 2023 05:21
-
-
Save reefwing/fd00139b2fc84285e22111ccc811fc11 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct ForceLookup { | |
uint16_t voltage; // millivolts (mV) | |
uint16_t force; // grams (g) | |
}; | |
ForceLookup table[FLT_SIZE] = { {0, 0}, {900, 20}, {1400, 50}, {1750, 100}, {2250, 200}, {2500, 310}, | |
{2800, 450}, {3000, 580}, {3200, 710}, {3400, 900}, {3450, 1000} }; | |
float interpolate(uint16_t adc_mV) { | |
// Based on ADC voltage (mV), lookup the force (g) and interpolate between adjacent values if required. | |
// Constrain range to lookup table min and max | |
adc_mV = constrain(adc_mV, 0, 3450); | |
for (int i = 0; i < FLT_SIZE - 1; i++) { | |
if (table[i].voltage <= adc_mV && table[i+1].voltage >= adc_mV) { | |
float diffx = adc_mV - table[i].voltage; | |
float diffn = table[i+1].voltage - table[i].voltage; | |
return (table[i].force + (table[i+1].force - table[i].force) * diffx / diffn); | |
} | |
} | |
return -1.0f; // Not in Range | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment