我目前正在使用两个微控制器,一个是用于AD转换的MSP430系列,另一个是Raspberry Pi ZeroW。该系统将在下周进入平流层的实验中使用。 MSP430G2553必须从Geiger计数器读取数据,然后按位发送给Pi。 Energia代码非常有效。 Raspberry Pi现在应该读取通过GPIO传输的字节,并将数字输入信号转换为人类可读的代码。那是哪里出了问题。我们已经测试了Python 3代码,而没有连接到Pi的MSP控制器已经接收到数据(!)(如果我们位于原子能发电站中间,则计数率会很高)。因此,确实存在错误。我认为问题出在将输入字节转换为人类可读的代码中,但是即使通过检查许多文章,我也找不到任何解决方案。问题部分应该是:
input = GPIO.input(8)
input_value = int.from_bytes(b'input', byteorder=sys.byteorder)
尽管代码的另一部分似乎可以正常工作,但我也将其附加在这里:
# coding: utf-8
# In[1]:
#!/usr/bin/python
import RPi.GPIO as GPIO
import os
import datetime
import time
import sys
from collections import Counter
GPIO.setwarnings(False) #read import from GPIO pin
GPIO.setmode(GPIO.BOARD)
GPIO.setup(8, GPIO.IN)
GPIO.setup(3, GPIO.OUT)
input = GPIO.input(8)
input_value = int.from_bytes(b'input', byteorder=sys.byteorder) #convert incoming digital signal to int
GPIO.output(3, GPIO.HIGH)
v = input_value * (3.0 / 1023.0) #conversion factor for voltage signal according to MSP430 handbook
vcount = 3.4
while True:
now = datetime.datetime.now()
curtime = now.strftime("%m/%d/%Y, %H:%M:%S")
endTime = datetime.datetime.now() + datetime.timedelta(seconds=60)
#use inner loop to count, how often the Geiger counter has received a alpha, beta or gamma photon within one minute. The ADC MSP430 sends a digital voltage count every 100 milliseconds to the Pi and the Pi writes a 1 into a list every time the voltage measured is higher than the background voltage (which means that a photon has been detected.) This part of the code is working.
while True:
countfile = open('/home/pi/Desktop/countfile.txt', 'a')
if v >= vcount:
countfile.write('1')
else:
pass
countfile.close()
if datetime.datetime.now() >= endTime:
break #end inner loop after 60 seconds
#use outer loop to check how often a particle has been counted within one minute and write those counts per minute to a csv file. This part of the code is completely working.
countfile = open('/home/pi/Desktop/countfile.txt', 'r')
countdata = countfile.read()
cpmc = Counter(countdata)
for number in ('1'):
cpm = '%s : %d' % (number, cpmc[number])
Geigerdata = open('/home/pi/Desktop/Geigerdata.csv', 'a')
Geigerdata.write('CPM= ' + cpm + ', Time= ' + curtime + '\n')
Geigerdata.close()
countfile.close()
countfile = open('/home/pi/Desktop/countfile.txt', 'w')
countfile.write('0')
countfile.close()
最后将C语言中的Energia代码用于信号的AD转换:
#include "Energia.h"
int sensorPin = A4;
int v=0;
int outputPin = 1.6;
void setup() {
Serial.begin(9600);
}
void loop() {
v = analogRead(sensorPin);
for (int outputPin = 0; outputPin < 10; outputPin++){
int bit = v & (1<<outputPin);
digitalWrite(outputPin, bit);
}
delay(100);
}
我只是附上这个作为参考,您知道Pi接收的字节看起来应该如何。我非常感谢社区的帮助,因为我们真的没时间了,研究团队中没有计算机专家。非常感谢!