DroneXControllerService.h 22.7 KB
Newer Older
pragash1's avatar
pragash1 committed
1
//    Copyright (C) 2017, ETH Zurich, D-ITET, Paul Beuchat
2
3
//
//    This file is part of D-FaLL-System.
pragash1's avatar
pragash1 committed
4
//
5
6
7
8
//    D-FaLL-System is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
pragash1's avatar
pragash1 committed
9
//
10
11
12
13
//    D-FaLL-System is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
pragash1's avatar
pragash1 committed
14
//
15
16
//    You should have received a copy of the GNU General Public License
//    along with D-FaLL-System.  If not, see <http://www.gnu.org/licenses/>.
pragash1's avatar
pragash1 committed
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
//
//    ----------------------------------------------------------------------------------
//    DDDD        FFFFF        L     L           SSSS  Y   Y   SSSS  TTTTT  EEEEE  M   M
//    D   D       F      aaa   L     L          S       Y Y   S        T    E      MM MM
//    D   D  ---  FFFF  a   a  L     L     ---   SSS     Y     SSS     T    EEE    M M M
//    D   D       F     a  aa  L     L              S    Y        S    T    E      M   M
//    DDDD        F      aa a  LLLL  LLLL       SSSS     Y    SSSS     T    EEEEE  M   M
//
//
//    DESCRIPTION:
//    Place for students to implement their controller
//
//    ----------------------------------------------------------------------------------





//    ----------------------------------------------------------------------------------
//    III  N   N   CCCC  L      U   U  DDDD   EEEEE   SSSS
//     I   NN  N  C      L      U   U  D   D  E      S
//     I   N N N  C      L      U   U  D   D  EEE     SSS
//     I   N  NN  C      L      U   U  D   D  E          S
//    III  N   N   CCCC  LLLLL   UUU   DDDD   EEEEE  SSSS
//    ----------------------------------------------------------------------------------

// These various headers need to be included so that this controller service can be
// connected with the D-FaLL system.

//some useful libraries
#include <math.h>
#include <stdlib.h>
#include "ros/ros.h"
mastefan's avatar
mastefan committed
51
52
53
#include "ros/package.h"
#include "visualization_msgs/Marker.h"

54
55
56
57
58
59
60
61

//the generated structs from the msg-files have to be included
#include "d_fall_pps/ViconData.h"
#include "d_fall_pps/Setpoint.h"
#include "d_fall_pps/ControlCommand.h"
#include "d_fall_pps/Controller.h"
#include "d_fall_pps/DebugMsg.h"
#include "d_fall_pps/CustomButton.h"
maruggv's avatar
maruggv committed
62
#include "d_fall_pps/CrazyflieContext.h"
63
64
65
66
67

// Include the Parameter Service shared definitions
#include "nodes/ParameterServiceDefinitions.h"

#include <std_msgs/Int32.h>
pragash1's avatar
pragash1 committed
68
#include <std_msgs/Float32.h>
69
70
71
72
73





mastefan's avatar
mastefan committed
74

75
76
77
78
79
80
81
82
83
84
85
86
87
//    ----------------------------------------------------------------------------------
//    DDDD   EEEEE  FFFFF  III  N   N  EEEEE   SSSS
//    D   D  E      F       I   NN  N  E      S
//    D   D  EEE    FFF     I   N N N  EEE     SSS
//    D   D  E      F       I   N  NN  E          S
//    DDDD   EEEEE  F      III  N   N  EEEEE  SSSS
//    ----------------------------------------------------------------------------------

// These constants are defined to make the code more readable and adaptable.

// Universal constants
#define PI 3.1415926535

pragash1's avatar
pragash1 committed
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#define RAD2DEG 180.0/PI
#define DEG2RAD PI/180.0



