TestDroneRemoteControl.cpp 12.4 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
/**
 *  @author Sylvain Colomer
 *  @date 18/04/19.
 * 
 * https://www.tldp.org/HOWTO/html_single/Text-Terminal-HOWTO/#colors
 * https://www.tldp.org/HOWTO/html_single/Text-Terminal-HOWTO/#colors
 * https://en.wikipedia.org/wiki/ANSI_escape_code
 * https://fr.wikipedia.org/wiki/Curses
 * http://www.termsys.demon.co.uk/vtansi.htm
 */

12
#include "include/PixhawkServer.h"
13
14
#include <iostream>
#include <string>
15
#include <iostream>
16
17
18
19
20
21
#include <fstream>
#include <thread>

//include "Com_server/action.h"


22
23

using namespace std;
24
25
using std::chrono::seconds;
using std::this_thread::sleep_for;
26

27
char device_path[255] = "/dev/ttyUSB0";
28
int device_baudrate = 57600;
29
30
31
32
33
34
35
//Drone drone1(device_path, device_baudrate);
Drone drone1;


    // General object
    shared_ptr<Serial_Port_ReadingThread> readingThread;
    shared_ptr<Serial_Port_WritingThread> writingThread;
36

37
38
ofstream MyFile;
ofstream MyFilew;
39
40
ofstream compassx;
ofstream compassy;
41

42
43
blc_channel blc_control_arm("/pixhawk.control.arm", BLC_CHANNEL_READ, 'IN16', 'NDEF', 1, 1); // 1=on, 0=off;
blc_channel blc_control_remote_vector("/pixhawk.control.remoteVectors", BLC_CHANNEL_WRITE, 'FL32', 'NDEF', 1, 4);
Amine AIT-GANI's avatar
TestOK    
Amine AIT-GANI committed
44
    //Channels use to control manually the drone  
45
blc_channel blc_control_motors("/pixhawk.control.motors", BLC_CHANNEL_READ, 'FL32', 'NDEF', 1, 6); //x, y, z, r
46

47
    //Channels use to command the drone
48
blc_channel blc_control_commands("/pixhawk.control.commands", BLC_CHANNEL_READ, 'FL32', 'NDEF', 1, 7); // 1=on, 0=off
49
blc_channel blc_highres_imu("/pixhawk.sensors.imu", BLC_CHANNEL_WRITE, 'FL32', 'NDEF', 1, 9);
50
51
52
53
54
	//Channel use to know altitude of drone
blc_channel blc_attitude("/pixhawk.sensors.attitude", BLC_CHANNEL_WRITE, 'FL32', 'NDEF', 1, 7);
    //Channel use to know GPS values of drone
blc_channel blc_local_position_ned("/pixhawk.sensors.local_position_ned", BLC_CHANNEL_WRITE, 'FL32', 'NDEF', 1, 6);
blc_channel blc_global_position("/pixhawk.sensors.global_position", BLC_CHANNEL_WRITE, 'FL32', 'NDEF', 1, 3);
55

56
57
    //Different blc channel use to publish data
blc_channel blc_heartbeat; //Hearbeat message  PG: PB ????? pas d'init  pour l'instant ????
58
blc_channel blc_test_detection("/pixhawk.test.detection", BLC_CHANNEL_READ, 'IN16', 'NDEF', 1, 7);
59
int j = 0;
60
61
62

