[Subject]
smart fan stopping movement when it detects an object
Click image for larger version

Name:	018.jpg
Views:	5
Size:	31.4 KB
ID:	177

[Board]
a.PHPoC Blue (P4S-342)
b. bread board extend board for PHPoC Blue
Click image for larger version

Name:	017.jpg
Views:	3
Size:	35.9 KB
ID:	171

[Sensor/Module]
a. servo motor: RB-65PG with pan tilt component
Click image for larger version

Name:	016.jpg
Views:	3
Size:	25.8 KB
ID:	172

b. ultrasonic sensor: HC-SR04
Click image for larger version

Name:	015.jpg
Views:	3
Size:	31.0 KB
ID:	173

c. fan motor: Keyes 111330 fan motor

Click image for larger version

Name:	014.jpg
Views:	5
Size:	22.0 KB
ID:	170

[Connection]
a. PHPoC Blue - Servo Motor
Click image for larger version

Name:	blue_servo_bb.jpg
Views:	4
Size:	84.7 KB
ID:	174

b. PHPoC Blue - Ultrasonic Sensor
Click image for larger version

Name:	blue_hc_sr04_bb.jpg
Views:	3
Size:	87.9 KB
ID:	175

c. PHPoC Blue - Fan Motor
Click image for larger version

Name:	blue_fan_motor_bb.jpg
Views:	8
Size:	83.9 KB
ID:	176

[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(12);
            if(
$dir_flag != 0)
                break;
        }
        
sleep(1);
    }
    elseif(
$dir_flag == 2)
        
$width -= 50;
    else
        
$width += 50;

    
$distance detect();

    if((
$distance 3) &amp;&amp; ($distance 25))
        
$dir_flag 0;
}

?>