// FOR WHICH BUTTON WAS PRESSED IN THE DRONEX CONTOLLER
#define DRONEX_BUTTON_GOTOSTART     1
#define DRONEX_BUTTON_CONNECT       2
#define DRONEX_BUTTON_PICKUP        3
#define DRONEX_BUTTON_GOTOEND       4
#define DRONEX_BUTTON_PUTDOWN       5
#define DRONEX_BUTTON_SQUAT         6
#define DRONEX_BUTTON_DISCONNECT    7
#define DRONEX_TAKE_OFF             8
#define DRONEX_LAND                 9
pragash1's avatar
pragash1 committed
103
#define DRONEX_ABORT                10
pragash1's avatar
pragash1 committed
104
105
106
107
108
109

#define DRONEX_BUTTON_1             11
#define DRONEX_BUTTON_2             12
#define DRONEX_BUTTON_3             13
#define DRONEX_BUTTON_4             14

maruggv's avatar
maruggv committed
110
111
112
113
114
#define DRONEX_INTEGRATOR_ON    	  15
#define DRONEX_INTEGRATOR_OFF     	16
#define DRONEX_INTEGRATOR_RESET 	  17
#define DRONEX_FOLLOW_TRAJECTORY    18
#define DRONEX_RESET                19
maruggv's avatar
maruggv committed
115

maruggv's avatar
maruggv committed
116
117
118
119
120
121
122
123
#define DRONEX_STATE_GROUND			          0
#define DRONEX_STATE_TAKING_OFF		        1
#define DRONEX_STATE_LAND_ON_GROUND		    2
#define DRONEX_STATE_APPROACH		          3
#define DRONEX_STATE_HOVER                4
#define DRONEX_STATE_LAND_ON_MOTHERSHIP   5
#define DRONEX_STATE_ON_MOTHERSHIP        6
#define DRONEX_STATE_FOLLOWING_TRAJECTORY  7
pragash1's avatar
pragash1 committed
124

maruggv's avatar
maruggv committed
125
126


pragash1's avatar
pragash1 committed
127

mastefan's avatar
mastefan committed
128

129
130
131
132
133
134
135
136
137
138
139
140
141
142
// These constants define the modes that can be used for controller the Crazyflie 2.0,
// the constants defined here need to be in agreement with those defined in the
// firmware running on the Crazyflie 2.0.
// The following is a short description about each mode:
// MOTOR_MODE    In this mode the Crazyflie will apply the requested 16-bit per motor
//               command directly to each of the motors
// RATE_MODE     In this mode the Crazyflie will apply the requested 16-bit per motor
//               command directly to each of the motors, and additionally request the
//               body frame roll, pitch, and yaw angular rates from the PID rate
//               controllers implemented in the Crazyflie 2.0 firmware.
// ANGE_MODE     In this mode the Crazyflie will apply the requested 16-bit per motor
//               command directly to each of the motors, and additionally request the
//               body frame roll, pitch, and yaw angles from the PID attitude
//               controllers implemented in the Crazyflie 2.0 firmware.
pragash1's avatar
pragash1 committed
143
144
145
146
147
148
149
150
#define CF_COMMAND_TYPE_MOTOR   6
#define CF_COMMAND_TYPE_RATE    7
#define CF_COMMAND_TYPE_ANGLE   8

// These constants define the method used for estimating the Inertial
// frame state.
// All methods are run at all times, this flag indicates which estimate
// is passed onto the controller.
151
152
// The following is a short description about each mode:
//
pragash1's avatar
pragash1 committed
153
154
155
156
157
158
159
160
161
162
163
// ESTIMATOR_METHOD_FINITE_DIFFERENCE
//       Takes the poisition and angles directly as measured,
//       and estimates the velocities as a finite different to the
//       previous measurement
//
// ESTIMATOR_METHOD_POINT_MASS_PER_DIMENSION
//       Uses a 2nd order random walk estimator independently for
//       each of (x,y,z,roll,pitch,yaw)
//
// ESTIMATOR_METHOD_QUADROTOR_MODEL_BASED
//       Uses the model of the quad-rotor and the previous inputs
164
//
pragash1's avatar
pragash1 committed
165
166
167
168
#define ESTIMATOR_METHOD_FINITE_DIFFERENCE          1
#define ESTIMATOR_METHOD_POINT_MASS_PER_DIMENSION   2   // (DEFAULT)
#define ESTIMATOR_METHOD_QUADROTOR_MODEL_BASED      3

