我和我的同学一直在这个项目上工作,基于这篇教学文章https://www.instructables.com/id/Building-a-Simple-Pendulum-and-Measuring-Motion-Wi/,我们的想法是制作一个钟摆,计算g力(从钟摆的周期),然后在我们连接到Arduino的液晶显示器上显示它的值。我们启动并运行了代码(它计算周期),并且我们理解Arduino必须进行某种类型的转换(utf-8)才能将它从电位计获得的值传递给Python。但是,当我们尝试将计算图形周期得到的值发送回arduino并在LCD上显示时,它显示634或其他类似的值,我们尝试用encode代替最初执行的解码,进行相反的操作,但它不起作用。我们不能检查它从串口获得的值,因为在python脚本运行时串口监视器根本不能打开。将Python脚本中计算的浮点数“传输”到Arduino中最实用的方法是什么,这样我们就可以计算g并将其显示在屏幕上。许多论坛建议将它们转换为字符串,而不是传输浮点数,因为arduino接收起来很容易,但我们不确定这是否有效。我相信这是一个简单的问题,但我们似乎就是无法理解。如果您在代码中发现任何其他错误,请让我知道,我们知道它有点粗略。谢谢。
Python代码:
arduino = serial.Serial('COM3', 115200, timeout=.1) #Open connection to Arduino
samples = 200 #We will take this many readings
angle_data = np.zeros(samples) #Creates a vector for our angle data
time_data = np.zeros(samples) #Creates a vector of same length for time
i = 0;
calibrate = 123 #Value to zero potentiometer reading when pendulum is motionless, read from Arduino
while i!=samples:
data = arduino.readline()[0:-2].decode('utf-8')
if data:
angle_data[i] = (float(data) - calibrate)*math.pi/180
time_data[i] = time.perf_counter()
print(angle_data[i])
i = i + 1
min = np.min(angle_data)
print (min)
min_pos, = np.where(angle_data == min)
min_x = time_data[min_pos]
print (min_x)
nos_left = int(min_pos)
max = 0;
for i in range(nos_left,200):
if angle_data[i] > max: max = angle_data[i]
print (max)
max_pos, = np.where(angle_data == max)
max_x = time_data[max_pos]
print (max_x)
period = (max_x - min_x) * 2
print (period)
gforce = (0.165 * 4 * (math.pi) * (math.pi)) / ((period) * (period))
print (gforce)
value_g = arduino.write(gforce)
plt.plot(time_data,angle_data,'ro')
plt.axis([0,time_data[samples-1],-math.pi,math.pi])
plt.xlabel("Time (seconds)")
plt.ylabel("Angle (Radians)")
plt.title("Pendulum Motion - Measured with Arduino and potentiometer")
plt.show()
arduino.close()Arduino代码
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
int period = 0;
void setup() {
lcd.begin(16, 2);
Serial.begin(115200); // use the same baud-rate as the python side
pinMode(A0,INPUT);
lcd.print(" Pendulo ");
int gforce = 0;
}
void loop() {
// set the cursor to column 0, line 1
// (note: line 1 is the second row, since counting begins with 0):
// print the number of seconds since reset:
int degrees;
degrees = getDegree();
Serial.println(degrees);
Serial.println("\n");
delay(50);
if (Serial.available() > 0) {
// read the incoming byte:
gforce = Serial.read();
Serial.print("I received: ");
Serial.println(gforce, DEC);
}
lcd.setCursor(0, 1);
lcd.print(gforce);
}
int getDegree()
{
int sensor_value = analogRead(A0);
float voltage;
voltage = (float)sensor_value*5/1023;
float degrees = (voltage*300)/5;
return degrees;
}发布于 2019-03-26 23:08:46
这似乎是Arduino Serial的parseFloat()方法的一个很好的例子:
if (Serial.available() > 0) {
/* instead of Serial.read(), use: */
gforce = Serial.parseFloat();
Serial.print("I received: ");
Serial.println(gforce, DEC);
}本质上,它取出接收到的串行数据中任何看起来像浮点数的东西,即使它与其他非数字字符混合在一起。
它还可以与Software Serial一起使用。
https://stackoverflow.com/questions/55347532
复制相似问题