Data_Drone.cpp 5.27 KB
Newer Older
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/**
 * @brief Class that model a drone
 * 
 * @author Sylvain Colomer
 * @date 23/04/19
 * @version 1.0
 */

#include "../include/Data_Drone.h"


//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% CONTRUCTOR %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

/**
 * Default constructor. Initialize var to first value
 */
Drone::Drone()
{

}

Drone::Drone(std::string serialPort_path, int serialPort_baudrate){
    communication=DRONE_SERIAL;
    serial1 = std::shared_ptr<Serial_Port>(new Serial_Port(serialPort_path, serialPort_baudrate));
}



Drone::~Drone()
{
    serial1.get()->close_serial();
}

//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% COMMUNICATION %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

int Drone::read_message(mavlink_message_t &message)
{
    if(communication == DRONE_SERIAL)
    {
    return serial1.get()->read_message(message);
    }
    return 1;
}

int Drone::write_message(mavlink_message_t message)
{
    if(communication == DRONE_SERIAL)
    {
        return serial1.get()->write_message(message);
    }
    else if(communication == DRONE_WIFI)
    {
        return 1;
    }
    return 1;
}

//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% INITIALISATION %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

int Drone::init_communication()
{
    if(communication==DRONE_SERIAL)
    {
        return Drone::serial1.get()->open_serial();
    }

    return 0;
}


/**
 * Initialisation of a drone 
 */
int Drone::init_parameters(uint limit)
{
    mavlink_message_t mavlink_message;
    bool flag_test = true;
    bool flag_success = true;
    uint counter=0;
    std::string buffer1="";

    while(flag_test)
    {
        
        flag_success = serial1.get()->read_message(mavlink_message);
        if(flag_success)
        {
            system_id = mavlink_message.sysid;
            component_id = mavlink_message.compid;
            
            flag_test=false;
        }
        
        if(counter >= limit) //Computation limit test
        {
            //Display_IHM::getInstanceOf().printLog("-> Error, no drone detected");
            return 1;
        }
        counter++;
        usleep(1000);
    }   

    return 0;
}


//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% CONTROL %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

/**
 * Arm with 1, un arm with 0
 */
int Drone::command_arm(int param1){
    //Message buffer
	mavlink_message_t message;
    mavlink_command_long_t command;
    
    command=mavlink_newCommand();
    command.target_system = 1;
    command.target_component = 1;
    command.command = MAV_CMD_COMPONENT_ARM_DISARM;
    command.confirmation = false;
    command.param1           = param1;

    mavlink_msg_command_long_encode(255, 1, &message, &command);

    if(param1 >0){
        //Display_IHM::getInstanceOf().printLog("ARMING");
    }else{
        //Display_IHM::getInstanceOf().printLog("STOP ARMING");
    }

    return write_message(message);
}

int Drone::command_kill(int param1){
    //Message buffer
	mavlink_message_t message;
    mavlink_command_long_t command;

    command=mavlink_newCommand();
    command.target_system = 1;
    command.target_component = 1;
    command.command = MAV_CMD_DO_FLIGHTTERMINATION;
    command.confirmation = false;
    command.param1           = param1;

    mavlink_msg_command_long_encode(255, 1, &message, &command);
    if(param1>0){
        //Display_IHM::getInstanceOf().printLog("KILL MODE");
    }else{
        //Display_IHM::getInstanceOf().printLog("OUT KILL MODE");
    }

    return write_message(message);
}

int Drone::command_setModeGuided(float param1){
    //Message buffer
	mavlink_message_t message;
    mavlink_command_long_t command;

    command=mavlink_newCommand();
    command.target_system = 1;
    command.target_component = 1;
    command.command          = MAV_CMD_NAV_GUIDED_ENABLE;
    command.confirmation     = false;
    command.param1           = param1; // flag >0.5 => start, <0.5 => stop

    mavlink_msg_command_long_encode(255, 1, &message, &command);
    if(param1>0){
        //Display_IHM::getInstanceOf().printLog("GUIDED MODE");
    }else{
        //Display_IHM::getInstanceOf().printLog("OUT GUIDED MODE");
    }

    return write_message(message);
}

int Drone::command_directControl(float x, float y, float z, float r){
	mavlink_message_t message;
    mavlink_msg_manual_control_pack(255, 1, &message, 1, 0, x, y, z, r);
    return write_message(message);
}

// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% ? %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

mavlink_command_long_t Drone::mavlink_newCommand(){
    mavlink_command_long_t com;
    com.target_system = 0;
    com.target_component = 0.;
    com.command = 0;
    com.confirmation = false;
    com.param1=0; 
    com.param2=0; 
    com.param3=0; 
    com.param4=0; 
    com.param5=0; 
    com.param6=0; 
    com.param7=0;
    return com;
}

int Drone::command_setMode(Drone_mode mode){

    switch(mode){
        case DRONE_OFF: 
        //We must reset all value to 0
            Drone::mode=mode;
        break;

        case DRONE_MANUAL_DIRECT: 
            if(remote_x!=0 && remote_y!=0 && remote_z!=0 && remote_r!=0){
                ////Display_IHM::getInstanceOf().printLog("Server: command refused");
                //beep();
            }else{
                //Display_IHM::getInstanceOf().printLog("Server: mode manual_direct");
                Drone::mode=mode;
            }
        break;

        default:

        break;
    }

    return 0;
}