luat_lib_mlx90640.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. /*
  2. @module mlx90640
  3. @summary 红外测温(MLX90640)
  4. @version 1.0
  5. @date 2022.1.20
  6. */
  7. #include "luat_base.h"
  8. #include "luat_malloc.h"
  9. #include <MLX90640_I2C_Driver.h>
  10. #include <MLX90640_API.h>
  11. #include <math.h>
  12. #include "luat_lcd.h"
  13. #define LUAT_LOG_TAG "mlx90640"
  14. #include "luat_log.h"
  15. static luat_lcd_conf_t* lcd_conf;
  16. #define FPS1HZ 0x01
  17. #define FPS2HZ 0x02
  18. #define FPS4HZ 0x04
  19. #define FPS8HZ 0x08
  20. #define FPS16HZ 0x10
  21. #define FPS32HZ 0x20
  22. #define FPS64HZ 0x40
  23. #define MLX90640_ADDR 0x33
  24. #define TA_SHIFT 8 //Default shift for MLX90640 in open air
  25. //low range of the sensor (this will be blue on the screen)
  26. #define MINTEMP 20
  27. //high range of the sensor (this will be red on the screen)
  28. #define MAXTEMP 35
  29. #define RAW_DATA_W 32
  30. #define RAW_DATA_H 24
  31. #define RAW_DATA_SIZE RAW_DATA_W*RAW_DATA_H
  32. typedef struct mlx_ctx
  33. {
  34. uint16_t eeMLX90640[832];
  35. float mlx90640To[RAW_DATA_SIZE];
  36. uint16_t frame[834];
  37. }mlx_ctx_t;
  38. static mlx_ctx_t* ctx = NULL;
  39. // static uint16_t eeMLX90640[832];
  40. // static float mlx90640To[RAW_DATA_SIZE];
  41. // static uint16_t frame[834];
  42. static float emissivity=0.95;
  43. static int status;
  44. static float vdd;
  45. static float Ta;
  46. const uint16_t camColors[] = {0x480F,
  47. 0x400F,0x400F,0x400F,0x4010,0x3810,0x3810,0x3810,0x3810,0x3010,0x3010,
  48. 0x3010,0x2810,0x2810,0x2810,0x2810,0x2010,0x2010,0x2010,0x1810,0x1810,
  49. 0x1811,0x1811,0x1011,0x1011,0x1011,0x0811,0x0811,0x0811,0x0011,0x0011,
  50. 0x0011,0x0011,0x0011,0x0031,0x0031,0x0051,0x0072,0x0072,0x0092,0x00B2,
  51. 0x00B2,0x00D2,0x00F2,0x00F2,0x0112,0x0132,0x0152,0x0152,0x0172,0x0192,
  52. 0x0192,0x01B2,0x01D2,0x01F3,0x01F3,0x0213,0x0233,0x0253,0x0253,0x0273,
  53. 0x0293,0x02B3,0x02D3,0x02D3,0x02F3,0x0313,0x0333,0x0333,0x0353,0x0373,
  54. 0x0394,0x03B4,0x03D4,0x03D4,0x03F4,0x0414,0x0434,0x0454,0x0474,0x0474,
  55. 0x0494,0x04B4,0x04D4,0x04F4,0x0514,0x0534,0x0534,0x0554,0x0554,0x0574,
  56. 0x0574,0x0573,0x0573,0x0573,0x0572,0x0572,0x0572,0x0571,0x0591,0x0591,
  57. 0x0590,0x0590,0x058F,0x058F,0x058F,0x058E,0x05AE,0x05AE,0x05AD,0x05AD,
  58. 0x05AD,0x05AC,0x05AC,0x05AB,0x05CB,0x05CB,0x05CA,0x05CA,0x05CA,0x05C9,
  59. 0x05C9,0x05C8,0x05E8,0x05E8,0x05E7,0x05E7,0x05E6,0x05E6,0x05E6,0x05E5,
  60. 0x05E5,0x0604,0x0604,0x0604,0x0603,0x0603,0x0602,0x0602,0x0601,0x0621,
  61. 0x0621,0x0620,0x0620,0x0620,0x0620,0x0E20,0x0E20,0x0E40,0x1640,0x1640,
  62. 0x1E40,0x1E40,0x2640,0x2640,0x2E40,0x2E60,0x3660,0x3660,0x3E60,0x3E60,
  63. 0x3E60,0x4660,0x4660,0x4E60,0x4E80,0x5680,0x5680,0x5E80,0x5E80,0x6680,
  64. 0x6680,0x6E80,0x6EA0,0x76A0,0x76A0,0x7EA0,0x7EA0,0x86A0,0x86A0,0x8EA0,
  65. 0x8EC0,0x96C0,0x96C0,0x9EC0,0x9EC0,0xA6C0,0xAEC0,0xAEC0,0xB6E0,0xB6E0,
  66. 0xBEE0,0xBEE0,0xC6E0,0xC6E0,0xCEE0,0xCEE0,0xD6E0,0xD700,0xDF00,0xDEE0,
  67. 0xDEC0,0xDEA0,0xDE80,0xDE80,0xE660,0xE640,0xE620,0xE600,0xE5E0,0xE5C0,
  68. 0xE5A0,0xE580,0xE560,0xE540,0xE520,0xE500,0xE4E0,0xE4C0,0xE4A0,0xE480,
  69. 0xE460,0xEC40,0xEC20,0xEC00,0xEBE0,0xEBC0,0xEBA0,0xEB80,0xEB60,0xEB40,
  70. 0xEB20,0xEB00,0xEAE0,0xEAC0,0xEAA0,0xEA80,0xEA60,0xEA40,0xF220,0xF200,
  71. 0xF1E0,0xF1C0,0xF1A0,0xF180,0xF160,0xF140,0xF100,0xF0E0,0xF0C0,0xF0A0,
  72. 0xF080,0xF060,0xF040,0xF020,0xF800,};
  73. uint8_t tempto255(float temp){
  74. return (uint8_t)round((temp+40)*255/340);
  75. }
  76. float map(float val, float I_Min, float I_Max, float O_Min, float O_Max){
  77. return(((val-I_Min)*((O_Max-O_Min)/(I_Max-I_Min)))+O_Min);
  78. }
  79. #define constrain(amt, low, high) ((amt)<(low)?(low):((amt)>(high)?(high):(amt)))
  80. static paramsMLX90640* mlx90640;
  81. uint8_t mlx90640_i2c_id;
  82. uint8_t mlx90640_i2c_speed;
  83. static uint8_t mlx90640_refresh_rate;
  84. /*
  85. 初始化MLX90640传感器
  86. @api mlx90640.init(i2c_id,i2c_speed,refresh_rate)
  87. @int 传感器所在的i2c总线id,默认为0
  88. @int 传感器所在的i2c总线速度,默认为i2c.FAST
  89. @int 传感器的测量速率,默认为4Hz
  90. @return bool 成功返回true, 否则返回nil或者false
  91. @usage
  92. if mlx90640.init(0,i2c.FAST,mlx90640.FPS4HZ) then
  93. log.info("mlx90640", "init ok")
  94. sys.wait(500) -- 稍等片刻
  95. while 1 do
  96. mlx90640.feed() -- 取一帧数据
  97. mlx90640.draw2lcd(0, 0 ,32 ,24)-- 需提前把lcd初始化好
  98. sys.wait(250) -- 默认是4HZ
  99. end
  100. else
  101. log.info("mlx90640", "init fail")
  102. end
  103. */
  104. static int l_mlx90640_init(lua_State *L){
  105. mlx90640_i2c_id = luaL_optinteger(L, 1 , 0);
  106. mlx90640_i2c_speed = luaL_optinteger(L, 2 , 1);
  107. mlx90640_refresh_rate = luaL_optinteger(L, 3 , 3);
  108. lcd_conf = luat_lcd_get_default();
  109. if (ctx == NULL) {
  110. ctx = luat_heap_malloc(sizeof(mlx_ctx_t));
  111. if (ctx == NULL) {
  112. LLOGE("out of memory when malloc mlx_ctx_t");
  113. return 0;
  114. }
  115. }
  116. MLX90640_I2CInit();
  117. mlx90640 = (paramsMLX90640*)luat_heap_malloc(sizeof(paramsMLX90640));
  118. MLX90640_SetRefreshRate(MLX90640_ADDR, mlx90640_refresh_rate);
  119. MLX90640_SetChessMode(MLX90640_ADDR);
  120. status = MLX90640_DumpEE(MLX90640_ADDR, ctx->eeMLX90640);
  121. if (status != 0){
  122. LLOGW("load system parameters error with code:%d",status);
  123. return 0;
  124. }
  125. status = MLX90640_ExtractParameters(ctx->eeMLX90640, mlx90640);
  126. if (status != 0) {
  127. LLOGW("Parameter extraction failed with error code:%d",status);
  128. return 0;
  129. }
  130. //初始化后此处先读帧,去掉初始化后一些错误数据
  131. for (size_t i = 0; i < 3; i++){
  132. int status = MLX90640_GetFrameData(MLX90640_ADDR, ctx->frame);
  133. if (status < 0){
  134. LLOGD("GetFrame Error: %d",status);
  135. return 0;
  136. }
  137. vdd = MLX90640_GetVdd(ctx->frame, mlx90640);
  138. Ta = MLX90640_GetTa(ctx->frame, mlx90640);
  139. MLX90640_CalculateTo(ctx->frame, mlx90640, emissivity , Ta - TA_SHIFT, ctx->mlx90640To);
  140. MLX90640_BadPixelsCorrection(mlx90640->brokenPixels, ctx->mlx90640To, 1, mlx90640);
  141. MLX90640_BadPixelsCorrection(mlx90640->outlierPixels, ctx->mlx90640To, 1, mlx90640);
  142. }
  143. lua_pushboolean(L, 1);
  144. return 1;
  145. }
  146. /*
  147. 取一帧数据
  148. @api mlx90640.feed()
  149. */
  150. static int l_mlx90640_feed(lua_State *L) {
  151. if (ctx == NULL) {
  152. LLOGE("mlx90640 NOT init yet");
  153. return 0;
  154. }
  155. int status = MLX90640_GetFrameData(MLX90640_ADDR, ctx->frame);
  156. if (status < 0){
  157. LLOGD("GetFrame Error: %d",status);
  158. return 0;
  159. }
  160. vdd = MLX90640_GetVdd(ctx->frame, mlx90640);
  161. Ta = MLX90640_GetTa(ctx->frame, mlx90640);
  162. MLX90640_CalculateTo(ctx->frame, mlx90640, emissivity , Ta - TA_SHIFT, ctx->mlx90640To);
  163. MLX90640_BadPixelsCorrection(mlx90640->brokenPixels, ctx->mlx90640To, 1, mlx90640);
  164. MLX90640_BadPixelsCorrection(mlx90640->outlierPixels, ctx->mlx90640To, 1, mlx90640);
  165. lua_pushboolean(L, 1);
  166. return 0;
  167. }
  168. /*
  169. 获取底层裸数据,浮点数矩阵
  170. @api mlx90640.raw_data()
  171. @return table 浮点数数据,768个像素对应的温度值
  172. */
  173. static int l_mlx90640_raw_data(lua_State *L) {
  174. if (ctx == NULL) {
  175. LLOGE("mlx90640 NOT init yet");
  176. return 0;
  177. }
  178. lua_createtable(L, RAW_DATA_SIZE, 0);
  179. for (size_t i = 0; i < RAW_DATA_SIZE; i++)
  180. {
  181. lua_pushnumber(L, ctx->mlx90640To[i]);
  182. lua_seti(L, -2, i + 1);
  183. }
  184. return 1;
  185. }
  186. /*
  187. 获取单一点数据
  188. @api mlx90640.raw_point(index)
  189. @int 索引值(0-767)
  190. @return number 单点温度值
  191. */
  192. static int l_mlx90640_raw_point(lua_State *L) {
  193. if (ctx == NULL) {
  194. LLOGE("mlx90640 NOT init yet");
  195. return 0;
  196. }
  197. lua_pushnumber(L, ctx->mlx90640To[luaL_checkinteger(L, 1)]);
  198. return 1;
  199. }
  200. /*
  201. 获取外壳温度
  202. @api mlx90640.ta_temp()
  203. @return number 外壳温度
  204. */
  205. static int l_mlx90640_ta_temp(lua_State *L) {
  206. lua_pushnumber(L, Ta);
  207. return 1;
  208. }
  209. /*
  210. 获取最高温度
  211. @api mlx90640.max_temp()
  212. @return number 最高温度
  213. @return number 最高温度位置
  214. */
  215. static int l_mlx90640_max_temp(lua_State *L) {
  216. float max_temp = -40;
  217. uint16_t index = 0;
  218. if (ctx == NULL) {
  219. LLOGE("mlx90640 NOT init yet");
  220. return 0;
  221. }
  222. for (size_t i = 0; i < RAW_DATA_SIZE; i++){
  223. if (ctx->mlx90640To[i]>max_temp)
  224. {
  225. max_temp = ctx->mlx90640To[i];
  226. index = i;
  227. }
  228. }
  229. lua_pushnumber(L, max_temp);
  230. lua_pushinteger(L, index+1);
  231. return 2;
  232. }
  233. /*
  234. 获取最低温度
  235. @api mlx90640.min_temp()
  236. @return number 最低温度
  237. @return number 最低温度位置
  238. */
  239. static int l_mlx90640_min_temp(lua_State *L) {
  240. float min_temp = 300;
  241. uint16_t index = 0;
  242. if (ctx == NULL) {
  243. LLOGE("mlx90640 NOT init yet");
  244. return 0;
  245. }
  246. for (size_t i = 0; i < RAW_DATA_SIZE; i++){
  247. if (ctx->mlx90640To[i]<min_temp)
  248. {
  249. min_temp = ctx->mlx90640To[i];
  250. index = i;
  251. }
  252. }
  253. lua_pushnumber(L, min_temp);
  254. lua_pushinteger(L, index+1);
  255. return 2;
  256. }
  257. /*
  258. 获取平均温度
  259. @api mlx90640.average_temp()
  260. @return number 平均温度
  261. */
  262. static int l_mlx90640_average_temp(lua_State *L) {
  263. float temp[RAW_DATA_H] = {0};
  264. float temp1=0;
  265. if (ctx == NULL) {
  266. LLOGE("mlx90640 NOT init yet");
  267. return 0;
  268. }
  269. for (size_t j = 0; j < RAW_DATA_H; j++)
  270. {
  271. for (size_t i = 0; i < RAW_DATA_W; i++)
  272. {
  273. temp1 += ctx->mlx90640To[i];
  274. }
  275. temp[j]=temp1/RAW_DATA_W;
  276. temp1 = 0;
  277. }
  278. for (size_t i = 0; i < RAW_DATA_H; i++){
  279. temp1+=temp[i];
  280. }
  281. lua_pushnumber(L, temp1/RAW_DATA_H);
  282. return 1;
  283. }
  284. /*
  285. 获取vdd
  286. @api mlx90640.get_vdd()
  287. @return number vdd
  288. */
  289. static int l_mlx90640_get_vdd(lua_State *L) {
  290. lua_pushnumber(L, vdd);
  291. return 1;
  292. }
  293. static uint8_t * luat_interpolation_double(uint8_t *src, uint16_t rows,uint16_t cols) {
  294. int w1 = cols;
  295. int h1 = rows;
  296. int w2 = w1*2;
  297. int h2 = h1*2;
  298. uint8_t* dst = (uint8_t*)luat_heap_malloc(rows*cols*4);
  299. for (size_t y = 0; y < h1; y++){
  300. for (size_t x = 0; x < w1; x++){
  301. dst[y*2*w2+x*2] = src[y*w1+x];
  302. if (x == w1 - 1){
  303. dst[y*2*w2+x*2+1] = (uint8_t)(dst[y*2*w2+x*2]*2-dst[y*2*w2+x*2-1]);
  304. if (y == h1 - 1){
  305. dst[(y*2+1)*w2+x*2] = (uint8_t)(dst[y*2*w2+x*2]*2-dst[(y*2-1)*w2+x*2]);
  306. }else{
  307. dst[(y*2+1)*w2+x*2] = (uint8_t)round((src[y*w1+x]+src[(y+1)*w1+x])/2);
  308. }
  309. }else{
  310. dst[y*2*w2+x*2+1] = (uint8_t)round((src[y*w1+x]+src[y*w1+x+1])/2);
  311. if (y == h1 - 1){
  312. dst[(y*2+1)*w2+x*2] = (uint8_t)(dst[y*2*w2+x*2]*2-dst[(y*2-1)*w2+x*2]);
  313. }else{
  314. dst[(y*2+1)*w2+x*2] = (uint8_t)round((src[y*w1+x]+src[(y+1)*w1+x])/2);
  315. }
  316. }
  317. }
  318. }
  319. for (size_t y = 0; y < h1; y++){
  320. for (size_t x = 0; x < w1; x++){
  321. if ((x == w1 - 1) && (y == h1 - 1)){
  322. dst[(y*2+1)*w2+x*2+1] = (uint8_t)round((dst[(y*2+1)*w2+x*2]+dst[y*2*w2+x*2+1])/2);
  323. }
  324. else if (y == h1 - 1){
  325. dst[(y*2+1)*w2+x*2+1] = (uint8_t)round((dst[(y*2+1)*w2+x*2]+dst[(y*2+1)*w2+x*2+2])/2);
  326. }
  327. else{
  328. dst[(y*2+1)*w2+x*2+1] = (uint8_t)round((dst[y*2*w2+x*2+1]+dst[(y*2+2)*w2+x*2+1])/2);
  329. }
  330. }
  331. }
  332. return dst;
  333. }
  334. static uint8_t * luat_interpolation(uint8_t *src, uint16_t rows,uint16_t cols,uint8_t fold) {
  335. uint8_t* index_data_out1 = NULL;
  336. uint8_t* index_data_out2 = NULL;
  337. for (size_t i = 2; i <= fold; i=i*2){
  338. if (i==2){
  339. index_data_out1 = luat_interpolation_double(src, rows,cols);
  340. luat_heap_free(src);
  341. }else{
  342. if (index_data_out1 == NULL){
  343. index_data_out1 = luat_interpolation_double(index_data_out2, rows,cols);
  344. luat_heap_free(index_data_out2);
  345. index_data_out2 = NULL;
  346. }else{
  347. index_data_out2 = luat_interpolation_double(index_data_out1, rows,cols);
  348. luat_heap_free(index_data_out1);
  349. index_data_out1 = NULL;
  350. }
  351. }
  352. rows = rows*2;
  353. cols = cols*2;
  354. }
  355. if (index_data_out1 != NULL){
  356. return index_data_out1;
  357. }else{
  358. return index_data_out2;
  359. }
  360. }
  361. /*
  362. 绘制到lcd
  363. @api mlx90640.draw2lcd(x, y, fold)
  364. @int 左上角x坐标
  365. @int 左上角y坐标
  366. @int 放大倍数,必须为2的指数倍(1,2,4,8,16...)默认为1
  367. @return bool 成功返回true,否则返回false
  368. */
  369. static int l_mlx90640_draw2lcd(lua_State *L) {
  370. if (lcd_conf == NULL) {
  371. LLOGW("init lcd first!!!");
  372. return 0;
  373. }
  374. uint16_t lcd_x = luaL_optinteger(L, 1 , 0);
  375. uint16_t lcd_y = luaL_optinteger(L, 2 , 0);
  376. uint8_t fold = luaL_optinteger(L, 3 , 1);
  377. uint8_t* index_data = luat_heap_malloc(RAW_DATA_SIZE);
  378. for (size_t i = 0; i < RAW_DATA_SIZE; i++){
  379. float t = ctx->mlx90640To[i];
  380. if (t<MINTEMP) t=MINTEMP;
  381. if (t>MAXTEMP) t=MAXTEMP;
  382. uint8_t colorIndex = (uint8_t)round(map(t, MINTEMP, MAXTEMP, 0, 255));
  383. colorIndex = constrain(colorIndex, 0, 255);
  384. index_data[i] = colorIndex;
  385. }
  386. uint8_t* index_data_out = NULL;
  387. if (fold==1){
  388. index_data_out = index_data;
  389. }else{
  390. index_data_out = luat_interpolation(index_data, RAW_DATA_H,RAW_DATA_W,fold);
  391. }
  392. int index_data_out_w = RAW_DATA_W*fold;
  393. int index_data_out_h = RAW_DATA_H*fold;
  394. luat_color_t line[index_data_out_w];
  395. for (size_t y = 0; y < index_data_out_h; y++){
  396. for (size_t x = 0; x < index_data_out_w; x++){
  397. line[x] = color_swap(camColors[index_data_out[y*index_data_out_w + x]]);
  398. }
  399. luat_lcd_draw(lcd_conf, lcd_x, lcd_y+y, lcd_x+index_data_out_w-1, lcd_y+y, line);
  400. }
  401. luat_heap_free(index_data_out);
  402. return 0;
  403. }
  404. #include "rotable2.h"
  405. static const rotable_Reg_t reg_mlx90640[] =
  406. {
  407. {"init", ROREG_FUNC(l_mlx90640_init) },
  408. {"feed", ROREG_FUNC(l_mlx90640_feed) },
  409. {"raw_data", ROREG_FUNC(l_mlx90640_raw_data)},
  410. {"raw_point", ROREG_FUNC(l_mlx90640_raw_point)},
  411. {"draw2lcd", ROREG_FUNC(l_mlx90640_draw2lcd)},
  412. {"ta_temp", ROREG_FUNC(l_mlx90640_ta_temp)},
  413. {"max_temp", ROREG_FUNC(l_mlx90640_max_temp)},
  414. {"min_temp", ROREG_FUNC(l_mlx90640_min_temp)},
  415. {"average_temp",ROREG_FUNC(l_mlx90640_average_temp)},
  416. {"get_vdd", ROREG_FUNC(l_mlx90640_get_vdd)},
  417. //@const FPS1HZ number FPS1HZ
  418. { "FPS1HZ", ROREG_INT(FPS1HZ)},
  419. //@const FPS2HZ number FPS2HZ
  420. { "FPS2HZ", ROREG_INT(FPS2HZ)},
  421. //@const FPS4HZ number FPS4HZ
  422. { "FPS4HZ", ROREG_INT(FPS4HZ)},
  423. //@const FPS8HZ number FPS8HZ
  424. { "FPS8HZ", ROREG_INT(FPS8HZ)},
  425. //@const FPS16HZ number FPS16HZ
  426. { "FPS16HZ", ROREG_INT(FPS16HZ)},
  427. //@const FPS32HZ number FPS32HZ
  428. { "FPS32HZ", ROREG_INT(FPS32HZ)},
  429. //@const FPS64HZ number FPS64HZ
  430. { "FPS64HZ", ROREG_INT(FPS64HZ)},
  431. { NULL, ROREG_INT(0) }
  432. };
  433. LUAMOD_API int luaopen_mlx90640( lua_State *L ) {
  434. luat_newlib2(L, reg_mlx90640);
  435. return 1;
  436. }