Subversion Repositories Projects

Rev

Rev 500 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 500 Rev 513
Line 1... Line 1...
1
#include "Handler.h"
1
#include "Handler.h"
2
#include "FlightLog.h"
2
#include "FlightLog.h"
-
 
3
#include <sstream>
-
 
4
#include <string>
Line 3... Line 5...
3
 
5
 
4
/**
6
/**
5
 * Constructor that gets a communication instance
7
 * Constructor that gets a communication instance
6
 */
8
 */
Line 12... Line 14...
12
//-------------FlightCtrl commands--------------------
14
//-------------FlightCtrl commands--------------------
13
/**
15
/**
14
 * read settings from FlightCtrl (settings index 0x00-0x05)
16
 * read settings from FlightCtrl (settings index 0x00-0x05)
15
 */
17
 */
16
void Handler::get_flightctrl_settings(int index) {
18
void Handler::get_flightctrl_settings(int index) {
-
 
19
    FlightLog::info_FC("getting FlightCtrl settings");
17
    char tx_data[2] = {index, 0};
20
    char tx_data[2] = {index, 0};
18
    com->send_cmd('q', ADDRESS_FC, tx_data, 1, true);
21
    com->send_cmd('q', ADDRESS_FC, tx_data, 1, true);
19
}
22
}
Line 20... Line 23...
20
 
23
 
21
/**
24
/**
22
 * write settings to FlightCtrl
25
 * write settings to FlightCtrl
23
 */
26
 */
-
 
27
void Handler::set_flightctrl_settings(char * tx_data) {
24
void Handler::set_flightctrl_settings(char * tx_data) {
28
    FlightLog::info_FC("setting FlightCtrl settings");
25
    com->send_cmd('s', ADDRESS_FC, tx_data, MaxParameter+2, true);
29
    com->send_cmd('s', ADDRESS_FC, tx_data, MaxParameter+2, true);
Line 26... Line 30...
26
}
30
}
27
 
31
 
Line 32... Line 36...
32
    char tx_data[MAX_MOTORS];
36
    char tx_data[MAX_MOTORS];
33
    for (int z = 0; z<MAX_MOTORS; z++)
37
    for (int z = 0; z<MAX_MOTORS; z++)
34
    {
38
    {
35
        tx_data[z] = motor.desired_speed[z];
39
        tx_data[z] = motor.desired_speed[z];
36
    }
40
    }
-
 
41
    FlightLog::info_FC("testing motor");
37
    com->send_cmd('t', ADDRESS_FC, tx_data, MAX_MOTORS, false);
42
    com->send_cmd('t', ADDRESS_FC, tx_data, MAX_MOTORS, false);
38
}
43
}
Line 39... Line 44...
39
 
44
 
