app1.cpp
Go to the documentation of this file.
1 
23 #include "app1/app1.h"
24 
25 // Register the application
27 
28 namespace app1{
29 
30  struct XY
31  {
32  float x;
33  float y;
34  };
35 
36  App1::App1() {}
37 
38  App1::~App1() {}
39 
40  void App1::stop() {}
41 
42  void App1::init()
43  {
44  //set parameters
45  delta = 7;
46  epsilon = 150;
47  }
48 
49  float App1::force_mag(float dist)
50  {
51  return -(epsilon/(dist+0.1)) *(pow(delta/(dist+0.1), 4) - pow(delta/(dist+0.1), 2));
52  }
53 
54  XY App1::force_sum(micros_swarm::NeighborBase n, XY &s)
55  {
56  micros_swarm::Base l = get_base();
57  float xl = l.x;
58  float yl = l.y;
59 
60  float xn = n.x;
61  float yn = n.y;
62 
63  float dist = sqrt(pow((xl-xn),2)+pow((yl-yn),2));
64 
65  float fm = force_mag(dist)/1000;
66  if(fm>0.5){
67  fm = 0.5;
68  }
69 
70  float fx = (fm/(dist+0.1))*(xn-xl);
71  float fy = (fm/(dist+0.1))*(yn-yl);
72 
73  s.x+=fx;
74  s.y+=fy;
75  return s;
76  }
77 
78  XY App1::direction()
79  {
80  XY sum;
81  sum.x = 0;
82  sum.y = 0;
83 
85  //n.print();
86  boost::function<XY(micros_swarm::NeighborBase, XY &)> bf = boost::bind(&App1::force_sum, this, _1, _2);
87  sum = n.reduce(bf, sum);
88 
89  return sum;
90  }
91 
92  void App1::publish_cmd(const ros::TimerEvent&)
93  {
94  XY v = direction();
95  geometry_msgs::Twist t;
96  t.linear.x = v.x;
97  t.linear.y = v.y;
98 
99  pub.publish(t);
100  }
101 
102  void App1::motion()
103  {
104  ros::NodeHandle nh;
105  timer = nh.createTimer(ros::Duration(0.1), &App1::publish_cmd, this);
106  }
107 
108  void App1::baseCallback(const nav_msgs::Odometry& lmsg)
109  {
110  float x = lmsg.pose.pose.position.x;
111  float y = lmsg.pose.pose.position.y;
112 
113  float vx = lmsg.twist.twist.linear.x;
114  float vy = lmsg.twist.twist.linear.y;
115 
116  micros_swarm::Base l(x, y, 0, vx, vy, 0, 1);
117  set_base(l);
118  }
119 
120  void App1::start()
121  {
122  init();
123 
124  ros::NodeHandle nh;
125  set_dis(8);
126  pub = nh.advertise<geometry_msgs::Twist>("cmd_vel", 1000);
127  sub = nh.subscribe("base_pose_ground_truth", 1000, &App1::baseCallback, this, ros::TransportHints().udp());
128 
129  motion();
130  }
131 };
132 
double epsilon
float x
Definition: app1.cpp:32
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)
Definition: app1.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)
float y
Definition: app1.cpp:33
#define PLUGINLIB_EXPORT_CLASS(class_type, base_class_type)
T2 reduce(T2(*f)(Type, T2 &), T2 &t2)


app1
Author(s):
autogenerated on Mon Jun 10 2019 14:02:12