app2.cpp
Go to the documentation of this file.
1 
23 #include "app2/app2.h"
24 
25 #define BARRIER_VSTIG 1
26 #define ROBOT_SUM 20
27 
28 #define RED_SWARM 1
29 #define BLUE_SWARM 2
30 
31 // Register the application
33 
34 namespace app2{
35 
36  struct XY
37  {
38  float x;
39  float y;
40  };
41 
42  App2::App2() {}
43 
44  App2::~App2() {}
45 
46  void App2::stop() {}
47 
48  void App2::init()
49  {
50  //set parameters
51  delta_kin = 5;
52  epsilon_kin = 100;
53 
54  delta_nonkin = 25;
55  epsilon_nonkin = 1000;
56  }
57 
58  float App2::force_mag_kin(float dist)
59  {
60  return -(epsilon_kin/(dist+0.1)) *(pow(delta_kin/(dist+0.1), 4) - pow(delta_kin/(dist+0.1), 2));
61  }
62 
63  float App2::force_mag_nonkin(float dist)
64  {
65  return -(epsilon_nonkin/(dist+0.1)) *(pow(delta_nonkin/(dist+0.1), 4) - pow(delta_nonkin/(dist+0.1), 2));
66  }
67 
68  XY App2::force_sum_kin(micros_swarm::NeighborBase n, XY &s)
69  {
70  micros_swarm::Base l = get_base();
71  float xl = l.x;
72  float yl = l.y;
73 
74  float xn = n.x;
75  float yn = n.y;
76 
77  float dist = sqrt(pow((xl-xn),2)+pow((yl-yn),2));
78 
79  float fm = force_mag_kin(dist)/1000;
80  if(fm>0.5) {
81  fm=0.5;
82  }
83 
84  float fx = (fm/(dist+0.1))*(xn-xl);
85  float fy = (fm/(dist+0.1))*(yn-yl);
86 
87  s.x += fx;
88  s.y += fy;
89  return s;
90  }
91 
92  XY App2::force_sum_nonkin(micros_swarm::NeighborBase n, XY &s)
93  {
94  micros_swarm::Base l = get_base();
95  float xl = l.x;
96  float yl = l.y;
97 
98  float xn = n.x;
99  float yn = n.y;
100 
101  float dist = sqrt(pow((xl-xn),2)+pow((yl-yn),2));
102 
103  float fm = force_mag_nonkin(dist)/1000;
104  if(fm>0.5) {
105  fm=0.5;
106  }
107 
108  float fx = (fm/(dist+0.1))*(xn-xl);
109  float fy = (fm/(dist+0.1))*(yn-yl);
110 
111  s.x += fx;
112  s.y += fy;
113  return s;
114  }
115 
116  XY App2::direction_red()
117  {
118  XY sum;
119  sum.x = 0;
120  sum.y = 0;
121 
123  boost::function<XY(micros_swarm::NeighborBase, XY &)> bf_kin = boost::bind(&App2::force_sum_kin, this, _1, _2);
124  boost::function<XY(micros_swarm::NeighborBase, XY &)> bf_nonkin = boost::bind(&App2::force_sum_nonkin, this, _1, _2);
125  sum = n.kin(RED_SWARM).reduce(bf_kin, sum);
126  sum = n.nonkin(RED_SWARM).reduce(bf_nonkin, sum);
127 
128  return sum;
129  }
130 
131  XY App2::direction_blue()
132  {
133  XY sum;
134  sum.x = 0;
135  sum.y = 0;
136 
138  boost::function<XY(micros_swarm::NeighborBase, XY &)> bf_kin = boost::bind(&App2::force_sum_kin, this, _1, _2);
139  boost::function<XY(micros_swarm::NeighborBase, XY &)> bf_nonkin = boost::bind(&App2::force_sum_nonkin, this, _1, _2);
140  sum = n.kin(BLUE_SWARM).reduce(bf_kin, sum);
141  sum = n.nonkin(BLUE_SWARM).reduce(bf_nonkin, sum);
142 
143  return sum;
144  }
145 
146  bool App2::red(int id)
147  {
148  if(id <= 9) {
149  return true;
150  }
151  return false;
152  }
153 
154  bool App2::blue(int id)
155  {
156  if(id >= 10) {
157  return true;
158  }
159  return false;
160  }
161 
162  void App2::publish_red_cmd(const ros::TimerEvent&)
163  {
164  XY v = direction_red();
165  geometry_msgs::Twist t;
166  t.linear.x = v.x;
167  t.linear.y = v.y;
168 
169  pub.publish(t);
170  }
171 
172  void App2::publish_blue_cmd(const ros::TimerEvent&)
173  {
174  XY v = direction_blue();
175  geometry_msgs::Twist t;
176  t.linear.x = v.x;
177  t.linear.y = v.y;
178 
179  pub.publish(t);
180  }
181 
182  void App2::motion_red()
183  {
184  ros::NodeHandle nh;
185  red_timer = nh.createTimer(ros::Duration(0.1), &App2::publish_red_cmd, this);
186  }
187 
188  void App2::motion_blue()
189  {
190  ros::NodeHandle nh;
191  blue_timer = nh.createTimer(ros::Duration(0.1), &App2::publish_blue_cmd, this);
192  }
193 
194  void App2::baseCallback(const nav_msgs::Odometry& lmsg)
195  {
196  float x = lmsg.pose.pose.position.x;
197  float y = lmsg.pose.pose.position.y;
198 
199  float vx = lmsg.twist.twist.linear.x;
200  float vy = lmsg.twist.twist.linear.y;
201 
202  micros_swarm::Base l(x, y, 0, vx, vy, 0, 1);
203  set_base(l);
204  }
205 
206  void App2::start()
207  {
208  init();
209 
210  ros::NodeHandle nh;
211  set_dis(40);
212  pub = nh.advertise<geometry_msgs::Twist>("cmd_vel", 1000);
213  sub = nh.subscribe("base_pose_ground_truth", 1000, &App2::baseCallback, this, ros::TransportHints().udp());
214 
215  boost::function<bool()> bfred = boost::bind(&App2::red, this, get_id());
216  boost::function<bool()> bfblue = boost::bind(&App2::blue, this, get_id());
217 
218  micros_swarm::Swarm red_swarm(RED_SWARM);
219  red_swarm.select(bfred);
220  micros_swarm::Swarm blue_swarm(BLUE_SWARM);
221  blue_swarm.select(bfblue);
222 
223  red_swarm.print();
224  blue_swarm.print();
225 
226  red_swarm.execute(boost::bind(&App2::motion_red, this));
227  blue_swarm.execute(boost::bind(&App2::motion_blue, this));
228  }
229 };
float y
Definition: app2.cpp:39
Neighbors< Type > nonkin(int swarm_id)
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
void init(const M_string &remappings)
float x
Definition: app2.cpp:38
void execute(const boost::function< void()> &f)
void print() const
Definition: app2.h:32
Timer createTimer(Rate r, Handler h, Obj o, bool oneshot=false, bool autostart=true) const
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
#define RED_SWARM
Definition: app2.cpp:28
void select(const boost::function< bool()> &bf)
Neighbors< Type > kin(int swarm_id)
#define BLUE_SWARM
Definition: app2.cpp:29
#define PLUGINLIB_EXPORT_CLASS(class_type, base_class_type)


app2
Author(s):
autogenerated on Mon Jun 10 2019 14:02:13