void command_loop()
{
Amine AIT-GANI's avatar
Amine AIT-GANI committed
63
	int ret; 
64
65
66
    mavlink_message_t message;
    string buffer_command = "";

67
	writingThread.get()->set_task_period(100000);
68
    cout<<"Enter command : \n";
69
70
    cin >> buffer_command;

71
72
73
74
75
76
77
78

/*_____________SET_MODE____________*/

//drone1.make_command_msg_rate(1);

//drone1.make_mode(1);
/*___________________________________*/

79
	
80
    if(buffer_command=="a")
81
    {
82
		cout<<"arm \n";
83
        drone1.command_arm(1);
84
85
        //drone1.make_mode(193);

86
    }
87
    else if(buffer_command=="!a")
88
89
    {
        
90
        drone1.command_arm(0);
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
    else if(buffer_command=="h"){
	cout<<"HOME_POSITION \n";
	drone1.request_home_position(1,1,1);
	}
	 else if(buffer_command=="e"){
	cout<<"X_POSITION \n";
	drone1.get_home_x(1);
	}
    else if(buffer_command=="o")
    {
		cout<<"current_consumed :"<<drone1.battery_status.current_consumed<<endl;
	cout<<"=========================================="<<endl;
	cout<<"energy_consumed :"<<drone1.battery_status.energy_consumed<<endl;
	cout<<"=========================================="<<endl;
	cout<<"temperature :"<<drone1.battery_status.temperature<<endl;
	cout<<"=========================================="<<endl;
	cout<<"voltages :"<<drone1.battery_status.voltages[10]<<endl;
	cout<<"=========================================="<<endl;
	cout<<"current_battery :"<<drone1.battery_status.current_battery<<endl;
	cout<<"=========================================="<<endl;
	cout<<"battery_function :"<<drone1.battery_status.battery_function<<endl;
	cout<<"=========================================="<<endl;
	cout<<"type :"<<drone1.battery_status.type<<endl;
	cout<<"=========================================="<<endl;
	cout<<"battery_remaining :"<<drone1.battery_status.battery_remaining<<endl;
	cout<<"=========================================="<<endl;
	cout<<"time_remaining :"<<drone1.battery_status.time_remaining<<endl;
	cout<<"=========================================="<<endl;
	cout<<"charge_state :"<<drone1.battery_status.charge_state<<endl;

	}
123
124
125
126
127
128
129
130
    else if(buffer_command=="m")
    {
        
        drone1.followhold();
    }
    else if(buffer_command=="p")
    {
		drone1.go_to();
131
		
132
	}
133
134
    else if(buffer_command=="f")
    {
135
		drone1.take_off();
136
       cout<<"fly \n";
137
   ret = drone1.take_off();
138
      cout<<"fly ret = "<<ret<<endl;
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
      cout<<"test"<<drone1.altitude.altitude_local<<endl;
      //cout<<"pos"<<altitude.altitude_local<<endl;
	}
    else if(buffer_command=="g")
    {
		cout<<"===================CORRDONNEES===================="<<endl;
		cout<<"HOME_LATITUDE_DEG"<<drone1.home_position.latitude<<endl;
		cout<<"_______________________"<<endl;
		cout<<"HOME_LONGITUDE_DEG"<<drone1.home_position.longitude<<endl;
		cout<<"_______________________"<<endl;
		cout<<"HOME_ALTITUDE_DEG"<<drone1.home_position.altitude<<endl;
		cout<<"_______________________"<<endl;
		cout<<"HOME_X_metres"<<drone1.home_position.x<<endl;
		cout<<"_______________________"<<endl;
		cout<<"HOME_Y_metres"<<drone1.home_position.y<<endl;
		cout<<"_______________________"<<endl;
		cout<<"HOME_Z_metres"<<drone1.home_position.z<<endl;
		cout<<"_______________________"<<endl;
		cout<<"BOUSSOLE"<<drone1.highres_imu.xmag<<endl;
	} 
	
160
161
162
163
    else if(buffer_command=="!f")
    {
        //drone1.take(0);
    }
164

165
    else  if(buffer_command=="i")
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
       cout<<"return \n";
        drone1.return_to_launch(1);
    }
    else if(buffer_command=="!i")
    {
        
        drone1.return_to_launch(0);
    }
    else  if(buffer_command=="l")
    {
       cout<<"landing \n";
        drone1.landing();
    }
 
     /*if(buffer_command=="u")
    {
       cout<<"takeoff \n";
        drone1.command_up(1);
    }
    else if(buffer_command=="!u")
    {
        
        drone1.command_up(0);
    }
    */
192

193

194
195
196
197
198
199
200
201
202
203
204
205
   else if(buffer_command=="r")
    {
       cout<<"right \n";
        drone1.command_right(1);
    }
    else if(buffer_command=="!r")
    {
        
        drone1.command_right(0);
    }
    
    else if(buffer_command=="k")
206
    {
207
208
       cout<<"kill \n";
        drone1.command_kill(1);
209
    }
210
    else if(buffer_command=="!k")
211
    {
212
        drone1.command_kill(0);
213
    }
Amine AIT-GANI's avatar
Amine AIT-GANI committed
214
215
216
217
    else if(buffer_command=="j")
    {
        drone1.move_drone_to(1,0,0);
    }
218
219
220
221
222
    else if(buffer_command=="y"){
		drone1.move_to(1,1,1);
	}
	
	
223
    else if(buffer_command=="z")
224
    {
225
        cout<<"motor 1 \n";
226
        drone1.move_drone_to(0,0.01,0);  //move forward 0.01m/s
227
228
229
230
    }
    else if(buffer_command=="s")
    {
        cout<<"motor 1 \n";
231
        drone1.move_drone_to(0,-0.01,0);  //move backward 0.01m/s
232
    }
233
    else if(buffer_command=="d")
234
    {
235
        drone1.move_drone_to(0.01,0,0);  //move right 0.01m/s
236
    }
237
    else if(buffer_command=="q")
238
    {
239
        drone1.move_drone_to(-0.01,0,0);  //move left 0.01m/s
240
    }
241
242
    else if(buffer_command=="w")
    {
243
        drone1.move_drone_to(0,0,1);  //UP for 0.01m/s
244
245
246
    }
    else if(buffer_command=="x")
    {
247
         drone1.move_drone_to(0,0,-0.01);  //DOWN for 0.01m/s
248
249
    }
    else if(buffer_command=="c")
250
    {
251
        drone1.command_directControl(0,0,0,1000);
252
    }
253
254
255
256
    else if(buffer_command=="v")
    {
        drone1.command_directControl(0,0,0,-1000);
    }
257
258
    else if(buffer_command=="!m")
    {
259
        drone1.command_directControl(0,0,0,0);
260
261
262
    }
    else if(buffer_command=="tone")
    {
263
       cout<<"Tone \n";
264
        //Display_IHM::getInstanceOf().printLog("PLAY TONE");
265
266
        mavlink_msg_play_tune_pack(255, drone1.component_id, &message, 1, 1 , "AAAA", "");
        drone1.write_message(message);
267
    }
268
    /*else if (buffer_command=="g")
269
270
271
272
273
274
275
    { 
		 drone1.command_setModeGuided(1);
	}
    else if (buffer_command=="!g")
    { 
		 drone1.command_setModeGuided(0);
	}
276
	*/
277
278
    else
    {
279
       cout<<"command not recognized\n";
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
        //Display_IHM::getInstanceOf().printLog("Error: what ?");
    }

    //PARAM_REQUEST_READ
}




