smart fan stopping movement when it detects an object
[Board]
a.PHPoC Blue (P4S-342)
b. bread board extend board for PHPoC Blue
[Sensor/Module]
a. servo motor: RB-65PG with pan tilt component
b. ultrasonic sensor: HC-SR04
c. fan motor: Keyes 111330 fan motor
[Connection]
a. PHPoC Blue - Servo Motor
b. PHPoC Blue - Ultrasonic Sensor
c. PHPoC Blue - Fan Motor
[Description]
a. automatic stand alone operation
b. continuously moves the body unless there is no object by servo motor
c. try to detects objects regardless of movement
d. fan motor keeps running
e. stops movement about one second if it detects an object
f. after one second, it moves again
[video]
[Source Code - task0.php]
PHP Code:
<?php
// define constants
define("WIDTH_MIN", 700);
define("WIDTH_MAX", 2300);
// define variables
$pid_ht0 = 0;
$pid_ht1 = 0;
$pid_ht3 = 0;
$dir_flag = 0;
$width = WIDTH_MIN;
// start fan function
function run_fan()
{
$pid = pid_open("/mmap/uio0");
pid_ioctl($pid, "set 0-1 mode out low");
pid_ioctl($pid, "set 0 output low");
pid_ioctl($pid, "set 1 output high");
pid_close($pid);
}
// control servo motor function
function init_ht3()
{
global $pid_ht3;
$pid_ht3 = pid_open("/mmap/ht3");
pid_ioctl($pid_ht3, "set div us");
pid_ioctl($pid_ht3, "set mode output pwm");
pid_ioctl($pid_ht3, "set count 1500 20000");
}
// output trigger pulse function
function send_trigger()
{
global $pid_ht0;
$pid_ht0 = pid_open("/mmap/ht0");
pid_ioctl($pid_ht0, "set div us");
pid_ioctl($pid_ht0, "set mode output pulse");
pid_ioctl($pid_ht0, "set count 5 5");
pid_ioctl($pid_ht0, "set repc 1");
pid_ioctl($pid_ht0, "start");
while(pid_ioctl($pid_ht0, "get state"))
;
pid_close($pid_ht0);
}
// capture sensor response function
function start_capture()
{
global $pid_ht1;
$pid_ht1 = pid_open("/mmap/ht1");
pid_ioctl($pid_ht1, "set div us");
pid_ioctl($pid_ht1, "set mode capture fall");
pid_ioctl($pid_ht1, "set trigger from pin rise");
pid_ioctl($pid_ht1, "set repc 1");
pid_ioctl($pid_ht1, "start");
send_trigger();
usleep(30000);
}
// detect distance function
function detect()
{
global $pid_ht0;
global $pid_ht1;
start_capture();
$state = pid_ioctl($pid_ht1, "get state");
if($state != 0)
{
pid_ioctl($pid_ht1, "stop");
$distance = -1;
}
else
{
$duration = pid_ioctl($pid_ht1, "get count 0");
$distance = (331.5 + 0.60714 * 25) * ($duration / 2) / 10000;
}
pid_close($pid_ht1);
return $distance;
}
// init servo motor and fun fan motor
init_ht3();
pid_ioctl($pid_ht3, "start");
run_fan();
// start main loop
while(1)
{
pid_ioctl($pid_ht3, "set count $width 20000");
if($width <= WIDTH_MIN)
$dir_flag = 1;
elseif($width >= WIDTH_MAX)
$dir_flag = 2;
if($dir_flag == 0)
{
while(1)
{
$dir_flag = rand(1, 2);
if($dir_flag != 0)
break;
}
sleep(1);
}
elseif($dir_flag == 2)
$width -= 50;
else
$width += 50;
$distance = detect();
if(($distance > 3) && ($distance < 25))
$dir_flag = 0;
}
?>