[TASK] Using an ultrasonic ranger

[TASK] Using an ultrasonic ranger

Postby admin » Wed Nov 04, 2015 5:02 pm

We will go through an example of controlling an ultrasonic ranger by using a PHPoC Blue.

Image

PHPoC Blue diagram
Image

Source Codes
1. task0.php
Code: Select all

<?php
 
if(_SERVER("REQUEST_METHOD"))
    exit; // avoid php execution via http request
 
include_once 
"/lib/sd_340.php";
 
echo 
"PHPoC example : P4S-34X / HT / HC-SR04 Ultrasonic Module\r\n";
 
// setup trigger pulse timer
ht_ioctl(0, "set mode output pulse");
ht_ioctl(0, "set div us");
ht_ioctl(0, "set repc 1");
ht_ioctl(0, "set count 5 10"); // 10us pulse width
 
// setup echo capture timer
ht_ioctl(1, "reset");
ht_ioctl(1, "set div us");
ht_ioctl(1, "set mode capture toggle");
ht_ioctl(1, "set trigger from pin rise");
ht_ioctl(1, "set repc 4");
 
while(1)
{
    ht_ioctl(1, "start"); // we should start capture timer first
    ht_ioctl(0, "start"); // start trigger pulse
 
    usleep
(100000); // sleep 100ms
    ht_ioctl(1, "stop");
 
    
// 1st capture value ("get count 0") is always zero.
    // we should get 2nd capture value;
    $us = ht_ioctl(1, "get count 1");
 
    $dist 
= $us * 340.0 / 2; // us to meter conversion
    $dist = $dist / 10000; // meter to centimeter conversion
 
    printf
("%d us, %.1f cm\r\n", $us, $dist);
 
    sleep
(1);
}
 
?>


Please download a PSP (PHPoC Support Package) of libraries and examples from PHPoC, and upload it in the PHPoC Blue.



Video
phpBB [video]
admin
Site Admin
 
Posts: 167
Joined: Mon Jan 20, 2014 4:40 pm

Return to Example

Who is online

Users browsing this forum: No registered users and 1 guest

cron