Block and SuperBlock minor cleanup

This commit is contained in:
Daniel Agar
2017-06-04 18:22:42 +08:00
committed by Lorenz Meier
parent 1671c32238
commit 3d6792c019
3 changed files with 54 additions and 50 deletions
+11 -17
View File
@@ -1,6 +1,6 @@
/**************************************************************************** /****************************************************************************
* *
* Copyright (C) 2012 PX4 Development Team. All rights reserved. * Copyright (C) 2012-2017 PX4 Development Team. All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions * modification, are permitted provided that the following conditions
@@ -37,25 +37,20 @@
* Controller library code * Controller library code
*/ */
#include <math.h> #include "Block.hpp"
#include <string.h> #include "BlockParam.hpp"
#include <stdio.h>
#include <cstring> #include <cstring>
#include <uORB/Subscription.hpp> #include <uORB/Subscription.hpp>
#include <uORB/Publication.hpp> #include <uORB/Publication.hpp>
#include "Block.hpp"
#include "BlockParam.hpp"
namespace control namespace control
{ {
Block::Block(SuperBlock *parent, const char *name) : Block::Block(SuperBlock *parent, const char *name) :
_name(name), _name(name),
_parent(parent), _parent(parent)
_dt(0)
{ {
if (getParent() != nullptr) { if (getParent() != nullptr) {
getParent()->getChildren().add(this); getParent()->getChildren().add(this);
@@ -93,7 +88,7 @@ void Block::updateParams()
if (count++ > maxParamsPerBlock) { if (count++ > maxParamsPerBlock) {
char name[blockNameLengthMax]; char name[blockNameLengthMax];
getName(name, blockNameLengthMax); getName(name, blockNameLengthMax);
printf("exceeded max params for block: %s\n", name); PX4_ERR("exceeded max params for block: %s", name);
break; break;
} }
@@ -112,7 +107,7 @@ void Block::updateSubscriptions()
if (count++ > maxSubscriptionsPerBlock) { if (count++ > maxSubscriptionsPerBlock) {
char name[blockNameLengthMax]; char name[blockNameLengthMax];
getName(name, blockNameLengthMax); getName(name, blockNameLengthMax);
printf("exceeded max subscriptions for block: %s\n", name); PX4_ERR("exceeded max subscriptions for block: %s", name);
break; break;
} }
@@ -130,7 +125,7 @@ void Block::updatePublications()
if (count++ > maxPublicationsPerBlock) { if (count++ > maxPublicationsPerBlock) {
char name[blockNameLengthMax]; char name[blockNameLengthMax];
getName(name, blockNameLengthMax); getName(name, blockNameLengthMax);
printf("exceeded max publications for block: %s\n", name); PX4_ERR("exceeded max publications for block: %s", name);
break; break;
} }
@@ -149,7 +144,7 @@ void SuperBlock::setDt(float dt)
if (count++ > maxChildrenPerBlock) { if (count++ > maxChildrenPerBlock) {
char name[blockNameLengthMax]; char name[blockNameLengthMax];
getName(name, blockNameLengthMax); getName(name, blockNameLengthMax);
printf("exceeded max children for block: %s\n", name); PX4_ERR("exceeded max children for block: %s", name);
break; break;
} }
@@ -167,7 +162,7 @@ void SuperBlock::updateChildParams()
if (count++ > maxChildrenPerBlock) { if (count++ > maxChildrenPerBlock) {
char name[blockNameLengthMax]; char name[blockNameLengthMax];
getName(name, blockNameLengthMax); getName(name, blockNameLengthMax);
printf("exceeded max children for block: %s\n", name); PX4_ERR("exceeded max children for block: %s", name);
break; break;
} }
@@ -185,7 +180,7 @@ void SuperBlock::updateChildSubscriptions()
if (count++ > maxChildrenPerBlock) { if (count++ > maxChildrenPerBlock) {
char name[blockNameLengthMax]; char name[blockNameLengthMax];
getName(name, blockNameLengthMax); getName(name, blockNameLengthMax);
printf("exceeded max children for block: %s\n", name); PX4_ERR("exceeded max children for block: %s", name);
break; break;
} }
@@ -203,7 +198,7 @@ void SuperBlock::updateChildPublications()
if (count++ > maxChildrenPerBlock) { if (count++ > maxChildrenPerBlock) {
char name[blockNameLengthMax]; char name[blockNameLengthMax];
getName(name, blockNameLengthMax); getName(name, blockNameLengthMax);
printf("exceeded max children for block: %s\n", name); PX4_ERR("exceeded max children for block: %s", name);
break; break;
} }
@@ -212,7 +207,6 @@ void SuperBlock::updateChildPublications()
} }
} }
} // namespace control } // namespace control
template class List<uORB::SubscriptionNode *>; template class List<uORB::SubscriptionNode *>;
+42 -32
View File
@@ -1,6 +1,6 @@
/**************************************************************************** /****************************************************************************
* *
* Copyright (C) 2012 PX4 Development Team. All rights reserved. * Copyright (C) 2012-2017 PX4 Development Team. All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions * modification, are permitted provided that the following conditions
@@ -32,16 +32,13 @@
****************************************************************************/ ****************************************************************************/
/** /**
* @file Block.h * @file Block.hpp
* *
* Controller library code * Controller library code
*/ */
#pragma once #pragma once
#include <stdint.h>
#include <inttypes.h>
#include <containers/List.hpp> #include <containers/List.hpp>
#include <uORB/Publication.hpp> #include <uORB/Publication.hpp>
#include <uORB/Subscription.hpp> #include <uORB/Subscription.hpp>
@@ -50,11 +47,11 @@
namespace control namespace control
{ {
static const uint16_t maxChildrenPerBlock = 100; static constexpr uint8_t maxChildrenPerBlock = 100;
static const uint16_t maxParamsPerBlock = 100; static constexpr uint8_t maxParamsPerBlock = 100;
static const uint16_t maxSubscriptionsPerBlock = 100; static constexpr uint8_t maxSubscriptionsPerBlock = 100;
static const uint16_t maxPublicationsPerBlock = 100; static constexpr uint8_t maxPublicationsPerBlock = 100;
static const uint8_t blockNameLengthMax = 40; static constexpr uint8_t blockNameLengthMax = 40;
// forward declaration // forward declaration
class BlockParamBase; class BlockParamBase;
@@ -62,39 +59,43 @@ class SuperBlock;
/** /**
*/ */
class __EXPORT Block : class __EXPORT Block : public ListNode<Block *>
public ListNode<Block *>
{ {
public: public:
friend class BlockParamBase; friend class BlockParamBase;
// methods
Block(SuperBlock *parent, const char *name); Block(SuperBlock *parent, const char *name);
virtual ~Block() = default;
// no copy, assignment, move, move assignment
Block(const Block &) = delete;
Block &operator=(const Block &) = delete;
Block(Block &&) = delete;
Block &operator=(Block &&) = delete;
void getName(char *name, size_t n); void getName(char *name, size_t n);
virtual ~Block() {};
virtual void updateParams(); virtual void updateParams();
virtual void updateSubscriptions(); virtual void updateSubscriptions();
virtual void updatePublications(); virtual void updatePublications();
virtual void setDt(float dt) { _dt = dt; } virtual void setDt(float dt) { _dt = dt; }
// accessors
float getDt() { return _dt; } float getDt() { return _dt; }
protected: protected:
// accessors
SuperBlock *getParent() { return _parent; } SuperBlock *getParent() { return _parent; }
List<uORB::SubscriptionNode *> &getSubscriptions() { return _subscriptions; } List<uORB::SubscriptionNode *> &getSubscriptions() { return _subscriptions; }
List<uORB::PublicationNode *> &getPublications() { return _publications; } List<uORB::PublicationNode *> &getPublications() { return _publications; }
List<BlockParamBase *> &getParams() { return _params; } List<BlockParamBase *> &getParams() { return _params; }
// attributes
const char *_name; const char *_name;
SuperBlock *_parent; SuperBlock *_parent;
float _dt; float _dt{0.0f};
List<uORB::SubscriptionNode *> _subscriptions; List<uORB::SubscriptionNode *> _subscriptions;
List<uORB::PublicationNode *> _publications; List<uORB::PublicationNode *> _publications;
List<BlockParamBase *> _params; List<BlockParamBase *> _params;
private:
/* this class has pointer data members and should not be copied (private constructor) */
Block(const control::Block &);
Block operator=(const control::Block &);
}; };
class __EXPORT SuperBlock : class __EXPORT SuperBlock :
@@ -102,35 +103,44 @@ class __EXPORT SuperBlock :
{ {
public: public:
friend class Block; friend class Block;
// methods
SuperBlock(SuperBlock *parent, const char *name) : Block(parent, name) {} SuperBlock(SuperBlock *parent, const char *name) : Block(parent, name) {};
virtual ~SuperBlock() {}; ~SuperBlock() = default;
virtual void setDt(float dt);
virtual void updateParams() // no copy, assignment, move, move assignment
SuperBlock(const SuperBlock &) = delete;
SuperBlock &operator=(const SuperBlock &) = delete;
SuperBlock(SuperBlock &&) = delete;
SuperBlock &operator=(SuperBlock &&) = delete;
void setDt(float dt) override;
void updateParams() override
{ {
Block::updateParams(); Block::updateParams();
if (getChildren().getHead() != nullptr) { updateChildParams(); } if (getChildren().getHead() != nullptr) { updateChildParams(); }
} }
virtual void updateSubscriptions()
void updateSubscriptions() override
{ {
Block::updateSubscriptions(); Block::updateSubscriptions();
if (getChildren().getHead() != nullptr) { updateChildSubscriptions(); } if (getChildren().getHead() != nullptr) { updateChildSubscriptions(); }
} }
virtual void updatePublications() void updatePublications() override
{ {
Block::updatePublications(); Block::updatePublications();
if (getChildren().getHead() != nullptr) { updateChildPublications(); } if (getChildren().getHead() != nullptr) { updateChildPublications(); }
} }
protected: protected:
// methods
List<Block *> &getChildren() { return _children; } List<Block *> &getChildren() { return _children; }
void updateChildParams(); void updateChildParams();
void updateChildSubscriptions(); void updateChildSubscriptions();
void updateChildPublications(); void updateChildPublications();
// attributes
List<Block *> _children; List<Block *> _children;
}; };
+1 -1
View File
@@ -77,7 +77,7 @@ BlockParamBase::BlockParamBase(Block *parent, const char *name, bool parent_pref
_handle = param_find(fullname); _handle = param_find(fullname);
if (_handle == PARAM_INVALID) { if (_handle == PARAM_INVALID) {
PX4_ERR("error finding param: %s\n", fullname); PX4_ERR("error finding param: %s", fullname);
} }
}; };