169
170
171
172
173
// Flying states
#define STATE_MOTORS_OFF 1
#define STATE_TAKE_OFF   2
#define STATE_FLYING     3
#define STATE_LAND       4
174
175
176
177

// Namespacing the package
using namespace d_fall_pps;

mastefan's avatar
mastefan committed
178
// Flight sequences
mastefan's avatar
mastefan committed
179
180
181
#define SEQUENCE_NONE 					0
#define SEQUENCE_LAND_ON_MOTHERSHIP 	1
#define SEQUENCE_HOVER_OVER_MOTHERSHIP 	2
mastefan's avatar
mastefan committed
182

183
184
185
186
187
188
189
190
191
192
193
194




//    ----------------------------------------------------------------------------------
//    V   V    A    RRRR   III    A    BBBB   L      EEEEE   SSSS
//    V   V   A A   R   R   I    A A   B   B  L      E      S
//    V   V  A   A  RRRR    I   A   A  BBBB   L      EEE     SSS
//     V V   AAAAA  R  R    I   AAAAA  B   B  L      E          S
//      V    A   A  R   R  III  A   A  BBBB   LLLLL  EEEEE  SSSS
//    ----------------------------------------------------------------------------------

pragash1's avatar
pragash1 committed
195
196
197
// Current time
int m_time_ticks = 0;
float m_time_seconds;
198

pragash1's avatar
pragash1 committed
199
200
// > Mass of the Crazyflie quad-rotor, in [grams]
float m_mass_CF_grams;
201

202
Setpoint dronexSetpoint;
203
Setpoint dronexVelocity;
pragash1's avatar
pragash1 committed
204
int flying_state = DRONEX_STATE_GROUND;
205
int previous_flying_state = DRONEX_STATE_GROUND;
206

mastefan's avatar
mastefan committed
207
int integratorFlag = DRONEX_INTEGRATOR_OFF;
mastefan's avatar
mastefan committed
208
float integrator_sum_XYZ[3] = {0,0,0};
209

mastefan's avatar
mastefan committed
210

maruggv's avatar
maruggv committed
211
// Controller Mode: fetched from .yaml
maruggv's avatar
maruggv committed
212
// 0:
maruggv's avatar
maruggv committed
213
214
// 1: Nested Controller, trajectory Follower
int controller_mode;
mastefan's avatar
mastefan committed
215
216

// Variable for choosing flight sequence
mastefan's avatar
mastefan committed
217
int flightSequence = 0;
mastefan's avatar
mastefan committed
218
219
220
221
222
223
224

// Flags of landing sequence
bool tookOffFlag = false;
bool approachedFlag = false;
bool landedFlag = false;

// Flag for saving starting coordinates
225
bool savedStartCoordinates = false;
226
227
bool is_height_reached = false;

mastefan's avatar
mastefan committed
228
float startCoordinateX;
maruggv's avatar
maruggv committed
229
float startCoordinateY;
mastefan's avatar
mastefan committed
230
float startCoordinateZ;
mastefan's avatar
mastefan committed
231

232
233
234
std::vector<float> startCoordinateFromMS(3, 0.0);
bool savedStartCoordinatesFromMS = false;
bool taking_off_from_ms_height_gain = true;
maruggv's avatar
maruggv committed
235
// tolerances (x,y,z), fetched from .yaml
mastefan's avatar
mastefan committed
236

237
238
239
std::vector<float> tol_takeoff (3, 0.0);
std::vector<float> tol_approach (3, 0.0);
std::vector<float> tol_land (3, 0.0);
mastefan's avatar
mastefan committed
240

maruggv's avatar
maruggv committed
241
242
243
244
245
246
247


// Trajectory: xcf0 -> xm1 -> xm2 -> xms
// xcf0: inital(Button pressed) position of Crazyflie
// xm1: next Point after the Crazyflie: the Drone should reach the final height by xm1
// xm2: Point behind the mothership
// xms: Point on mothership where we will land
248
249
250
251
std::vector<float> xcf0(3, 0.0);
std::vector<float> xm1(3, 0.0);
std::vector<float> xm2(3, 0.0);
std::vector<float> xms(3, 0.0);
maruggv's avatar
maruggv committed
252

