Python未正确从Arduino收集数据(需要帮助同步)

2024-04-28 23:19:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要帮助使我的Arduinos输出与Python同步。我使用arduinos模拟输出连接到一些光电二极管,通过检查每个光电二极管上的电压来跟踪LED,然后将其输出到python中进行处理。目前我的代码如下:

//Arduino
float A0vo = 0;  // variable to store the value coming from the sensor
float A1vo = 0;  // variable to store the value coming from the sensor
float A2vo = 0;  // variable to store the value coming from the sensor
float A3vo = 0;  // variable to store the value coming from the sensor
float A4vo = 0;  // variable to store the value coming from the sensor
float A5vo = 0;  // variable to store the value coming from the sensor
float A6vo = 0;  // variable to store the value coming from the sensor
float A7vo = 0;  // variable to store the value coming from the sensor
float A8vo = 0;  // variable to store the value coming from the sensor
float A9vo = 0;  // variable to store the value coming from the sensor
float A10vo = 0;  // variable to store the value coming from the sensor
float A11vo = 0;  // variable to store the value coming from the sensor
float A12vo = 0;  // variable to store the value coming from the sensor
float A13vo = 0;  // variable to store the value coming from the sensor
float A14vo = 0;  // variable to store the value coming from the sensor
float A15vo = 0;  // variable to store the value coming from the sensor
float VT = 0; // variale to store the total value of the voltages from the sensors
float POS = 0; // variable to store the position coeficient of the LED
float FP = 0; // variable to store the final position of the LED
float ZP = 0.52; // change for zero position
float cal[16] = {-24, -21, -18, -15, -12, -9, -6, -3, 0, 3, 6, 9, 12, 15, 18, 21}; //calibration array to corresponding photodiodes position in cm

void setup() {
  Serial.begin(19200); // set baud rate
}

void loop() {
  // read the value from the sensor:
  A0vo = analogRead(A0); // extract voltage at given photodiode
  A1vo = analogRead(A1); // extract voltage at given photodiode
  A2vo = analogRead(A2); // extract voltage at given photodiode
  A3vo = analogRead(A3); // extract voltage at given photodiode
  A4vo = analogRead(A4); // extract voltage at given photodiode
  A5vo = analogRead(A5); // extract voltage at given photodiode
  A6vo = analogRead(A6); // extract voltage at given photodiode
  A7vo = analogRead(A7); // extract voltage at given photodiode
  A8vo = analogRead(A8); // extract voltage at given photodiode
  A9vo = analogRead(A9); // extract voltage at given photodiode
  A10vo = analogRead(A10); // extract voltage at given photodiode
  A11vo = analogRead(A11); // extract voltage at given photodiode
  A12vo = analogRead(A12); // extract voltage at given photodiode
  A13vo = analogRead(A13); // extract voltage at given photodiode
  A14vo = analogRead(A14); // extract voltage at given photodiode
  A15vo = analogRead(A15); // extract voltage at given photodiode
  VT = (A0vo + A1vo + A3vo + A4vo + A5vo + A6vo + A7vo + A8vo + A9vo + A10vo + A11vo + A12vo + A13vo + A14vo + A15vo); // populate total voltage variable
  POS = (A0vo*cal[0] + A1vo*cal[1] + A2vo*cal[2] + A3vo*cal[3] + A4vo*cal[4] + A5vo*cal[5] + A6vo*cal[6] + A7vo*cal[7] + A8vo*cal[8] + A9vo*cal[9] + A10vo*cal[10] + A11vo*cal[11] + A12vo*cal[12] + A13vo*cal[13] + A14vo*cal[14] + A15vo*cal[15]); // populate position coefficent variable
  FP = (POS/VT); // calculate final position
//

  Serial.println(FP+ZP);
delay(10); // set loop delay 1000=1sec for aquistion
}

#Python

import serial
import time
import numpy as np
import sys

ser = serial.Serial('COM3', 19200, timeout=0) #config serial port to read


outpl = []
outp = []
outp2 = []
outp3 = []
outp4 = []

TI = 10 #time for data collection to continue for in seconds

t_start = time.time()
t_end = time.time() + TI
while time.time() < t_end:
    try:
        outp = ser.read(3) #reads serial port
        outp2 = np.array([float(i) for i in((outp.decode('utf-8'))).split(',')]) #decodes and prints data
        outp4 = ','.join(str(e) for e in outp2) #converts to pastable format 
        outpl.append(outp2) #
        print(outp4)
        print((int(((time.time()-t_start)/((time.time()+TI)-t_start))*200)), end="\r") #loading in %
        time.sleep(0.01)
    except ValueError: #checks for errors
        pass

print('DONE!')
ser.close() #close serial

当我每秒获取10个或更少的数据点时,它工作正常,但我每秒需要大约100个点

Arduinos系列监视器示例:

-5.06
-4.75
-4.35
-3.93
-3.56
-3.15
-2.84
-2.49
-2.17
-1.91
-1.55
-1.21
-0.98
-0.65
-0.42
-0.19
0.09
0.41
0.72
1.08
1.51
1.91
2.30
2.67
3.17
3.60
4.17
4.85
5.44

Python输出示例:

0.0
0.4
7.0
0.0
0.42
10.0
35.0
0.0
0.2
9.0
0.0
0.24
10.0
15.0
0.0
0.0
7.0
0.0
95.0
0.0
0.74
9.5
3.0
0.0
26.0
0.0
0.04
8.8
0.0
0.0
49.0
0.0
0.22
7.9
2.0
0.0
66.0

我猜我需要以某种方式同步它们,但我不知道如何做到这一点!谢谢你的帮助,因为我对这类事情还不太熟悉


Tags: thetostorefromvalueextractsensorfloat
2条回答

你用的是哪种Arduino?大多数处理器的速度为16MHz,但ESP32和其他一些处理器的速度为100-200MHz。如果您需要以每秒100点的速度对16个管脚进行采样,这可能会使处理器达到最大值。您可以通过在两个不同的Arduinos之间拆分读数进行测试,并查看您可以达到的最大采样率

在这一行outp = ser.read(3) #reads serial port中,您正在为每个循环读取3个字节的数据。您确定您从arduino发送的数据仅此而已吗

也许可以尝试使用ser.readline(),这将确保您读取的是终止的行

希望有帮助

相关问题 更多 >