TestDroneRemoteControl.cpp 5.47 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
15
16
17
#include <iostream>
#include <string>

using namespace std;

18
19
20
string device_path = "/dev/ttyUSB0";
int device_baudrate = 57600;
Drone drone1(device_path, device_baudrate);
21
22


23
24
25
26
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);
    //Channels use to control manually the drone
blc_channel blc_control_motors("/pixhawk.control.motors", BLC_CHANNEL_READ, 'FL32', 'NDEF', 1, 4); //x, y, z, r
27

28
29
30
    //Channels use to command the drone
blc_channel blc_control_commands("/pixhawk.control.commands", BLC_CHANNEL_READ, 'FL32', 'NDEF', 1, 4); // 1=on, 0=off
blc_channel blc_highres_imu("/pixhawk.sensors.highres_imu", BLC_CHANNEL_WRITE, 'FL32', 'NDEF', 1, 9);
31

32
33
    //Different blc channel use to publish data
blc_channel blc_heartbeat; //Hearbeat message  PG: PB ????? pas d'init  pour l'instant ????
34
35
36
37
38
39
40


void command_loop()
{
    mavlink_message_t message;
    string buffer_command = "";

41
    cout<<"Enter command : \n";
42
43
44
45
46
    cin >> buffer_command;

    if(buffer_command=="a1")
    {
       cout<<"arm \n";
47
        drone1.command_arm(1);
48
49
50
51
    }
    else if(buffer_command=="!a1")
    {
        
52
        drone1.command_arm(0);
53
54
55
    }
    else if(buffer_command=="k1")
    {
56
57
       cout<<"kill \n";
        drone1.command_kill(1);
58
59
60
    }
    else if(buffer_command=="!k1")
    {
61
        drone1.command_kill(0);
62
63
64
    }
    else if(buffer_command=="m1")
    {
65
66
67
        cout<<"motor 1 \n";

        drone1.command_directControl(1000,0,0,0);
68
69
70
    }
    else if(buffer_command=="m2")
    {
71
        drone1.command_directControl(0,1000,0,0);
72
73
74
    }
    else if(buffer_command=="m3")
    {
75
        drone1.command_directControl(0,0,1000,0);
76
77
78
    }
    else if(buffer_command=="m4")
    {
79
        drone1.command_directControl(0,0,0,1000);
80
81
82
    }
    else if(buffer_command=="!m")
    {
83
        drone1.command_directControl(0,0,0,0);
84
85
86
    }
    else if(buffer_command=="tone")
    {
87
       cout<<"Tone \n";
88
        //Display_IHM::getInstanceOf().printLog("PLAY TONE");
89
90
        mavlink_msg_play_tune_pack(255, drone1.component_id, &message, 1, 1 , "AAAA", "");
        drone1.write_message(message);
91
92
93
    }
    else
    {
94
       cout<<"command not recognized\n";
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
        //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);
// }
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207



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

    string buffer1="";
    // General object
    shared_ptr<Serial_Port_ReadingThread> readingThread;
    shared_ptr<Serial_Port_WritingThread> writingThread;
//    shared_ptr<Display_IHM> ihm;

   // drone1 = shared_ptr<Drone>(new Drone(device_path, device_baudrate));
  //  drone1 = new Drone(device_path, device_baudrate);

    readingThread = shared_ptr<Serial_Port_ReadingThread>(new Serial_Port_ReadingThread(500, 200));
    writingThread = shared_ptr<Serial_Port_WritingThread>(new Serial_Port_WritingThread(500, 200));

    cout<<"Welcome to pixhawk server"<<endl;
    cout<<"Init communication"<<endl;
    cout<<"-> Open "+device_path<<endl;
    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);

    //Display_IHM::getInstanceOf().printLog("Begin");
    readingThread.get()->start();
    writingThread.get()->start();
    sleep(1);
    
    cout<<"Arm"<<endl;
    drone1.command_arm(1);
    sleep(1);

    cout<<"test motors"<<endl;
    drone1.command_directControl(0,1000,0,0);
    sleep(1);

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

//    readingThread.get()->stop();
//    writingThread.get()->stop();
   while(true)
   {
      command_loop();
   }

    exit(0);
}