[Subject]
Controlling Pan Tilt by 3-Axis Accelerometer
Click image for larger version

Name:	project.jpg
Views:	13
Size:	115.7 KB
ID:	194

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

Click image for larger version

Name:	breadboard_ex_board.jpg
Views:	9
Size:	102.4 KB
ID:	195

[Sensor/Module]
a. Servo Motors * 2: RB-65PG Pan Tilt Type (Up-Down, Left-Right)
Click image for larger version

Name:	servo.jpg
Views:	9
Size:	105.0 KB
ID:	196

b. 3 Axis Accelerometer: AM-3AXIS Ver.03
Click image for larger version

Name:	accelerometer.jpg
Views:	9
Size:	128.0 KB
ID:	197

[Connection]
Click image for larger version

Name:	3axis_2servo_bb.png
Views:	14
Size:	280.5 KB
ID:	198

[Description]
1. Get acceleration values of each of 3 axis(X, Y and Z) via an accelerometer
2. Control an angle of servo motors according to extent of gradient of an accelerometer due to gravity
3. Control servo #1 by gradient of X-Z axis
4. Control servo #2 by gradient of Y-Z axis

[video]



[Source Code - task0.php]
PHP Code:
<?php

include "/lib/sd_340.php";

$g_arr_value = array(000);

function 
init_ht($dev)
{
    
$pid_ht pid_open("/mmap/ht$dev");
    
pid_ioctl($pid_ht"set div us");
    
pid_ioctl($pid_ht"set mode output pwm");
    
pid_ioctl($pid_ht"set count 1500 20000");
    return 
$pid_ht;
}

// init and start servo motors
$pid_ht0 init_ht(0);
$pid_ht1 init_ht(1);
pid_ioctl($pid_ht0"start");
pid_ioctl($pid_ht1"start");

// main loop
while(1)
{
    for(
$i 0$i 3$i++)
    {
        
// get adc input values
        
adc_setup(0$i);
        
$value = ((adc_in(0) / 4095.0 3.3) - 1.65) / 0.66;
        if(
$value 0)
        {
            if((
$value 0.1) || ($value 1))
                
$value floor($value);
        }
        elseif(
$value 0)
        {
            if((
$value > -0.1) || ($value < -1))
                
$value ceil($value);
        }

        
$g_arr_value[$i] = $value;

        
// calculate offset
        
$offset round(($g_arr_value[$i] * 800));
        
$width 1500 $offset;

        
// control servo motors
        
switch($i)
        {
        case 
0:
            
pid_ioctl($pid_ht0"set count $width 20000");
            continue;
        case 
1:
            
pid_ioctl($pid_ht1"set count $width 20000");
            continue;
        }
    }
}
?>
Attached Files