253
254
std::vector<float> direction_ms_to_cf(3, 0.0);
std::vector<float> direction_ms_to_cf_ms_bodyframe(3, 0.0);
mastefan's avatar
mastefan committed
255
float xm2_sign[2]  = {1.0, 0.0}; 	// sign for (x,y) in {-1, 0, 1}
256
257


mastefan's avatar
mastefan committed
258
259
260
261
262
263
264
265
266
267
//TODO we might want to use float64 instead of float
// trajectory generation variables
double trajectory_start_time = 0;
double total_time_since_start = 0;
bool first_trajectory_calculation = true;

// lookahead for trajectory: fetched from .yaml
float trajectory_deltaT_position = 0;
float trajectory_deltaT_velocity = 0;

maruggv's avatar
maruggv committed
268
269
270
271
float trajectory_duration_1 = 0; // Duration of flight between xcf0 and xm1
float trajectory_duration_2 = 0; // Duration of flight between xm1 and xm2
float trajectory_duration_3 = 0; // Duration of flight between xm2 and xms

mastefan's avatar
mastefan committed
272

maruggv's avatar
maruggv committed
273
274
275
276
float trajectory_t1 = 0; // Time at xm1
float trajectory_t2 = 0; // Time at xm2
float trajectory_t3 = 0; // Time at xms

277
278


maruggv's avatar
maruggv committed
279
280
281
// fetched from .yaml
float xm1_x_distance;
float xm2_distance_to_ms;
282
float xm2_height_over_ms;
maruggv's avatar
maruggv committed
283

mastefan's avatar
mastefan committed
284
285
float xm1_height;

286
287
288
289
290
291
292
float xm1_normalizing_factor = 3;   // normalize the difference between xcf0 and xms
float xm1_scaling_factor = 0.6;     // Scale in the direction of the the difference between xcf0 and xms

float xm2_distance_to_ms_at_zero_velocity = 0.6;
float xm2_distance_to_ms_scaling_factor = 1;


maruggv's avatar
maruggv committed
293
// Calculate from approximate distance and velocity the total duration of the trajectory
mastefan's avatar
mastefan committed
294
float trajectory_velocity_of_CF_init;
maruggv's avatar
maruggv committed
295
float trajectory_velocity_of_CF; // fetched from yaml
maruggv's avatar
maruggv committed
296
297
298
float trajectory_total_distance = 0;
float trajectory_duration = 0;

mastefan's avatar
mastefan committed
299
300
float acceleration[3] = {0.5, 0.5, 0.1};

mastefan's avatar
mastefan committed
301
302


maruggv's avatar
maruggv committed
303
// returned trajectory setpoint & velocity
maruggv's avatar
maruggv committed
304
305
float trajectory_setpoint[4]; // x,y,z,yaw
float trajectory_velocity[3]; // x,y,z
maruggv's avatar
maruggv committed
306

mastefan's avatar
mastefan committed
307

maruggv's avatar
maruggv committed
308
// Origin of the Flying zone
maruggv's avatar
maruggv committed
309
310
311
312
313
314
float originX = 0;
float originY = 0;
d_fall_pps::AreaBounds area;



maruggv's avatar
maruggv committed
315
// thrust factor to do a smooth landing
maruggv's avatar
maruggv committed
316
317
float thrust_factor = 1;

mastefan's avatar
mastefan committed
318
319
320
bool motorsOFFBool = false;
double motorsOFFStartTime;

maruggv's avatar
maruggv committed
321
322
323
324
325
326
327
328
329
330










mastefan's avatar
mastefan committed
331
332


333
334
//describes the area of the crazyflie and other parameters
//CrazyflieContext context;
335

pragash1's avatar
pragash1 committed
336
337
// > Total mass of the Crazyflie plus whatever it is carrying, in [grams]
float m_mass_total_grams;
mastefan's avatar
mastefan committed
338
float gravity = 9.81;
pragash1's avatar
pragash1 committed
339
340

