文章概览
简介
amg8833 是红外成像模块,返回 8×8 个温度值
分辨率是0.25°C,精度±2.5℃
速度最快 10Hz
大概100多块钱,体积很小,精度很低,像素也很少,想不到能有啥用途
可以在7m距离内检测到人
详细看这个,模块性质和寄存器功能
代码
使用方法:amg88xxInit -> readPixels
amg8833.c
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
struct pctl _pctl;
struct rst _rst;
struct fpsc _fpsc;
struct intc _intc;
struct stat _stat;
struct sclr _sclr;
struct ave _ave;
struct inthl _inthl;
struct inthh _inthh;
struct intll _intll;
struct intlh _intlh;
struct ihysl _ihysl;
struct ihysh _ihysh;
struct tthl _tthl;
struct tthh _tthh;
uint8_t getPCTL(void){ return _pctl.PCTL; }
uint8_t getRST(void){ return _rst.RST; }
uint8_t getFPSC(void){ return _fpsc.FPS & 0x01; }
uint8_t getINTC(void){ return (_intc.INTMOD << 1 | _intc.INTEN) & 0x03; }
uint8_t getSTAT(void){ return ( (_stat.OVF_THS << 3) | (_stat.OVF_IRS << 2) | (_stat.INTF << 1) ) & 0x07; }
uint8_t getSCLR(void){ return ((_sclr.OVT_CLR << 3) | (_sclr.OVS_CLR << 2) | (_sclr.INTCLR << 1)) & 0x07; }
uint8_t getAVE(void){ return (_ave.MAMOD << 5); }
uint8_t getINTHL(void){ return _inthl.INT_LVL_H; }
uint8_t getINTHH(void){ return _inthh.INT_LVL_H; }
uint8_t getINTLL(void){ return _intll.INT_LVL_L; }
uint8_t getINTLH(void){ return (_intlh.INT_LVL_L & 0xF); }
uint8_t getIHYSL(void){ return _ihysl.INT_HYS; }
uint8_t getIHYSH(void){ return (_ihysh.INT_HYS & 0xF); }
uint8_t getTTHL(void){ return _tthl.TEMP; }
uint8_t getTTHH(void){ return ( (_tthh.SIGN << 3) | _tthh.TEMP) & 0xF; }
uint8_t min(uint8_t a, uint8_t b){ return a < b ? a : b; }
extern I2C_HandleTypeDef hi2c1;
/**************************************************************************/
/*!
@brief Setups the I2C interface and hardware
@param addr Optional I2C address the sensor can be found on. Default is 0x69
@returns True if device is set up, false on any failure
*/
/**************************************************************************/
int amg88xxInit(void)
{
//enter normal mode
_pctl.PCTL = AMG88xx_NORMAL_MODE;
write8(AMG88xx_PCTL, getPCTL());
//software reset
_rst.RST = AMG88xx_INITIAL_RESET;
write8(AMG88xx_RST, getRST());
//disable interrupts by default
disableInterrupt();
//set to 10 FPS
_fpsc.FPS = AMG88xx_FPS_10;
write8(AMG88xx_FPSC, getFPSC());
HAL_Delay(100);
return 0;
}
/**************************************************************************/
/*!
@brief Set the moving average mode.
@param mode if True is passed, output will be twice the moving average
*/
/**************************************************************************/
void setMovingAverageMode(int mode)
{
_ave.MAMOD = mode;
write8(AMG88xx_AVE, getAVE());
}
/**************************************************************************/
/*!
@brief Set the interrupt levels. The hysteresis value defaults to .95 * high
@param high the value above which an interrupt will be triggered
@param low the value below which an interrupt will be triggered
*/
/**************************************************************************/
void setInterruptLevels(float high, float low)
{
setInterruptLevelsHist(high, low, high * .95f);
}
/**************************************************************************/
/*!
@brief Set the interrupt levels
@param high the value above which an interrupt will be triggered
@param low the value below which an interrupt will be triggered
@param hysteresis the hysteresis value for interrupt detection
*/
/**************************************************************************/
void setInterruptLevelsHist(float high, float low, float hysteresis)
{
int highConv = high / AMG88xx_PIXEL_TEMP_CONVERSION;
highConv = constrain(highConv, -4095, 4095);
_inthl.INT_LVL_H = highConv & 0xFF;
_inthh.INT_LVL_H = (highConv & 0xF) >> 4;
write8(AMG88xx_INTHL, getINTHL());
write8(AMG88xx_INTHH, getINTHH());
int lowConv = low / AMG88xx_PIXEL_TEMP_CONVERSION;
lowConv = constrain(lowConv, -4095, 4095);
_intll.INT_LVL_L = lowConv & 0xFF;
_intlh.INT_LVL_L = (lowConv & 0xF) >> 4;
write8(AMG88xx_INTLL, getINTLL());
write8(AMG88xx_INTLH, getINTLH());
int hysConv = hysteresis / AMG88xx_PIXEL_TEMP_CONVERSION;
hysConv = constrain(hysConv, -4095, 4095);
_ihysl.INT_HYS = hysConv & 0xFF;
_ihysh.INT_HYS = (hysConv & 0xF) >> 4;
write8(AMG88xx_IHYSL, getIHYSL());
write8(AMG88xx_IHYSH, getIHYSH());
}
/**************************************************************************/
/*!
@brief enable the interrupt pin on the device.
*/
/**************************************************************************/
void enableInterrupt()
{
_intc.INTEN = 1;
write8(AMG88xx_INTC, getINTC());
}
/**************************************************************************/
/*!
@brief disable the interrupt pin on the device
*/
/**************************************************************************/
void disableInterrupt()
{
_intc.INTEN = 0;
write8(AMG88xx_INTC, getINTC());
}
/**************************************************************************/
/*!
@brief Set the interrupt to either absolute value or difference mode
@param mode passing AMG88xx_DIFFERENCE sets the device to difference mode, AMG88xx_ABSOLUTE_VALUE sets to absolute value mode.
*/
/**************************************************************************/
void setInterruptMode(uint8_t mode)
{
_intc.INTMOD = mode;
write8(AMG88xx_INTC, getINTC());
}
/**************************************************************************/
/*!
@brief Read the state of the triggered interrupts on the device. The full interrupt register is 8 bytes in length.
@param buf the pointer to where the returned data will be stored
@param size Optional number of bytes to read. Default is 8 bytes.
@returns up to 8 bytes of data in buf
*/
/**************************************************************************/
void getInterrupt(uint8_t *buf, uint8_t size)
{
uint8_t bytesToRead = min(size, (uint8_t)8);
read(AMG88xx_INT_OFFSET, buf, bytesToRead);
}
/**************************************************************************/
/*!
@brief Clear any triggered interrupts
*/
/**************************************************************************/
void clearInterrupt()
{
_rst.RST = AMG88xx_FLAG_RESET;
write8(AMG88xx_RST, getRST());
}
/**************************************************************************/
/*!
@brief read the onboard thermistor
@returns a the floating point temperature in degrees Celsius
*/
/**************************************************************************/
float readThermistor()
{
uint8_t raw[2];
read(AMG88xx_TTHL, raw, 2);
uint16_t recast = ((uint16_t)raw[1] << 8) | ((uint16_t)raw[0]);
return signedMag12ToFloat(recast) * AMG88xx_THERMISTOR_CONVERSION;
}
/**************************************************************************/
/*!
@brief Read Infrared sensor values
@param buf the array to place the pixels in
@param size Optionsl number of bytes to read (up to 64). Default is 64 bytes.
@return up to 64 bytes of pixel data in buf
*/
/**************************************************************************/
void readPixels(float *buf, uint8_t size)
{
uint16_t recast;
float converted;
uint8_t bytesToRead = min((uint8_t)(size << 1), (uint8_t)(AMG88xx_PIXEL_ARRAY_SIZE << 1));
uint8_t rawArray[bytesToRead];
read(AMG88xx_PIXEL_OFFSET, rawArray, bytesToRead);
for(int i=0; i<size; i++){
uint8_t pos = i << 1;
recast = ((uint16_t)rawArray[pos + 1] << 8) | ((uint16_t)rawArray[pos]);
converted = signedMag12ToFloat(recast) * AMG88xx_PIXEL_TEMP_CONVERSION;
buf[i] = converted;
}
}
void readPixelsRaw(int16_t* buf)
{
read(AMG88xx_PIXEL_OFFSET, (uint8_t*)buf, 128);
}
/**************************************************************************/
/*!
@brief write one byte of data to the specified register
@param reg the register to write to
@param value the value to write
*/
/**************************************************************************/
void write8(uint8_t reg, uint8_t value)
{
write(reg, &value, 1);
}
/**************************************************************************/
/*!
@brief read one byte of data from the specified register
@param reg the register to read
@returns one byte of register data
*/
/**************************************************************************/
uint8_t read8(uint8_t reg)
{
uint8_t ret;
read(reg, &ret, 1);
return ret;
}
void read(uint8_t reg, uint8_t *buf, uint8_t num)
{
HAL_StatusTypeDef err;
err = HAL_I2C_Mem_Read(&hi2c1, (AMG88xx_ADDRESS), reg, 1, buf, num, 0xffff);
if(err != HAL_OK)
while(1);
}
void write(uint8_t reg, uint8_t *buf, uint8_t num)
{
HAL_StatusTypeDef err;
err = HAL_I2C_Mem_Write(&hi2c1, (AMG88xx_ADDRESS), reg, 1, buf, num, 0xffff);
if(err != HAL_OK)
while(1);
}
/**************************************************************************/
/*!
@brief convert a 12-bit signed magnitude value to a floating point number
@param val the 12-bit signed magnitude value to be converted
@returns the converted floating point value
*/
/**************************************************************************/
float signedMag12ToFloat(uint16_t val)
{
//take first 11 bits as absolute val
uint16_t absVal = (val & 0x7FF);
return (val & 0x8000) ? 0 - (float)absVal : (float)absVal ;
}
/**************************************************************************
插值法重构输出
***************************************************************************/
int Resize(int16_t* pixelsIn, int16_t* pixelsOut, int w, int h, int w2, int h2)
{
int A, B, C, D, x, y, index, gray ;
float x_ratio = ((float)(w-1))/w2 ;
float y_ratio = ((float)(h-1))/h2 ;
float x_diff, y_diff;
int offset = 0 ;
for (int i=0;i<h2;i++) {
for (int j=0;j<w2;j++) {
x = (int)(x_ratio * j) ;
y = (int)(y_ratio * i) ;
x_diff = (x_ratio * j) - x ;
y_diff = (y_ratio * i) - y ;
index = y*w+x ;
A = pixelsIn[index] & 0xffff;
B = pixelsIn[index+1] & 0xffff;
C = pixelsIn[index+w] & 0xffff;
D = pixelsIn[index+w+1] & 0xffff;
gray = (int)(
A*(1-x_diff)*(1-y_diff) + B*(x_diff)*(1-y_diff) +
C*(y_diff)*(1-x_diff) + D*(x_diff*y_diff)
) ;
pixelsOut[offset++] = gray ;
}
}
return 0;
}
amg8833.h
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
/*=========================================================================
I2C ADDRESS/BITS
-----------------------------------------------------------------------*/
/*=========================================================================*/
/*=========================================================================
REGISTERS
-----------------------------------------------------------------------*/
enum
{
AMG88xx_PCTL = 0x00,
AMG88xx_RST = 0x01,
AMG88xx_FPSC = 0x02,
AMG88xx_INTC = 0x03,
AMG88xx_STAT = 0x04,
AMG88xx_SCLR = 0x05,
//0x06 reserved
AMG88xx_AVE = 0x07,
AMG88xx_INTHL = 0x08,
AMG88xx_INTHH = 0x09,
AMG88xx_INTLL = 0x0A,
AMG88xx_INTLH = 0x0B,
AMG88xx_IHYSL = 0x0C,
AMG88xx_IHYSH = 0x0D,
AMG88xx_TTHL = 0x0E,
AMG88xx_TTHH = 0x0F,
AMG88xx_INT_OFFSET = 0x010,
AMG88xx_PIXEL_OFFSET = 0x80
};
enum power_modes{
AMG88xx_NORMAL_MODE = 0x00,
AMG88xx_SLEEP_MODE = 0x01,
AMG88xx_STAND_BY_60 = 0x20,
AMG88xx_STAND_BY_10 = 0x21
};
enum sw_resets {
AMG88xx_FLAG_RESET = 0x30,
AMG88xx_INITIAL_RESET = 0x3F
};
enum frame_rates {
AMG88xx_FPS_10 = 0x00,
AMG88xx_FPS_1 = 0x01
};
enum int_enables{
AMG88xx_INT_DISABLED = 0x00,
AMG88xx_INT_ENABLED = 0x01
};
enum int_modes {
AMG88xx_DIFFERENCE = 0x00,
AMG88xx_ABSOLUTE_VALUE = 0x01
};
/*=========================================================================*/
/**************************************************************************/
/*!
@brief Class that stores state and functions for interacting with AMG88xx IR sensor chips
*/
/**************************************************************************/
int amg88xxInit(void);
void readPixels(float *buf, uint8_t size/* = AMG88xx_PIXEL_ARRAY_SIZE */);
void readPixelsRaw(int16_t* buf);
float readThermistor(void);
void setMovingAverageMode(int mode);
void enableInterrupt(void);
void disableInterrupt(void);
void setInterruptMode(uint8_t mode);
void getInterrupt(uint8_t *buf, uint8_t /*size = 8 */);
void clearInterrupt(void);
//this will automatically set hysteresis to 95% of the high value
void setInterruptLevels(float high, float low);
//this will manually set hysteresis
void setInterruptLevelsHist(float high, float low, float hysteresis);
void write8(uint8_t reg, uint8_t value);
void write16(uint8_t reg, uint16_t value);
uint8_t read8(uint8_t reg);
void read(uint8_t reg, uint8_t *buf, uint8_t num);
void write(uint8_t reg, uint8_t *buf, uint8_t num);
float signedMag12ToFloat(uint16_t val);
// The power control register
struct pctl {
// 0x00 = Normal Mode
// 0x01 = Sleep Mode
// 0x20 = Stand-by mode (60 sec intermittence)
// 0x21 = Stand-by mode (10 sec intermittence)
uint8_t PCTL : 8;
};
//reset register
struct rst {
//0x30 = flag reset (all clear status reg 0x04, interrupt flag and interrupt table)
//0x3F = initial reset (brings flag reset and returns to initial setting)
uint8_t RST : 8;
};
//frame rate register
struct fpsc {
//0 = 10FPS
//1 = 1FPS
uint8_t FPS : 1;
};
//interrupt control register
struct intc {
// 0 = INT output reactive (Hi-Z)
// 1 = INT output active
uint8_t INTEN : 1;
// 0 = Difference interrupt mode
// 1 = absolute value interrupt mode
uint8_t INTMOD : 1;
};
//status register
struct stat {
uint8_t unused : 1;
//interrupt outbreak (val of interrupt table reg)
uint8_t INTF : 1;
//temperature output overflow (val of temperature reg)
uint8_t OVF_IRS : 1;
//thermistor temperature output overflow (value of thermistor)
uint8_t OVF_THS : 1;
};
//status clear register
//write to clear overflow flag and interrupt flag
//after writing automatically turns to 0x00
struct sclr {
uint8_t unused : 1;
//interrupt flag clear
uint8_t INTCLR : 1;
//temp output overflow flag clear
uint8_t OVS_CLR : 1;
//thermistor temp output overflow flag clear
uint8_t OVT_CLR : 1;
};
//average register
//for setting moving average output mode
struct ave {
uint8_t unused : 5;
//1 = twice moving average mode
uint8_t MAMOD : 1;
};
//interrupt level registers
//for setting upper / lower limit hysteresis on interrupt level
//interrupt level upper limit setting. Interrupt output
// and interrupt pixel table are set when value exceeds set value
struct inthl {
uint8_t INT_LVL_H : 8;
};
struct inthh {
uint8_t INT_LVL_H : 4;
};
//interrupt level lower limit. Interrupt output
//and interrupt pixel table are set when value is lower than set value
struct intll {
uint8_t INT_LVL_L : 8;
};
struct intlh {
uint8_t INT_LVL_L : 4;
};
//setting of interrupt hysteresis level when interrupt is generated.
//should not be higher than interrupt level
struct ihysl {
uint8_t INT_HYS : 8;
};
struct ihysh {
uint8_t INT_HYS : 4;
};
//thermistor register
//SIGNED MAGNITUDE FORMAT
struct tthl {
uint8_t TEMP : 8;
};
struct tthh {
uint8_t TEMP : 3;
uint8_t SIGN : 1;
};
//temperature registers 0x80 - 0xFF
/*
//read to indicate temperature data per 1 pixel
//SIGNED MAGNITUDE FORMAT
struct t01l {
uint8_t TEMP : 8;
uint8_t get(){
return TEMP;
}
};
struct t01l _t01l;
struct t01h {
uint8_t TEMP : 3;
uint8_t SIGN : 1;
uint8_t get(){
return ( (SIGN << 3) | TEMP) & 0xF;
}
};
struct t01h _t01h;
*/
int Resize(int16_t* pixelsIn, int16_t* pixelsOut, int w, int h, int w2, int h2);python 读取串口,并用 cv2 库成像
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
48import numpy as np
import cv2
import serial
from time import sleep
serial = serial.Serial('COM5', 115200, timeout=0.5)
if serial.isOpen() :
print("open success")
else :
print("open failed")
_datas = [[118.0, 119.0, 116.0, 117.0, 118.0, 116.0, 117.0, 117.0], [118.0, 119.0, 116.0, 117.0, 118.0, 116.0, 117.0, 117.0], [118.0, 119.0, 116.0, 117.0, 118.0, 116.0, 117.0, 117.0], [118.0, 119.0, 116.0, 117.0, 118.0, 116.0, 117.0, 117.0], [118.0, 119.0, 116.0, 117.0, 118.0, 116.0, 117.0, 117.0], [118.0, 119.0, 116.0, 117.0, 118.0, 116.0, 117.0, 117.0], [118.0, 119.0, 116.0, 117.0, 118.0, 116.0, 117.0, 117.0], [118.0, 119.0, 116.0, 117.0, 118.0, 116.0, 117.0, 117.0]]
def my_limit(num):
if num > 255:
return 255
if num < 0:
return 0
else: return num
while True:
if cv2.waitKey(25) & 0xFF == ord('q'): # 关闭视频
break
sleep(0.002)
data =serial.readline() # 读取一行数据,读到\n为止
if data != b'' :
data = data.decode("unicode_escape").split(" ") # 转码,非常重要
if data[0] != "px":
for i in range(8):
for j in range(8):
# 这里的参数需要调
_datas[i][j] = my_limit((float(data[i * 8 + j])-10) * 25)
print(float(data[i*8+j]))
img = np.array(_datas, np.uint8)
new_img = cv2.resize(img, (720, 720), interpolation=cv2.INTER_LANCZOS4)
# new_img = cv2.cvtColor(new_img, cv2.COLOR_BGR2GRAY)
# image_np=cv2.cvtColor(image_np,cv2.COLOR_GRAY2BGR)
new_img = cv2.applyColorMap(new_img, cv2.COLORMAP_JET)
cv2.imshow("INTER_NEAREST", new_img)
cv2.waitKey(25)
cv2.destroyAllWindows()
多想多做,发篇一作