2012-02-11 07:51:35 -04:00
|
|
|
// -*- tab-width: 4; Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*-
|
2013-08-29 02:34:34 -03:00
|
|
|
/*
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
2012-02-11 07:51:35 -04:00
|
|
|
|
2013-08-29 02:34:34 -03:00
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
//
|
2012-02-11 07:51:35 -04:00
|
|
|
/// @file AP_Param.h
|
|
|
|
/// @brief A system for managing and storing variables that are of
|
|
|
|
/// general interest to the system.
|
2015-12-23 13:25:02 -04:00
|
|
|
#pragma once
|
2012-02-11 07:51:35 -04:00
|
|
|
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdint.h>
|
2015-05-03 01:47:40 -03:00
|
|
|
#include <math.h>
|
2012-02-11 07:51:35 -04:00
|
|
|
|
2015-12-23 13:25:02 -04:00
|
|
|
#include <AP_HAL/AP_HAL.h>
|
2015-08-11 03:28:45 -03:00
|
|
|
#include <StorageManager/StorageManager.h>
|
2012-02-11 07:51:35 -04:00
|
|
|
|
2015-12-23 13:25:02 -04:00
|
|
|
#include "float.h"
|
|
|
|
|
2012-11-20 06:35:22 -04:00
|
|
|
#define AP_MAX_NAME_SIZE 16
|
2015-12-27 16:22:32 -04:00
|
|
|
|
|
|
|
/*
|
|
|
|
flags for variables in var_info and group tables
|
|
|
|
*/
|
2015-12-29 07:43:25 -04:00
|
|
|
|
|
|
|
// a nested offset is for subgroups that are not subclasses
|
2015-12-27 16:22:32 -04:00
|
|
|
#define AP_PARAM_FLAG_NESTED_OFFSET 1
|
2015-12-29 07:43:25 -04:00
|
|
|
|
|
|
|
// a pointer variable is for dynamically allocated objects
|
2015-12-29 06:21:54 -04:00
|
|
|
#define AP_PARAM_FLAG_POINTER 2
|
2012-02-11 07:51:35 -04:00
|
|
|
|
2015-12-29 07:43:25 -04:00
|
|
|
// an enable variable allows a whole subtree of variables to be made
|
|
|
|
// invisible
|
|
|
|
#define AP_PARAM_FLAG_ENABLE 4
|
|
|
|
|
2012-03-11 17:30:09 -03:00
|
|
|
// a variant of offsetof() to work around C++ restrictions.
|
2012-02-11 07:51:35 -04:00
|
|
|
// this can only be used when the offset of a variable in a object
|
|
|
|
// is constant and known at compile time
|
2015-12-29 06:21:54 -04:00
|
|
|
#define AP_VAROFFSET(type, element) (((ptrdiff_t)(&((const type *)1)->element))-1)
|
2012-02-12 03:22:57 -04:00
|
|
|
|
|
|
|
// find the type of a variable given the class and element
|
2015-06-04 16:08:31 -03:00
|
|
|
#define AP_CLASSTYPE(class, element) ((uint8_t)(((const class *) 1)->element.vtype))
|
2012-02-12 03:22:57 -04:00
|
|
|
|
|
|
|
// declare a group var_info line
|
2015-12-29 07:43:25 -04:00
|
|
|
#define AP_GROUPINFO_FLAGS(name, idx, class, element, def, flags) { AP_CLASSTYPE(class, element), idx, name, AP_VAROFFSET(class, element), {def_value : def}, flags }
|
|
|
|
|
|
|
|
// declare a group var_info line
|
|
|
|
#define AP_GROUPINFO(name, idx, class, element, def) AP_GROUPINFO_FLAGS(name, idx, class, element, def, 0)
|
2012-02-12 16:43:09 -04:00
|
|
|
|
2012-02-12 03:22:57 -04:00
|
|
|
// declare a nested group entry in a group var_info
|
2015-12-27 16:22:32 -04:00
|
|
|
#define AP_NESTEDGROUPINFO(class, idx) { AP_PARAM_GROUP, idx, "", 0, { group_info : class::var_info }, 0 }
|
|
|
|
|
|
|
|
// declare a subgroup entry in a group var_info. This is for having another arbitrary object as a member of the parameter list of
|
|
|
|
// an object
|
|
|
|
#define AP_SUBGROUPINFO(element, name, idx, thisclass, elclass) { AP_PARAM_GROUP, idx, name, AP_VAROFFSET(thisclass, element), { group_info : elclass::var_info }, AP_PARAM_FLAG_NESTED_OFFSET }
|
2012-02-12 03:22:57 -04:00
|
|
|
|
2015-12-29 06:21:54 -04:00
|
|
|
// declare a pointer subgroup entry in a group var_info
|
|
|
|
#define AP_SUBGROUPPTR(element, name, idx, thisclass, elclass) { AP_PARAM_GROUP, idx, name, AP_VAROFFSET(thisclass, element), { group_info : elclass::var_info }, AP_PARAM_FLAG_POINTER }
|
|
|
|
|
2012-08-17 03:17:46 -03:00
|
|
|
#define AP_GROUPEND { AP_PARAM_NONE, 0xFF, "", 0, { group_info : NULL } }
|
|
|
|
#define AP_VAREND { AP_PARAM_NONE, "", 0, NULL, { group_info : NULL } }
|
2012-02-11 07:51:35 -04:00
|
|
|
|
|
|
|
enum ap_var_type {
|
|
|
|
AP_PARAM_NONE = 0,
|
|
|
|
AP_PARAM_INT8,
|
|
|
|
AP_PARAM_INT16,
|
|
|
|
AP_PARAM_INT32,
|
|
|
|
AP_PARAM_FLOAT,
|
|
|
|
AP_PARAM_VECTOR3F,
|
|
|
|
AP_PARAM_GROUP
|
|
|
|
};
|
|
|
|
|
2015-12-27 16:22:32 -04:00
|
|
|
|
2012-02-11 07:51:35 -04:00
|
|
|
/// Base class for variables.
|
|
|
|
///
|
|
|
|
/// Provides naming and lookup services for variables.
|
|
|
|
///
|
|
|
|
class AP_Param
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
// the Info and GroupInfo structures are passed by the main
|
|
|
|
// program in setup() to give information on how variables are
|
|
|
|
// named and their location in memory
|
|
|
|
struct GroupInfo {
|
|
|
|
uint8_t type; // AP_PARAM_*
|
2012-02-12 18:54:46 -04:00
|
|
|
uint8_t idx; // identifier within the group
|
2012-11-20 06:35:22 -04:00
|
|
|
const char name[AP_MAX_NAME_SIZE+1];
|
2015-12-29 06:21:54 -04:00
|
|
|
ptrdiff_t offset; // offset within the object
|
2012-08-06 22:00:43 -03:00
|
|
|
union {
|
|
|
|
const struct GroupInfo *group_info;
|
|
|
|
const float def_value;
|
|
|
|
};
|
2015-12-27 16:22:32 -04:00
|
|
|
uint8_t flags;
|
2012-02-11 07:51:35 -04:00
|
|
|
};
|
|
|
|
struct Info {
|
|
|
|
uint8_t type; // AP_PARAM_*
|
2012-11-20 06:35:22 -04:00
|
|
|
const char name[AP_MAX_NAME_SIZE+1];
|
2015-12-30 17:27:22 -04:00
|
|
|
uint16_t key; // k_param_*
|
2015-05-13 07:04:19 -03:00
|
|
|
const void *ptr; // pointer to the variable in memory
|
2012-08-06 22:00:43 -03:00
|
|
|
union {
|
|
|
|
const struct GroupInfo *group_info;
|
|
|
|
const float def_value;
|
|
|
|
};
|
2012-02-11 07:51:35 -04:00
|
|
|
};
|
2013-05-30 08:45:25 -03:00
|
|
|
struct ConversionInfo {
|
2015-12-30 17:27:22 -04:00
|
|
|
uint16_t old_key; // k_param_*
|
2013-05-30 08:45:25 -03:00
|
|
|
uint8_t old_group_element; // index in old object
|
|
|
|
enum ap_var_type type; // AP_PARAM_*
|
|
|
|
const char new_name[AP_MAX_NAME_SIZE+1];
|
|
|
|
};
|
2012-02-11 07:51:35 -04:00
|
|
|
|
|
|
|
// called once at startup to setup the _var_info[] table. This
|
|
|
|
// will also check the EEPROM header and re-initialise it if the
|
|
|
|
// wrong version is found
|
2012-12-13 18:58:23 -04:00
|
|
|
static bool setup();
|
|
|
|
|
|
|
|
// constructor with var_info
|
2014-08-13 01:43:19 -03:00
|
|
|
AP_Param(const struct Info *info)
|
|
|
|
{
|
2012-12-13 18:58:23 -04:00
|
|
|
_var_info = info;
|
|
|
|
uint16_t i;
|
2015-12-27 16:22:32 -04:00
|
|
|
for (i=0; info[i].type != AP_PARAM_NONE; i++) ;
|
2012-12-13 18:58:23 -04:00
|
|
|
_num_vars = i;
|
|
|
|
}
|
2012-08-06 22:00:43 -03:00
|
|
|
|
2012-12-12 17:40:42 -04:00
|
|
|
// empty constructor
|
|
|
|
AP_Param() {}
|
2012-08-06 22:00:43 -03:00
|
|
|
|
|
|
|
// a token used for first()/next() state
|
|
|
|
typedef struct {
|
2015-12-30 17:27:22 -04:00
|
|
|
uint32_t key : 9;
|
|
|
|
uint32_t idx : 5; // offset into array types
|
2012-08-17 03:17:46 -03:00
|
|
|
uint32_t group_element : 18;
|
2012-08-06 22:00:43 -03:00
|
|
|
} ParamToken;
|
2012-02-11 07:51:35 -04:00
|
|
|
|
2012-02-17 00:12:48 -04:00
|
|
|
// return true if AP_Param has been initialised via setup()
|
|
|
|
static bool initialised(void);
|
|
|
|
|
2012-08-17 03:17:46 -03:00
|
|
|
/// Copy the variable's name, prefixed by any containing group name, to a
|
|
|
|
/// buffer.
|
2012-02-11 07:51:35 -04:00
|
|
|
///
|
|
|
|
/// If the variable has no name, the buffer will contain an empty string.
|
|
|
|
///
|
|
|
|
/// Note that if the combination of names is larger than the buffer, the
|
|
|
|
/// result in the buffer will be truncated.
|
|
|
|
///
|
2013-01-08 18:45:02 -04:00
|
|
|
/// @param token token giving current variable
|
2012-02-11 07:51:35 -04:00
|
|
|
/// @param buffer The destination buffer
|
|
|
|
/// @param bufferSize Total size of the destination buffer.
|
|
|
|
///
|
2015-12-27 16:22:32 -04:00
|
|
|
void copy_name_info(const struct AP_Param::Info *info,
|
|
|
|
const struct GroupInfo *ginfo,
|
|
|
|
const struct GroupInfo *ginfo0,
|
|
|
|
uint8_t idx, char *buffer, size_t bufferSize, bool force_scalar=false) const;
|
|
|
|
|
2015-10-06 19:08:59 -03:00
|
|
|
/// Copy the variable's name, prefixed by any containing group name, to a
|
|
|
|
/// buffer.
|
|
|
|
///
|
|
|
|
/// Uses token to look up AP_Param::Info for the variable
|
2013-04-19 04:46:40 -03:00
|
|
|
void copy_name_token(const ParamToken &token, char *buffer, size_t bufferSize, bool force_scalar=false) const;
|
2013-01-08 00:28:16 -04:00
|
|
|
|
2012-02-11 07:51:35 -04:00
|
|
|
/// Find a variable by name.
|
|
|
|
///
|
|
|
|
/// If the variable has no name, it cannot be found by this interface.
|
|
|
|
///
|
|
|
|
/// @param name The full name of the variable to be found.
|
|
|
|
/// @return A pointer to the variable, or NULL if
|
|
|
|
/// it does not exist.
|
|
|
|
///
|
2012-08-17 03:17:46 -03:00
|
|
|
static AP_Param * find(const char *name, enum ap_var_type *ptype);
|
2012-02-11 07:51:35 -04:00
|
|
|
|
2012-09-19 18:42:35 -03:00
|
|
|
/// Find a variable by index.
|
|
|
|
///
|
|
|
|
///
|
|
|
|
/// @param idx The index of the variable
|
|
|
|
/// @return A pointer to the variable, or NULL if
|
|
|
|
/// it does not exist.
|
|
|
|
///
|
2013-01-08 18:45:02 -04:00
|
|
|
static AP_Param * find_by_index(uint16_t idx, enum ap_var_type *ptype, ParamToken *token);
|
2012-09-19 18:42:35 -03:00
|
|
|
|
2015-12-29 06:21:54 -04:00
|
|
|
|
|
|
|
/// Find a variable by pointer
|
|
|
|
///
|
|
|
|
///
|
|
|
|
/// @param p Pointer to variable
|
|
|
|
/// @return token for variable
|
|
|
|
static bool find_by_pointer(AP_Param *p, ParamToken &token);
|
|
|
|
|
2012-12-13 18:58:23 -04:00
|
|
|
/// Find a object in the top level var_info table
|
|
|
|
///
|
|
|
|
/// If the variable has no name, it cannot be found by this interface.
|
|
|
|
///
|
|
|
|
/// @param name The full name of the variable to be found.
|
|
|
|
///
|
|
|
|
static AP_Param * find_object(const char *name);
|
|
|
|
|
2015-10-06 19:08:59 -03:00
|
|
|
/// Notify GCS of current parameter value
|
|
|
|
///
|
|
|
|
void notify() const;
|
|
|
|
|
2015-11-08 23:27:39 -04:00
|
|
|
// send a parameter to all GCS instances
|
|
|
|
void send_parameter(char *name, enum ap_var_type param_header_type) const;
|
|
|
|
|
2012-02-11 07:51:35 -04:00
|
|
|
/// Save the current value of the variable to EEPROM.
|
|
|
|
///
|
2013-06-02 01:48:36 -03:00
|
|
|
/// @param force_save If true then force save even if default
|
|
|
|
///
|
2012-02-11 07:51:35 -04:00
|
|
|
/// @return True if the variable was saved successfully.
|
|
|
|
///
|
2013-06-02 01:48:36 -03:00
|
|
|
bool save(bool force_save=false);
|
2012-02-11 07:51:35 -04:00
|
|
|
|
|
|
|
/// Load the variable from EEPROM.
|
|
|
|
///
|
|
|
|
/// @return True if the variable was loaded successfully.
|
|
|
|
///
|
|
|
|
bool load(void);
|
|
|
|
|
|
|
|
/// Load all variables from EEPROM
|
|
|
|
///
|
|
|
|
/// This function performs a best-efforts attempt to load all
|
|
|
|
/// of the variables from EEPROM. If some fail to load, their
|
|
|
|
/// values will remain as they are.
|
|
|
|
///
|
|
|
|
/// @return False if any variable failed to load
|
|
|
|
///
|
|
|
|
static bool load_all(void);
|
|
|
|
|
2015-12-29 06:21:54 -04:00
|
|
|
static void load_object_from_eeprom(const void *object_pointer, const struct GroupInfo *group_info);
|
|
|
|
|
2012-08-17 03:17:46 -03:00
|
|
|
// set a AP_Param variable to a specified value
|
|
|
|
static void set_value(enum ap_var_type type, void *ptr, float def_value);
|
2012-08-06 22:00:43 -03:00
|
|
|
|
2015-04-01 19:54:13 -03:00
|
|
|
/*
|
2015-08-10 04:25:29 -03:00
|
|
|
set a parameter to a float
|
2015-04-01 19:54:13 -03:00
|
|
|
*/
|
2015-08-10 04:25:29 -03:00
|
|
|
void set_float(float value, enum ap_var_type var_type);
|
2015-04-01 19:54:13 -03:00
|
|
|
|
2012-08-06 22:00:43 -03:00
|
|
|
// load default values for scalars in a group
|
2012-12-12 17:40:42 -04:00
|
|
|
static void setup_object_defaults(const void *object_pointer, const struct GroupInfo *group_info);
|
2012-08-06 22:00:43 -03:00
|
|
|
|
2014-07-20 20:37:41 -03:00
|
|
|
// set a value directly in an object. This should only be used by
|
|
|
|
// example code, not by mainline vehicle code
|
|
|
|
static void set_object_value(const void *object_pointer,
|
|
|
|
const struct GroupInfo *group_info,
|
|
|
|
const char *name, float value);
|
|
|
|
|
2012-12-12 17:40:42 -04:00
|
|
|
// load default values for all scalars in the main sketch. This
|
|
|
|
// does not recurse into the sub-objects
|
2012-12-13 18:58:23 -04:00
|
|
|
static void setup_sketch_defaults(void);
|
2012-08-06 22:00:43 -03:00
|
|
|
|
2013-05-30 08:45:25 -03:00
|
|
|
// convert old vehicle parameters to new object parameters
|
|
|
|
static void convert_old_parameters(const struct ConversionInfo *conversion_table, uint8_t table_size);
|
|
|
|
|
2012-02-11 07:51:35 -04:00
|
|
|
/// Erase all variables in EEPROM.
|
|
|
|
///
|
2012-08-17 03:17:46 -03:00
|
|
|
static void erase_all(void);
|
2012-02-11 07:51:35 -04:00
|
|
|
|
2012-02-12 19:16:55 -04:00
|
|
|
/// print the value of all variables
|
2015-08-28 05:28:26 -03:00
|
|
|
static void show_all(AP_HAL::BetterStream *port, bool showKeyValues=false);
|
2013-04-19 04:46:40 -03:00
|
|
|
|
|
|
|
/// print the value of one variable
|
|
|
|
static void show(const AP_Param *param,
|
|
|
|
const char *name,
|
|
|
|
enum ap_var_type ptype,
|
|
|
|
AP_HAL::BetterStream *port);
|
|
|
|
|
|
|
|
/// print the value of one variable
|
|
|
|
static void show(const AP_Param *param,
|
|
|
|
const ParamToken &token,
|
|
|
|
enum ap_var_type ptype,
|
|
|
|
AP_HAL::BetterStream *port);
|
2012-02-12 19:16:55 -04:00
|
|
|
|
2012-02-12 03:22:57 -04:00
|
|
|
/// Returns the first variable
|
|
|
|
///
|
|
|
|
/// @return The first variable in _var_info, or NULL if
|
|
|
|
/// there are none.
|
|
|
|
///
|
2012-08-17 03:17:46 -03:00
|
|
|
static AP_Param * first(ParamToken *token, enum ap_var_type *ptype);
|
2012-02-12 03:22:57 -04:00
|
|
|
|
|
|
|
/// Returns the next variable in _var_info, recursing into groups
|
|
|
|
/// as needed
|
2012-08-17 03:17:46 -03:00
|
|
|
static AP_Param * next(ParamToken *token, enum ap_var_type *ptype);
|
2012-02-12 03:22:57 -04:00
|
|
|
|
|
|
|
/// Returns the next scalar variable in _var_info, recursing into groups
|
|
|
|
/// as needed
|
2012-08-17 03:17:46 -03:00
|
|
|
static AP_Param * next_scalar(ParamToken *token, enum ap_var_type *ptype);
|
2012-02-11 07:51:35 -04:00
|
|
|
|
2012-02-12 04:18:58 -04:00
|
|
|
/// cast a variable to a float given its type
|
2013-04-19 04:46:40 -03:00
|
|
|
float cast_to_float(enum ap_var_type type) const;
|
2012-02-12 04:18:58 -04:00
|
|
|
|
2014-01-30 22:07:41 -04:00
|
|
|
// check var table for consistency
|
|
|
|
static bool check_var_info(void);
|
|
|
|
|
2015-10-21 23:57:05 -03:00
|
|
|
// return true if the parameter is configured in the defaults file
|
|
|
|
bool configured_in_defaults_file(void);
|
|
|
|
|
|
|
|
// return true if the parameter is configured in EEPROM/FRAM
|
|
|
|
bool configured_in_storage(void);
|
|
|
|
|
|
|
|
// return true if the parameter is configured
|
|
|
|
bool configured(void) { return configured_in_defaults_file() || configured_in_storage(); }
|
|
|
|
|
2016-01-05 23:41:07 -04:00
|
|
|
// count of parameters in tree
|
|
|
|
static uint16_t count_parameters(void);
|
|
|
|
|
2012-02-11 07:51:35 -04:00
|
|
|
private:
|
2012-02-12 03:22:57 -04:00
|
|
|
/// EEPROM header
|
|
|
|
///
|
|
|
|
/// This structure is placed at the head of the EEPROM to indicate
|
|
|
|
/// that the ROM is formatted for AP_Param.
|
|
|
|
///
|
|
|
|
struct EEPROM_header {
|
2012-08-17 03:17:46 -03:00
|
|
|
uint8_t magic[2];
|
|
|
|
uint8_t revision;
|
|
|
|
uint8_t spare;
|
2012-02-12 03:22:57 -04:00
|
|
|
};
|
|
|
|
|
2012-07-10 01:03:34 -03:00
|
|
|
/* This header is prepended to a variable stored in EEPROM.
|
2012-08-17 03:17:46 -03:00
|
|
|
* The meaning is as follows:
|
|
|
|
*
|
|
|
|
* - key: the k_param enum value from Parameter.h in the sketch
|
|
|
|
*
|
|
|
|
* - group_element: This is zero for top level parameters. For
|
|
|
|
* parameters stored within an object this is divided
|
|
|
|
* into 3 lots of 6 bits, allowing for three levels
|
|
|
|
* of object to be stored in the eeprom
|
|
|
|
*
|
|
|
|
* - type: the ap_var_type value for the variable
|
|
|
|
*/
|
2012-02-12 03:22:57 -04:00
|
|
|
struct Param_header {
|
2015-12-30 17:27:22 -04:00
|
|
|
// to get 9 bits for key we needed to split it into two parts to keep binary compatibility
|
|
|
|
uint32_t key_low : 8;
|
|
|
|
uint32_t type : 5;
|
|
|
|
uint32_t key_high : 1;
|
2012-08-17 03:17:46 -03:00
|
|
|
uint32_t group_element : 18;
|
2012-02-12 03:22:57 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
// number of bits in each level of nesting of groups
|
2012-08-17 03:17:46 -03:00
|
|
|
static const uint8_t _group_level_shift = 6;
|
|
|
|
static const uint8_t _group_bits = 18;
|
|
|
|
|
2015-12-30 17:27:22 -04:00
|
|
|
static const uint16_t _sentinal_key = 0x1FF;
|
|
|
|
static const uint8_t _sentinal_type = 0x1F;
|
2012-08-17 03:17:46 -03:00
|
|
|
static const uint8_t _sentinal_group = 0xFF;
|
|
|
|
|
2014-11-09 17:09:08 -04:00
|
|
|
static bool check_group_info(const struct GroupInfo *group_info, uint16_t *total_size,
|
|
|
|
uint8_t max_bits, uint8_t prefix_length);
|
2015-12-30 17:27:22 -04:00
|
|
|
static bool duplicate_key(uint16_t vindex, uint16_t key);
|
2015-12-29 06:21:54 -04:00
|
|
|
|
2015-12-30 17:27:22 -04:00
|
|
|
static bool adjust_group_offset(uint16_t vindex, const struct GroupInfo &group_info, ptrdiff_t &new_offset);
|
2015-12-29 06:21:54 -04:00
|
|
|
|
2012-08-17 03:17:46 -03:00
|
|
|
const struct Info * find_var_info_group(
|
|
|
|
const struct GroupInfo * group_info,
|
2015-12-30 17:27:22 -04:00
|
|
|
uint16_t vindex,
|
2012-08-17 03:17:46 -03:00
|
|
|
uint8_t group_base,
|
|
|
|
uint8_t group_shift,
|
2015-12-29 06:21:54 -04:00
|
|
|
ptrdiff_t group_offset,
|
2012-11-05 07:08:43 -04:00
|
|
|
uint32_t * group_element,
|
2015-12-27 16:22:32 -04:00
|
|
|
const struct GroupInfo * &group_ret,
|
|
|
|
const struct GroupInfo * &group_ret0,
|
2013-04-19 04:46:40 -03:00
|
|
|
uint8_t * idx) const;
|
2012-08-17 03:17:46 -03:00
|
|
|
const struct Info * find_var_info(
|
2012-11-05 07:08:43 -04:00
|
|
|
uint32_t * group_element,
|
2015-12-27 16:22:32 -04:00
|
|
|
const struct GroupInfo * &group_ret,
|
|
|
|
const struct GroupInfo * &group_ret0,
|
2015-10-06 19:08:59 -03:00
|
|
|
uint8_t * idx) const;
|
2013-04-19 04:46:40 -03:00
|
|
|
const struct Info * find_var_info_token(const ParamToken &token,
|
2013-01-08 00:28:16 -04:00
|
|
|
uint32_t * group_element,
|
2015-12-27 16:22:32 -04:00
|
|
|
const struct GroupInfo * &group_ret,
|
|
|
|
const struct GroupInfo * &group_ret0,
|
2013-04-19 04:46:40 -03:00
|
|
|
uint8_t * idx) const;
|
2012-08-17 03:17:46 -03:00
|
|
|
static const struct Info * find_by_header_group(
|
|
|
|
struct Param_header phdr, void **ptr,
|
2015-12-30 17:27:22 -04:00
|
|
|
uint16_t vindex,
|
2012-08-17 03:17:46 -03:00
|
|
|
const struct GroupInfo *group_info,
|
|
|
|
uint8_t group_base,
|
2015-12-27 16:22:32 -04:00
|
|
|
uint8_t group_shift,
|
2015-12-29 06:21:54 -04:00
|
|
|
ptrdiff_t group_offset);
|
2012-08-17 03:17:46 -03:00
|
|
|
static const struct Info * find_by_header(
|
|
|
|
struct Param_header phdr,
|
|
|
|
void **ptr);
|
|
|
|
void add_vector3f_suffix(
|
|
|
|
char *buffer,
|
|
|
|
size_t buffer_size,
|
2013-04-19 04:46:40 -03:00
|
|
|
uint8_t idx) const;
|
2012-08-17 03:17:46 -03:00
|
|
|
static AP_Param * find_group(
|
|
|
|
const char *name,
|
2015-12-30 17:27:22 -04:00
|
|
|
uint16_t vindex,
|
2015-12-29 06:21:54 -04:00
|
|
|
ptrdiff_t group_offset,
|
2012-08-17 03:17:46 -03:00
|
|
|
const struct GroupInfo *group_info,
|
|
|
|
enum ap_var_type *ptype);
|
|
|
|
static void write_sentinal(uint16_t ofs);
|
2015-12-30 17:27:22 -04:00
|
|
|
static uint16_t get_key(const Param_header &phdr);
|
|
|
|
static void set_key(Param_header &phdr, uint16_t key);
|
|
|
|
static bool is_sentinal(const Param_header &phrd);
|
2013-05-30 08:45:25 -03:00
|
|
|
static bool scan(
|
2012-08-17 03:17:46 -03:00
|
|
|
const struct Param_header *phdr,
|
|
|
|
uint16_t *pofs);
|
2013-01-01 21:51:25 -04:00
|
|
|
static uint8_t type_size(enum ap_var_type type);
|
2012-08-17 03:17:46 -03:00
|
|
|
static void eeprom_write_check(
|
|
|
|
const void *ptr,
|
|
|
|
uint16_t ofs,
|
|
|
|
uint8_t size);
|
|
|
|
static AP_Param * next_group(
|
2015-12-30 17:27:22 -04:00
|
|
|
uint16_t vindex,
|
2012-08-17 03:17:46 -03:00
|
|
|
const struct GroupInfo *group_info,
|
|
|
|
bool *found_current,
|
|
|
|
uint8_t group_base,
|
|
|
|
uint8_t group_shift,
|
2015-12-29 06:21:54 -04:00
|
|
|
ptrdiff_t group_offset,
|
2012-08-17 03:17:46 -03:00
|
|
|
ParamToken *token,
|
|
|
|
enum ap_var_type *ptype);
|
|
|
|
|
2015-04-01 22:24:45 -03:00
|
|
|
// find a default value given a pointer to a default value in flash
|
|
|
|
static float get_default_value(const float *def_value_ptr);
|
|
|
|
|
|
|
|
/*
|
|
|
|
find the def_value for a variable by name
|
|
|
|
*/
|
|
|
|
static const float *find_def_value_ptr(const char *name);
|
|
|
|
|
|
|
|
#if HAL_OS_POSIX_IO == 1
|
|
|
|
/*
|
|
|
|
load a parameter defaults file. This happens as part of load_all()
|
|
|
|
*/
|
|
|
|
static bool parse_param_line(char *line, char **vname, float &value);
|
|
|
|
static bool load_defaults_file(const char *filename);
|
|
|
|
#endif
|
|
|
|
|
2014-08-13 01:43:19 -03:00
|
|
|
static StorageAccess _storage;
|
2015-12-30 17:27:22 -04:00
|
|
|
static uint16_t _num_vars;
|
2016-01-05 23:41:07 -04:00
|
|
|
static uint16_t _parameter_count;
|
2012-08-17 03:17:46 -03:00
|
|
|
static const struct Info * _var_info;
|
2012-02-11 07:51:35 -04:00
|
|
|
|
2015-04-01 22:24:45 -03:00
|
|
|
/*
|
|
|
|
list of overridden values from load_defaults_file()
|
|
|
|
*/
|
|
|
|
struct param_override {
|
|
|
|
const float *def_value_ptr;
|
|
|
|
float value;
|
|
|
|
};
|
|
|
|
static struct param_override *param_overrides;
|
|
|
|
static uint16_t num_param_overrides;
|
|
|
|
|
2012-02-11 07:51:35 -04:00
|
|
|
// values filled into the EEPROM header
|
2012-08-17 03:17:46 -03:00
|
|
|
static const uint8_t k_EEPROM_magic0 = 0x50;
|
|
|
|
static const uint8_t k_EEPROM_magic1 = 0x41; ///< "AP"
|
|
|
|
static const uint8_t k_EEPROM_revision = 6; ///< current format revision
|
2013-05-30 08:45:25 -03:00
|
|
|
|
|
|
|
// convert old vehicle parameters to new object parameters
|
|
|
|
static void convert_old_parameter(const struct ConversionInfo *info);
|
2012-02-11 07:51:35 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
/// Template class for scalar variables.
|
|
|
|
///
|
|
|
|
/// Objects of this type have a value, and can be treated in many ways as though they
|
|
|
|
/// were the value.
|
|
|
|
///
|
|
|
|
/// @tparam T The scalar type of the variable
|
|
|
|
/// @tparam PT The AP_PARAM_* type
|
|
|
|
///
|
|
|
|
template<typename T, ap_var_type PT>
|
|
|
|
class AP_ParamT : public AP_Param
|
|
|
|
{
|
|
|
|
public:
|
2012-08-17 03:17:46 -03:00
|
|
|
static const ap_var_type vtype = PT;
|
2012-02-11 07:51:35 -04:00
|
|
|
|
|
|
|
/// Value getter
|
|
|
|
///
|
2013-04-17 21:52:40 -03:00
|
|
|
const T &get(void) const {
|
2012-02-11 07:51:35 -04:00
|
|
|
return _value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Value setter
|
|
|
|
///
|
2013-04-17 21:52:40 -03:00
|
|
|
void set(const T &v) {
|
2012-02-11 07:51:35 -04:00
|
|
|
_value = v;
|
|
|
|
}
|
2015-10-21 23:57:05 -03:00
|
|
|
|
|
|
|
/// Sets if the parameter is unconfigured
|
|
|
|
///
|
|
|
|
void set_default(const T &v) {
|
|
|
|
if (!configured()) {
|
|
|
|
set(v);
|
|
|
|
}
|
|
|
|
}
|
2015-10-06 19:08:59 -03:00
|
|
|
|
|
|
|
/// Value setter - set value, tell GCS
|
|
|
|
///
|
|
|
|
void set_and_notify(const T &v) {
|
|
|
|
set(v);
|
|
|
|
notify();
|
|
|
|
}
|
2012-02-11 07:51:35 -04:00
|
|
|
|
|
|
|
/// Combined set and save
|
|
|
|
///
|
2013-04-17 21:52:40 -03:00
|
|
|
bool set_and_save(const T &v) {
|
2015-05-03 01:47:40 -03:00
|
|
|
bool force = fabsf(_value - v) < FLT_EPSILON;
|
2012-02-11 07:51:35 -04:00
|
|
|
set(v);
|
2013-06-02 01:48:36 -03:00
|
|
|
return save(force);
|
2012-02-17 00:13:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Combined set and save, but only does the save if the value if
|
|
|
|
/// different from the current ram value, thus saving us a
|
|
|
|
/// scan(). This should only be used where we have not set() the
|
|
|
|
/// value separately, as otherwise the value in EEPROM won't be
|
|
|
|
/// updated correctly.
|
2013-05-01 21:18:36 -03:00
|
|
|
bool set_and_save_ifchanged(const T &v) {
|
2012-02-17 00:13:00 -04:00
|
|
|
if (v == _value) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
set(v);
|
2013-06-02 01:48:36 -03:00
|
|
|
return save(true);
|
2012-02-11 07:51:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Conversion to T returns a reference to the value.
|
|
|
|
///
|
|
|
|
/// This allows the class to be used in many situations where the value would be legal.
|
|
|
|
///
|
2013-04-17 21:52:40 -03:00
|
|
|
operator const T &() const {
|
2012-02-11 07:51:35 -04:00
|
|
|
return _value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Copy assignment from T is equivalent to ::set.
|
|
|
|
///
|
2013-04-17 21:52:40 -03:00
|
|
|
AP_ParamT<T,PT>& operator= (const T &v) {
|
2012-02-11 07:51:35 -04:00
|
|
|
_value = v;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2013-04-17 22:12:46 -03:00
|
|
|
/// bit ops on parameters
|
|
|
|
///
|
|
|
|
AP_ParamT<T,PT>& operator |=(const T &v) {
|
|
|
|
_value |= v;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
AP_ParamT<T,PT>& operator &=(const T &v) {
|
|
|
|
_value &= v;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
AP_ParamT<T,PT>& operator +=(const T &v) {
|
|
|
|
_value += v;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
AP_ParamT<T,PT>& operator -=(const T &v) {
|
|
|
|
_value -= v;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2012-02-11 07:51:35 -04:00
|
|
|
/// AP_ParamT types can implement AP_Param::cast_to_float
|
|
|
|
///
|
2013-05-01 21:18:36 -03:00
|
|
|
float cast_to_float(void) const {
|
2012-02-11 07:51:35 -04:00
|
|
|
return (float)_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
T _value;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/// Template class for non-scalar variables.
|
|
|
|
///
|
|
|
|
/// Objects of this type have a value, and can be treated in many ways as though they
|
|
|
|
/// were the value.
|
|
|
|
///
|
|
|
|
/// @tparam T The scalar type of the variable
|
|
|
|
/// @tparam PT AP_PARAM_* type
|
|
|
|
///
|
|
|
|
template<typename T, ap_var_type PT>
|
|
|
|
class AP_ParamV : public AP_Param
|
|
|
|
{
|
|
|
|
public:
|
2012-08-06 22:00:43 -03:00
|
|
|
|
2012-08-17 03:17:46 -03:00
|
|
|
static const ap_var_type vtype = PT;
|
2012-02-11 07:51:35 -04:00
|
|
|
|
|
|
|
/// Value getter
|
|
|
|
///
|
2013-04-21 09:23:46 -03:00
|
|
|
const T &get(void) const {
|
2012-02-11 07:51:35 -04:00
|
|
|
return _value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Value setter
|
|
|
|
///
|
2013-05-01 21:18:36 -03:00
|
|
|
void set(const T &v) {
|
2012-02-11 07:51:35 -04:00
|
|
|
_value = v;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Combined set and save
|
|
|
|
///
|
2013-05-01 21:18:36 -03:00
|
|
|
bool set_and_save(const T &v) {
|
2013-06-02 01:48:36 -03:00
|
|
|
bool force = (_value != v);
|
2012-02-11 07:51:35 -04:00
|
|
|
set(v);
|
2013-06-02 01:48:36 -03:00
|
|
|
return save(force);
|
2012-02-11 07:51:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Conversion to T returns a reference to the value.
|
|
|
|
///
|
|
|
|
/// This allows the class to be used in many situations where the value would be legal.
|
|
|
|
///
|
2013-04-21 09:23:46 -03:00
|
|
|
operator const T &() const {
|
2012-02-11 07:51:35 -04:00
|
|
|
return _value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Copy assignment from T is equivalent to ::set.
|
|
|
|
///
|
2013-05-01 21:18:36 -03:00
|
|
|
AP_ParamV<T,PT>& operator=(const T &v) {
|
2012-02-11 07:51:35 -04:00
|
|
|
_value = v;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2012-08-17 03:17:46 -03:00
|
|
|
T _value;
|
2012-02-11 07:51:35 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/// Template class for array variables.
|
|
|
|
///
|
|
|
|
/// Objects created using this template behave like arrays of the type T,
|
|
|
|
/// but are stored like single variables.
|
|
|
|
///
|
|
|
|
/// @tparam T The scalar type of the variable
|
|
|
|
/// @tparam N number of elements
|
|
|
|
/// @tparam PT the AP_PARAM_* type
|
|
|
|
///
|
|
|
|
template<typename T, uint8_t N, ap_var_type PT>
|
|
|
|
class AP_ParamA : public AP_Param
|
|
|
|
{
|
|
|
|
public:
|
2012-08-06 22:00:43 -03:00
|
|
|
|
2012-02-11 07:51:35 -04:00
|
|
|
static const ap_var_type vtype = PT;
|
|
|
|
|
|
|
|
/// Array operator accesses members.
|
|
|
|
///
|
|
|
|
/// @note It would be nice to range-check i here, but then what would we return?
|
|
|
|
///
|
2013-05-01 21:18:36 -03:00
|
|
|
const T & operator[](uint8_t i) {
|
2012-02-11 07:51:35 -04:00
|
|
|
return _value[i];
|
|
|
|
}
|
|
|
|
|
2013-05-01 21:18:36 -03:00
|
|
|
const T & operator[](int8_t i) {
|
2013-01-01 22:54:03 -04:00
|
|
|
return _value[(uint8_t)i];
|
|
|
|
}
|
|
|
|
|
2012-02-11 07:51:35 -04:00
|
|
|
/// Value getter
|
|
|
|
///
|
|
|
|
/// @note Returns zero for index values out of range.
|
|
|
|
///
|
|
|
|
T get(uint8_t i) const {
|
|
|
|
if (i < N) {
|
|
|
|
return _value[i];
|
|
|
|
} else {
|
|
|
|
return (T)0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Value setter
|
|
|
|
///
|
|
|
|
/// @note Attempts to set an index out of range are discarded.
|
|
|
|
///
|
2013-05-01 21:18:36 -03:00
|
|
|
void set(uint8_t i, const T &v) {
|
2012-02-11 07:51:35 -04:00
|
|
|
if (i < N) {
|
|
|
|
_value[i] = v;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
T _value[N];
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// Convenience macro for defining instances of the AP_ParamT template.
|
|
|
|
///
|
2012-02-26 19:22:29 -04:00
|
|
|
// declare a scalar type
|
|
|
|
// _t is the base type
|
|
|
|
// _suffix is the suffix on the AP_* type name
|
|
|
|
// _pt is the enum ap_var_type type
|
2012-08-17 03:17:46 -03:00
|
|
|
#define AP_PARAMDEF(_t, _suffix, _pt) typedef AP_ParamT<_t, _pt> AP_ ## _suffix;
|
2012-02-11 07:51:35 -04:00
|
|
|
AP_PARAMDEF(float, Float, AP_PARAM_FLOAT); // defines AP_Float
|
|
|
|
AP_PARAMDEF(int8_t, Int8, AP_PARAM_INT8); // defines AP_Int8
|
|
|
|
AP_PARAMDEF(int16_t, Int16, AP_PARAM_INT16); // defines AP_Int16
|
|
|
|
AP_PARAMDEF(int32_t, Int32, AP_PARAM_INT32); // defines AP_Int32
|
|
|
|
|
2012-02-26 19:22:29 -04:00
|
|
|
// declare a non-scalar type
|
2012-02-17 19:00:26 -04:00
|
|
|
// this is used in AP_Math.h
|
2012-02-26 19:22:29 -04:00
|
|
|
// _t is the base type
|
|
|
|
// _suffix is the suffix on the AP_* type name
|
|
|
|
// _pt is the enum ap_var_type type
|
2012-08-17 03:17:46 -03:00
|
|
|
#define AP_PARAMDEFV(_t, _suffix, _pt) typedef AP_ParamV<_t, _pt> AP_ ## _suffix;
|