// > The setpoints for (x,y,z) position and yaw angle, in that order
mastefan's avatar
mastefan committed
341
342
float m_setpoint[4] = {0.0,0.0,0.0,0.0};
float m_setpoint_for_controller[4] = {0.0,0.0,0.0,0.0};
pragash1's avatar
pragash1 committed
343

344
// > The velocities for (x,y,z) position, in that order
mastefan's avatar
mastefan committed
345
346
float m_velocity[3] = {0.0,0.0,0.0};
float m_velocity_for_controller[3] = {0.0,0.0,0.0};
347

pragash1's avatar
pragash1 committed
348
349
350
351
352
// > Small adjustments to the x-y setpoint
float m_xAdjustment = 0.0f;
float m_yAdjustment = 0.0f;

// Boolean for whether to limit rate of change of the setpoint
mastefan's avatar
mastefan committed
353
bool m_shouldSmoothSetpointChanges = false;
pragash1's avatar
pragash1 committed
354
355
356
357
358
359
360
361
362
363
364
365

// Max setpoint change per second
float m_max_setpoint_change_per_second_horizontal;
float m_max_setpoint_change_per_second_vertical;
float m_max_setpoint_change_per_second_yaw_degrees;
float m_max_setpoint_change_per_second_yaw_radians;


// Frequency at which the controller is running
float m_vicon_frequency;


maruggv's avatar
maruggv committed
366
367
368
float prev_MS_pos[3] = {0,0,0};
float current_MS_pos[3];
float mothership_vel[3];
mastefan's avatar
mastefan committed
369
370


pragash1's avatar
pragash1 committed
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
// THE FOLLOWING PARAMETERS ARE USED
// FOR THE LOW-LEVEL CONTROLLER

// Frequency at which the controller is running
float control_frequency;

// > Coefficients of the 16-bit command to thrust conversion
std::vector<float> motorPoly(3);

// The LQR Controller parameters for "CONTROLLER_MODE_LQR_RATE"
std::vector<float> gainMatrixThrust_NineStateVector (9,0.0);
std::vector<float> gainMatrixRollRate               (9,0.0);
std::vector<float> gainMatrixPitchRate              (9,0.0);
std::vector<float> gainMatrixYawRate                (9,0.0);

mastefan's avatar
mastefan committed
386
387
388
389
390
391
392
393
394
std::vector<float> gainMatrixYawRatefromAngle 		(9,0.0);
std::vector<float> gainMatrixPitchRatefromAngle     (9,0.0);
std::vector<float> gainMatrixRollRatefromAngle      (9,0.0);

std::vector<float> gainMatrixThrust_SixStateVector 	(9,0.0);
std::vector<float> gainMatrixRollAngle               (9,0.0);
std::vector<float> gainMatrixPitchAngle              (9,0.0);


395
// Integrator parameters
mastefan's avatar
mastefan committed
396
397
std::vector<float> gainIntegratorRate					(3,0.0);
std::vector<float> gainIntegratorAngle					(3,0.0);
398
std::vector<float> integrator_max 						(3,0.0);
mastefan's avatar
mastefan committed
399

maruggv's avatar
maruggv committed
400
401
402
// Feedforward gain (velocity -> angle)
std::vector<float> gainFeedforwardAnglefromVelocity	(3,0.0);

403

mastefan's avatar
mastefan committed
404

pragash1's avatar
pragash1 committed
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
// The 16-bit command limits
float cmd_sixteenbit_min;
float cmd_sixteenbit_max;


// VARIABLES FOR THE ESTIMATOR

// Frequency at which the controller is running
float estimator_frequency;

// > A flag for which estimator to use:
int estimator_method = ESTIMATOR_METHOD_FINITE_DIFFERENCE;
// > The current state interial estimate,
//   for use by the controller
float current_stateInertialEstimate[12];

// > The measurement of the Crazyflie at the "current" time step,
//   to avoid confusion
float current_xzy_rpy_measurement[6];

// > The measurement of the Crazyflie at the "previous" time step,
//   used for computing finite difference velocities
float previous_xzy_rpy_measurement[6];

// > The full 12 state estimate maintained by the finite
//   difference state estimator
float stateInterialEstimate_viaFiniteDifference[12];
432

