-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenerico.cpp
192 lines (108 loc) · 3.22 KB
/
generico.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#include <test/trajectory.h>
#include <iostream>
#include <numeric>
#include <ros/ros.h>
#include <geometry_msgs/PoseStamped.h>
#include <vector>
#include <algorithm>
#include <functional>
using namespace std;
//calcola i coefficienti del polinomio passante per n punti.
void Trajectory::set_coeff(Eigen::VectorXd x, Eigen::VectorXd y, Eigen::VectorXd z, Eigen::VectorXd t)
{
int row_degree=n+4;
int column_degree=n+4; // ho n punti + 4 condizioni (2 acc e due vel) -1. grado polinomio (n+3)
int i=0; //indicatore righe
int j=0; //indicatore colonne
// vettore che tiene conto delle posizioni,velocità e accelerazioni
//Definisco la matrice di Vandermonde
MatrixXd V(row_degree,column_degree);
// parti della matrice con valori numerici
for(i=0; i<n ; i++){
V(i,0)=1;
}
for(i=n; i<n+4 ; i++){
V(i,0)=0;
}
for(i=n; i<n+2; i++){
V(i,1)=1;
}
for(i=n+2; i<n+4 ; i++){
V(i,1)=0;
}
for(i=n+2; i<n+4 ; i++){
V(i,3)=2;
}
// Matrice relativa alle posizioni.
for(i=0; i< n; i++){
for(j=1; j < n+4; j++){
}
V(i,j)=pow(t(i),j);
}
//Matrice relativa alle velocità
for(i=n; i< n+2; i++){
for(j=2; j < n+4; j++){
}
V(i,j)=j*pow(t(i-n),j-1);
}
// Matrice relativa alle accelerazioni
for(i=n+2; i<n+4; i++){
for(j=3; j < n+4; j++){
}
V(i,j)=j*(j-1)*pow(t(i-n-2),j-2);
}
// calcolo dei coef del polinomio
cout << "Determinante matrice di Vandermonde " << V.determinant() << endl;
cout << "Inversa della matrice\n" << V.inverse() << endl;
coeff_x=V.inverse*x;
coeff_y=V.inverse*y;
coeff_z=V.inverse*z;
}
// calcola la posizione tramite coefficienti calcolati dalla set.
void Trajectory::get_points(double* x, double* y, double* z, double t)
{
x = std::inner_product(std::begin(coeff_x),std::end(coeff_x), t.begin(), 0);
y = std::inner_product(std::begin(coeff_y),std::end(coeff_y), t.begin(), 0);
z = std::inner_product(std::begin(coeff_z),std::end(coeff_z), t.begin(), 0);
}
struct Waypoint{
double t;
} waypoint[3];
int main(int argc, char **argv)
{
Trajectory traj;
traj.n=3;
double posx;
double posy;
double posz;
double t_i;
Eigen::VectorXd pf={0,0,0};
Eigen::VectorXd ps={0,0,5};
Eigen::VectorXd pt={2,0,5};
Eigen::VectorXd tempo(3);
Eigen::VectorXd x_vect={pf(0),ps(0),pt(0),0,0,0};
Eigen::VectorXd y_vect={pf(1),ps(1),pt(1),0,0,0};
Eigen::VectorXd z_vect={pf(2),ps(2),pt(2),0,0,0};
ros::init(argc, argv, "trajectory_node");
ros::NodeHandle nh;
ROS_INFO("Test_info");
ros::Publisher local_pos_pub = nh.advertise<geometry_msgs::PoseStamped>
("mavros/setpoint_position/local", 10);
ros::Rate rate(20.0);
geometry_msgs::PoseStamped pose;
ros::Time last_request = ros::Time::now();
double secs =last_request.toSec();
waypoint.time[0]=secs;
ros::Duration d;
double d_secs = d.toSec();
d_secs=0.1;
waypoint.time[1]=secs+d_secs;
traj.set_coeff(x_vect,y_vect,z_vect,tempo);
traj.get_points(posx,posy,posz,t_i);
while(ros::ok()){
local_pos_pub.publish(pose); //pubblica nel topic
ros::spinOnce();
rate.sleep();
}
return 0;
}