servo2 2.1.0.0
Loading...
Searching...
No Matches
servo2


Servo 2 Click

Servo 2 Click demo application is developed using the NECTO Studio, ensuring compatibility with mikroSDK's open-source libraries and tools. Designed for plug-and-play implementation and testing, the demo is fully compatible with all development, starter, and mikromedia boards featuring a mikroBUS™ socket.


Click Library

  • Author : Stefan Filipovic
  • Date : Dec 2024.
  • Type : PWM type

Software Support

Example Description

This example demonstrates the use of the Servo 2 Click board for controlling the angle of servo motors. The board is capable of driving multiple servos, and the example illustrates how to change the angle of all connected servos simultaneously within a defined range.

Example Libraries

  • MikroSDK.Board
  • MikroSDK.Log
  • Click.Servo2

Example Key Functions

  • servo2_cfg_setup Config Object Initialization function.
    void servo2_cfg_setup(servo2_cfg_t *cfg)
    Servo 2 configuration object setup function.
    Servo 2 Click configuration object.
    Definition servo2.h:174
  • servo2_init Initialization function.
    err_t servo2_init ( servo2_t *ctx, servo2_cfg_t *cfg );
    err_t servo2_init(servo2_t *ctx, servo2_cfg_t *cfg)
    Servo 2 initialization function.
    Servo 2 Click context object.
    Definition servo2.h:148
  • servo2_set_angle This function sets the servo angle for a specific channel or all channels.
    err_t servo2_set_angle ( servo2_t *ctx, uint8_t ch_num, uint8_t angle );
    err_t servo2_set_angle(servo2_t *ctx, uint8_t ch_num, uint8_t angle)
    Servo 2 set angle function.
  • servo2_update_output This function updates the PWM output values for all channels by writing them to the device.
    void servo2_update_output(servo2_t *ctx)
    Servo 2 update output function.
  • servo2_set_channel_pwm This function sets the PWM output for a specific channel or all channels.
    err_t servo2_set_channel_pwm ( servo2_t *ctx, uint8_t ch_num, uint16_t pwm_out );
    err_t servo2_set_channel_pwm(servo2_t *ctx, uint8_t ch_num, uint16_t pwm_out)
    Servo 2 set channel PWM function.

Application Init

Initializes the logger module and configures the Servo 2 Click board. The PWM communication is established, and the device is prepared for controlling the servos.

void application_init ( void )
{
log_cfg_t log_cfg;
servo2_cfg_t servo2_cfg;
LOG_MAP_USB_UART( log_cfg );
log_init( &logger, &log_cfg );
log_info( &logger, " Application Init " );
// Click initialization.
servo2_cfg_setup( &servo2_cfg );
SERVO2_MAP_MIKROBUS( servo2_cfg, MIKROBUS_1 );
if ( PWM_ERROR == servo2_init( &servo2, &servo2_cfg ) )
{
log_error( &logger, " Communication init." );
for ( ; ; );
}
log_info( &logger, " Application Task " );
}
#define SERVO2_MAP_MIKROBUS(cfg, mikrobus)
MikroBUS pin mapping.
Definition servo2.h:131
void application_init(void)
Definition main.c:35

Application Task

Gradually changes the angle of all connected servo motors from a minimum to a maximum value, and then back to the minimum, creating a sweeping motion. The current angle is logged during each update.

void application_task ( void )
{
static uint8_t angle = SERVO2_ANGLE_MIN;
static int8_t step = 1;
log_printf( &logger, "All channels angle: %u\r\n\n", angle );
servo2_set_angle ( &servo2, SERVO2_CHANNEL_ALL, angle );
servo2_update_output ( &servo2 );
angle += step;
if ( angle > SERVO2_ANGLE_MAX )
{
step = -step;
angle += step;
}
else if ( angle < SERVO2_ANGLE_MIN )
{
step = -step;
angle += step;
}
}
#define SERVO2_ANGLE_MIN
Servo 2 angle and channel setting.
Definition servo2.h:87
#define SERVO2_CHANNEL_ALL
Definition servo2.h:105
#define SERVO2_ANGLE_MAX
Definition servo2.h:88
void application_task(void)
Definition main.c:65

Note

Ensure that the servo motors are properly connected to the Servo 2 Click board and are compatible with the specified angle range such as the SG90 Micro Servo motors.

Application Output

This Click board can be interfaced and monitored in two ways:

  • Application Output - Use the "Application Output" window in Debug mode for real-time data monitoring. Set it up properly by following this tutorial.
  • UART Terminal - Monitor data via the UART Terminal using a USB to UART converter. For detailed instructions, check out this tutorial.

Additional Notes and Information

The complete application code and a ready-to-use project are available through the NECTO Studio Package Manager for direct installation in the NECTO Studio. The application code can also be found on the MIKROE GitHub account.