mastefan's avatar
mastefan committed
433

pragash1's avatar
pragash1 committed
434
435
436
// > The full 12 state estimate maintained by the point mass
//   kalman filter state estimator
float stateInterialEstimate_viaPointMassKalmanFilter[12];
437

pragash1's avatar
pragash1 committed
438
439
440
441
442
443
444
445
446
447
448
449
450
// THE POINT MASS KALMAN FILTER (PMKF) GAINS AND ERROR EVOLUATION
// > For the (x,y,z) position
std::vector<float> PMKF_Ahat_row1_for_positions (2,0.0);
std::vector<float> PMKF_Ahat_row2_for_positions (2,0.0);
std::vector<float> PMKF_Kinf_for_positions      (2,0.0);
// > For the (roll,pitch,yaw) angles
std::vector<float> PMKF_Ahat_row1_for_angles    (2,0.0);
std::vector<float> PMKF_Ahat_row2_for_angles    (2,0.0);
std::vector<float> PMKF_Kinf_for_angles         (2,0.0);



// VARIABLES FOR THE NAMESPACES FOR THE PARAMETER SERVICES
451
452
453
454
455
// > For the paramter service of this agent
std::string namespace_to_own_agent_parameter_service;
// > For the parameter service of the coordinator
std::string namespace_to_coordinator_parameter_service;

pragash1's avatar
pragash1 committed
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478

// ROS PUBLISHER FOR SENDING OUT THE DEBUG MESSAGES
ros::Publisher debugPublisher;


// VARIABLES RELATING TO PERFORMING THE CONVERSION INTO BODY FRAME

// Boolean whether to execute the convert into body frame function
bool shouldPerformConvertIntoBodyFrame = false;


// VARIABLES RELATING TO THE PUBLISHING OF A DEBUG MESSAGE

// Boolean indiciating whether the "Debug Message" of this agent should be published or not
bool shouldPublishDebugMessage = false;

// Boolean indiciating whether the debugging ROS_INFO_STREAM should be displayed or not
bool shouldDisplayDebugInfo = false;


// VARIABLES RELATING TO PUBLISHING CURRENT POSITION AND FOLLOWING ANOTHER AGENT'S
// POSITION

479
480
481
// The ID of this agent, i.e., the ID of this compute
int my_agentID = 0;

pragash1's avatar
pragash1 committed
482
483
484
485
486
487
488
489
490
491
492
// Boolean indicating whether the (x,y,z,yaw) of this agent should be published or not
// > The default behaviour is: do not publish,
// > This varaible is changed based on parameters loaded from the YAML file
bool shouldPublishCurrent_xyz_yaw = false;


// ROS Publisher for my current (x,y,z,yaw) position
ros::Publisher my_current_xyz_yaw_publisher;

// ROS Publisher for the current setpoint
ros::Publisher dronexSetpointToGUIPublisher;
493
494
// publisher for flying state
ros::Publisher flyingStatePublisher;
495

mastefan's avatar
mastefan committed
496
visualization_msgs::Marker drone_position_list;
mastefan's avatar
mastefan committed
497
visualization_msgs::Marker xm2_position_list;
498
visualization_msgs::Marker setpoint_position_list;
499
500
visualization_msgs::Marker rviz_mothership;

mastefan's avatar
mastefan committed
501

502
503
ros::Publisher rviz_points_trajectory_publisher;
ros::Publisher rviz_points_trajectory_generated_publisher;
mastefan's avatar
mastefan committed
504
ros::Publisher rviz_xm2_publisher;
505

506
507
508
509
510
511
512
513
514
515
bool reset_rviz = false;

float red_1 = 1.0f;
float green_1 = 0.0f;
float blue_1 = 0.0f;

float red_2 = 0.0f;
float green_2 = 1.0f;
float blue_2 = 0.0f;

mastefan's avatar
mastefan committed
516

