Controlling Pan Tilt by 3-Axis Accelerometer
[Board]
a. PHPoC Blue (P4S-342)
b. bread board extend board for PHPoC Blue
[Sensor/Module]
a. Servo Motors * 2: RB-65PG Pan Tilt Type (Up-Down, Left-Right)
b. 3 Axis Accelerometer: AM-3AXIS Ver.03
[Connection]
[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(0, 0, 0);
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;
}
}
}
?>