forked from createskyblue/OpenT12
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCalibration.cpp
261 lines (221 loc) · 6.94 KB
/
Calibration.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
#include "OpenT12.h"
double PTemp[FixNum] = { TempP1, TempP2, TempP3, TempP4 }; //温度拟合系数
long Calibration_Base[FixNum] = { 150, 200, 250, 300, 325 ,350, 375,400, 425,450 };
long Calibration_Input[FixNum] = { 0 };
//显示曲线系数
void ShowCurveCoefficient(void) {
Clear();
char buffer[50];
for (long i = 0;i < 4;i++) {
sprintf(buffer, "P[%d]=%.8f\n", i, PTemp[i]);
Disp.setCursor(12, i * 12 + 8);
Disp.print(buffer);
}
while (!sys_KeyProcess()) {
Display();
}
DrawTempCurve();
}
//绘制温度曲线
void DrawTempCurve(void) {
int x;
int count;
sys_Counter_Set(0, 63, 1, 0);
while (!sys_KeyProcess()) {
Clear();
count = sys_Counter_Get();
//绘制参考文字
char buffer[20];
sprintf(buffer, "ADC %d", count * 64);
DrawHighLightText(128 - Disp.getUTF8Width(buffer) - 2, 36, buffer);
sprintf(buffer, "温度 %.1lf", CalculateTemp(count * 64,PTemp));
DrawHighLightText(128 - Disp.getUTF8Width(buffer) - 2, 51, buffer);
//绘制曲线
Disp.setDrawColor(2);
for (int y = 0; y < 64; y++) {
x = map(CalculateTemp(y * 64, PTemp), 0, CalculateTemp(4095, PTemp) + 1, 0, 127);
Disp.drawPixel(x, 63 - y);
//画指示针
if (y == count)
Draw_Slow_Bitmap(x - 4, 63 - y -4,PositioningCursor, 8, 8);
}
Disp.setDrawColor(1);
//利用抖动产生灰度 绘制底部参考网格
if (DisplayFlashTick % 2)
for (int yy = 0; yy < 64; yy += 8)
for (int xx = 0; xx < 128; xx += 8) Disp.drawPixel(xx + 2, yy + 4);
Display();
}
}
//校准界面
void CalibrationTemperature(void) {
Pop_Windows("长按取消校准");
delay(1000);
//暂时解除菜单flag,安全机制在菜单开启的时候不允许加热
Menu_System_State = 0;
bool ExitCalibration_Flag = false;
uint8_t key=0;
char buffer[20];
int SetADC = 0;
int ADC,LastADC;
double TmpP[FixNum] = {0.0};
//暂时清除烙铁头不存在的错误状态:否则输出上锁
ERROREvent = false;
sys_Counter_Set(0, 4095, 1, GetADC0());
for (uint8_t i = 0;i < FixNum;) {
Clear();
SetADC = (int)sys_Counter_Get();
ADC = GetADC0();
//加热
if (ADC!=-1) {
LastADC = ADC;
Log(LOG_INFO, "[校准]ADC获取成功");
if (LastADC < SetADC) SetPOWER(255);
else {
printf("%d >= %d\n", LastADC, SetADC);
SetPOWER(0);
}
}else Log(LOG_INFO,"ADC错误");
//绘制参考文字
sprintf(buffer, "目标:%ld°C", Calibration_Base[i]);
DrawHighLightText(128 - Disp.getUTF8Width(buffer) - 2, 21, buffer);
sprintf(buffer, "采样ADC %d", LastADC);
DrawHighLightText(128 - Disp.getUTF8Width(buffer) - 2, 36, buffer);
sprintf(buffer, "设定ADC:%d", SetADC);
DrawHighLightText(128 - Disp.getUTF8Width(buffer) - 2, 51, buffer);
Disp.setDrawColor(2);
//绘制进度条
Disp.drawBox(0, 0, map(i, 0, FixNum-1,0,128),4);
//绘制曲线
uint8_t x;
for (int y = 0; y < 64; y++) {
x = map(CalculateTemp(y * 64, TmpP), 0, CalculateTemp(4095, TmpP) + 1, 0, 127);
Disp.drawPixel(x, 63 - y);
}
Disp.setDrawColor(1);
Display();
//处理按键
key = sys_KeyProcess();
switch(key) {
case 1:
case 3:
delay(50);
Calibration_Input[i] = SetADC;
polyfit(i + 1, Calibration_Input, Calibration_Base, 3, TmpP);
i++;
break;
case 2:
ExitCalibration_Flag = true;
i = 255;
break;
default:break;
}
}
//关闭功率管输出
SetPOWER(0);
//若中途退出,则不保存
if (!ExitCalibration_Flag) {
//进行曲线拟合
polyfit(FixNum, Calibration_Input, Calibration_Base, 3, PTemp);
Pop_Windows("曲线拟合完成!");
delay(800);
ShowCurveCoefficient();
}else{
Pop_Windows("取消校准");
delay(1000);
}
Menu_System_State = 1;
}
//********** 曲线拟合程序 **********
//曲线拟合算法来至https://blog.csdn.net/m0_37362454/article/details/82456616 by欧阳小俊
/*============================================================
高斯消元法计算得到 n 次多项式的系数
n: 系数的个数
ata: 线性矩阵
sumxy: 线性方程组的Y值
p: 返回拟合的结果
============================================================*/
void gauss_solve(long n, double A[], double x[], double b[])
{
long i, j, k, r;
double max;
for (k = 0; k < n - 1; k++)
{
max = fabs(A[k * n + k]); // find maxmum
r = k;
for (i = k + 1; i < n - 1; i++)
{
if (max < fabs(A[i * n + i]))
{
max = fabs(A[i * n + i]);
r = i;
}
}
if (r != k)
{
for (i = 0; i < n; i++) //change array:A[k]&A[r]
{
max = A[k * n + i];
A[k * n + i] = A[r * n + i];
A[r * n + i] = max;
}
max = b[k]; //change array:b[k]&b[r]
b[k] = b[r];
b[r] = max;
}
for (i = k + 1; i < n; i++)
{
for (j = k + 1; j < n; j++)
A[i * n + j] -= A[i * n + k] * A[k * n + j] / A[k * n + k];
b[i] -= A[i * n + k] * b[k] / A[k * n + k];
}
}
for (i = n - 1; i >= 0; x[i] /= A[i * n + i], i--)
{
for (j = i + 1, x[i] = b[i]; j < n; j++)
x[i] -= A[i * n + j] * x[j];
}
}
/*==================polyfit(n,x,y,poly_n,a)===================*/
/*=======拟合y=a0+a1*x+a2*x^2+……+apoly_n*x^poly_n========*/
/*=====n是数据个数 xy是数据值 poly_n是多项式的项数======*/
/*===返回a0,a1,a2,……a[poly_n],系数比项数多一(常数项)=====*/
void polyfit(long n, long x[], long y[], long poly_n, double p[])
{
long i, j;
double *tempx, *tempy, *sumxx, *sumxy, *ata;
tempx = (double *)calloc(n , sizeof(double));
sumxx = (double *)calloc((poly_n * 2 + 1) , sizeof(double));
tempy = (double *)calloc(n , sizeof(double));
sumxy = (double *)calloc((poly_n + 1) , sizeof(double));
ata = (double *)calloc( (poly_n + 1) * (poly_n + 1) , sizeof(double) );
for (i = 0; i < n; i++)
{
tempx[i] = 1;
tempy[i] = y[i];
}
for (i = 0; i < 2 * poly_n + 1; i++)
{
for (sumxx[i] = 0, j = 0; j < n; j++)
{
sumxx[i] += tempx[j];
tempx[j] *= x[j];
}
}
for (i = 0; i < poly_n + 1; i++)
{
for (sumxy[i] = 0, j = 0; j < n; j++)
{
sumxy[i] += tempy[j];
tempy[j] *= x[j];
}
}
for (i = 0; i < poly_n + 1; i++)
{
for (j = 0; j < poly_n + 1; j++)
{
ata[i * (poly_n + 1) + j] = sumxx[i + j];
}
}
gauss_solve(poly_n + 1, ata, p, sumxy);
}