517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
// RELEVANT NOTES ABOUT THE VARIABLES DECLARE HERE:
// The "CrazyflieData" type used for the "request" variable is a
// structure as defined in the file "CrazyflieData.msg" which has the following
// properties:
//     string crazyflieName              The name given to the Crazyflie in the Vicon software
//     float64 x                         The x position of the Crazyflie [metres]
//     float64 y                         The y position of the Crazyflie [metres]
//     float64 z                         The z position of the Crazyflie [metres]
//     float64 roll                      The roll component of the intrinsic Euler angles [radians]
//     float64 pitch                     The pitch component of the intrinsic Euler angles [radians]
//     float64 yaw                       The yaw component of the intrinsic Euler angles [radians]
//     float64 acquiringTime #delta t    The time elapsed since the previous "CrazyflieData" was received [seconds]
//     bool occluded                     A boolean indicted whether the Crazyflie for visible at the time of this measurement





//    ----------------------------------------------------------------------------------
//    FFFFF  U   U  N   N   CCCC  TTTTT  III   OOO   N   N
//    F      U   U  NN  N  C        T     I   O   O  NN  N
//    FFF    U   U  N N N  C        T     I   O   O  N N N
//    F      U   U  N  NN  C        T     I   O   O  N  NN
//    F       UUU   N   N   CCCC    T    III   OOO   N   N
//
//    PPPP   RRRR    OOO   TTTTT   OOO   TTTTT  Y   Y  PPPP   EEEEE   SSSS
//    P   P  R   R  O   O    T    O   O    T     Y Y   P   P  E      S
//    PPPP   RRRR   O   O    T    O   O    T      Y    PPPP   EEE     SSS
//    P      R  R   O   O    T    O   O    T      Y    P      E          S
//    P      R   R   OOO     T     OOO     T      Y    P      EEEEE  SSSS
//    ----------------------------------------------------------------------------------

// These function prototypes are not strictly required for this code to complile, but
// adding the function prototypes here means the the functions can be written below in
// any order. If the function prototypes are not included then the function need to
// written below in an order that ensure each function is implemented before it is
// called from another function, hence why the "main" function is at the bottom.

pragash1's avatar
pragash1 committed
555
556
557
558
559
560

// ADDED FOR THE DRONEX
void perControlCycleOperations();

// CALLBACK FROM ROS MESSAGES RECEIVED
void buttonPressedCallback(const std_msgs::Int32& msg);
mastefan's avatar
mastefan committed
561
void dronexCoordinateCallback(const ViconData& viconData);
562
563
//void coordinatesToLocal(CrazyflieData& cf);

pragash1's avatar
pragash1 committed
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580

// SEPARATE CALLBACK FUNCTIONS FOR EACH BUTTON
void buttonPressed_gotoStart();
void buttonPressed_connect();
void buttonPressed_pickup();
void buttonPressed_gotoEnd();
void buttonPressed_putdown();
void buttonPressed_squat();
void buttonPressed_disconnect();

void buttonPressed_1();
void buttonPressed_2();
void buttonPressed_3();
void buttonPressed_4();

void buttonPressed_take_off();
void buttonPressed_land();
pragash1's avatar
pragash1 committed
581
void buttonPressed_abort();
pragash1's avatar
pragash1 committed
582

583
584
585
void buttonPressed_integrator_on();
void buttonPressed_integrator_off();
void buttonPressed_integrator_reset();
maruggv's avatar
maruggv committed
586
587
588
void buttonPressed_follow_trajectory();

void buttonPressed_reset();
mastefan's avatar
mastefan committed
589
590
void setarrayzero3(float arr[3], int n);
void setarrayzero4(float arr[4], int n);
pragash1's avatar
pragash1 committed
591
592


maruggv's avatar
maruggv committed
593
594

void crazyfliecontextRefresh(d_fall_pps::CrazyflieContext context);
pragash1's avatar
pragash1 committed
595
void motorsOFF(Controller::Response &response);
mastefan's avatar
mastefan committed
596
void motorsOFFSmooth(Controller::Response &response);
pragash1's avatar
pragash1 committed
597

maruggv's avatar
maruggv committed
598
599
600
601
602
603
604


