forked from damienmaguire/Stm32-vcu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
temp_meas.cpp
129 lines (106 loc) · 4.33 KB
/
temp_meas.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
* This file is part of the tumanako_vc project.
*
* Copyright (C) 2010 Johannes Huebner <[email protected]>
* Copyright (C) 2010 Edward Cheeseman <[email protected]>
* Copyright (C) 2009 Uwe Hermann <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#define __TEMP_LU_TABLES
#include "temp_meas.h"
#include "my_math.h"
#include <stdint.h>
#define TABLEN(a) sizeof(a) / sizeof(a[0])
enum coeff { PTC, NTC };
typedef struct TempSensor
{
int tempMin;
int tempMax;
uint8_t step;
uint8_t tabSize;
enum coeff coeff;
const uint16_t *lookup;
} TEMP_SENSOR;
/* Temp sensor with JCurve */
static const uint16_t JCurve[] = { JCURVE };
/* Temp sensor in Semikron Skiip82 module */
static const uint16_t Semikron[] = { SEMIKRON };
/* Temp sensor in MBB600 IGBT module */
static const uint16_t mbb600[] = { MBB600 };
/* Temp sensor KTY83-110 */
static const uint16_t Kty83[] = { KTY83 };
/* Temp sensor KTY84-130 */
static const uint16_t Kty84[] = { KTY84 };
/* Temp sensor in Nissan Leaf motor */
static const uint16_t leaf[] = { LEAF };
/* Temp sensor in Nissan Leaf Gen 2 inverter heat sink */
static const uint16_t leafhs[] = { LEAFHS };
static const uint16_t kty81m[] = { KTY81_M };
/* Temp sensor embedded in Tesla rear motor */
static const uint16_t Tesla100k[] = { TESLA_100K };
/* Temp sensor embedded in Tesla rear heatsink */
static const uint16_t Tesla52k[] = { TESLA_52K };
/* Coolant fluid sensor in Tesla LDU */
static const uint16_t TeslaFluid[] = { TESLA_LDU_FLUID };
/* Temp sensor embedded in Tesla rear heatsink */
static const uint16_t Tesla10k[] = { TESLA_10K };
/* Temp sensor embedded in many Toyota motors */
static const uint16_t Toyota[] = { TOYOTA_M };
/* contributed by Fabian Brauss */
/* Temp sensor KTY81-121 */
static const uint16_t Kty81hs[] = { KTY81_HS };
/* Temp sensor PT1000 */
static const uint16_t Pt1000[] = { PT1000 };
/* Temp sensor NTC K45 2k2 (with parallel 2k!) */
static const uint16_t NtcK45[] = { NTCK45 };
static const TEMP_SENSOR sensors[] =
{
{ -25, 105, 5, TABLEN(JCurve), NTC, JCurve },
{ 0, 100, 5, TABLEN(Semikron), PTC, Semikron },
{ -5, 100, 5, TABLEN(mbb600), PTC, mbb600 },
{ -50, 150, 10, TABLEN(Kty81hs), NTC, Kty81hs },
{ -50, 150, 10, TABLEN(Pt1000), PTC, Pt1000 },
{ -50, 150, 5, TABLEN(NtcK45), NTC, NtcK45 },
{ -10, 100, 10, TABLEN(leafhs), NTC, leafhs },
{ -50, 170, 10, TABLEN(Kty83), PTC, Kty83 },
{ -40, 300, 10, TABLEN(Kty84), PTC, Kty84 },
{ -20, 150, 10, TABLEN(leaf), NTC, leaf },
{ -50, 150, 10, TABLEN(kty81m), PTC, kty81m },
{ -20, 200, 5, TABLEN(Toyota), PTC, Toyota },
{ -20, 190, 5, TABLEN(Tesla100k), PTC, Tesla100k },
{ 0, 100, 10, TABLEN(Tesla52k), PTC, Tesla52k },
{ 5, 100, 5, TABLEN(TeslaFluid),PTC, TeslaFluid },
{ -20, 190, 5, TABLEN(Tesla10k), PTC, Tesla10k },
};
float TempMeas::Lookup(int digit, Sensors sensorId)
{
if (sensorId >= TEMP_LAST) return 0;
int index = sensorId >= TEMP_KTY83 ? sensorId - TEMP_KTY83 + NUM_HS_SENSORS : sensorId;
const TEMP_SENSOR * sensor = &sensors[index];
uint16_t last = sensor->lookup[0] + (sensor->coeff == NTC?-1:+1);
for (uint32_t i = 0; i < sensor->tabSize; i++)
{
uint16_t cur = sensor->lookup[i];
if ((sensor->coeff == NTC && cur >= digit) || (sensor->coeff == PTC && cur <= digit))
{
float a = sensor->coeff == NTC?cur - digit:digit - cur;
float b = sensor->coeff == NTC?cur - last:last - cur;
float interpolated = sensor->step * i + sensor->tempMin - sensor->step * a / b;
return MIN(MAX(interpolated, sensor->tempMin),sensor->tempMax);
}
last = cur;
}
return sensor->tempMax;
}