Announcement

Collapse
No announcement yet.

Servo Motor - Controlling Servo Motor from Webpage using Hypertext

Collapse
X
Collapse
  •  

  • Servo Motor - Controlling Servo Motor from Webpage using Hypertext


    Tutorial Video



    Hardware
    • PHPoC Blue (+ USB WLAN) or PHPoC Black (+ Ethernet cable)
    • Micro USB to USB Cable (to upload source code to PHPoC Device)
    • Servo Motor
    • Jumper Cable


    About Servo Motor

    Stardard servo motor is used to control of angular (usually between 0 and 180 degrees). The servo motor used in this example includes three wires:
    • VCC wire.
    • GND wire.
    • Signal wire (receives the PWM control signal from controller).
    How the servo motor works: servo motor can be controlled by generating PWM signal to signal wire of servo motor. The motor sepcification defines cycle time, minimum and maximum duty cycle.
    • If generating PWM signal with minimum duty cycle to signal wire, the servo motor rotate to 0 degree.
    • If generating PWM signal with maximun duty cycle to signal wire, the servo motor rotate to 180 degree.
    • If generating PWM signal with duty cycle between minimum and maximum value to signal wire, the servo motor rotate to position that is proportional to duty cycle.
    How to control servo motor:
    • Harware timer and software timer on PHPoC device can generate PWM signal. Therefore, It just needs connect the HT pin (or selectable IO pin if using software timer) of PHPoC device to the signal wire of servo motor. And then use hardware/software timer to control servo motor.


    Wiring Diagram



    Quick Steps

    Source code of this example is a part of PHPoC Support Packet (PSP). You need to:
    • Download PHPoC Support Package.
    • Upload example\p4s\02.html_text\03.ht_pwm_servo to PHPoC Blue/Black.
    • Configure network parameters (e.g. WiFi SSID, password, IP address ...).
    • Access webpage on PHPoC using Web Browser on your PC or smart phone (See How To).
    If you use PHPoC for the first time, see How To Use PSP.


    Source Code

    Source files includes:index.php file, which contains source code of web page. It is only run in response to request from Web Browser.

    index.php

    [Full Code]
    PHP Code:
    <?php

    include_once "/lib/sd_340.php";

    define("PWM_PERIOD"20000); // 20000us (20ms)
    define("WIDTH_MIN"600);
    define("WIDTH_MAX"2450);

    $flags 0;
    $angle 0;

    um_read(00$flags4); // read flags (offset 0, 32bit integer)
    um_read(04$angle4); // read angle (offset 4, 32bit integer)

    if(!$flags)
    {
     
    ht_pwm_setup(0, (WIDTH_MIN WIDTH_MAX) / 2PWM_PERIOD"us");

     
    $flags |= 0x00000001// set init flag
     
    um_write(00int2bin($flags4)); // write flags (offset 0, 32bit integer)

     
    $angle 90;
     
    um_write(04int2bin($angle4)); // write angle (offset 4, 32bit integer)
    }

    if((
    $cw _GET("cw")))
     
    $delta = -(int)$cw// clock wise
    else
    if((
    $ccw _GET("ccw")))
     
    $delta = (int)$ccw// counter clock wise
    else
     
    $delta 0;

    if(
    $delta)
    {
     
    um_read(04$angle4); // read angle (offset 4, 32bit integer)

     
    $angle += $delta;

     if(
    $angle 180)
      
    $angle 180;

     if(
    $angle 0)
      
    $angle 0;

     
    um_write(04int2bin($angle4)); // write angle (offset 4, 32bit integer)

     
    $width WIDTH_MIN + (int)round($angle 180.0 * (WIDTH_MAX WIDTH_MIN));

     
    ht_pwm_width(0$widthPWM_PERIOD);
    }

    ?>
    <html>
    <head>
    <title>PHPoC / <?echo system("uname -i")?></title>
    <meta name="viewport" content="width=device-width, initial-scale=0.7">
    <style> body { text-align: center; } </style>
    </head>
    <body>

    <h2>

    HT / Tower Pro SG92R Micro Servo<br>

    <br>

    <a href="index.php?cw=45">-45'</a>&nbsp;
    <a href="index.php?cw=15">-15'</a>
    &nbsp;
    <?php printf("CW&nbsp;&nbsp;%d'&nbsp;&nbsp;CCW"$angle); ?>
    &nbsp;
    <a href="index.php?ccw=15">+15'</a>&nbsp;
    <a href="index.php?ccw=45">+45'</a>&nbsp;

    </h2>

    </body>
    </html>



    [Explanation]

    Source code of index.php file is composed of HTML, CSS and PHPoC code.

    PHPoC code is interpreted on on PHPoC device. It includes two piece of code.

    The first piece of code does:
    • Read initial states (initialized or not) and current angle in flash memory
    • Setup and initialize timer to generate PWM if not initialized
    • Read direction (clockwise or counter-clockwise) and the increment from HTTP request
    • Calculate new angle (new_angle = current_angle + increment)
    • Update new angle value on flash memory
    • Calculate PWM duty cycle based on angle value
    • Generate PWM signal to control servo motor
      PHP Code:
      <?php

          
      include_once "/lib/sd_340.php";

          
      define("PWM_PERIOD"20000); // 20000us (20ms)
          
      define("WIDTH_MIN"600);
          
      define("WIDTH_MAX"2450);

          
      $flags 0;
          
      $angle 0;

          
      um_read(00$flags4); // read flags (offset 0, 32bit integer)
          
      um_read(04$angle4); // read angle (offset 4, 32bit integer)

          
      if(!$flags)
          {
           
      ht_pwm_setup(0, (WIDTH_MIN WIDTH_MAX) / 2PWM_PERIOD"us");

           
      $flags |= 0x00000001// set init flag
           
      um_write(00int2bin($flags4)); // write flags (offset 0, 32bit integer)

           
      $angle 90;
           
      um_write(04int2bin($angle4)); // write angle (offset 4, 32bit integer)
          
      }

          if((
      $cw _GET("cw")))
           
      $delta = -(int)$cw// clock wise
          
      else
          if((
      $ccw _GET("ccw")))
           
      $delta = (int)$ccw// counter clock wise
          
      else
           
      $delta 0;

          if(
      $delta)
          {
           
      um_read(04$angle4); // read angle (offset 4, 32bit integer)

           
      $angle += $delta;

           if(
      $angle 180)
            
      $angle 180;

           if(
      $angle 0)
            
      $angle 0;

           
      um_write(04int2bin($angle4)); // write angle (offset 4, 32bit integer)

           
      $width WIDTH_MIN + (int)round($angle 180.0 * (WIDTH_MAX WIDTH_MIN));

           
      ht_pwm_width(0$widthPWM_PERIOD);
          }

          
      ?>


    The second piece of code prints the rotated angle value to Webpage
    • PHP Code:
      <?php printf("CW&nbsp;&nbsp;%d'&nbsp;&nbsp;CCW"$angle); ?>


    PHPoC code may add/update the content of HTML, CSS or JavaScript code. Once PHPoC code is interpreted in PHPoC, the remaining code is client-side code and it is returned to Web Browser. Web Browser receives this code and interpret it to display the webpage.
    • HTML: describes the structure of Web pages
    • CSS: describes how HTML elements are to be displayed
    For detail explanation, see How to Control Devices via HTTP Request




    See Also

    Other Resources
    Last edited by support; 02-09-2018, 08:46 AM.
      Posting comments is disabled.

    Categories

    Collapse

    Latest Articles

    Collapse

    • Arduino - RS-485 Expansion Board
      by support
      PES-2607 is an easy-to-use RS422/RS485 Expansion Board for Arduino Uno and Mega, which allows Arduino to exchange data with serial device via RS422 or RS485.
      Especially, Arduino does NOT use UART pins to communicate with RS422/RS485 expansion board. Therefore, users can use Arduino UART pins for other purposes.
      Moreover, A single Arduino Uno/Mega can communicate with multiple RS422/RS485 expansion boards (up to 14) without using Arduino UART pins.

      Library and examples for...
      11-13-2018, 02:45 PM
    • Arduino - RS-422 Expansion Board
      by support
      PES-2607 is an easy-to-use RS422/RS485 Expansion Board for Arduino Uno and Mega, which allows Arduino to exchange data with serial device via RS422 or RS485.
      Especially, Arduino does NOT use UART pins to communicate with RS422/RS485 expansion board. Therefore, users can use Arduino UART pins for other purposes.
      Moreover, A single Arduino Uno/Mega can communicate with multiple RS422/RS485 expansion boards (up to 14) without using Arduino UART pins.

      Library and examples for...
      11-13-2018, 02:44 PM
    • Arduino - RS-232 Expansion Board
      by support
      PES-2606 is an easy-to-use RS-232 Expansion Board for Arduino Uno and Mega, which allows Arduino to exchange data with serial device via RS-232.
      Especially, Arduino does NOT use UART pins to communicate with RS-232 expansion board. Therefore, users can use Arduino UART pins for other purposes.
      Moreover, A single Arduino Uno/Mega can communicate with multiple RS-232 expansion boards (up to 14) without using Arduino UART pins.

      Library and example for the RS-232 expansion board...
      11-13-2018, 02:43 PM
    • Arduino - Stepper Motor Controller
      by support
      PES-2605 is an easy-to-use stepper motor controller for Arduino Uno and Mega, which uses micro-stepping method to precisely control stepper motor.
      Library and example for the stepper motor controller are part of of PhpocExpansion library for Arduino. The library reference is available here.

      This tutorial shows how to use the step motor controller with an example of PhpocExpansion library for Arduino.


      Hardware Required...
      11-13-2018, 02:41 PM
    • Arduino - DC Motor Controller
      by support
      PES-2604 is an easy-to-use DC motor controller for Arduino Uno and Mega.
      Library and example for the DC motor controller are part of of PhpocExpansion library for Arduino. The library reference is available here.

      This tutorial shows how to use the DC motor controller with an example of PhpocExpansion library for Arduino.


      Hardware Required...
      11-13-2018, 02:40 PM
    • Arduino - Digital Input Board
      by support
      PES-2602 is an easy-to-use 4-port Input Expansion Board for Arduino Uno and Mega, which allows Arduino to monitor state of DC electric device. In addition, it can monitor NPN, PNP and dry contact(relay).
      Library and example for the 4-port input expansion board are part of of PhpocExpansion library for Arduino. The library reference is available here.

      This tutorial shows how to use 4-port input expansion board with an example of PhpocExpansion library for Arduino.

      ...
      11-13-2018, 02:39 PM
    Working...
    X