Penn Electric Racing / Mbed 2 deprecated SystemManagement

Dependencies:   mbed CANBuffer Watchdog MODSERIAL mbed-rtos xbeeRelay IAP

Fork of SystemManagement by Martin Deng

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Temperature.cpp Source File

Temperature.cpp

00001 #include "Temperature.h"
00002 
00003 float NXFT15XH103_TABLE_IN[] = { 0.050512723, 0.056425741, 0.06305631, 0.07054559, 0.079189687, 0.088921283, 0.100071994, 0, 0.127018769, 0.143322197, 0.161706765, 0.182539034, 0.205908044, 0.232186732, 0.261393013, 0.293785311, 0.329354168, 0.368048534, 0.409646378, 0.453820525, 0.5, 0.547490837, 0.595469256, 0.642984648, 0.689103062, 0.732941648, 0.77373518, 0.810924767, 0.844154225, 0.873281379, 0.898354357, 0.919578592, 0.937262775, 0.951781202 };
00004 float NXFT15XH103_TABLE_OUT[] = { 125, 120, 115, 110, 105, 100, 95, 90, 85, 80, 75, 70, 65, 60, 55, 50, 45, 40, 35, 30, 25, 20, 15, 10, 5, 0, -5, -10, -15, -20, -25, -30, -35, -40 };
00005 
00006 LOOKUP_TABLE_T NXFT15XH103_TABLE = {
00007     NXFT15XH103_TABLE_IN,
00008     NXFT15XH103_TABLE_OUT,
00009     sizeof(NXFT15XH103_TABLE)/sizeof(float)
00010 };
00011 
00012 Temperature::Temperature(LOOKUP_TABLE_T *_table, PinName _pin) : pin(_pin) {
00013     table = _table;
00014 }
00015 float Temperature::convert(float reading) {
00016     float in = reading;
00017     if (in < table->input[0]) return INFINITY;                    // Out of range of the table
00018     if (in > table->input[table->numEntries-1]) return -INFINITY;    // Out of range of the table
00019     int lowerIndex = 0;
00020     int upperIndex = table->numEntries-1;
00021     for (int i = 0; i < table->numEntries; i++) {                  // Converge on the entries that surround the input
00022         if (in >= table->input[lowerIndex]) { lowerIndex = i; }
00023         if (in <= table->input[upperIndex]) { upperIndex = table->numEntries-1 - i; }
00024     }
00025     // Interpolate and return
00026     return table->output[lowerIndex] + (table->output[upperIndex] - table->output[lowerIndex]) * ((in - table->input[lowerIndex]) / (table->input[upperIndex] - table->input[lowerIndex]));
00027 }
00028 float Temperature::readRaw() {
00029     return pin.read();
00030 }
00031 float Temperature::read() {
00032     return convert(pin.read());
00033 }