Raspberry Pi - 与启用i2c的红外温度传感器(MLX90614)接口
因此,我被招募到我们学校的Baja赛车队,在那里我们设计构建并与越野沙丘车类型赛车竞争。我是CS专业大学毕业生,在Python方面有相当多的经验,因此被要求帮助电气团队与我们想要的所有传感器接口。到目前为止这么好,但现在我正在使用一个读取环境温度和物体温度的红外温度传感器。 (我们打算把它放在引擎上的某个地方读取它的温度并输出到我们的GUI)Raspberry Pi - 与启用i2c的红外温度传感器(MLX90614)接口
问题是似乎只有使用这种传感器的库全部用C编写,通常与arduinos一起使用...尽管如此,我编译和编辑了一些我在网上找到的C代码,它效果很好!在C. :(因为我们的项目完全基于python;我真的很喜欢通过i2c和Python读取这个传感器的一些帮助,虽然我没有很多编写库的经验,尤其是对于电子学。提示将是巨大的,引导我在正确的方向下面是我们目前使用的我基本上要在Python同样的事情的C代码:!
//fordit: gcc MLXi2c.c -o i2c -l bcm2835
#include <stdio.h>
#include <bcm2835.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include<time.h>
#define AVG 1 //averaging samples
#define LOGTIME 10 //loging period
int main(int argc, char **argv)
{
unsigned char buf[6];
unsigned char i,reg;
double temp=0,calc=0, skytemp,atemp;
FILE *flog;
flog=fopen("mlxlog.csv", "a");..
bcm2835_init();
bcm2835_i2c_begin();
bcm2835_i2c_set_baudrate(25000);.
// set address...........................................................................................
bcm2835_i2c_setSlaveAddress(0x5a);
....
printf("\nOk, your device is working!!\n");
....
....
while(1) {
time_t t = time(NULL);
<------>struct tm tm = *localtime(&t);
<------>calc=0;
<------>reg=7;
<------>for(i=0;i<AVG;i++){
<------> bcm2835_i2c_begin();
<------> bcm2835_i2c_write (®, 1);
<------> bcm2835_i2c_read_register_rs(®,&buf[0],3);
<------> temp = (double) (((buf[1]) << 8) + buf[0]);
<------> temp = (temp * 0.02)-0.01;
<--> temp = temp - 273.15;
<------> calc+=temp;
<------> sleep(1);
<------> }
<------>skytemp=calc/AVG;
<------>calc=0;
<------>reg=6;
<------>for(i=0;i<AVG;i++){
<------> bcm2835_i2c_begin();
<------> bcm2835_i2c_write (®, 1);
<------> bcm2835_i2c_read_register_rs(®,&buf[0],3);
<------> temp = (double) (((buf[1]) << 8) + buf[0]);
<------> temp = (temp * 0.02)-0.01;
<--> temp = temp - 273.15;
<------> calc+=temp;
<------> sleep(1);
<------> }
<------>atemp=calc/AVG;
<------>printf("%02d-%02d %02d:%02d:%02d\n Tambi=%04.2f C, Tobj=%04.2f C\n", tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec,atemp,skytemp);
<------>fprintf(flog,"%04d-%02d-%02d %02d:%02d:%02d,%04.2f,%04.02f\n",tm.tm_year+1900, tm.tm_mon +1, tm.tm_mday,tm.tm_hour, tm.tm_min, tm.tm_sec,atemp,skytemp);
<------>fflush(flog);
<------>sleep(LOGTIME-(2*AVG));
}
...
printf("[done]\n");
}
在此先感谢
- 埃迪
变化:
- 删除
while(1)
- 每次执行只有一个读。如果传感器需要重复启动,则可能需要for/while循环;只是不要让它成为一个无限循环,除非你打算从python杀死进程。 - 最后的
printf
现在输出一个JSON字符串,由开始和结束管道/ |分隔。 - 添加
return 0;
到main()
所以Python的子模块知道 什么事 - 删除评论和句号导致编译器错误(低C级专家,有显著的句号?)
保存为mlx90614_query.c和编译:
//fordit: gcc mlx90614_query.c -o mlx90614_query -l bcm2835
#include <stdio.h>
#include <bcm2835.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <time.h>
#define AVG 1 //averaging samples
#define LOGTIME 10 //loging period
int main(int argc, char **argv)
{
unsigned char buf[6];
unsigned char i,reg;
double temp=0,calc=0, skytemp,atemp;
FILE *flog;
flog=fopen("mlxlog.csv", "a");
bcm2835_init();
bcm2835_i2c_begin();
bcm2835_i2c_set_baudrate(25000);
// set address
bcm2835_i2c_setSlaveAddress(0x5a);
printf("\nOk, your device is working!!\n");
time_t t = time(NULL);
struct tm tm = *localtime(&t);
calc=0;
reg=7;
for(i=0;i<AVG;i++){
bcm2835_i2c_begin();
bcm2835_i2c_write (®, 1);
bcm2835_i2c_read_register_rs(®,&buf[0],3);
temp = (double) (((buf[1]) << 8) + buf[0]);
temp = (temp * 0.02)-0.01;
temp = temp - 273.15;
calc+=temp;
sleep(1);
}
skytemp=calc/AVG;
calc=0;
reg=6;
for(i=0;i<AVG;i++){
bcm2835_i2c_begin();
bcm2835_i2c_write (®, 1);
bcm2835_i2c_read_register_rs(®,&buf[0],3);
temp = (double) (((buf[1]) << 8) + buf[0]);
temp = (temp * 0.02)-0.01;
temp = temp - 273.15;
calc+=temp;
sleep(1);
}
atemp=calc/AVG;
printf("|{\"time\":{\"month\":\"%02d\",\"day\":\"%02d\",\"hour\":\"%02d\",\"min\":\"%02d\",\"sec\":\"%02d\"},\"data\":{\"t_ambi\":\"%04.2f\",\"t_obj\":\"%04.2f\"}}|\n", tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec,atemp,skytemp);
fprintf(flog,"%04d-%02d-%02d %02d:%02d:%02d,%04.2f,%04.02f\n",tm.tm_year+1900, tm.tm_mon +1, tm.tm_mday,tm.tm_hour, tm.tm_min, tm.tm_sec,atemp,skytemp);
fflush(flog);
sleep(LOGTIME-(2*AVG));
printf("[done]\n");
return 0;
}
你的C程序现在输出现有的调试消息加上此JSON(“| “作为分隔符):
{
"time": {
"month": "03",
"day": "09",
"hour": "20",
"min": "24",
"sec": "28"
},
"data": {
"t_ambi": "77.77",
"t_obj": "34.44"
}
}
保存此在同一文件夹中的python脚本作为mlx90614_query(多余的空格会造成麻烦,如果你复制/粘贴到交互式解释):
from __future__ import print_function
import json
import subprocess
import sys
sensor_dict = {}
py3 = False
# python version check determines string handling
try:
if (sys.version_info > (3, 0)):
py3 = True
except:
pass
try:
subp_ret = subprocess.check_output(["./mlx90614_query"])
# at this point, subprocess succeeded and subp_ret holds your output
if py3:
subp_ret = subp_ret.decode('utf8')
sensor_dict = json.loads(subp_ret.split("|")[1].strip())
# cast temperatures to float and print
for k in sensor_dict["data"]:
val = float(sensor_dict["data"][k])
sensor_dict["data"][k] = val
print (k, "\t", val)
# cast date/time segments to int and print
for k in sensor_dict["time"]:
val = int(sensor_dict["time"][k])
sensor_dict["time"][k] = val
print (k, "\t", val)
# Now go win that race! :P
except Exception as e:
print(str(e))
输出:
$ gcc mlx90614_query.c -o mlx90614_query -l bcm2835
$
$ ./mlx90614_query
Ok, your device is working!!
|{"time":{"month":"03","day":"09","hour":"21","min":"45","sec":"53"},"data":{"t_ambi":"0.00","t_obj":"0.00"}}|
[done]
$ python3.4 mlx_print.py
t_obj 34.44
t_ambi 77.77
hour 21
sec 33
min 58
month 3
day 9
$ python2 mlx_print.py
t_ambi 77.77
t_obj 34.44
min 58
sec 37
day 9
hour 21
month 3
对不起,窃取你的功课 - 只需< 3码:d
非常感谢!该团队真诚地感谢你。虽然有一个问题,我有办法加快速度吗?当我运行mlx_print.py时,实际输出任何数据需要10秒钟,这是否与采样平均值有关?谢谢你 –
不客气:)虽然有关延迟的奇怪。我没有进入低级i2C的东西,没有你的传感器,所以我也做不了多少测试。这可能是抽样平均数,但很难从我的立场说 - 我会看看互联网是否有关于此事的更多信息。当我用一些随机的硬编码值运行脚本+ c程序时,它立即返回。您是否尝试过几次运行c程序?没有python脚本会延迟吗?总是? – jDo
请尝试编译[this one](http://pastebin.com/raw/PnLVC44v)来查看它是否改变了任何内容。我从字面上只删除了while循环,并且这次添加了return语句;没有清理或其他任何东西(我可能在上次引入了一个微妙的错误)。你可以摆脱睡眠(LOGTIME-(2 * AVG));'但是首先尝试它。编译/ usr/bin/ld时出现错误 – jDo
为什么不简单地从Python中调用C程序而不是重写整个事情呢? – jDo
我不知道你能做到吗?你介意阐述吗? 我其实想到这个,如果我能从C程序中得到返回的值到Python,我会是金。 –
如果你有一个不能无限运行的程序,就像'import subprocess; sens_val = subprocess.check_output([“path_to_your_c_program”]) 如果你的C程序运行无限(例如在一个'while(1)'循环中就像在arduino上一样),你可以简单地删除那个循环,每次执行读取一次传感器读数,将其打印到标准输出并退出。 – jDo