PX4 Firmware
PX4 Autopilot Software http://px4.io
Block.cpp
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (C) 2012-2017 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 Block.cpp
36  *
37  * Controller library code
38  */
39 
40 #include "Block.hpp"
41 #include "BlockParam.hpp"
42 
43 #include <cstring>
44 
45 namespace control
46 {
47 
48 Block::Block(SuperBlock *parent, const char *name) :
49  _name(name),
50  _parent(parent)
51 {
52  if (getParent() != nullptr) {
53  getParent()->getChildren().add(this);
54  }
55 }
56 
57 void Block::getName(char *buf, size_t n)
58 {
59  if (getParent() == nullptr) {
60  strncpy(buf, _name, n);
61  // ensure string is terminated
62  buf[n - 1] = '\0';
63 
64  } else {
65  char parentName[blockNameLengthMax];
66  getParent()->getName(parentName, n);
67 
68  if (!strcmp(_name, "")) {
69  strncpy(buf, parentName, n);
70  // ensure string is terminated
71  buf[n - 1] = '\0';
72 
73  } else {
74  snprintf(buf, n, "%s_%s", parentName, _name);
75  }
76  }
77 }
78 
80 {
81  BlockParamBase *param = getParams().getHead();
82  int count = 0;
83 
84  while (param != nullptr) {
85  if (count++ > maxParamsPerBlock) {
88  PX4_ERR("exceeded max params for block: %s", name);
89  break;
90  }
91 
92  //printf("updating param: %s\n", param->getName());
93  param->update();
94  param = param->getSibling();
95  }
96 
98 }
99 
101 {
102  Block::setDt(dt);
103  Block *child = getChildren().getHead();
104  int count = 0;
105 
106  while (child != nullptr) {
107  if (count++ > maxChildrenPerBlock) {
108  char name[blockNameLengthMax];
110  PX4_ERR("exceeded max children for block: %s", name);
111  break;
112  }
113 
114  child->setDt(dt);
115  child = child->getSibling();
116  }
117 }
118 
120 {
121  Block *child = getChildren().getHead();
122  int count = 0;
123 
124  while (child != nullptr) {
125  if (count++ > maxChildrenPerBlock) {
126  char name[blockNameLengthMax];
128  PX4_ERR("exceeded max children for block: %s", name);
129  break;
130  }
131 
132  child->updateParams();
133  child = child->getSibling();
134  }
135 }
136 
137 } // namespace control
138 
139 template class List<control::BlockParamBase *>;
const char * _name
Definition: Block.hpp:87
virtual void updateParamsSubclass()
Definition: Block.hpp:82
static constexpr uint8_t maxParamsPerBlock
Definition: Block.hpp:50
void getName(char *name, size_t n)
Definition: Block.cpp:57
virtual void setDt(float dt)
Definition: Block.hpp:77
Controller library code.
Definition: List.hpp:59
static constexpr uint8_t maxChildrenPerBlock
Definition: Block.hpp:49
void updateChildParams()
Definition: Block.cpp:119
List< Block * > & getChildren()
Definition: Block.hpp:119
void setDt(float dt) override
Definition: Block.cpp:100
static constexpr uint8_t blockNameLengthMax
Definition: Block.hpp:51
Controller library code.
const char * name
Definition: tests_main.c:58
float dt
Definition: px4io.c:73
Block(SuperBlock *parent, const char *name)
Definition: Block.cpp:48
virtual void updateParams()
Definition: Block.cpp:79
SuperBlock * getParent()
Definition: Block.hpp:84
const T getSibling() const
Definition: List.hpp:50
List< BlockParamBase * > & getParams()
Definition: Block.hpp:85
virtual bool update()=0