40
void Handler::reset_motor() {
45
void Handler::reset_motor() {
Line 48... Line 53...
48
 
53
 
49
/**
54
/**
50
 * read mixer values from FlightCtrl
55
 * read mixer values from FlightCtrl
51
 */
56
 */
-
 
57
void Handler::read_motor_mixer() {
52
void Handler::read_motor_mixer() {
58
    FlightLog::info_FC("reading motor mixer values");
53
    char tx_data[1] = {0};
59
    char tx_data[1] = {0};
54
    //com->log("read motor mixer");
60
    //com->log("read motor mixer");
55
    com->send_cmd('n', ADDRESS_FC, tx_data, 1, true);
61
    com->send_cmd('n', ADDRESS_FC, tx_data, 1, true);
Line 56... Line 62...
56
}
62
}
57
 
63
 
58
/**
64
/**
59
 * write motor mixer values to FlightCtrl
65
 * write motor mixer values to FlightCtrl
-
 
66
 */
60
 */
67
void Handler::write_motor_mixer(char * tx_data, int length) {
61
void Handler::write_motor_mixer(char * tx_data, int length) {
68
    FlightLog::info_FC("setting motor mixer values");
Line 62... Line 69...
62
    com->send_cmd('m', ADDRESS_FC, tx_data, length, true);
69
    com->send_cmd('m', ADDRESS_FC, tx_data, length, true);
63
}
70
}
Line 69... Line 76...
69
//-------------NaviCtrl commands--------------------
76
//-------------NaviCtrl commands--------------------
70
/**
77
/**
71
 * set debug values for NaviCtrl
78
 * set debug values for NaviCtrl
72
 */
79
 */
73
void Handler::set_navictrl_debug(int speed) {
80
void Handler::set_navictrl_debug(int speed) {
-
 
81
    FlightLog::info_NC("setting NaviCtrl debug speed");
74
    char tx_data[1] = { speed };
82
    char tx_data[1] = { speed };
75
    com->send_cmd('o', ADDRESS_NC, tx_data, 1, false);
83
    com->send_cmd('o', ADDRESS_NC, tx_data, 1, false);
76
}
84
}
Line 77... Line 85...
77
 
85
 
Line 84... Line 92...
84
 
92
 
85
/**
93
/**
86
 * send a waypoint to the NaviCtrl (the MikroKopter will fly to the position emidiately)
94
 * send a waypoint to the NaviCtrl (the MikroKopter will fly to the position emidiately)
87
 */
95
 */
-
 
96
void Handler::send_waypoint(Waypoint_t desired_pos) {
88
void Handler::send_waypoint(Waypoint_t desired_pos) {
97
    FlightLog::info_GPS("sending Waypoint");
89
    com->send_cmd('s', ADDRESS_NC, (char *)&desired_pos, sizeof(desired_pos), false);
98
    com->send_cmd('s', ADDRESS_NC, (char *)&desired_pos, sizeof(desired_pos), false);
Line 90... Line 99...
90
}
99
}
91
 
100
 
92
/**
101
/**
93
 * add waypoint to waypoint list
102
 * add waypoint to waypoint list
-
 
103
 */
94
 */
104
void Handler::add_waypoint(Waypoint_t wp) {
95
void Handler::add_waypoint(Waypoint_t wp) {
105
    FlightLog::info_GPS("adding Waypoint");
Line 96... Line 106...
96
    com->send_cmd('w', ADDRESS_NC, (char *)&wp, sizeof(wp), false);
106
    com->send_cmd('w', ADDRESS_NC, (char *)&wp, sizeof(wp), false);
97
}
107
}
Line 104... Line 114...
104
    wp.Position.Status = INVALID;
114
    wp.Position.Status = INVALID;
105
    send_waypoint(wp);
115
    send_waypoint(wp);
106
}
116
}
107
//-------------switch between Hardware--------------------
117
//-------------switch between Hardware--------------------
108
void Handler::switch_navictrl() {
118
void Handler::switch_navictrl() {
-
 
119
    FlightLog::info("switching to NaviCtrl");
109
    char tx_data[6] = { 0x1B, 0x1B, 0x55, 0xAA, 0x00, '\r'};
120
    char tx_data[6] = { 0x1B, 0x1B, 0x55, 0xAA, 0x00, '\r'};
110
    com->send_cmd('#', ADDRESS_NC, tx_data, 6, false);
121
    com->send_cmd('#', ADDRESS_NC, tx_data, 6, false);
111
}
122
}
Line 112... Line 123...
112
 
123
 
-
 
124
void Handler::switch_flightctrl() {
113
void Handler::switch_flightctrl() {
125
    FlightLog::info("switching to FlightCtrl");
114
    char tx_data[1] = { 0 };
126
    char tx_data[1] = { 0 };
115
    com->send_cmd('u', ADDRESS_NC, tx_data, 1, false);
127
    com->send_cmd('u', ADDRESS_NC, tx_data, 1, false);
Line 116... Line 128...
116
}
128
}
-
 
129
 
117
 