// else if(buffer_command=="1") // SET_ATTITUDE_TARGET
// {
//     Display_IHM::getInstanceOf().printLog("U");
//     mavlink_setAttitudeTarget(&message, *drone1.get(), 800, 0, 0, 0);
//     drone1.get()->write_message(message);
// }
// else if(buffer_command=="2") // SET_ATTITUDE_TARGET
// {
//     Display_IHM::getInstanceOf().printLog("");
//     mavlink_setAttitudeTarget(&message, *drone1.get(), 0, 800, 0, 0);
//     drone1.get()->write_message(message);
// }
// else if(buffer_command=="3") // SET_ATTITUDE_TARGET
// {
//     Display_IHM::getInstanceOf().printLog("");
//     mavlink_setAttitudeTarget(&message, *drone1.get(), 0, 0, 800, 0);
//     drone1.get()->write_message(message);
// }
// else if(buffer_command=="4") // SET_ATTITUDE_TARGET
// {
//     Display_IHM::getInstanceOf().printLog("");
//     mavlink_setAttitudeTarget(&message, *drone1.get(), 0, 0, 0, 800);
//     drone1.get()->write_message(message);
// }
313

314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/**
 * @fn void signalHandler(int number)
 * @brief Handles received signals. Triggered when a signal is received.
 *
 * @param number The signal code.
 **/
