Controlling Pan Tilt by 3-Axis Accelerometer

Controlling Pan Tilt by 3-Axis Accelerometer

Postby Roy » Tue Dec 08, 2015 2:09 pm

[Subject]
Controlling Pan Tilt by 3-Axis Accelerometer
project.jpg
project.jpg (133.01 KiB) Viewed 2587 times


[Board]
a. PHPoC Blue (P4S-342)
b. bread board extend board for PHPoC Blue
breadboard_ex_board.jpg
breadboard_ex_board.jpg (118.47 KiB) Viewed 2587 times


[Sensor/Module]
a. Servo Motors * 2: RB-65PG Pan Tilt Type (Up-Down, Left-Right)
servo.jpg
servo.jpg (121.18 KiB) Viewed 2587 times


b. 3 Axis Accelerometer: AM-3AXIS Ver.03
accelerometer.jpg
accelerometer.jpg (151.24 KiB) Viewed 2587 times


[Connection]
3axis_2servo_bb.png
3axis_2servo_bb.png (223.27 KiB) Viewed 2593 times


[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]
phpBB [video]


[Source Code - task0.php]
Code: Select all
<?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;
        }
    }
}
?>
Roy
 
Posts: 8
Joined: Fri Mar 14, 2014 10:49 am

Re: Controlling Pan Tilt by 3-Axis Accelerometer

Postby ibrahimalnasser » Mon Mar 27, 2017 6:29 am

Hello sir, thank you for this effort.
could you explain you this .
$value = ((adc_in(0) / 4095.0 * 3.3) - 1.65) / 0.66;
ibrahimalnasser
 
Posts: 4
Joined: Mon Mar 13, 2017 9:23 am

Re: Controlling Pan Tilt by 3-Axis Accelerometer

Postby admin » Mon Mar 27, 2017 5:36 pm

ibrahimalnasser wrote:Hello sir, thank you for this effort.
could you explain you this .
$value = ((adc_in(0) / 4095.0 * 3.3) - 1.65) / 0.66;


I am afraid that there is no English datasheet of the accelerometer.
I'll explain the formula one-by-one.

A. (adc_in(0) / 4095.0 * 3.3V)
The adc_in(0) is for getting ADC value of PHPoC.
It returns a value between 0 and 4095.
And the reference voltage for ADC was 3.3V.
In conclusion, this part gets the voltage output from the accelerometer. (0 ~ 3.3V)

B. A - 1.65
According to the manual of accelerometer, it defines 0g to 1.65V at 3.3V.
I gave an offset for this definition.
The "g" (or "G") means a unit of acceleration of gravity.

C. B / 0.66
According to the manual of accelerometer, it defines the two sensitivity modes.
One is 2G range mode (-2G ~ +2G) and it is the default value. (what I used)
The other one is 6G range mode and it is enabled when the full scale pin is HIGH.
I used 2G range mode and the unit is defined to 660mV/g on the manual.
That is why I divided the (2) to 0.66.

In conclusion, the formula is for getting a value between -2(g) and +2(g) from the accelerometer.
admin
Site Admin
 
Posts: 153
Joined: Mon Jan 20, 2014 4:40 pm

Re: Controlling Pan Tilt by 3-Axis Accelerometer

Postby ibrahimalnasser » Wed Mar 29, 2017 7:10 am

thank you very much sir, now it's clear to me ;)
ibrahimalnasser
 
Posts: 4
Joined: Mon Mar 13, 2017 9:23 am


Return to Project

Who is online

Users browsing this forum: No registered users and 1 guest

cron