Subversion Repositories Projects

Rev

Rev 500 | Go to most recent revision | Show entire file | Regard 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>
3
 
5
 
4
/**
6
/**
5
 * Constructor that gets a communication instance
7
 * Constructor that gets a communication instance
6
 */
8
 */
7
Handler::Handler(Communication * com, KopterData * data) {
9
Handler::Handler(Communication * com, KopterData * data) {
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
}
20
 
23
 
21
/**
24
/**
22
 * write settings to FlightCtrl
25
 * write settings to FlightCtrl
23
 */
26
 */
24
void Handler::set_flightctrl_settings(char * tx_data) {
27
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);
26
}
30
}
27
 
31
 
28
/**
32
/**
29
 * test one or more motors
33
 * test one or more motors
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
}
39
 
44
 
40
void Handler::reset_motor() {
45
void Handler::reset_motor() {
41
    for (int z = 0; z<MAX_MOTORS; z++)
46
    for (int z = 0; z<MAX_MOTORS; z++)
Line 48... Line 53...
48
 
53
 
49
/**
54
/**
50
 * read mixer values from FlightCtrl
55
 * read mixer values from FlightCtrl
51
 */
56
 */
52
void Handler::read_motor_mixer() {
57
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);
56
}
62
}
57
 
63
 
58
/**
64
/**
59
 * write motor mixer values to FlightCtrl
65
 * write motor mixer values to FlightCtrl
60
 */
66
 */
61
void Handler::write_motor_mixer(char * tx_data, int length) {
67
void Handler::write_motor_mixer(char * tx_data, int length) {
-
 
68
    FlightLog::info_FC("setting motor mixer values");
62
    com->send_cmd('m', ADDRESS_FC, tx_data, length, true);
69
    com->send_cmd('m', ADDRESS_FC, tx_data, length, true);
63
}
70
}
64
 
71
 
65
int Handler::get_motor_config(char * tx_data) {
72
int Handler::get_motor_config(char * tx_data) {
66
    return -1;
73
    return -1;
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
}
77
 
85
 
78
/**
86
/**
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
 */
88
void Handler::send_waypoint(Waypoint_t desired_pos) {
96
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);
90
}
99
}
91
 
100
 
92
/**
101
/**
93
 * add waypoint to waypoint list
102
 * add waypoint to waypoint list
94
 */
103
 */
95
void Handler::add_waypoint(Waypoint_t wp) {
104
void Handler::add_waypoint(Waypoint_t wp) {
-
 
105
    FlightLog::info_GPS("adding Waypoint");
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
}
98
 
108
 
99
/**
109
/**
100
 * clear waypoint list on MK
110
 * clear waypoint list on MK
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
}
112
 
123
 
113
void Handler::switch_flightctrl() {
124
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);
116
}
128
}
117
 
129
 
118
void Handler::switch_mk3mag() {
130
void Handler::switch_mk3mag() {
-
 
131
    FlightLog::info("switching to MK3MAG");
119
    char tx_data[1] = { 1 };
132
    char tx_data[1] = { 1 };
120
    com->send_cmd('u', ADDRESS_NC, tx_data, 1, false);
133
    com->send_cmd('u', ADDRESS_NC, tx_data, 1, false);
121
}
134
}
122
 
135
 
123
//-------------commands for MK3MAG-----------------
136
//-------------commands for MK3MAG-----------------
Line 127... Line 140...
127
 
140
 
128
/**
141
/**
129
 * set debug values for all components
142
 * set debug values for all components
130
 */
143
 */
131
void Handler::set_all_debug(int speed) {
144
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);
134
}
148
}
135
 
149
 
136
/**
150
/**
Line 142... Line 156...
142
 
156
 
143
/**
157
/**
144
 * get all analog labels
158
 * get all analog labels
145
 */
159
 */
146
void Handler::get_analog() {
160
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);
149
}
164
}
150
 
165
 
151
/**
166
/**
152
 * get values from LCD / show LCD
167
 * get values from LCD / show LCD
153
 */
168
 */
154
void Handler::show_lcd() {
169
void Handler::show_lcd() {
-
 
170
    FlightLog::info("show LCD");
155
    char tx_data[1] = {0};
171
    char tx_data[1] = {0};
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
/**
160
 * got to next LCD Page
176
 * got to next LCD Page
161
 */
177
 */
162
void Handler::lcd_up() {
178
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)
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
 
169
/**
186
/**
170
 * got to previous LCD Page
187
 * got to previous LCD Page
171
 */
188
 */
172
void Handler::lcd_down() {
189
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 };
174
    if (data->lcd_cur != 0)
192
    if (data->lcd_cur != 0)
175
        tx_data[0] = data->lcd_cur-1;
193
        tx_data[0] = data->lcd_cur-1;
176
    com->send_cmd('l', ADDRESS_ALL, tx_data, 1, true);
194
    com->send_cmd('l', ADDRESS_ALL, tx_data, 1, true);
177
}
195
}
178
 
196
 
179
void Handler::get_version() {
197
void Handler::get_version() {
-
 
198
    FlightLog::info("get version");
180
    //TODO: Check if is this correct or do we need data from switch_...
199
    //TODO: Check if is this correct or do we need data from switch_...
181
    char tx_data[1] = { 0 };
200
    char tx_data[1] = { 0 };
182
    com->send_cmd('v', ADDRESS_ALL, tx_data, 0, true);
201
    com->send_cmd('v', ADDRESS_ALL, tx_data, 0, true);
183
}
202
}
184
 
203
 