void signalHandler(int number) 
{
   switch(number) 
   {
   case SIGINT :
      printf("SIGINT caught\n"); //CTRL+C 
      exit(0);
      //TODO handle SIGINT reception situation
      break;

   case SIGTERM :
      printf("SIGTERM caught\n"); //kill (term command)
      //TODO handle SIGTERM reception situation
      exit(0);
      break;

   case SIGFPE :
      printf("SIGFPE caught\n"); //Floating-Point arithmetic Exception
      //TODO handle SIGFPE reception situation
      exit(0); //to prevent infinite loop
      
      break;

   default :
      printf("Signal number : %d caught\n", number);
   }
}
347
348
349
350
351
352
353
354

/**
 * Only the main exe of the programm
 * @return
 */
int main(int argc, char *argv[])
{
    // General var
355
356
  //  bool flag_run = true; //Flag use for the main loop of the system

357
   struct sigaction action;
358
   if(argc>1)  {strcpy(device_path ,argv[1]); printf("port = %s \n",device_path);}
359
360
361
362
363
364
365
366
367
368
  
   //signal handler initialisation
   sigfillset(&action.sa_mask);
   action.sa_handler = signalHandler;
   action.sa_flags = 0;

   //Make the list of all SIG to catch (will not be caught if not listed here, the list is not definitive and all are not necessarily usefull)
   if(sigaction(SIGINT, &action, NULL) != 0) printf("SIGINT couldn't be attached\n"); //ctrl+C
   if(sigaction(SIGTERM, &action, NULL) != 0) printf("SIGTERM couldn't be attached\n"); //kill [PID]
   if(sigaction(SIGFPE, &action, NULL) != 0) printf("SIGFPE couldn't be attached\n"); //integer divided by 0 
369
   drone1.open(device_path, device_baudrate);
370
371

    string buffer1="";
372

373
374
375
376
//    shared_ptr<Display_IHM> ihm;

   // drone1 = shared_ptr<Drone>(new Drone(device_path, device_baudrate));
  //  drone1 = new Drone(device_path, device_baudrate);
377
378
379
380
381
	
				     
//		ofstream MyFile("mesures.txt");
		MyFile.open("mesures.txt");    
		MyFilew.open("tcdrain.txt");    
382
383
		compassx.open("compassx.txt");
		compassy.open("compassy.txt");
384

385
386
387
388
   writingThread = shared_ptr<Serial_Port_WritingThread>(new Serial_Port_WritingThread(1000000, 200));  //beug ne marche pas 
																						
   readingThread = shared_ptr<Serial_Port_ReadingThread>(new Serial_Port_ReadingThread(10000, 200)); //beug ne marche pas 
																						
389

390
    cout<<"Welcome to pixhawk server"<<endl;
391
    cout<<"Init communicationn"<<endl;
392
    cout<<"-> Open  " <<device_path<<endl;
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
    if(drone1.init_communication()==0)
    { 
        cout<<"-> Succes"<<endl;
    }
    else
    {
        cout<<"-> Fail, exiting now"<<endl;
        sleep(2);
        exit(1);
    }
    
    cout<<"Pull parameters"<<endl;
    if(drone1.init_parameters(10000)==0)
    { 
        cout<<"-> Succes"<<endl;
    }
    else
    {
        cout<<"-> Fail, exiting"<<endl;
        sleep(2); 
        exit(1);
    }

    // Display
    //Display_IHM::getInstanceOf().displayDroneState(drone1);
    //Display_IHM::getInstanceOf().displayMotorsState(drone1);

420
421
422
    //Display_IHM::getInstanceOf().printLog("Begin");   
    
   readingThread.get()->start();
423
424
425
    writingThread.get()->start();
    sleep(1);
    
426
427
428
    //cout<<"Arm"<<endl;
    //drone1.command_arm(1);
    //sleep(1);
429

430
431
432
    //cout<<"test motors"<<endl;
    //drone1.command_directControl(0,1000,0,0);
    //sleep(1);
433
434
435
436
437
438
439

//    cout<<"Unarm"<<endl;    
//    drone1.get()->command_arm(0);
//    sleep(1);

//    readingThread.get()->stop();
//    writingThread.get()->stop();
440

441
442
443
444
   while(true)
   {
      command_loop();
   }
445
446
		MyFile.close();
		MyFilew.close();
447
448
		compassx.close();
		compassy.close();
449
450
    exit(0);
}