random_model_controller.cpp
Go to the documentation of this file.
1 #include <algorithm>
2 #include <ctime>
3 #include <vector>
4 
6 #include <radial_menu_msgs/State.h>
7 #include <ros/console.h>
8 #include <ros/init.h>
9 #include <ros/node_handle.h>
10 #include <ros/publisher.h>
11 #include <ros/rate.h>
12 
13 namespace rmm = radial_menu_model;
14 
15 // **************************
16 // Operations on a menu model
17 // **************************
18 
20  std::vector< rmm::ItemConstPtr > sibilings(level->sibilings());
21  std::random_shuffle(sibilings.begin(), sibilings.end());
22  for (const rmm::ItemConstPtr &s : sibilings) {
23  if (s) {
24  return s;
25  }
26  }
27  return rmm::ItemConstPtr();
28 }
29 
30 bool point(rmm::Model *const model) {
32  if (model->canPoint(s)) {
33  ROS_INFO_STREAM("Pointing '" << s->name() << "' ...");
34  model->point(s);
35  return true;
36  } else {
37  return false;
38  }
39 }
40 
41 bool unpoint(rmm::Model *const model) {
43  if (model->canUnpoint(s)) {
44  ROS_INFO_STREAM("Unpointing '" << s->name() << "' ...");
45  model->unpoint(s);
46  return true;
47  } else {
48  return false;
49  }
50 }
51 
52 bool select(rmm::Model *const model) {
54  if (model->canSelect(s)) {
55  ROS_INFO_STREAM("Selecting '" << s->name() << "' ...");
56  model->select(s, /* allow_multi_selection = */ false);
57  return true;
58  } else {
59  return false;
60  }
61 }
62 
63 bool deselect(rmm::Model *const model) {
65  if (model->canDeselect(s)) {
66  ROS_INFO_STREAM("Deselecting '" << s->name() << "' ...");
67  model->deselect(s);
68  return true;
69  } else {
70  return false;
71  }
72 }
73 
74 bool descend(rmm::Model *const model) {
76  if (model->canDescend(s)) {
77  ROS_INFO_STREAM("Descending from '" << s->name() << "' ...");
78  model->descend(s, /* allow_multi_selection = */ false);
79  return true;
80  } else {
81  return false;
82  }
83 }
84 
85 bool ascend(rmm::Model *const model) {
86  if (model->canAscend()) {
87  ROS_INFO("Ascending ...");
88  model->ascend();
89  return true;
90  } else {
91  return false;
92  }
93 }
94 
95 // *****************
96 // The main function
97 // *****************
98 
99 int main(int argc, char *argv[]) {
100  ros::init(argc, argv, "random_model_controller");
101  ros::NodeHandle nh;
102 
103  //
104  ros::Publisher state_pub(nh.advertise< radial_menu_msgs::State >("menu_state", 1, true));
105 
106  // load a menu from param
108  if (!model.setDescriptionFromParam("menu_description")) {
109  return 1;
110  }
111  model.setEnabled(true);
112  ROS_INFO_STREAM("Model:\n" << model.toString());
113  ROS_INFO_STREAM("State:\n" << *model.exportState());
114 
115  // perform random operations on the menu model
116  typedef bool (*Operation)(rmm::Model *const);
117  std::array< Operation, 6 > operations = {point, unpoint, select, deselect, descend, ascend};
118  ros::Rate rate(1.);
119  while (ros::ok()) {
120  // shuffle operation order and try one by one until the first success
121  std::random_shuffle(operations.begin(), operations.end());
122  for (const Operation op : operations) {
123  if ((*op)(&model)) {
124  ROS_INFO_STREAM("Model:\n" << model.toString());
125  ROS_INFO_STREAM("State:\n" << *model.exportState());
126  break;
127  }
128  }
129  //
130  state_pub.publish(model.exportState());
131  rate.sleep();
132  }
133 
134  return 0;
135 }
std::shared_ptr< const Item > ItemConstPtr
XmlRpcServer s
rmm::ItemConstPtr randomSibiling(const rmm::ItemConstPtr &level)
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
ItemConstPtr currentLevel() const
bool canUnpoint(const ItemConstPtr &item) const
bool point(rmm::Model *const model)
bool setDescriptionFromParam(const std::string &param_name)
bool canDescend(const ItemConstPtr &item) const
bool unpoint(rmm::Model *const model)
void publish(const boost::shared_ptr< M > &message) const
radial_menu_msgs::StatePtr exportState(const ros::Time stamp=ros::Time::now()) const
#define ROS_INFO(...)
ROSCPP_DECL bool ok()
radial_menu_model::Model model
bool ascend(rmm::Model *const model)
void select(const ItemConstPtr &item, const bool allow_multi_selection)
bool descend(rmm::Model *const model)
bool deselect(rmm::Model *const model)
int main(int argc, char *argv[])
std::string toString() const
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
void unpoint(const ItemConstPtr &item)
bool canPoint(const ItemConstPtr &item) const
bool sleep()
void deselect(const ItemConstPtr &item)
#define ROS_INFO_STREAM(args)
bool select(rmm::Model *const model)
bool canSelect(const ItemConstPtr &item) const
void descend(const ItemConstPtr &item, const bool allow_multi_selection)
void point(const ItemConstPtr &item)
bool canDeselect(const ItemConstPtr &item) const
void setEnabled(const bool is_enabled)


radial_menu_example
Author(s):
autogenerated on Mon Feb 28 2022 23:22:11