PX4 Firmware
PX4 Autopilot Software http://px4.io
parameters.cpp
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (c) 2016-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 parameters.cpp
36  *
37  * @author Beat Kueng <beat-kueng@gmx.net>
38  */
39 
40 #include "parameters.h"
41 
42 #include <px4_platform_common/log.h>
43 #include <px4_platform_common/px4_config.h>
44 #include <drivers/drv_adc.h>
45 
46 namespace battery_status
47 {
48 
50 {
51  parameter_handles.battery_voltage_scaling = param_find("BAT_CNT_V_VOLT");
52  parameter_handles.battery_current_scaling = param_find("BAT_CNT_V_CURR");
53  parameter_handles.battery_current_offset = param_find("BAT_V_OFFS_CURR");
54  parameter_handles.battery_v_div = param_find("BAT_V_DIV");
55  parameter_handles.battery_a_per_v = param_find("BAT_A_PER_V");
56  parameter_handles.battery_source = param_find("BAT_SOURCE");
57  parameter_handles.battery_adc_channel = param_find("BAT_ADC_CHANNEL");
58 }
59 
60 int update_parameters(const ParameterHandles &parameter_handles, Parameters &parameters)
61 {
62  int ret = PX4_OK;
63 
64  const char *paramerr = "FAIL PARM LOAD";
65 
66  /* scaling of ADC ticks to battery voltage */
67  if (param_get(parameter_handles.battery_voltage_scaling, &(parameters.battery_voltage_scaling)) != OK) {
68  PX4_WARN("%s", paramerr);
69 
70  } else if (parameters.battery_voltage_scaling < 0.0f) {
71  /* apply scaling according to defaults if set to default */
72  parameters.battery_voltage_scaling = (BOARD_ADC_POS_REF_V_FOR_VOLTAGE_CHAN / px4_arch_adc_dn_fullcount());
74  }
75 
76  /* scaling of ADC ticks to battery current */
77  if (param_get(parameter_handles.battery_current_scaling, &(parameters.battery_current_scaling)) != OK) {
78  PX4_WARN("%s", paramerr);
79 
80  } else if (parameters.battery_current_scaling < 0.0f) {
81  /* apply scaling according to defaults if set to default */
82  parameters.battery_current_scaling = (BOARD_ADC_POS_REF_V_FOR_CURRENT_CHAN / px4_arch_adc_dn_fullcount());
84  }
85 
86  if (param_get(parameter_handles.battery_current_offset, &(parameters.battery_current_offset)) != OK) {
87  PX4_WARN("%s", paramerr);
88 
89  }
90 
91  if (param_get(parameter_handles.battery_v_div, &(parameters.battery_v_div)) != OK) {
92  PX4_WARN("%s", paramerr);
93  parameters.battery_v_div = 0.0f;
94 
95  } else if (parameters.battery_v_div <= 0.0f) {
96  /* apply scaling according to defaults if set to default */
97 
98  parameters.battery_v_div = BOARD_BATTERY1_V_DIV;
99  param_set_no_notification(parameter_handles.battery_v_div, &parameters.battery_v_div);
100  }
101 
102  if (param_get(parameter_handles.battery_a_per_v, &(parameters.battery_a_per_v)) != OK) {
103  PX4_WARN("%s", paramerr);
104  parameters.battery_a_per_v = 0.0f;
105 
106  } else if (parameters.battery_a_per_v <= 0.0f) {
107  /* apply scaling according to defaults if set to default */
108 
109  parameters.battery_a_per_v = BOARD_BATTERY1_A_PER_V;
110  param_set_no_notification(parameter_handles.battery_a_per_v, &parameters.battery_a_per_v);
111  }
112 
113  param_get(parameter_handles.battery_source, &(parameters.battery_source));
114  param_get(parameter_handles.battery_adc_channel, &(parameters.battery_adc_channel));
115 
116  return ret;
117 }
118 
119 } /* namespace battery_status */
int param_get(param_t param, void *val)
Copy the value of a parameter.
Definition: parameters.cpp:589
uint32_t px4_arch_adc_dn_fullcount(void)
Get the adc digital number full count.
ADC driver interface.
param_t param_find(const char *name)
Look up a parameter by name.
Definition: parameters.cpp:370
void initialize_parameter_handles(ParameterHandles &parameter_handles)
initialize ParameterHandles struct
Definition: parameters.cpp:49
int update_parameters(const ParameterHandles &parameter_handles, Parameters &parameters)
Read out the parameters using the handles into the parameters struct.
Definition: parameters.cpp:60
int param_set_no_notification(param_t param, const void *val)
Set the value of a parameter, but do not notify the system about the change.
Definition: parameters.cpp:820
#define OK
Definition: uavcan_main.cpp:71