aboutsummaryrefslogtreecommitdiff
path: root/src/modules/controllib/block
diff options
context:
space:
mode:
authorLorenz Meier <lm@inf.ethz.ch>2013-04-28 09:54:11 +0200
committerLorenz Meier <lm@inf.ethz.ch>2013-04-28 09:54:11 +0200
commit13fc6703862862f4263d8d5d085b7a16b87190e1 (patch)
tree47f3a17cb6f38b1aafe22e1cdef085cd73cd3a1d /src/modules/controllib/block
parentf57439b90e23de260259dec051d3e2ead2d61c8c (diff)
downloadpx4-firmware-13fc6703862862f4263d8d5d085b7a16b87190e1.tar.gz
px4-firmware-13fc6703862862f4263d8d5d085b7a16b87190e1.tar.bz2
px4-firmware-13fc6703862862f4263d8d5d085b7a16b87190e1.zip
Moved last libs, drivers and headers, cleaned up IO build
Diffstat (limited to 'src/modules/controllib/block')
-rw-r--r--src/modules/controllib/block/Block.cpp210
-rw-r--r--src/modules/controllib/block/Block.hpp131
-rw-r--r--src/modules/controllib/block/BlockParam.cpp79
-rw-r--r--src/modules/controllib/block/BlockParam.hpp90
-rw-r--r--src/modules/controllib/block/List.hpp71
-rw-r--r--src/modules/controllib/block/UOrbPublication.cpp39
-rw-r--r--src/modules/controllib/block/UOrbPublication.hpp118
-rw-r--r--src/modules/controllib/block/UOrbSubscription.cpp51
-rw-r--r--src/modules/controllib/block/UOrbSubscription.hpp137
9 files changed, 926 insertions, 0 deletions
diff --git a/src/modules/controllib/block/Block.cpp b/src/modules/controllib/block/Block.cpp
new file mode 100644
index 000000000..5994d2315
--- /dev/null
+++ b/src/modules/controllib/block/Block.cpp
@@ -0,0 +1,210 @@
+/****************************************************************************
+ *
+ * Copyright (C) 2012 PX4 Development Team. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name PX4 nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ****************************************************************************/
+
+/**
+ * @file Block.cpp
+ *
+ * Controller library code
+ */
+
+#include <math.h>
+#include <string.h>
+#include <stdio.h>
+
+#include "Block.hpp"
+#include "BlockParam.hpp"
+#include "UOrbSubscription.hpp"
+#include "UOrbPublication.hpp"
+
+namespace control
+{
+
+Block::Block(SuperBlock *parent, const char *name) :
+ _name(name),
+ _parent(parent),
+ _dt(0),
+ _subscriptions(),
+ _params()
+{
+ if (getParent() != NULL) {
+ getParent()->getChildren().add(this);
+ }
+}
+
+void Block::getName(char *buf, size_t n)
+{
+ if (getParent() == NULL) {
+ strncpy(buf, _name, n);
+
+ } else {
+ char parentName[blockNameLengthMax];
+ getParent()->getName(parentName, n);
+
+ if (!strcmp(_name, "")) {
+ strncpy(buf, parentName, blockNameLengthMax);
+
+ } else {
+ snprintf(buf, blockNameLengthMax, "%s_%s", parentName, _name);
+ }
+ }
+}
+
+void Block::updateParams()
+{
+ BlockParamBase *param = getParams().getHead();
+ int count = 0;
+
+ while (param != NULL) {
+ if (count++ > maxParamsPerBlock) {
+ char name[blockNameLengthMax];
+ getName(name, blockNameLengthMax);
+ printf("exceeded max params for block: %s\n", name);
+ break;
+ }
+
+ //printf("updating param: %s\n", param->getName());
+ param->update();
+ param = param->getSibling();
+ }
+}
+
+void Block::updateSubscriptions()
+{
+ UOrbSubscriptionBase *sub = getSubscriptions().getHead();
+ int count = 0;
+
+ while (sub != NULL) {
+ if (count++ > maxSubscriptionsPerBlock) {
+ char name[blockNameLengthMax];
+ getName(name, blockNameLengthMax);
+ printf("exceeded max subscriptions for block: %s\n", name);
+ break;
+ }
+
+ sub->update();
+ sub = sub->getSibling();
+ }
+}
+
+void Block::updatePublications()
+{
+ UOrbPublicationBase *pub = getPublications().getHead();
+ int count = 0;
+
+ while (pub != NULL) {
+ if (count++ > maxPublicationsPerBlock) {
+ char name[blockNameLengthMax];
+ getName(name, blockNameLengthMax);
+ printf("exceeded max publications for block: %s\n", name);
+ break;
+ }
+
+ pub->update();
+ pub = pub->getSibling();
+ }
+}
+
+void SuperBlock::setDt(float dt)
+{
+ Block::setDt(dt);
+ Block *child = getChildren().getHead();
+ int count = 0;
+
+ while (child != NULL) {
+ if (count++ > maxChildrenPerBlock) {
+ char name[40];
+ getName(name, 40);
+ printf("exceeded max children for block: %s\n", name);
+ break;
+ }
+
+ child->setDt(dt);
+ child = child->getSibling();
+ }
+}
+
+void SuperBlock::updateChildParams()
+{
+ Block *child = getChildren().getHead();
+ int count = 0;
+
+ while (child != NULL) {
+ if (count++ > maxChildrenPerBlock) {
+ char name[40];
+ getName(name, 40);
+ printf("exceeded max children for block: %s\n", name);
+ break;
+ }
+
+ child->updateParams();
+ child = child->getSibling();
+ }
+}
+
+void SuperBlock::updateChildSubscriptions()
+{
+ Block *child = getChildren().getHead();
+ int count = 0;
+
+ while (child != NULL) {
+ if (count++ > maxChildrenPerBlock) {
+ char name[40];
+ getName(name, 40);
+ printf("exceeded max children for block: %s\n", name);
+ break;
+ }
+
+ child->updateSubscriptions();
+ child = child->getSibling();
+ }
+}
+
+void SuperBlock::updateChildPublications()
+{
+ Block *child = getChildren().getHead();
+ int count = 0;
+
+ while (child != NULL) {
+ if (count++ > maxChildrenPerBlock) {
+ char name[40];
+ getName(name, 40);
+ printf("exceeded max children for block: %s\n", name);
+ break;
+ }
+
+ child->updatePublications();
+ child = child->getSibling();
+ }
+}
+
+} // namespace control
diff --git a/src/modules/controllib/block/Block.hpp b/src/modules/controllib/block/Block.hpp
new file mode 100644
index 000000000..258701f27
--- /dev/null
+++ b/src/modules/controllib/block/Block.hpp
@@ -0,0 +1,131 @@
+/****************************************************************************
+ *
+ * Copyright (C) 2012 PX4 Development Team. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name PX4 nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ****************************************************************************/
+
+/**
+ * @file Block.h
+ *
+ * Controller library code
+ */
+
+#pragma once
+
+#include <stdint.h>
+#include <inttypes.h>
+
+#include "List.hpp"
+
+namespace control
+{
+
+static const uint16_t maxChildrenPerBlock = 100;
+static const uint16_t maxParamsPerBlock = 100;
+static const uint16_t maxSubscriptionsPerBlock = 100;
+static const uint16_t maxPublicationsPerBlock = 100;
+static const uint8_t blockNameLengthMax = 80;
+
+// forward declaration
+class BlockParamBase;
+class UOrbSubscriptionBase;
+class UOrbPublicationBase;
+class SuperBlock;
+
+/**
+ */
+class __EXPORT Block :
+ public ListNode<Block *>
+{
+public:
+ friend class BlockParamBase;
+// methods
+ Block(SuperBlock *parent, const char *name);
+ void getName(char *name, size_t n);
+ virtual ~Block() {};
+ virtual void updateParams();
+ virtual void updateSubscriptions();
+ virtual void updatePublications();
+ virtual void setDt(float dt) { _dt = dt; }
+// accessors
+ float getDt() { return _dt; }
+protected:
+// accessors
+ SuperBlock *getParent() { return _parent; }
+ List<UOrbSubscriptionBase *> & getSubscriptions() { return _subscriptions; }
+ List<UOrbPublicationBase *> & getPublications() { return _publications; }
+ List<BlockParamBase *> & getParams() { return _params; }
+// attributes
+ const char *_name;
+ SuperBlock *_parent;
+ float _dt;
+ List<UOrbSubscriptionBase *> _subscriptions;
+ List<UOrbPublicationBase *> _publications;
+ List<BlockParamBase *> _params;
+};
+
+class __EXPORT SuperBlock :
+ public Block
+{
+public:
+ friend class Block;
+// methods
+ SuperBlock(SuperBlock *parent, const char *name) :
+ Block(parent, name),
+ _children() {
+ }
+ virtual ~SuperBlock() {};
+ virtual void setDt(float dt);
+ virtual void updateParams() {
+ Block::updateParams();
+
+ if (getChildren().getHead() != NULL) updateChildParams();
+ }
+ virtual void updateSubscriptions() {
+ Block::updateSubscriptions();
+
+ if (getChildren().getHead() != NULL) updateChildSubscriptions();
+ }
+ virtual void updatePublications() {
+ Block::updatePublications();
+
+ if (getChildren().getHead() != NULL) updateChildPublications();
+ }
+protected:
+// methods
+ List<Block *> & getChildren() { return _children; }
+ void updateChildParams();
+ void updateChildSubscriptions();
+ void updateChildPublications();
+// attributes
+ List<Block *> _children;
+};
+
+} // namespace control
diff --git a/src/modules/controllib/block/BlockParam.cpp b/src/modules/controllib/block/BlockParam.cpp
new file mode 100644
index 000000000..fd12e365d
--- /dev/null
+++ b/src/modules/controllib/block/BlockParam.cpp
@@ -0,0 +1,79 @@
+/****************************************************************************
+ *
+ * Copyright (C) 2012 PX4 Development Team. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name PX4 nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ****************************************************************************/
+
+/**
+ * @file Blockparam.cpp
+ *
+ * Controller library code
+ */
+
+#include <math.h>
+#include <stdio.h>
+#include <string.h>
+
+#include "BlockParam.hpp"
+
+namespace control
+{
+
+BlockParamBase::BlockParamBase(Block *parent, const char *name, bool parent_prefix) :
+ _handle(PARAM_INVALID)
+{
+ char fullname[blockNameLengthMax];
+
+ if (parent == NULL) {
+ strncpy(fullname, name, blockNameLengthMax);
+
+ } else {
+ char parentName[blockNameLengthMax];
+ parent->getName(parentName, blockNameLengthMax);
+
+ if (!strcmp(name, "")) {
+ strncpy(fullname, parentName, blockNameLengthMax);
+
+ } else if (parent_prefix) {
+ snprintf(fullname, blockNameLengthMax, "%s_%s", parentName, name);
+ } else {
+ strncpy(fullname, name, blockNameLengthMax);
+ }
+
+ parent->getParams().add(this);
+ }
+
+ _handle = param_find(fullname);
+
+ if (_handle == PARAM_INVALID)
+ printf("error finding param: %s\n", fullname);
+};
+
+} // namespace control
diff --git a/src/modules/controllib/block/BlockParam.hpp b/src/modules/controllib/block/BlockParam.hpp
new file mode 100644
index 000000000..58a9bfc0d
--- /dev/null
+++ b/src/modules/controllib/block/BlockParam.hpp
@@ -0,0 +1,90 @@
+/****************************************************************************
+ *
+ * Copyright (C) 2012 PX4 Development Team. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name PX4 nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ****************************************************************************/
+
+/**
+ * @file BlockParam.h
+ *
+ * Controller library code
+ */
+
+#pragma once
+
+#include <systemlib/param/param.h>
+
+#include "Block.hpp"
+#include "List.hpp"
+
+namespace control
+{
+
+/**
+ * A base class for block params that enables traversing linked list.
+ */
+class __EXPORT BlockParamBase : public ListNode<BlockParamBase *>
+{
+public:
+ /**
+ * Instantiate a block param base.
+ *
+ * @param parent_prefix Set to true to include the parent name in the parameter name
+ */
+ BlockParamBase(Block *parent, const char *name, bool parent_prefix=true);
+ virtual ~BlockParamBase() {};
+ virtual void update() = 0;
+ const char *getName() { return param_name(_handle); }
+protected:
+ param_t _handle;
+};
+
+/**
+ * Parameters that are tied to blocks for updating and nameing.
+ */
+template<class T>
+class __EXPORT BlockParam : public BlockParamBase
+{
+public:
+ BlockParam(Block *block, const char *name, bool parent_prefix=true) :
+ BlockParamBase(block, name, parent_prefix),
+ _val() {
+ update();
+ }
+ T get() { return _val; }
+ void set(T val) { _val = val; }
+ void update() {
+ if (_handle != PARAM_INVALID) param_get(_handle, &_val);
+ }
+protected:
+ T _val;
+};
+
+} // namespace control
diff --git a/src/modules/controllib/block/List.hpp b/src/modules/controllib/block/List.hpp
new file mode 100644
index 000000000..96b0b94d1
--- /dev/null
+++ b/src/modules/controllib/block/List.hpp
@@ -0,0 +1,71 @@
+/****************************************************************************
+ *
+ * Copyright (C) 2012 PX4 Development Team. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name PX4 nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ****************************************************************************/
+
+/**
+ * @file Node.h
+ *
+ * A node of a linked list.
+ */
+
+#pragma once
+
+template<class T>
+class __EXPORT ListNode
+{
+public:
+ ListNode() : _sibling(NULL) {
+ }
+ void setSibling(T sibling) { _sibling = sibling; }
+ T getSibling() { return _sibling; }
+ T get() {
+ return _sibling;
+ }
+protected:
+ T _sibling;
+};
+
+template<class T>
+class __EXPORT List
+{
+public:
+ List() : _head() {
+ }
+ void add(T newNode) {
+ newNode->setSibling(getHead());
+ setHead(newNode);
+ }
+ T getHead() { return _head; }
+private:
+ void setHead(T &head) { _head = head; }
+ T _head;
+};
diff --git a/src/modules/controllib/block/UOrbPublication.cpp b/src/modules/controllib/block/UOrbPublication.cpp
new file mode 100644
index 000000000..f69b39d90
--- /dev/null
+++ b/src/modules/controllib/block/UOrbPublication.cpp
@@ -0,0 +1,39 @@
+/****************************************************************************
+ *
+ * Copyright (C) 2012 PX4 Development Team. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name PX4 nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ****************************************************************************/
+
+/**
+ * @file UOrbPublication.cpp
+ *
+ */
+
+#include "UOrbPublication.hpp"
diff --git a/src/modules/controllib/block/UOrbPublication.hpp b/src/modules/controllib/block/UOrbPublication.hpp
new file mode 100644
index 000000000..a36f4429f
--- /dev/null
+++ b/src/modules/controllib/block/UOrbPublication.hpp
@@ -0,0 +1,118 @@
+/****************************************************************************
+ *
+ * Copyright (C) 2012 PX4 Development Team. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name PX4 nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ****************************************************************************/
+
+/**
+ * @file UOrbPublication.h
+ *
+ */
+
+#pragma once
+
+#include <uORB/uORB.h>
+#include "Block.hpp"
+#include "List.hpp"
+
+
+namespace control
+{
+
+class Block;
+
+/**
+ * Base publication warapper class, used in list traversal
+ * of various publications.
+ */
+class __EXPORT UOrbPublicationBase : public ListNode<control::UOrbPublicationBase *>
+{
+public:
+
+ UOrbPublicationBase(
+ List<UOrbPublicationBase *> * list,
+ const struct orb_metadata *meta) :
+ _meta(meta),
+ _handle() {
+ if (list != NULL) list->add(this);
+ }
+ void update() {
+ orb_publish(getMeta(), getHandle(), getDataVoidPtr());
+ }
+ virtual void *getDataVoidPtr() = 0;
+ virtual ~UOrbPublicationBase() {
+ orb_unsubscribe(getHandle());
+ }
+ const struct orb_metadata *getMeta() { return _meta; }
+ int getHandle() { return _handle; }
+protected:
+ void setHandle(orb_advert_t handle) { _handle = handle; }
+ const struct orb_metadata *_meta;
+ orb_advert_t _handle;
+};
+
+/**
+ * UOrb Publication wrapper class
+ */
+template<class T>
+class UOrbPublication :
+ public T, // this must be first!
+ public UOrbPublicationBase
+{
+public:
+ /**
+ * Constructor
+ *
+ * @param list A list interface for adding to list during construction
+ * @param meta The uORB metadata (usually from the ORB_ID() macro)
+ * for the topic.
+ */
+ UOrbPublication(
+ List<UOrbPublicationBase *> * list,
+ const struct orb_metadata *meta) :
+ T(), // initialize data structure to zero
+ UOrbPublicationBase(list, meta) {
+ // It is important that we call T()
+ // before we publish the data, so we
+ // call this here instead of the base class
+ setHandle(orb_advertise(getMeta(), getDataVoidPtr()));
+ }
+ virtual ~UOrbPublication() {}
+ /*
+ * XXX
+ * This function gets the T struct, assuming
+ * the struct is the first base class, this
+ * should use dynamic cast, but doesn't
+ * seem to be available
+ */
+ void *getDataVoidPtr() { return (void *)(T *)(this); }
+};
+
+} // namespace control
diff --git a/src/modules/controllib/block/UOrbSubscription.cpp b/src/modules/controllib/block/UOrbSubscription.cpp
new file mode 100644
index 000000000..022cadd24
--- /dev/null
+++ b/src/modules/controllib/block/UOrbSubscription.cpp
@@ -0,0 +1,51 @@
+/****************************************************************************
+ *
+ * Copyright (C) 2012 PX4 Development Team. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name PX4 nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ****************************************************************************/
+
+/**
+ * @file UOrbSubscription.cpp
+ *
+ */
+
+#include "UOrbSubscription.hpp"
+
+namespace control
+{
+
+bool __EXPORT UOrbSubscriptionBase::updated()
+{
+ bool isUpdated = false;
+ orb_check(_handle, &isUpdated);
+ return isUpdated;
+}
+
+} // namespace control
diff --git a/src/modules/controllib/block/UOrbSubscription.hpp b/src/modules/controllib/block/UOrbSubscription.hpp
new file mode 100644
index 000000000..22cc2e114
--- /dev/null
+++ b/src/modules/controllib/block/UOrbSubscription.hpp
@@ -0,0 +1,137 @@
+/****************************************************************************
+ *
+ * Copyright (C) 2012 PX4 Development Team. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name PX4 nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ****************************************************************************/
+
+/**
+ * @file UOrbSubscription.h
+ *
+ */
+
+#pragma once
+
+#include <uORB/uORB.h>
+#include "Block.hpp"
+#include "List.hpp"
+
+
+namespace control
+{
+
+class Block;
+
+/**
+ * Base subscription warapper class, used in list traversal
+ * of various subscriptions.
+ */
+class __EXPORT UOrbSubscriptionBase :
+ public ListNode<control::UOrbSubscriptionBase *>
+{
+public:
+// methods
+
+ /**
+ * Constructor
+ *
+ * @param meta The uORB metadata (usually from the ORB_ID() macro)
+ * for the topic.
+ */
+ UOrbSubscriptionBase(
+ List<UOrbSubscriptionBase *> * list,
+ const struct orb_metadata *meta) :
+ _meta(meta),
+ _handle() {
+ if (list != NULL) list->add(this);
+ }
+ bool updated();
+ void update() {
+ if (updated()) {
+ orb_copy(_meta, _handle, getDataVoidPtr());
+ }
+ }
+ virtual void *getDataVoidPtr() = 0;
+ virtual ~UOrbSubscriptionBase() {
+ orb_unsubscribe(_handle);
+ }
+// accessors
+ const struct orb_metadata *getMeta() { return _meta; }
+ int getHandle() { return _handle; }
+protected:
+// accessors
+ void setHandle(int handle) { _handle = handle; }
+// attributes
+ const struct orb_metadata *_meta;
+ int _handle;
+};
+
+/**
+ * UOrb Subscription wrapper class
+ */
+template<class T>
+class __EXPORT UOrbSubscription :
+ public T, // this must be first!
+ public UOrbSubscriptionBase
+{
+public:
+ /**
+ * Constructor
+ *
+ * @param list A list interface for adding to list during construction
+ * @param meta The uORB metadata (usually from the ORB_ID() macro)
+ * for the topic.
+ * @param interval The minimum interval in milliseconds between updates
+ */
+ UOrbSubscription(
+ List<UOrbSubscriptionBase *> * list,
+ const struct orb_metadata *meta, unsigned interval) :
+ T(), // initialize data structure to zero
+ UOrbSubscriptionBase(list, meta) {
+ setHandle(orb_subscribe(getMeta()));
+ orb_set_interval(getHandle(), interval);
+ }
+
+ /**
+ * Deconstructor
+ */
+ virtual ~UOrbSubscription() {}
+
+ /*
+ * XXX
+ * This function gets the T struct, assuming
+ * the struct is the first base class, this
+ * should use dynamic cast, but doesn't
+ * seem to be available
+ */
+ void *getDataVoidPtr() { return (void *)(T *)(this); }
+ T getData() { return T(*this); }
+};
+
+} // namespace control