185
void Handler::get_ppm_channels() {
204
void Handler::get_ppm_channels() {
-
 
205
     FlightLog::info("get PPM channels");
186
    char tx_data[1] = { 0 };
206
    char tx_data[1] = { 0 };
187
    com->send_cmd('p', ADDRESS_ALL, tx_data, 0, false);
207
    com->send_cmd('p', ADDRESS_ALL, tx_data, 0, false);
188
}
208
}
189
 
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;
-
 
218
        case 0x10 : mode = "Serial Error"; break;
-
 
219
        case 0x20 : mode = "Target reached"; break;
-
 
220
        case 0x40 : mode = "Manual Control"; break;
-
 
221
    }
-
 
222
   
-
 
223
}
-
 
224
 
190
/**
225
/**
191
 * receive data
226
 * receive data
192
 */
227
 */
193
void Handler::receive_data(char * incomming, int length) {
228
void Handler::receive_data(char * incomming, int length) {
194
    if (incomming[0] != '#')
229
    if (incomming[0] != '#') {
195
        FlightLog::error("this frame is not correct");
230
        FlightLog::error("this frame is not correct");
196
        FlightLog::info(incomming);
231
        FlightLog::info(incomming);
197
        return;
232
        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
 
240
 
205
    Parser::decode64(incomming, length, data, 3);
241
    Parser::decode64(incomming, length, data, 3);
206
 
242
 
207
    switch(hardwareID)
243
    switch(hardwareID)
208
    {
244
    {
209
        case ADDRESS_FC :
245
        case ADDRESS_FC :
210
            switch(cmd)
246
            switch(cmd)
211
            {
247
            {
212
                // Motor-Mixer
248
                // Motor-Mixer
213
                case 'N' :
249
                case 'N' :
214
                    //if (Parser::decode64(RX))
-
 
215
                    //{
-
 
216
                        com->stop_resend();
250
                    com->stop_resend();
217
                        //decoded data
251
                    //decoded data
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
                    {
-
 
255
                        //TODO: Handler::receivedMotorConfig(incomming)
221
                            //f_MotorMixer->set_MotorConfig(RX);
256
                        //f_MotorMixer->set_MotorConfig(RX);
222
                        }
257
                    }
223
                    //}
-
 
224
                    break;
258
                    break;
225
                // Motor-Mixer Schreib-Best├Ątigung
259
                // Motor-Mixer Schreib-Best├Ątigung
226
                case 'M' :
260
                case 'M' :
227
                    com->stop_resend();
261
                    com->stop_resend();
228
 
262
 
229
                    if (data[0] == 1)
263
                    if (data[0] == 1)
230
                    {
264
                    {
231
                        FlightLog::info("motor values written to FlightCtrl.");
265
                        FlightLog::info_FC("motor values written to FlightCtrl.");
232
                        //lb_Status->setText(tr("MotorMixer-Daten in FC geschrieben."));
266
                        //lb_Status->setText(tr("MotorMixer-Daten in FC geschrieben."));
233
                    } else {
267
                    } else {
234
                        FlightLog::warning("could not write motor values to FlightCtrl!");
268
                        FlightLog::warning("could not write motor values to FlightCtrl!");
235
                    }
269
                    }
236
                    break;
270
                    break;
237
 
271
 
238
                // Stick-Belegung der Fernsteuerung
272
                // Stick-Belegung der Fernsteuerung
239
                case 'P' : // DONE 0.71g
273
                case 'P' : // DONE 0.71g
240
                    FlightLog::info("received stick-settings from FlightCtrl:");
274
                    FlightLog::info_FC("received stick-settings:");
-
 
275
                    for (int i = 1; i <= 8; i++) {
-
 
276
                        std::stringstream d;
-
 
277
                        d << "Stickvalue for Stick nr. " << i << ": " << Parser::dataToInt((char *)data, i*2, true);;
-
 
278
                        std::string val;
-
 
279
                        d >> val;
-
 
280
                        FlightLog::info_FC((char *)val.c_str());
-
 
281
                    }
241
                    /*f_Settings->pb_K1->setValue(Parser::dataToInt(RX.decode,  2,true));
282
                    /*f_Settings->pb_K1->setValue(Parser::dataToInt(RX.decode,  2,true));
242
                    f_Settings->pb_K2->setValue(Parser::dataToInt(RX.decode,  4,true));
283
                    f_Settings->pb_K2->setValue(Parser::dataToInt(RX.decode,  4,true));
243
                    f_Settings->pb_K3->setValue(Parser::dataToInt(RX.decode,  6,true));
284
                    f_Settings->pb_K3->setValue(Parser::dataToInt(RX.decode,  6,true));
244
                    f_Settings->pb_K4->setValue(Parser::dataToInt(RX.decode,  8,true));
285
                    f_Settings->pb_K4->setValue(Parser::dataToInt(RX.decode,  8,true));
245
                    f_Settings->pb_K5->setValue(Parser::dataToInt(RX.decode, 10 ,true));
286
                    f_Settings->pb_K5->setValue(Parser::dataToInt(RX.decode, 10 ,true));
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
            }
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
            }
298
//        case ADDRESS_MK3MAG :
351
//        case ADDRESS_MK3MAG :
299
 
352
 
300
        default :
353
        default :
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));
309
 
362
 
310
                    f_LCD->show_Data(LCD);
363
                    f_LCD->show_Data(LCD);
311
 
364
 
312
                    LCD_Page     = RX.decode[0];
365
                    LCD_Page     = RX.decode[0];