manual_node.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2015-2018 Michele Colledanchise - All Rights Reserved
2  * Copyright (C) 2018-2020 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 
16 #include <ncurses.h>
17 
18 namespace BT
19 {
20 
21 
22 ManualSelectorNode::ManualSelectorNode(const std::string& name, const NodeConfiguration& config)
23  : ControlNode::ControlNode(name, config )
24  , running_child_idx_(-1)
25  , previously_executed_idx_(-1)
26 {
27  setRegistrationID("ManualSelector");
28 }
29 
31 {
32  if( running_child_idx_ >= 0 )
33  {
34  haltChild( size_t(running_child_idx_) );
35  }
36  running_child_idx_ = -1;
38 }
39 
41 {
42  const size_t children_count = children_nodes_.size();
43 
44  if( children_count == 0 )
45  {
46  return selectStatus();
47  }
48 
49  bool repeat_last = false;
50  getInput(REPEAT_LAST_SELECTION, repeat_last);
51 
52  int idx = 0;
53 
54  if( repeat_last && previously_executed_idx_ >= 0)
55  {
57  }
58  else{
60  idx = selectChild();
62 
63  if( idx == NUM_SUCCESS ){
64  return NodeStatus::SUCCESS;
65  }
66  if( idx == NUM_FAILURE ){
67  return NodeStatus::FAILURE;
68  }
69  if( idx == NUM_RUNNING ){
70  return NodeStatus::RUNNING;
71  }
72  }
73 
74  NodeStatus ret = children_nodes_[idx]->executeTick();
75  if(ret == NodeStatus::RUNNING)
76  {
77  running_child_idx_ = idx;
78  }
79  return ret;
80 }
81 
83 {
84  WINDOW *win;
85  initscr();
86  cbreak();
87 
88  win = newwin( 6, 70, 1, 1 ); // create a new window
89 
90  mvwprintw( win, 0, 0, "No children." );
91  mvwprintw( win, 1, 0, "Press: S to return SUCCESFULL," );
92  mvwprintw( win, 2, 0, " F to return FAILURE, or" );
93  mvwprintw( win, 3, 0, " R to return RUNNING." );
94 
95  wrefresh( win ); // update the terminal screen
96  noecho(); // disable echoing of characters on the screen
97  keypad( win, TRUE ); // enable keyboard input for the window.
98  curs_set( 0 ); // hide the default screen cursor.
99 
100  int ch = 0;
101  NodeStatus ret;
102  while(1)
103  {
104  if( ch == 's' || ch == 'S')
105  {
106  ret = NodeStatus::SUCCESS;
107  break;
108  }
109  else if( ch == 'f' || ch == 'F')
110  {
111  ret = NodeStatus::FAILURE;
112  break;
113  }
114  else if( ch == 'r' || ch == 'R')
115  {
116  ret = NodeStatus::RUNNING;
117  break;
118  }
119  ch = wgetch(win);
120  }
121  werase( win ) ;
122  wrefresh( win );
123  delwin( win );
124  endwin();
125  return ret;
126 }
127 
129 {
130  const size_t children_count = children_nodes_.size();
131 
132  std::vector<std::string> list;
133  list.reserve(children_count);
134  for(const auto& child: children_nodes_)
135  {
136  list.push_back(child->name());
137  }
138 
139  size_t width = 10;
140  for(const auto& str: list) {
141  width = std::max(width, str.size()+2);
142  }
143 
144  WINDOW *win;
145  initscr();
146  cbreak();
147 
148  win = newwin( children_count+6, 70, 1, 1 ); // create a new window
149 
150  mvwprintw( win, 0, 0, "Use UP/DOWN arrow to select the child, Enter to confirm." );
151  mvwprintw( win, 1, 0, "Press: S to skip and return SUCCESFULL," );
152  mvwprintw( win, 2, 0, " F to skip and return FAILURE, or" );
153  mvwprintw( win, 3, 0, " R to skip and return RUNNING." );
154 
155  // now print all the menu items and highlight the first one
156  for(size_t i=0; i<list.size(); i++ )
157  {
158  mvwprintw( win, i+5, 0, "%2d. %s", i+1, list[i].c_str() );
159  }
160 
161  wrefresh( win ); // update the terminal screen
162  noecho(); // disable echoing of characters on the screen
163  keypad( win, TRUE ); // enable keyboard input for the window.
164  curs_set( 0 ); // hide the default screen cursor.
165 
166  uint8_t row = 0;
167  int ch=0;
168  while(1)
169  {
170  // right pad with spaces to make the items appear with even width.
171  wattroff( win, A_STANDOUT );
172  mvwprintw( win, row+5, 4, "%s", list[row].c_str() );
173  // use a variable to increment or decrement the value based on the input.
174  if( ch == KEY_DOWN )
175  {
176  row = (row == children_count-1) ? 0 : row+1;
177  }
178  else if( ch == KEY_UP )
179  {
180  row = ( row == 0) ? (children_count-1) : row-1;
181  }
182  else if( ch == KEY_ENTER || ch == 10 )
183  {
184  break;
185  }
186  else if( ch == 's' || ch == 'S')
187  {
188  row = NUM_SUCCESS;
189  break;
190  }
191  else if( ch == 'f' || ch == 'F')
192  {
193  row = NUM_FAILURE;
194  break;
195  }
196  else if( ch == 'r' || ch == 'R')
197  {
198  row = NUM_RUNNING;
199  break;
200  }
201 
202  // now highlight the next item in the list.
203  wattron( win, A_STANDOUT );
204  mvwprintw( win, row+5, 4, "%s", list[row].c_str());
205  ch = wgetch(win);
206  };
207 
208  werase( win ) ;
209  wrefresh( win );
210  delwin( win );
211  endwin();
212  return row;
213 }
214 
215 }
virtual void halt() override
Definition: manual_node.cpp:30
const std::string & name() const
Name of the instance, not the type.
Definition: tree_node.cpp:73
std::vector< TreeNode * > children_nodes_
Definition: control_node.h:25
virtual void halt() override
static constexpr const char * REPEAT_LAST_SELECTION
Definition: manual_node.h:40
ManualSelectorNode(const std::string &name, const NodeConfiguration &config)
Definition: manual_node.cpp:22
const TreeNode * child(size_t index) const
Definition: control_node.h:39
Result getInput(const std::string &key, T &destination) const
Definition: tree_node.h:192
NodeStatus selectStatus() const
Definition: manual_node.cpp:82
void setRegistrationID(StringView ID)
Definition: tree_node.h:163
uint8_t selectChild() const
void haltChild(size_t i)
NodeStatus
Definition: basic_types.h:35
virtual BT::NodeStatus tick() override
Method to be implemented by the user.
Definition: manual_node.cpp:40
void setStatus(NodeStatus new_status)
Definition: tree_node.cpp:40


behaviotree_cpp_v3
Author(s): Michele Colledanchise, Davide Faconti
autogenerated on Tue May 4 2021 02:56:25