130
void Handler::switch_mk3mag() {
118
void Handler::switch_mk3mag() {
131
    FlightLog::info("switching to MK3MAG");
119
    char tx_data[1] = { 1 };
132
    char tx_data[1] = { 1 };
Line 120... Line 133...
120
    com->send_cmd('u', ADDRESS_NC, tx_data, 1, false);
133
    com->send_cmd('u', ADDRESS_NC, tx_data, 1, false);
Line 127... Line 140...
127
 
140
 
128
/**
141
/**
129
 * set debug values for all components
142
 * set debug values for all components
130
 */
143
 */
-
 
144
void Handler::set_all_debug(int speed) {
131
void Handler::set_all_debug(int speed) {
145
    FlightLog::info("setting debug speed");
132
    char tx_data[1] = { speed };
146
    char tx_data[1] = { speed };
133
    com->send_cmd('d', ADDRESS_ALL, tx_data, 1, false);
147
    com->send_cmd('d', ADDRESS_ALL, tx_data, 1, false);
Line 134... Line 148...
134
}
148
}
Line 142... Line 156...
142
 
156
 
143
/**
157
/**
144
 * get all analog labels
158
 * get all analog labels
145
 */
159
 */
-
 
160
void Handler::get_analog() {
146
void Handler::get_analog() {
161
    FlightLog::info("get analog values");
147
    char tx_data[1] = { 0 };
162
    char tx_data[1] = { 0 };
148
    com->send_cmd('a', ADDRESS_ALL, tx_data, 1, true);
163
    com->send_cmd('a', ADDRESS_ALL, tx_data, 1, true);
Line 149... Line 164...
149
}
164
}
150
 
165
 
151
/**
166
/**
152
 * get values from LCD / show LCD
167
 * get values from LCD / show LCD
-
 
168
 */
153
 */
169
void Handler::show_lcd() {
154
void Handler::show_lcd() {
170
    FlightLog::info("show LCD");
155
    char tx_data[1] = {0};
171
    char tx_data[1] = {0};
Line 156... Line 172...
156
    com->send_cmd('l', ADDRESS_ALL, tx_data, 1, true);
172
    com->send_cmd('l', ADDRESS_ALL, tx_data, 1, true);
157
}
173
}
158
 
174
 
159
/**
175
/**
-
 
176
 * got to next LCD Page
160
 * got to next LCD Page
177
 */
161
 */
178
void Handler::lcd_up() {
162
void Handler::lcd_up() {
179
    FlightLog::info("going up one LCD page");
163
    char tx_data[2] = { 0, 0 };
180
    char tx_data[2] = { 0, 0 };
164
    if (data->lcd_cur != data->lcd_max)
181
    if (data->lcd_cur != data->lcd_max)
Line 165... Line 182...
165
        tx_data[0] = data->lcd_cur+1;
182
        tx_data[0] = data->lcd_cur+1;
166
    com->send_cmd('l', ADDRESS_ALL, tx_data, 1, true);
183
    com->send_cmd('l', ADDRESS_ALL, tx_data, 1, true);
167
}
184
}
168
 
185
 
-
 
186
/**
169
/**
187
 * got to previous LCD Page
170
 * got to previous LCD Page
188
 */
171
 */
189
void Handler::lcd_down() {
172
void Handler::lcd_down() {
190
    FlightLog::info("going down one LCD page");
173
    char tx_data[2] = { 0, 0 };
191
    char tx_data[2] = { 0, 0 };
Line 174... Line 192...
174
    if (data->lcd_cur != 0)
192
    if (data->lcd_cur != 0)
-
 
193
        tx_data[0] = data->lcd_cur-1;
175
        tx_data[0] = data->lcd_cur-1;
194
    com->send_cmd('l', ADDRESS_ALL, tx_data, 1, true);
176
    com->send_cmd('l', ADDRESS_ALL, tx_data, 1, true);
195
}
177
}
196
 
178
 
197
void Handler::get_version() {
Line 179... Line 198...
179
void Handler::get_version() {
198
    FlightLog::info("get version");
-
 
199
    //TODO: Check if is this correct or do we need data from switch_...
180
    //TODO: Check if is this correct or do we need data from switch_...
200
    char tx_data[1] = { 0 };
181
    char tx_data[1] = { 0 };
201
    com->send_cmd('v', ADDRESS_ALL, tx_data, 0, true);
182
    com->send_cmd('v', ADDRESS_ALL, tx_data, 0, true);
202
}
Line -... Line 203...
-
 
203
 
-
 
204
void Handler::get_ppm_channels() {
-
 
205
     FlightLog::info("get PPM channels");
-
 
206
    char tx_data[1] = { 0 };
-
 
207
    com->send_cmd('p', ADDRESS_ALL, tx_data, 0, false);
-
 
208
}
-
 
209
 
-
 
210
void Handler::new_navi_data(char * data) {
-
 
211
    std::string mode;
-
 
212
    switch(data[N_NC_FLAGS])
-
 
213
    {
-
 
214
        case 0x01 : mode = "Free"; break;
-
 
215
        case 0x02 : mode = "Position Hold"; break;
-
 
216
        case 0x04 : mode = "Coming Home"; break;
-
 
217
        case 0x08 : mode = "Range Limit"; break;
183
}
218
        case 0x10 : mode = "Serial Error"; break;
184
 
219
        case 0x20 : mode = "Target reached"; break;
185
void Handler::get_ppm_channels() {
220
        case 0x40 : mode = "Manual Control"; break;
186
    char tx_data[1] = { 0 };
221
    }
187
    com->send_cmd('p', ADDRESS_ALL, tx_data, 0, false);
222
   
188
}
223
}
189
 
224
 
190
/**
225
/**
-
 
226
 * receive data
191
 * receive data
227
 */
Line 192... Line 228...
192
 */
228
void Handler::receive_data(char * incomming, int length) {
193
void Handler::receive_data(char * incomming, int length) {
229
    if (incomming[0] != '#') {
194
    if (incomming[0] != '#')
230
        FlightLog::error("this frame is not correct");
195
        FlightLog::error("this frame is not correct");
231
        FlightLog::info(incomming);
Line 196... Line 232...
196
        FlightLog::info(incomming);
232
        return;
Line 197... Line 233...
197
        return;
233
    }
198
    int hardwareID = incomming[1] - 'a';
234
    int hardwareID = incomming[1] - 'a';
199
 
235
 
200
    //The cmd is also known as ID-Byte (or ID for short) in the wiki-dokumentation
236
    //The cmd is also known as ID-Byte (or ID for short) in the wiki-dokumentation
201
    char cmd = incomming[2];
237
    char cmd = incomming[2];
202
    //decode data
238
    //decoded data
203
    unsigned char data[150];
239
    unsigned char data[MAX_DATA_SIZE];
204
 
-
 
205
    Parser::decode64(incomming, length, data, 3);
-
 
206
 
240
 
207
    switch(hardwareID)
241
    Parser::decode64(incomming, length, data, 3);
208
    {
242
 
209
        case ADDRESS_FC :
243
    switch(hardwareID)
210
            switch(cmd)
244
    {
211
            {
245
        case ADDRESS_FC :
212
                // Motor-Mixer
246
            switch(cmd)
213
                case 'N' :
247
            {
214
                    //if (Parser::decode64(RX))
248
                // Motor-Mixer
215
                    //{
249
                case 'N' :
216
                        com->stop_resend();
250
                    com->stop_resend();
217
                        //decoded data
251
                    //decoded data
Line 218... Line 252...
218
                        FlightLog::info("received motortest values from FlightCtrl");
252
                    FlightLog::info_FC("received motortest values from FlightCtrl");
219
                        if (data[0] == VERSION_MIXER)
253
                    if (data[0] == VERSION_MIXER)
220
                        {
254
                    {
221
                            //f_MotorMixer->set_MotorConfig(RX);
255
                        //TODO: Handler::receivedMotorConfig(incomming)
222
                        }
256
                        //f_MotorMixer->set_MotorConfig(RX);
223
                    //}
257
                    }
224
                    break;
258
                    break;
225
                // Motor-Mixer Schreib-Bestätigung
259
                // Motor-Mixer Schreib-Bestätigung
Line 226... Line 260...
226
                case 'M' :
260
                case 'M' :
227
                    com->stop_resend();
261
                    com->stop_resend();
228
 
262
 
-
 
263
                    if (data[0] == 1)
-
 
264
                    {
-
 
265
                        FlightLog::info_FC("motor values written to FlightCtrl.");
-
 
266
                        //lb_Status->setText(tr("MotorMixer-Daten in FC geschrieben."));
-
 
267
                    } else {
-
 
268
                        FlightLog::warning("could not write motor values to FlightCtrl!");
-
 
269
                    }
229
                    if (data[0] == 1)
270
                    break;
230
                    {
271
 
231
                        FlightLog::info("motor values written to FlightCtrl.");
272
                // Stick-Belegung der Fernsteuerung
232
                        //lb_Status->setText(tr("MotorMixer-Daten in FC geschrieben."));
273
                case 'P' : // DONE 0.71g
233
                    } else {
274
                    FlightLog::info_FC("received stick-settings:");
Line 248... Line 289...
248
                    f_Settings->pb_K8->setValue(Parser::dataToInt(RX.decode, 16,true));*/
289
                    f_Settings->pb_K8->setValue(Parser::dataToInt(RX.decode, 16,true));*/
249
                    break;
290
                    break;
250
                // Settings lesen
291
                // Settings lesen
251
                case 'Q' : // DONE 0.71g
292
                case 'Q' : // DONE 0.71g
252
                    com->stop_resend();
293
                    com->stop_resend();
253
                    FlightLog::info("received settings from FlightCtrl");
294
                    FlightLog::info_FC("received settings");
254
                    if (data[1] == VERSION_SETTINGS)
295
                    if (data[1] == VERSION_SETTINGS)
255
                    {
296
                    {
256
                        int Settings_ID = data[0];
297
                        int settings_id = data[0];
-
 
298
                        std::stringstream d;
-
 
299
                        d << "Settings-ID: " << settings_id;
-
 
300
                        std::string val;
-
 
301
                        d >> val;
-
 
302
                        FlightLog::info_FC((char *)val.c_str());
-
 
303
                        for (int i = 0; i < MaxParameter; i++) {
-
 
304
                            d.flush();
-
 
305
                            d << "Value for Parameter nr. " << i << ": " << (int)data[i+2];
-
 
306
                            d >> val;
-
 
307
                            FlightLog::info_FC((char *)val.c_str());
-
 
308
                        }
-
 
309
                       
257
                        /*for (int a = 0; a < MaxParameter; a++)
310
                        /*for (int a = 0; a < MaxParameter; a++)
258
                        {
311
                        {
259
                            FCSettings[a] = RX.decode[a + 2];
312
                            FCSettings[a] = RX.decode[a + 2];
260
                        }
313
                        }
261
                        f_Settings->show_FCSettings(Settings_ID, FCSettings);
314
                        f_Settings->show_FCSettings(Settings_ID, FCSettings);
Line 279... Line 332...
279
                    }
332
                    }
280
                    break;
333
                    break;
281
                // Settings written
334
                // Settings written
282
                case 'S' : // DONE 0.71g
335
                case 'S' : // DONE 0.71g
283
                    com->stop_resend();
336
                    com->stop_resend();
284
                    FlightLog::info("settings written successfully to FlightCtrl");
337
                    FlightLog::info_FC("settings written successfully");
285
                    //TODO: QMessagebox("settings written successful") ?
338
                    //TODO: QMessagebox("settings written successful") ?
286
                    break;
339
                    break;
287
            }
340
            }
Line 288... Line 341...
288
 
341
 
289
        case ADDRESS_NC :
342
        case ADDRESS_NC :
290
            switch(cmd)
343
            switch(cmd)
291
            {
344
            {
292
                // Navigationsdaten
345
                // Navigationsdaten
293
                case 'O' : // NOT DONE 0.12h
346
                case 'O' : // NOT DONE 0.12h
294
                    //new_NaviData(RX);
347
                    //new_NaviData(RX);
295
                    FlightLog::info("received navigation data from NaviCtrl");
348
                    FlightLog::info_NC("received navigation data");
296
                    break;
349
                    break;
297
            }
350
            }
Line 298... Line 351...
298
//        case ADDRESS_MK3MAG :
351
//        case ADDRESS_MK3MAG :
Line 302... Line 355...
302
            {
355
            {
303
                // LCD-Anzeige
356
                // LCD-Anzeige
304
                case 'L' : // DONE 0.71g
357
                case 'L' : // DONE 0.71g
305
                    com->stop_resend();
358
                    com->stop_resend();
306
                    FlightLog::info("received LCD page.");
359
                    FlightLog::info("received LCD page.");
307
                    /*int LCD[150];
360
                    /*int LCD[MAX_DATA_SIZE];
308
                    memcpy(LCD,RX.decode, sizeof(RX.decode));
361
                    memcpy(LCD,RX.decode, sizeof(RX.decode));
Line 309... Line 362...
309
 
362
 
Line 310... Line 363...
310
                    f_LCD->show_Data(LCD);
363
                    f_LCD->show_Data(LCD);