PX4 Firmware
PX4 Autopilot Software http://px4.io
test_servo.c
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (C) 2012-2019 PX4 Development Team. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in
13  * the documentation and/or other materials provided with the
14  * distribution.
15  * 3. Neither the name PX4 nor the names of its contributors may be
16  * used to endorse or promote products derived from this software
17  * without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
26  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  *
32  ****************************************************************************/
33 
34 /**
35  * @file test_servo.c
36  * Tests the servo outputs
37  */
38 
39 #include <px4_platform_common/time.h>
40 #include <px4_platform_common/px4_config.h>
41 
42 #include <sys/types.h>
43 
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <unistd.h>
47 #include <fcntl.h>
48 #include <errno.h>
49 #include <time.h>
50 
51 #include <arch/board/board.h>
52 #include <drivers/drv_pwm_output.h>
53 #include <systemlib/err.h>
54 
55 #include "tests_main.h"
56 
57 int test_servo(int argc, char *argv[])
58 {
59  int fd;
60  int result = 0;
62  servo_position_t pos;
63 
64  fd = open(PWM_OUTPUT0_DEVICE_PATH, O_RDWR);
65 
66  if (fd < 0) {
67  printf("failed opening /dev/pwm_servo\n");
68  goto out;
69  }
70 
71  result = read(fd, &data, sizeof(data));
72 
73  if (result != sizeof(data)) {
74  printf("failed bulk-reading channel values\n");
75  goto out;
76  }
77 
78  printf("Servo readback, pairs of values should match defaults\n");
79 
80  unsigned servo_count;
81  result = ioctl(fd, PWM_SERVO_GET_COUNT, (unsigned long)&servo_count);
82 
83  if (result != OK) {
84  warnx("PWM_SERVO_GET_COUNT");
85  goto out;
86  }
87 
88  for (unsigned i = 0; i < servo_count; i++) {
89  result = ioctl(fd, PWM_SERVO_GET(i), (unsigned long)&pos);
90 
91  if (result < 0) {
92  printf("failed reading channel %u\n", i);
93  goto out;
94  }
95 
96  printf("%u: %u %u\n", i, pos, data[i]);
97 
98  }
99 
100  /* tell safety that its ok to disable it with the switch */
101  result = ioctl(fd, PWM_SERVO_SET_ARM_OK, 0);
102 
103  if (result != OK) {
104  warnx("FAIL: PWM_SERVO_SET_ARM_OK");
105  goto out;
106  }
107 
108  /* tell output device that the system is armed (it will output values if safety is off) */
109  result = ioctl(fd, PWM_SERVO_ARM, 0);
110 
111  if (result != OK) {
112  warnx("FAIL: PWM_SERVO_ARM");
113  goto out;
114  }
115 
116  px4_usleep(5000000);
117  printf("Advancing channel 0 to 1500\n");
118  result = ioctl(fd, PWM_SERVO_SET(0), 1500);
119  printf("Advancing channel 1 to 1800\n");
120  result = ioctl(fd, PWM_SERVO_SET(1), 1800);
121 out:
122 
123  if (fd >= 0) {
124  close(fd);
125  }
126 
127  return result;
128 }
#define PWM_SERVO_SET_ARM_OK
set the &#39;ARM ok&#39; bit, which activates the safety switch
uint16_t servo_position_t
Servo output signal type, value is actual servo output pulse width in microseconds.
#define PWM_SERVO_GET_COUNT
get the number of servos in *(unsigned *)arg
#define PWM_SERVO_ARM
arm all servo outputs handle by this driver
#define PWM_SERVO_SET(_servo)
set a single servo to a specific value
static void read(bootloader_app_shared_t *pshared)
#define PWM_OUTPUT0_DEVICE_PATH
uint8_t * data
Definition: dataman.cpp:149
#define PWM_OUTPUT_MAX_CHANNELS
#define warnx(...)
Definition: err.h:95
Simple error/warning functions, heavily inspired by the BSD functions of the same names...
Tests declaration file.
#define PWM_SERVO_GET(_servo)
get a single specific servo value
int fd
Definition: dataman.cpp:146
int test_servo(int argc, char *argv[])
Definition: test_servo.c:57
#define OK
Definition: uavcan_main.cpp:71