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

时间:2019-04-04 13:02:54

标签: python arduino synchronization

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

VOLUME

//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
}

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

Arduino串行监视器的示例:

#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

Python输出示例:

-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

我猜我需要以某种方式同步它们,但是我不确定该怎么做!感谢您的帮助,因为我对这种事情还很陌生。

2 个答案:

答案 0 :(得分:0)

您正在使用哪个Arduino?大多数处理器的速度为16MHz,但ESP32和其他一些处理器的速度约为100-200MHz。如果您需要以每秒100个点的速度对16个引脚进行采样,则可能会使处理器发挥最大作用。您可以通过将读数分成两个不同的Arduino进行测试,并查看可以达到的最大采样率。

答案 1 :(得分:0)

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

也许尝试使用ser.readline()可以确保您正在读取终止行?

希望有帮助,