Dawn Framework 1.0
Universal data acquisition framework for embedded systems
ahrs.hxx
1// dawn/include/dawn/prog/ahrs.hxx
2//
3// SPDX-License-Identifier: Apache-2.0
4//
5
6#pragma once
7
8#include <inttypes.h>
9
10#include "Fusion/Fusion.h"
11#include "dawn/io/ddata.hxx"
12#include "dawn/prog/common.hxx"
13
14namespace dawn
15{
16class CIOCommon;
17
22class CProgAhrs : public CProgCommon
23{
24public:
25 enum
26 {
27 PROG_AHRS_CFG_FIRST = 0,
28 PROG_AHRS_CFG_ACCEL = 1,
29 PROG_AHRS_CFG_GYRO = 2,
30 PROG_AHRS_CFG_OUTPUT = 3,
31 PROG_AHRS_CFG_PARAMS = 4,
32 PROG_AHRS_CFG_MAG = 5,
33 PROG_AHRS_CFG_LAST = 31
34 };
35
36 struct
37 {
38 uint32_t gain;
39 uint32_t accel_rejection;
40 uint32_t recovery_period;
41 uint32_t rate;
42 } typedef SProgAhrsParams;
43
44 explicit CProgAhrs(CDescObject &desc);
45 ~CProgAhrs() override;
46
47#ifdef CONFIG_DAWN_OBJECT_HAS_NAME
48 const char *getClassNameStr() const override
49 {
50 return "ahrs";
51 }
52#endif
53
54 int configure() override;
55 int init() override;
56 int deinit() override;
57 int doStart() override;
58 int doStop() override;
59 bool hasThread() const override;
60
61 constexpr static SObjectId::ObjectId objectId(uint16_t inst)
62 {
64 SObjectId::OBJTYPE_PROG, CProgCommon::PROG_CLASS_AHRS, SObjectId::DTYPE_ANY, 0, inst);
65 }
66
67 constexpr static SObjectCfg::ObjectCfgId cfgId(bool rw, uint8_t size, uint8_t id)
68 {
70 SObjectId::OBJTYPE_PROG, CProgCommon::PROG_CLASS_AHRS, SObjectId::DTYPE_ANY, rw, size, id);
71 }
72
73 constexpr static SObjectCfg::ObjectCfgId cfgIdAccel()
74 {
75 return CProgAhrs::cfgId(false, 1, PROG_AHRS_CFG_ACCEL);
76 }
77
78 constexpr static SObjectCfg::ObjectCfgId cfgIdGyro()
79 {
80 return CProgAhrs::cfgId(false, 1, PROG_AHRS_CFG_GYRO);
81 }
82
83 constexpr static SObjectCfg::ObjectCfgId cfgIdMag()
84 {
85 return CProgAhrs::cfgId(false, 1, PROG_AHRS_CFG_MAG);
86 }
87
88 constexpr static SObjectCfg::ObjectCfgId cfgIdOutput()
89 {
90 return CProgAhrs::cfgId(false, 1, PROG_AHRS_CFG_OUTPUT);
91 }
92
93 constexpr static SObjectCfg::ObjectCfgId cfgParams(bool rw = false)
94 {
95 return CProgAhrs::cfgId(rw, sizeof(SProgAhrsParams) / 4, PROG_AHRS_CFG_PARAMS);
96 }
97
98protected:
99 int onSetObjConfig(SObjectCfg::ObjectCfgId objcfg, uint32_t *data, size_t len) override;
100
101private:
102 CIOCommon *accel;
103 CIOCommon *gyro;
104 CIOCommon *mag;
105 CIOCommon *output;
106 SObjectId::ObjectId accelId;
107 SObjectId::ObjectId gyroId;
109 SObjectId::ObjectId outputId;
110 io_ddata_t *outData;
111 bool active;
112 bool registered;
113
114 // Cached params (decoded floats).
115
116 float pGain;
117 float pRejection;
118 float pPeriod;
119 float pRate;
120
121 // Filter state.
122
123 FusionAhrs ahrs;
124 FusionOffset offset;
125 FusionVector accelG;
126 FusionVector magRaw;
127 bool haveAccel;
128 bool haveMag;
129 uint64_t lastNs;
130
131 static int accelNotifierCb(void *priv, io_ddata_t *data);
132 static int gyroNotifierCb(void *priv, io_ddata_t *data);
133 static int magNotifierCb(void *priv, io_ddata_t *data);
134
135 int configureDesc(const CDescObject &desc);
136 int validateInput(CIOCommon *io) const;
137 void applySettings();
138 void handleGyro(io_ddata_t *data);
139 static bool paramsValid(float gain, float rejection, float period, float rate);
140};
141} // namespace dawn
Descriptor wrapper for individual object configuration.
Base class for all I/O objects.
Definition common.hxx:27
AHRS sensor fusion (x-io Fusion): accel + gyro IOs in, world-frame linear acceleration (gravity remov...
Definition ahrs.hxx:23
int init()
One-time initialize object after bindings are resolved.
Definition ahrs.cxx:192
int doStart()
Start implementation hook.
Definition ahrs.cxx:416
bool hasThread() const
Check if a background thread is active.
Definition ahrs.cxx:482
int deinit()
De-initialize object.
Definition ahrs.cxx:255
int onSetObjConfig(SObjectCfg::ObjectCfgId objcfg, uint32_t *data, size_t len)
Pre-update hook for runtime configuration writes.
Definition ahrs.cxx:377
int configure()
Configure object from descriptor data.
Definition ahrs.cxx:150
int doStop()
Stop implementation hook.
Definition ahrs.cxx:456
Base class for all PROG (processing) objects.
Definition common.hxx:27
static ObjectCfgId objectCfg(uint8_t type, uint16_t cls, uint8_t dtype, bool rw, uint16_t size, uint8_t id)
Construct 32-bit ConfigID from component fields.
uint32_t ObjectCfgId
ConfigID type - single 32-bit value.
Definition objectcfg.hxx:60
Out-of-tree user-extension hooks for Dawn.
Definition bindable.hxx:13
@ OBJTYPE_PROG
Program/algorithm object type.
Definition objectid.hxx:202
@ DTYPE_ANY
Wildcard data type (matches any actual type).
Definition objectid.hxx:68
uint32_t ObjectId
ObjectID type - single 32-bit value.
Definition objectid.hxx:44
static ObjectId objectId(uint8_t type, uint16_t cls, uint8_t dtype, uint8_t flags, uint16_t priv)
Construct 32-bit ObjectID from component fields.
Definition objectid.hxx:290
Heap-allocated dynamic I/O data buffer.
Definition ddata.hxx:21