天天看點

arduino序列槽螢幕顯示nan_Arduino-python 傳資料 并且顯示圖像

arduino序列槽螢幕顯示nan_Arduino-python 傳資料 并且顯示圖像

1 Arduino 接線圖

arduino序列槽螢幕顯示nan_Arduino-python 傳資料 并且顯示圖像

2 arduino的程式

int 
           

3 Python 顯示資料

import 
           

4 顯示效果

arduino序列槽螢幕顯示nan_Arduino-python 傳資料 并且顯示圖像

5 擴充 添加另外一個box

如果需要将添加一個目标 target , 就需要建立一個 box 原點與圓柱一樣y=-0.5, 長度窄點 length =0.2,width,height=3顔色設定為綠色

target = box(pos = vector(0,-0.5,0),length=0.2,width = 3,height=3,color=color.green)

#最終需要将其放在圓柱的尾端 
target.pox.x = -3+pos  #其中-3為初始的長度 需要減去
           

完整代碼如下:

import serial    #導入序列槽庫
from vpython import *     #導入vpython  進行資料繪圖庫
arduino = serial.Serial("com20",9600)    #連接配接序列槽
rod = cylinder(radius=0.5,length=6,pos=vector(-3,0,0))  #繪制一個半徑0.5 長6 像素的圓柱 坐标點在 x=-3,y=0,z=0
lab = label(pos=vector(0,1,0),text='distance is : ',height=10,box = False) #繪制一個标簽,存放數字,左邊x=0,z=0.y=1,高度10 沒有外框線
target=box(pos=vector(0,-.5,0), length=0.2, width=3, height=3, color=color.green)
while 1:

    try:
        if(arduino.inWaiting()>0):   #如果有序列槽資料
            data = float(arduino.readline().strip()) #讀取資料 去除 其他換行符合 轉換為小數
            rod.length= data           #将圓柱的長度設定為讀取的數值
            print(data+-3)

            lab.text = 'distance is : ' + str(data)  #文字顯示
            target.pos.x = -3 + data
    except:   #跳過異常
        pass
           
arduino序列槽螢幕顯示nan_Arduino-python 傳資料 并且顯示圖像

繼續閱讀