close

(1) 超音波測距離-使用python

參考資料: Raspberry Pi 筆記:超音波測距離

[材料]

• Raspberry Pi 主板 x1
• US-100超音波模組 x1
• 連接線  x4

[基礎知識:超音波測距]

超音波測距的方式是發射一個電波,當電波遇到物體反射回來,再被測距儀偵測到反射電波,利用來回時間與音波的速度算出距離,計算公式如下:
距離=(音波發射與接收時間差 * 聲音速度(340M/S))/2;
聲音的速度,在一般空氣中約為每秒340公尺,因來回時間要將距離除以二,才是單程的距離。實際的聲音速度決定於好幾個環境因素,其中一個是溫度,計算時需將環境因素考慮在內,才能更精確計算距離。
US-100這個型號,根據規格文件,這個模組最遠可測得2公分~4.5公尺, 輸入電壓大約 2.4V ~ 5.5V,其偵測廣度大約是15度。這個模組具有溫度感測,距離值已經溫度調校,無需再根據環境溫度對超音波聲速進行校正。

[線路連接與電路圖](圖片以HC-SR04替代)

• US-100 VCC接pin2(+5V),GND接pin6(GND)
• US-100 Trig/TX接Pin16(GPIO23),Echo/RX接Pin18(GPIO24)

undefined
[程式碼]

import RPi.GPIO as GPIO
import time
trigger_pin = 23
echo_pin = 24

GPIO.setmode(GPIO.BCM)
GPIO.setup(trigger_pin, GPIO.OUT)
GPIO.setup(echo_pin, GPIO.IN)

def send_trigger_pulse():
    GPIO.output(trigger_pin, True)
    time.sleep(0.001)
    GPIO.output(trigger_pin, False)

def wait_for_echo(value, timeout):
    count = timeout
    while GPIO.input(echo_pin) != value and count > 0:
        count = count - 1

def get_distance():
    send_trigger_pulse()
    wait_for_echo(True, 5000)
    start = time.time()
    wait_for_echo(False, 5000)
    finish = time.time()
    pulse_len = finish - start
    distance_cm = pulse_len * 340 *100 /2
    distance_in = distance_cm / 2.5
    return (distance_cm, distance_in)

while True:
    print("cm=%f\tinches=%f" % get_distance())
    time.sleep(1)

<2>使用霍爾效應感測器測量車輛速度

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)

pulse = 0
distance = 0
rpm = 0.00
speed = 0.00
wheel_c = 20
hall = 18
elapse = 0.00

start = time.time()

GPIO.setup(hall, GPIO.IN, pull_up_down = GPIO.PUD_UP)


def get_rpm():
    return rpm

def get_speed():
    return speed

def get_distance():
    return distance

def get_elapse():
    return elapse

def get_pulse(number):
    global elapse,distance,start,pulse,speed,rpm,multiplier
    cycle = 0
    pulse+=1
    cycle+=1
    if pulse > 0:
        elapse = time.time() - start
        pulse -=1
    if cycle > 0:
        distance += wheel_c
        cycle -= 1

    speed = (wheel_c*multiplier)/100000
    rpm = 1/elapse *60

    start = time.time()

try:

    time.sleep(1)

    GPIO.add_event_detect(hall,GPIO.FALLING,callback = get_pulse,bouncetime=20)
    while True:
    print('rpm:{0:.2f} speed:{1:.2f} distance:{2} elapse:{3:.4f}'.format(rpm,speed,distance,elapse))
        time.sleep(0.1) #to reduce CPU load, print every 100 milliseconds


except KeyboardInterrupt:
    print('You have pressed Ctrl+C! How dare you stopped this beautiful thing?!')
    GPIO.cleanup()

Speed sensor calculation using Python
How to use Python to measure speed of bike like a digital speedometer, using a Raspberry Pi?
How To Use A Hall Effect Sensor With The Raspberry Pi
Getting feedback from a hall sensor with Raspberry Pi
https://www.sunfounder.com/learn/sensor-kit-v2-0-for-b/lesson-17-hall-sensor-sensor-kit-v2-0-for-b.html
http://www.14core.com/wiring-the-3144-hall-effect-sensor-with-raspberry-pi/
https://stackoverflow.com/questions/40738776/how-to-use-python-to-measure-speed-of-bike-like-a-digital-speedometer-using-a-r

 

<3>How to connect your phone to your ESP8266 module
Control ESP8266 Over the Internet (from Anywhere)


 

 

 

 

 

 

 

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 stanley 的頭像
    stanley

    史坦利Stanley程式Maker的部落格

    stanley 發表在 痞客邦 留言(0) 人氣()