2015-05-28 22:34:28 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2015 Eistec AB
|
|
|
|
*
|
|
|
|
* This file is subject to the terms and conditions of the GNU Lesser General
|
|
|
|
* Public License v2.1. See the file LICENSE in the top level directory for more
|
|
|
|
* details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @ingroup tests
|
|
|
|
* @{
|
|
|
|
*
|
|
|
|
* @file
|
|
|
|
* @brief Test for servo driver
|
|
|
|
*
|
|
|
|
* This test initializes the given servo device and moves it between
|
|
|
|
* 1.000 -- 2.000 ms, roughly -/+ 90 degrees from the middle position if the
|
|
|
|
* connected servo is a standard RC servo.
|
|
|
|
*
|
2015-09-20 13:47:39 +02:00
|
|
|
* @author Joakim Nohlgård <joakim.nohlgard@eistec.se>
|
2015-05-28 22:34:28 +02:00
|
|
|
*
|
|
|
|
* @}
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include "cpu.h"
|
|
|
|
#include "board.h"
|
2015-09-03 21:36:13 +02:00
|
|
|
#include "xtimer.h"
|
2015-05-28 22:34:28 +02:00
|
|
|
#include "periph/pwm.h"
|
|
|
|
#include "servo.h"
|
|
|
|
|
2016-12-15 12:18:16 +01:00
|
|
|
#define DEV PWM_DEV(0)
|
2015-05-28 22:34:28 +02:00
|
|
|
#define CHANNEL 0
|
|
|
|
|
|
|
|
#define SERVO_MIN (1000U)
|
|
|
|
#define SERVO_MAX (2000U)
|
|
|
|
|
|
|
|
/* these are defined outside the limits of the servo_init min/max parameters above */
|
|
|
|
/* we will test the clamping functionality of the servo_set function. */
|
|
|
|
#define STEP_LOWER_BOUND (900U)
|
|
|
|
#define STEP_UPPER_BOUND (2100U)
|
|
|
|
|
|
|
|
/* Step size that we move per WAIT us */
|
|
|
|
#define STEP (10U)
|
|
|
|
|
|
|
|
/* Sleep time between updates, no need to update the servo position more than
|
|
|
|
* once per cycle */
|
|
|
|
#define WAIT (10000U)
|
|
|
|
|
|
|
|
static servo_t servo;
|
|
|
|
|
|
|
|
int main(void)
|
|
|
|
{
|
|
|
|
int res;
|
2017-10-26 01:36:31 +02:00
|
|
|
unsigned int pos = (STEP_LOWER_BOUND + STEP_UPPER_BOUND) / 2;
|
2015-05-28 22:34:28 +02:00
|
|
|
int step = STEP;
|
|
|
|
|
|
|
|
puts("\nRIOT RC servo test");
|
|
|
|
puts("Connect an RC servo or scope to PWM_0 channel 0 to see anything");
|
|
|
|
|
|
|
|
res = servo_init(&servo, DEV, CHANNEL, SERVO_MIN, SERVO_MAX);
|
|
|
|
if (res < 0) {
|
|
|
|
puts("Errors while initializing servo");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
puts("Servo initialized.");
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
servo_set(&servo, pos);
|
|
|
|
|
|
|
|
pos += step;
|
|
|
|
if (pos <= STEP_LOWER_BOUND || pos >= STEP_UPPER_BOUND) {
|
|
|
|
step = -step;
|
|
|
|
}
|
|
|
|
|
2015-09-03 21:36:13 +02:00
|
|
|
xtimer_usleep(WAIT);
|
2015-05-28 22:34:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|