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
  6. Bluetooth System
  7. IoT System

 

Note: this table of contents will be updated as soon as I publish more posts.

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

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.

electric diagram

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°.

servo

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.