BehaviorTree
Core Library to create and execute Behavior Trees
Loading...
Searching...
No Matches
repeat_node.h
1/* Copyright (C) 2015-2018 Michele Colledanchise - All Rights Reserved
2 * Copyright (C) 2018-2025 Davide Faconti, Eurecat - All Rights Reserved
3*
4* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
5* to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
6* and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
7* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8*
9* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
10* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
11* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12*/
13
14#pragma once
15
16#include "behaviortree_cpp/decorator_node.h"
17
18namespace BT
19{
20/**
21 * @brief The RepeatNode is used to execute a child several times, as long
22 * as it succeed.
23 *
24 * To succeed, the child must return SUCCESS N times (port "num_cycles").
25 *
26 * If the child returns FAILURE, the loop is stopped and this node
27 * returns FAILURE.
28 *
29 * Example:
30 *
31 * <Repeat num_cycles="3">
32 * <ClapYourHandsOnce/>
33 * </Repeat>
34 */
35class RepeatNode : public DecoratorNode
36{
37public:
38 RepeatNode(const std::string& name, int NTries);
39
40 RepeatNode(const std::string& name, const NodeConfig& config);
41
42 ~RepeatNode() override = default;
43
44 RepeatNode(const RepeatNode&) = delete;
45 RepeatNode& operator=(const RepeatNode&) = delete;
46 RepeatNode(RepeatNode&&) = delete;
47 RepeatNode& operator=(RepeatNode&&) = delete;
48
49 static PortsList providedPorts()
50 {
51 return { InputPort<int>(NUM_CYCLES, "Repeat a successful child up to N times. "
52 "Use -1 to create an infinite loop.") };
53 }
54
55private:
56 int num_cycles_;
57 int repeat_count_;
58
59 bool read_parameter_from_ports_;
60 static constexpr const char* NUM_CYCLES = "num_cycles";
61
62 virtual NodeStatus tick() override;
63
64 void halt() override;
65};
66
67} // namespace BT
The DecoratorNode is the base class for nodes that have exactly one child.
Definition: decorator_node.h:19
The RepeatNode is used to execute a child several times, as long as it succeed.
Definition: repeat_node.h:36
Definition: action_node.h:24
NodeStatus
Definition: basic_types.h:34
Definition: tree_node.h:105