(全网独一份,一线项目经验,拒绝转载,违者必究)
硬件平台:
Arduino Uno
HC-06蓝牙模块(出场配对密码:1234)
力传感器(购买链接附下)
软件平台:
Arduino IDE
Anaconda Spyder
Serial Chart
Arduino bluetooth controller
arduino 软件代码:
int Work_Mode = 2;//0是正常的调试模式,1是蓝牙(hc-06)工作模式;2是python读取模式void setup() {// put your setup code here, to run once:Serial.begin(9600);}float exchangementToWeight(float sensorvalue){//数据的标定于转化函数 (102.5,5000);(2000,2757.85);float k = (5000-2757.85)/(102.5-2000);float b = 5000-102.5*k;float weight = (sensorvalue-b)/k;return weight-870.927;}void loop() {// put your main code here, to run repeatedly:int sensorvalue1 = analogRead(A0);int sensorvalue2 = analogRead(A1);int sensorvalue3 = analogRead(A2);int sensorvalue4 = analogRead(A3);int sensorvalue5 = analogRead(A4);float voltage1 = sensorvalue1*(5000/1024);float voltage2 = sensorvalue2*(5000/1024);float voltage3 = sensorvalue3*(5000/1024);float voltage4 = sensorvalue4*(5000/1024);float voltage5 = sensorvalue5*(5000/1024);float weight_array[5] = {exchangementToWeight(voltage1),exchangementToWeight(voltage2),exchangementToWeight(voltage3),exchangementToWeight(voltage4),exchangementToWeight(voltage5)};if(Work_Mode==0){Serial.print(weight_array[0],DEC);Serial.print(",");Serial.print(weight_array[1],DEC);Serial.print(",");Serial.print(weight_array[2],DEC);Serial.print(",");Serial.print(weight_array[3],DEC);Serial.print(",");Serial.print(weight_array[4],DEC);Serial.print(",");Serial.print("\r\n");delay(100); }if(Work_Mode==1){int i = 0;Serial.print("hello world");while(i<5){Serial.print("the weight of sensor ");Serial.println(i,DEC);Serial.println(weight_array[i],DEC);i++;delay(100);}}if(Work_Mode==2){Serial.print(weight_array[0],DEC);Serial.print(" ");Serial.print(weight_array[1],DEC);Serial.print(" ");Serial.print(weight_array[2],DEC);Serial.print(" ");Serial.print(weight_array[3],DEC);Serial.print(" ");Serial.print(weight_array[4],DEC);Serial.print("\r");Serial.print("\n");delay(100);}
}
python代码
# 读取arduino串口数据并保存
import serial
import matplotlib.pyplot as plt
import numpy as np
import timeser = serial.Serial(port='COM7', baudrate=9600)
#COM口的值要时刻修改!
data = []
data0 = []
data1 = []
data2 = []
data3 = []
data4 = []data_time_list = []
data_time = 0
times = 1
limit = 100000while True:# 读取一行的输出data = ser.readline()# 读取的结果位byte形式,我们转码成字符串形式data_mid = data.decode()# 以空格位分隔符进行字符串分割readdata = data_mid.split(" ")times = times + 1# map() 会根据提供的函数对指定序列做映射,第一个参数 function 以参数序列中的每一个元素调用 function 函数,返回包含每次 function 函数返回值的新列表。if len(readdata) == 5:while data_time < limit :data_array = list(map(float, readdata))data_time_list.append(data_time)# 构建图表的x轴data_time = data_time + 1data0.append(data_array[0])data1.append(data_array[1])data2.append(data_array[2])data3.append(data_array[3])data4.append(data_array[4])if data_time == limit:break# data_time_list = range(30)
# data0 = range(30)plt.figure(times)plt.plot(data_time_list, data0)plt.plot(data_time_list, data1)plt.plot(data_time_list, data2)plt.plot(data_time_list, data3)plt.plot(data_time_list, data4)plt.title("The forces from the different sensors")plt.xlabel('Time/S')plt.ylabel('Force/N')plt.show(times)time.sleep(100)plt.close(times)
SerialChart 配置教程:
1.https://blog.csdn.net/fengzhiwu3/article/details/85330330
2.http://blog.sina.com.cn/s/blog_a55f2f4e01014egl.html
Arduino Bluetooth Controller下载地址:(需要科学上网)
https://play.google.com/store/apps/details?id=com.giumig.apps.bluetoothserialmonitor&hl=en_US&gl=US
HC-06的使用教程与Arduino Bluetooth Controller交互的教程:
HC-06蓝牙模块使用小结
反思与总结:
1.关于工作模式的建立:
通过int mode 可以选择不同的工作模式,这方便了开发者进行模块化开发,从而提高了开发效率与代码的逻辑清晰层次。
int Work_Mode = 2;//0是正常的调试模式,1是蓝牙(hc-06)工作模式;2是python读取模式
2.关于arduino与python的联合开发问题:
通过serial串口接口,我们可以将python与arduino进行通信处理,只不过需要在arduino与python间进行数据格式的定制,本项目中格式定义为 :
【data1 data2 data3 data4 data5】
(即通过空格进行数据分离)
3.关于数据传输过程中的问题(极其有价值)
注意在我的代码中有一行:
if len(readdata) == 5:
这一行存在的原因是笔者经历了惨痛教训以后才加上去的,由于我们根据arduino代码可以看出笔者是一次性传输了5个数据作为一组到python中进行处理,但是由于数据存在丢包和我在python进行运行操作时并不能够非常准时的在一组数据传输完以后再读取数据,所以经常会出现数据的list有部分元素为空,进而造成数组越界或者无法进行map(float ,str)操作的情况,而在检查了传输数据的length以后,我们可以有效避免这一问题。
所以在进行数据传输时记得进行数据的检查!
后附:(项目构架图)- Xmind 生成