Go to the documentation of this file.00001
00023 #include "app1/app1.h"
00024
00025
00026 PLUGINLIB_EXPORT_CLASS(app1::App1, micros_swarm::Application)
00027
00028 namespace app1{
00029
00030 struct XY
00031 {
00032 float x;
00033 float y;
00034 };
00035
00036 App1::App1() {}
00037
00038 App1::~App1() {}
00039
00040 void App1::stop() {}
00041
00042 void App1::init()
00043 {
00044
00045 delta = 7;
00046 epsilon = 150;
00047 }
00048
00049 float App1::force_mag(float dist)
00050 {
00051 return -(epsilon/(dist+0.1)) *(pow(delta/(dist+0.1), 4) - pow(delta/(dist+0.1), 2));
00052 }
00053
00054 XY App1::force_sum(micros_swarm::NeighborBase n, XY &s)
00055 {
00056 micros_swarm::Base l = get_base();
00057 float xl = l.x;
00058 float yl = l.y;
00059
00060 float xn = n.x;
00061 float yn = n.y;
00062
00063 float dist = sqrt(pow((xl-xn),2)+pow((yl-yn),2));
00064
00065 float fm = force_mag(dist)/1000;
00066 if(fm>0.5){
00067 fm = 0.5;
00068 }
00069
00070 float fx = (fm/(dist+0.1))*(xn-xl);
00071 float fy = (fm/(dist+0.1))*(yn-yl);
00072
00073 s.x+=fx;
00074 s.y+=fy;
00075 return s;
00076 }
00077
00078 XY App1::direction()
00079 {
00080 XY sum;
00081 sum.x = 0;
00082 sum.y = 0;
00083
00084 micros_swarm::Neighbors<micros_swarm::NeighborBase> n(true);
00085
00086 boost::function<XY(micros_swarm::NeighborBase, XY &)> bf = boost::bind(&App1::force_sum, this, _1, _2);
00087 sum = n.reduce(bf, sum);
00088
00089 return sum;
00090 }
00091
00092 void App1::publish_cmd(const ros::TimerEvent&)
00093 {
00094 XY v = direction();
00095 geometry_msgs::Twist t;
00096 t.linear.x = v.x;
00097 t.linear.y = v.y;
00098
00099 pub.publish(t);
00100 }
00101
00102 void App1::motion()
00103 {
00104 ros::NodeHandle nh;
00105 timer = nh.createTimer(ros::Duration(0.1), &App1::publish_cmd, this);
00106 }
00107
00108 void App1::baseCallback(const nav_msgs::Odometry& lmsg)
00109 {
00110 float x = lmsg.pose.pose.position.x;
00111 float y = lmsg.pose.pose.position.y;
00112
00113 float vx = lmsg.twist.twist.linear.x;
00114 float vy = lmsg.twist.twist.linear.y;
00115
00116 micros_swarm::Base l(x, y, 0, vx, vy, 0, 1);
00117 set_base(l);
00118 }
00119
00120 void App1::start()
00121 {
00122 init();
00123
00124 ros::NodeHandle nh;
00125 set_dis(8);
00126 pub = nh.advertise<geometry_msgs::Twist>("cmd_vel", 1000);
00127 sub = nh.subscribe("base_pose_ground_truth", 1000, &App1::baseCallback, this, ros::TransportHints().udp());
00128
00129 motion();
00130 }
00131 };
00132