element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Members
    Members
    • Benefits of Membership
    • Achievement Levels
    • Members Area
    • Personal Blogs
    • Feedback and Support
    • What's New on element14
  • Learn
    Learn
    • Learning Center
    • eBooks
    • STEM Academy
    • Webinars, Training and Events
    • More
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • More
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • More
  • Products
    Products
    • Arduino
    • Dev Tools
    • Manufacturers
    • Raspberry Pi
    • RoadTests & Reviews
    • Avnet Boards Community
    • More
  • Store
    Store
    • Visit Your Store
    • Choose Another Store
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
Low Power IoT Design Challenge
  • Challenges & Projects
  • Design Challenges
  • Low Power IoT Design Challenge
  • More
  • Cancel
Low Power IoT Design Challenge
Blog Object Classifier and Counter #3 - Controlling a Servo
  • Blog
  • Forum
  • Documents
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Blog Post Actions
  • Subscribe by email
  • More
  • Cancel
  • Share
  • Subscribe by email
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: guillengap
  • Date Created: 22 Sep 2021 4:17 AM Date Created
  • Views 125 views
  • Likes 0 likes
  • Comments 0 comments
  • infineon
  • low power iot design challenge
  • psoc 6 pioneer kit
Related
Recommended

Object Classifier and Counter #3 - Controlling a Servo

guillengap
guillengap
22 Sep 2021

Table of Contents

  1. Project Introduction
  2. Getting Started
  3. Controlling a Servo
  4. Controlling a Robot Arm
  5. Sorting Objects with the 3D Magnetic Sensor 2Go TLE493D

 

 

**********************************************************************************************************************

Controlling a Servo

In this chapter I'm going to show how to program a servo. I clarify that I didn't find clear information about servos in the documentation of this board, I only found some information in the discussion forums but it corresponded to obsolete boards and it didn't work on my board. What I show you next is the result of my work, I had to start from the PWM signal and I spent a lot of time in this issue with the help of my digital oscilloscope. In the figure below I show you the electrical diagram used in this chapter.

In the figure below I show you the electrical connection of my servo, I have also added my oscilloscope to see the PWM signal in a test.

 

Servo Control Theory

 

Controlling a servo involves sending a modulated square-wave pulse which is known as pulse-Width-Modulation (PWM) or Pulse-Duration-Modulation (PDM). If signal voltage from peak to peak (amplitude) is taken care as per the datasheet (which is generally 3V to 5V), then there two other main factors to be considered while sending a PWM signal to servo; “Frequency” and “Duty cycle”.

 

Frequency

 

Servo expects continuous pulses to move or hold its position. Frequency (or repetition time, or cycle time) is the number of times a positive pulse is fed to servo in a unit time (which is generally measured in seconds); for analog servos, frequency is generally 50 times a second (50Hz) and for digital servos it is 300 times a second (300Hz).

 

Duty Cycle

 

“Duty cycle” is the width of positive pulse and a deciding factor for servo’s angular position. For example, if you have a servo with 180° turn, then 90° is the center position of the servo with 0° being minimum, and 180°, being the maximum. Now, if a positive pulse of 1.5ms is sent, then the servo stays at 90° as long as it receives the same pulse. If another pulse of 1ms is sent, the circuit tries to move the shaft to 0°, and a pulse of 2ms tries to move the output shaft to 180°.

Different servo models have different minimum and maximum pulse requirements, so I had to do the calibration of my servos. In the sketch below I show you the coding to turn my servo from 0 to 180 degrees, and then from 180 to 0 degrees; this happens every time I press the user button on my board. Below I show you the code uploaded to the PSoC 62S2 WiFi BT Pioneer board: controlling_servo.c

 

// AUTHOR: GUILLERMO PEREZ GUILLEN


#include "cy_pdl.h"
#include "cy_retarget_io.h"
#include "cyhal.h"
#include "cybsp.h"


/******************************************************************************
 * Macros
 *****************************************************************************/
#define DELAY_SHORT_MS          (250)   /* milliseconds */
#define DELAY_LONG_MS           (500)   /* milliseconds */
#define LED_BLINK_COUNT         (4)
#define GPIO_INTERRUPT_PRIORITY (7u)


/* PWM Frequency */
#define PWM_FREQUENCY (50u)


/* PWM Duty-cycle */
#define PWM_DUTY_CYCLE (12.5f)


/*******************************************************************************
* Function Prototypes
********************************************************************************/
static void gpio_interrupt_handler(void *handler_arg, cyhal_gpio_event_t event);


/*******************************************************************************
* Global Variables
********************************************************************************/
volatile bool gpio_intr_flag = false;