// Trajectory methods
void calculateTrajectory(Controller::Request &request);
std::vector<float> trajectory_to_ms(Controller::Request &request, double t);
float calculate_distance_in_xyz(std::vector<float> p1, std::vector<float> p2);

605
// CONTROLLER COMPUTATIONS
pragash1's avatar
pragash1 committed
606
// > The function that is called to "start" all estimation and control computations
607
bool calculateControlOutput(Controller::Request &request, Controller::Response &response);
mastefan's avatar
mastefan committed
608
void calculateControlOutputDroneX(Controller::Request &request, Controller::Response &response, float (&stateErrorBody)[12]);
pragash1's avatar
pragash1 committed
609

610
611
612
// > The various functions that implement an LQR controller
void calculateControlOutput_viaLQRforRates( float stateErrorBody[12], Controller::Request &request, Controller::Response &response);
void calculateControlOutput_viaLQRforRates_Nested(float stateErrorBody[12], Controller::Request &request, Controller::Response &response);
mastefan's avatar
mastefan committed
613
614

// Velocity estimator
maruggv's avatar
maruggv committed
615
void calculateMSVelocity(Controller::Request &request);
mastefan's avatar
mastefan committed
616
617
618
619

// Integrator
void integrator_XYZ(float (&stateErrorBody)[12]);

pragash1's avatar
pragash1 committed
620
621
622
623
624
625
626
627
628
629
630
631
632
// ESTIMATOR COMPUTATIONS
void performEstimatorUpdate_forStateInterial(Controller::Request &request);
void performEstimatorUpdate_forStateInterial_viaFiniteDifference();
void performEstimatorUpdate_forStateInterial_viaPointMassKalmanFilter();


// PUBLISHING OF THE DEBUG MESSAGE
void construct_and_publish_debug_message(Controller::Request &request, Controller::Response &response);


// TRANSFORMATION FROM INTERIAL ESTIMATE TO BODY FRAME ERROR
void convert_stateInertial_to_bodyFrameError(float stateInertial[12], float setpoint[4], float (&bodyFrameError)[12]);

633
// TRANSFORMATION OF THE (x,y) INERTIAL FRAME ERROR INTO AN (x,y) BODY FRAME ERROR
pragash1's avatar
pragash1 committed
634
void convertIntoBodyFrame(float stateInertial[12], float (&stateBody)[12], float yaw_measured);
635
636
637
638
639
640

// CONVERSION FROM THRUST IN NEWTONS TO 16-BIT COMMAND
float computeMotorPolyBackward(float thrust);

// SETPOINT CHANGE CALLBACK
void setpointCallback(const Setpoint& newSetpoint);
mastefan's avatar
mastefan committed
641
void velocityCallback(const Setpoint& newVelocity);
pragash1's avatar
pragash1 committed
642
643
644
void xyz_yaw_to_follow_callback(const Setpoint& newSetpoint);

void publishCurrentSetpoint();
645
646

// CUSTOM COMMAND RECEIVED CALLBACK
pragash1's avatar
pragash1 committed
647
648
649
650
//void customCommandReceivedCallback(const CustomButton& commandReceived);

// PUBLISH THE CURRENT X,Y,Z, AND YAW
void publish_current_xyz_yaw(float x, float y, float z, float yaw);
651
652
653

// LOAD PARAMETERS
float getParameterFloat(ros::NodeHandle& nodeHandle, std::string name);
pragash1's avatar
pragash1 committed
654
655
656
657
658
void  getParameterFloatVector(ros::NodeHandle& nodeHandle, std::string name, std::vector<float>& val, int length);
int   getParameterInt(ros::NodeHandle& nodeHandle, std::string name);
void  getParameterIntVectorWithKnownLength(ros::NodeHandle& nodeHandle, std::string name, std::vector<int>& val, int length);
int   getParameterIntVectorWithUnknownLength(ros::NodeHandle& nodeHandle, std::string name, std::vector<int>& val);
bool  getParameterBool(ros::NodeHandle& nodeHandle, std::string name);
659
660
661
662

void yamlReadyForFetchCallback(const std_msgs::Int32& msg);
void fetchYamlParameters(ros::NodeHandle& nodeHandle);
void processFetchedParameters();