PX4 Firmware
PX4 Autopilot Software http://px4.io
test_jig_voltages.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_jig_voltages.c
36  * Tests for jig voltages.
37  */
38 
39 #include <px4_platform_common/px4_config.h>
40 #include <px4_platform_common/defines.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 
50 #include "tests_main.h"
51 
52 #include <drivers/drv_adc.h>
53 #include <systemlib/err.h>
54 
55 int test_jig_voltages(int argc, char *argv[])
56 {
57  int fd = open(ADC0_DEVICE_PATH, O_RDONLY);
58  int ret = OK;
59 
60  if (fd < 0) {
61  PX4_ERR("can't open ADC device");
62  return 1;
63  }
64 
65  /* make space for the maximum channels */
67 
68  /* read all channels available */
69  ssize_t count = read(fd, data, sizeof(data));
70 
71  if (count < 0) {
72  close(fd);
73  PX4_ERR("can't read from ADC driver. Forgot 'adc start' command?");
74  return 1;
75  }
76 
77  unsigned channels = count / sizeof(data[0]);
78 
79  for (unsigned j = 0; j < channels; j++) {
80  printf("%d: %u ", data[j].am_channel, data[j].am_data);
81  }
82 
83  printf("\n");
84 
85  PX4_INFO("\t ADC operational.\n");
86 
87  /* Expected values */
88  int16_t expected_min[] = {2800, 2800, 1800, 800};
89  int16_t expected_max[] = {3100, 3100, 2100, 1100};
90  char *check_res[channels];
91 
92  if (channels < 4) {
93  close(fd);
94  PX4_ERR("not all four test channels available, aborting.");
95  return 1;
96 
97  } else {
98  /* Check values */
99  check_res[0] = (expected_min[0] < data[0].am_data && expected_max[0] > data[0].am_data) ? "OK" : "FAIL";
100  check_res[1] = (expected_min[1] < data[1].am_data && expected_max[1] > data[1].am_data) ? "OK" : "FAIL";
101  check_res[2] = (expected_min[2] < data[2].am_data && expected_max[2] > data[2].am_data) ? "OK" : "FAIL";
102  check_res[3] = (expected_min[3] < data[3].am_data && expected_max[3] > data[3].am_data) ? "OK" : "FAIL";
103 
104  /* Accumulate result */
105  ret += (expected_min[0] > data[0].am_data || expected_max[0] < data[0].am_data) ? 1 : 0;
106  ret += (expected_min[1] > data[1].am_data || expected_max[1] < data[1].am_data) ? 1 : 0;
107  ret += (expected_min[2] > data[2].am_data || expected_max[2] < data[2].am_data) ? 1 : 0;
108  ret += (expected_min[3] > data[3].am_data || expected_max[3] < data[3].am_data) ? 1 : 0;
109 
110  PX4_INFO("Sample:");
111  PX4_INFO("channel: %d value: %d (allowed min: %d, allowed max: %d), result: %s",
112  data[0].am_channel, (int)(data[0].am_data), expected_min[0], expected_max[0], check_res[0]);
113  PX4_INFO("channel: %d value: %d (allowed min: %d, allowed max: %d), result: %s",
114  data[1].am_channel, (int)(data[1].am_data), expected_min[1], expected_max[1], check_res[1]);
115  PX4_INFO("channel: %d value: %d (allowed min: %d, allowed max: %d), result: %s",
116  data[2].am_channel, (int)(data[2].am_data), expected_min[2], expected_max[2], check_res[2]);
117  PX4_INFO("channel: %d value: %d (allowed min: %d, allowed max: %d), result: %s",
118  data[3].am_channel, (int)(data[3].am_data), expected_min[3], expected_max[3], check_res[3]);
119 
120  if (ret != OK) {
121  PX4_ERR("\t JIG voltages test FAILED. Some channels where out of allowed range. Check supply voltages.");
122  goto errout_with_dev;
123  }
124  }
125 
126  PX4_INFO("JIG voltages test successful.");
127 
128 errout_with_dev:
129 
130  if (fd != 0) { close(fd); }
131 
132  return ret;
133 }
px4_adc_msg_t
Definition: drv_adc.h:56
#define PX4_MAX_ADC_CHANNELS
Definition: drv_adc.h:52
ADC driver interface.
#define ADC0_DEVICE_PATH
Definition: drv_adc.h:59
static void read(bootloader_app_shared_t *pshared)
int test_jig_voltages(int argc, char *argv[])
uint8_t * data
Definition: dataman.cpp:149
Simple error/warning functions, heavily inspired by the BSD functions of the same names...
Tests declaration file.
int fd
Definition: dataman.cpp:146
#define OK
Definition: uavcan_main.cpp:71