/*******************************************************************************
* Function Name: main
*******************************************************************************/
int main(void)
{
    cy_rslt_t result;
    uint32_t count = 0;
    uint32_t delay_led_blink = DELAY_LONG_MS;


    /* PWM object */
    cyhal_pwm_t pwm_led_control;


    /* Initialize the device and board peripherals */
    result = cybsp_init();
    
    /* Board init failed. Stop program execution */
    if (result != CY_RSLT_SUCCESS)
    {
        CY_ASSERT(0);
    }


    /* Initialize retarget-io to use the debug UART port */
    result = cy_retarget_io_init(CYBSP_DEBUG_UART_TX, CYBSP_DEBUG_UART_RX,
                                 CY_RETARGET_IO_BAUDRATE);


    /* Initialize the user LED */
    result = cyhal_gpio_init(CYBSP_USER_LED, CYHAL_GPIO_DIR_OUTPUT,
                    CYHAL_GPIO_DRIVE_STRONG, CYBSP_LED_STATE_OFF);


    /* Initialize the user button */
    result = cyhal_gpio_init(CYBSP_USER_BTN, CYHAL_GPIO_DIR_INPUT,
                    CYHAL_GPIO_DRIVE_PULLUP, CYBSP_BTN_OFF);


    /* Initialize the PWM */
    result = cyhal_pwm_init(&pwm_led_control, P7_5, NULL);


    /* Configure GPIO interrupt */
    cyhal_gpio_register_callback(CYBSP_USER_BTN,
                                 gpio_interrupt_handler, NULL);
    cyhal_gpio_enable_event(CYBSP_USER_BTN, CYHAL_GPIO_IRQ_FALL,
                                 GPIO_INTERRUPT_PRIORITY, true);


    /* Enable global interrupts */
    __enable_irq();


    /* Set the PWM output frequency and duty cycle */
    result = cyhal_pwm_set_duty_cycle(&pwm_led_control, PWM_DUTY_CYCLE, PWM_FREQUENCY);


    /* Start the PWM */
    result = cyhal_pwm_start(&pwm_led_control);
    result = cyhal_pwm_stop(&pwm_led_control);
    cyhal_system_delay_ms(10);


    /* \x1b[2J\x1b[;H - ANSI ESC sequence for clear screen */
    printf("\x1b[2J\x1b[;H");
    printf("**************** PSoC 6 MCU: GPIO Interrupt *****************\r\n");


    for (;;)
    {
        /* Check the interrupt status */
        if (true == gpio_intr_flag)
        {
            gpio_intr_flag = false;


            /* Update LED toggle delay */
            if (DELAY_LONG_MS == delay_led_blink)
            {
                delay_led_blink = DELAY_SHORT_MS;
            }
            else
            {
                delay_led_blink = DELAY_LONG_MS;
            }
        }


        /* Blink LED four times */
        for (count = 0; count < LED_BLINK_COUNT; count++)
        {
            cyhal_gpio_write(CYBSP_USER_LED, CYBSP_LED_STATE_ON);
            cyhal_system_delay_ms(delay_led_blink);
            cyhal_gpio_write(CYBSP_USER_LED, CYBSP_LED_STATE_OFF);
            cyhal_system_delay_ms(delay_led_blink);
        }
        cyhal_system_delay_ms(10);


        for (int i = 180; i >= 0; i--){
        float PWM_DUTY_CYCLE_3 = ((i*9.5)/180)+3; // 7.5
        result = cyhal_pwm_set_duty_cycle(&pwm_led_control, PWM_DUTY_CYCLE_3, PWM_FREQUENCY);
        result = cyhal_pwm_start(&pwm_led_control);
            cyhal_system_delay_ms(10);
        }


    result = cyhal_pwm_stop(&pwm_led_control);
        cyhal_system_delay_ms(10);


        for (int i = 0; i <= 180; i++){
        float PWM_DUTY_CYCLE_3 = ((i*9.5)/180)+3;
        result = cyhal_pwm_set_duty_cycle(&pwm_led_control, PWM_DUTY_CYCLE_3, PWM_FREQUENCY);
        result = cyhal_pwm_start(&pwm_led_control);
            cyhal_system_delay_ms(10);
        }


    result = cyhal_pwm_stop(&pwm_led_control);
        cyhal_system_delay_ms(10);


        /* Enter deep sleep mode */
        cyhal_system_deepsleep();
    }
}


/*******************************************************************************
* Function Name: gpio_interrupt_handler
*******************************************************************************/


static void gpio_interrupt_handler(void *handler_arg, cyhal_gpio_irq_event_t event)
{
    gpio_intr_flag = true;
}

 

In the video below I show you a test with two servos.

 

You don't have permission to edit metadata of this video.
Edit media
x
Upload Preview

 

Conclusion:

  • Here I spent a lot of time reading the documentation and discovered that there was no updated information on servos with "Modus Toolbox" and my programming board. However, this doesn´t discourage me and I used my own knowledge and skills to do this work that can be useful to any user.
  • I attach the code used in my Github repository: object-classifier-and-counter
Anonymous
Element14

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2022 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • Facebook
  • Twitter
  • linkedin
  • YouTube