Rev 449 | Rev 513 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
462 | Brean | 1 | #include "Handler.h" |
2 | #include <iostream> |
||
391 | Brean | 3 | |
4 | /** |
||
392 | Brean | 5 | * Constructor that gets a communication instance |
6 | */ |
||
440 | Brean | 7 | Handler::Handler(Communication * com, KopterData * data) { |
392 | Brean | 8 | this->com = com; |
440 | Brean | 9 | this->data = data; |
392 | Brean | 10 | } |
11 | |||
399 | Brean | 12 | //-------------FlightCtrl commands-------------------- |
392 | Brean | 13 | /** |
399 | Brean | 14 | * read settings from FlightCtrl (settings index 0x00-0x05) |
15 | */ |
||
16 | void Handler::get_flightctrl_settings(int index) { |
||
17 | char tx_data[2] = {index, 0}; |
||
18 | com->send_cmd('q', ADDRESS_FC, tx_data, 1, true); |
||
19 | } |
||
20 | |||
21 | /** |
||
22 | * write settings to FlightCtrl |
||
23 | */ |
||
24 | void Handler::set_flightctrl_settings(char * tx_data) { |
||
25 | com->send_cmd('s', ADDRESS_FC, tx_data, MaxParameter+2, true); |
||
26 | } |
||
27 | |||
28 | /** |
||
29 | * test one or more motors |
||
30 | */ |
||
442 | Brean | 31 | void Handler::motor_test(sMotorData motor) { |
32 | char tx_data[MAX_MOTORS]; |
||
33 | for (int z = 0; z<MAX_MOTORS; z++) |
||
399 | Brean | 34 | { |
442 | Brean | 35 | tx_data[z] = motor.desired_speed[z]; |
399 | Brean | 36 | } |
442 | Brean | 37 | com->send_cmd('t', ADDRESS_FC, tx_data, MAX_MOTORS, false); |
399 | Brean | 38 | } |
39 | |||
440 | Brean | 40 | void Handler::reset_motor() { |
442 | Brean | 41 | for (int z = 0; z<MAX_MOTORS; z++) |
440 | Brean | 42 | { |
442 | Brean | 43 | data->motor.desired_speed[z] = 0; |
440 | Brean | 44 | } |
45 | |||
442 | Brean | 46 | motor_test(data->motor); |
440 | Brean | 47 | } |
48 | |||
399 | Brean | 49 | /** |
391 | Brean | 50 | * read mixer values from FlightCtrl |
51 | */ |
||
440 | Brean | 52 | void Handler::read_motor_mixer() { |
396 | Brean | 53 | char tx_data[1] = {0}; |
54 | //com->log("read motor mixer"); |
||
397 | Brean | 55 | com->send_cmd('n', ADDRESS_FC, tx_data, 1, true); |
393 | Brean | 56 | } |
57 | |||
399 | Brean | 58 | /** |
59 | * write motor mixer values to FlightCtrl |
||
60 | */ |
||
440 | Brean | 61 | void Handler::write_motor_mixer(char * tx_data, int length) { |
399 | Brean | 62 | com->send_cmd('m', ADDRESS_FC, tx_data, length, true); |
63 | } |
||
64 | |||
440 | Brean | 65 | int Handler::get_motor_config(char * tx_data) { |
66 | return -1; |
||
396 | Brean | 67 | } |
68 | |||
399 | Brean | 69 | //-------------NaviCtrl commands-------------------- |
70 | /** |
||
71 | * set debug values for NaviCtrl |
||
72 | */ |
||
73 | void Handler::set_navictrl_debug(int speed) { |
||
74 | char tx_data[1] = { speed }; |
||
75 | com->send_cmd('o', ADDRESS_NC, tx_data, 1, false); |
||
76 | } |
||
77 | |||
78 | /** |
||
79 | * stop debug for NaviCtrl |
||
80 | */ |
||
81 | void Handler::stop_navictrl_debug() { |
||
82 | set_navictrl_debug(0); |
||
83 | } |
||
84 | |||
85 | /** |
||
449 | Brean | 86 | * send a waypoint to the NaviCtrl (the MikroKopter will fly to the position emidiately) |
399 | Brean | 87 | */ |
88 | void Handler::send_waypoint(Waypoint_t desired_pos) { |
||
89 | com->send_cmd('s', ADDRESS_NC, (char *)&desired_pos, sizeof(desired_pos), false); |
||
90 | } |
||
91 | |||
92 | /** |
||
93 | * add waypoint to waypoint list |
||
94 | */ |
||
95 | void Handler::add_waypoint(Waypoint_t wp) { |
||
96 | com->send_cmd('w', ADDRESS_NC, (char *)&wp, sizeof(wp), false); |
||
97 | } |
||
98 | |||
99 | /** |
||
100 | * clear waypoint list on MK |
||
101 | */ |
||
102 | void Handler::delete_waypoints() { |
||
103 | Waypoint_t wp; |
||
104 | wp.Position.Status = INVALID; |
||
105 | send_waypoint(wp); |
||
106 | } |
||
107 | //-------------switch between Hardware-------------------- |
||
108 | void Handler::switch_navictrl() { |
||
109 | char tx_data[6] = { 0x1B, 0x1B, 0x55, 0xAA, 0x00, '\r'}; |
||
110 | com->send_cmd('#', ADDRESS_NC, tx_data, 6, false); |
||
111 | } |
||
112 | |||
113 | void Handler::switch_flightctrl() { |
||
114 | char tx_data[1] = { 0 }; |
||
115 | com->send_cmd('u', ADDRESS_NC, tx_data, 1, false); |
||
116 | } |
||
117 | |||
118 | void Handler::switch_mk3mag() { |
||
119 | char tx_data[1] = { 1 }; |
||
120 | com->send_cmd('u', ADDRESS_NC, tx_data, 1, false); |
||
121 | } |
||
122 | |||
123 | //-------------commands for MK3MAG----------------- |
||
124 | |||
125 | |||
126 | //-------------commands for all-------------------- |
||
127 | |||
128 | /** |
||
129 | * set debug values for all components |
||
130 | */ |
||
131 | void Handler::set_all_debug(int speed) { |
||
132 | char tx_data[1] = { speed }; |
||
133 | com->send_cmd('d', ADDRESS_ALL, tx_data, 1, false); |
||
134 | } |
||
135 | |||
136 | /** |
||
137 | * stop debug for all components |
||
138 | */ |
||
139 | void Handler::stop_all_debug() { |
||
140 | set_all_debug(0); |
||
141 | } |
||
142 | |||
143 | /** |
||
144 | * get all analog labels |
||
145 | */ |
||
146 | void Handler::get_analog() { |
||
147 | char tx_data[1] = { 0 }; |
||
148 | com->send_cmd('a', ADDRESS_ALL, tx_data, 1, true); |
||
149 | } |
||
150 | |||
151 | /** |
||
152 | * get values from LCD / show LCD |
||
153 | */ |
||
154 | void Handler::show_lcd() { |
||
155 | char tx_data[1] = {0}; |
||
156 | com->send_cmd('l', ADDRESS_ALL, tx_data, 1, true); |
||
157 | } |
||
158 | |||
159 | /** |
||
160 | * got to next LCD Page |
||
161 | */ |
||
162 | void Handler::lcd_up() { |
||
163 | char tx_data[2] = { 0, 0 }; |
||
440 | Brean | 164 | if (data->lcd_cur != data->lcd_max) |
165 | tx_data[0] = data->lcd_cur+1; |
||
399 | Brean | 166 | com->send_cmd('l', ADDRESS_ALL, tx_data, 1, true); |
167 | } |
||
168 | |||
169 | /** |
||
170 | * got to previous LCD Page |
||
171 | */ |
||
172 | void Handler::lcd_down() { |
||
173 | char tx_data[2] = { 0, 0 }; |
||
440 | Brean | 174 | if (data->lcd_cur != 0) |
175 | tx_data[0] = data->lcd_cur-1; |
||
399 | Brean | 176 | com->send_cmd('l', ADDRESS_ALL, tx_data, 1, true); |
177 | } |
||
178 | |||
179 | void Handler::get_version() { |
||
180 | //TODO: Check if is this correct or do we need data from switch_... |
||
181 | char tx_data[1] = { 0 }; |
||
182 | com->send_cmd('v', ADDRESS_ALL, tx_data, 0, true); |
||
183 | } |
||
184 | |||
185 | void Handler::get_ppm_channels() { |
||
186 | char tx_data[1] = { 0 }; |
||
187 | com->send_cmd('p', ADDRESS_ALL, tx_data, 0, false); |
||
188 | } |
||
189 | |||
190 | /** |
||
191 | * receive data |
||
192 | */ |
||
449 | Brean | 193 | //Parser::decode64(data); |
194 | void Handler::receive_data(int hardwareID, int cmd, char * data) { |
||
393 | Brean | 195 | switch(hardwareID) |
196 | { |
||
197 | case ADDRESS_FC : |
||
449 | Brean | 198 | switch(cmd) |
393 | Brean | 199 | { |
200 | // Motor-Mixer |
||
201 | case 'N' : |
||
449 | Brean | 202 | //if (Parser::decode64(RX)) |
203 | //{ |
||
397 | Brean | 204 | com->stop_resend(); |
449 | Brean | 205 | //decoded data |
206 | if (data[0] == VERSION_MIXER) |
||
393 | Brean | 207 | { |
396 | Brean | 208 | //f_MotorMixer->set_MotorConfig(RX); |
393 | Brean | 209 | } |
449 | Brean | 210 | //} |
211 | break; |
||
393 | Brean | 212 | // Motor-Mixer Schreib-Bestätigung |
213 | case 'M' : |
||
449 | Brean | 214 | com->stop_resend(); |
215 | |||
216 | if (data[0] == 1) |
||
393 | Brean | 217 | { |
449 | Brean | 218 | //lb_Status->setText(tr("MotorMixer-Daten in FC geschrieben.")); |
393 | Brean | 219 | } |
449 | Brean | 220 | break; |
393 | Brean | 221 | |
222 | // Stick-Belegung der Fernsteuerung |
||
223 | case 'P' : // DONE 0.71g |
||
449 | Brean | 224 | /*f_Settings->pb_K1->setValue(Parser::dataToInt(RX.decode, 2,true)); |
225 | f_Settings->pb_K2->setValue(Parser::dataToInt(RX.decode, 4,true)); |
||
226 | f_Settings->pb_K3->setValue(Parser::dataToInt(RX.decode, 6,true)); |
||
227 | f_Settings->pb_K4->setValue(Parser::dataToInt(RX.decode, 8,true)); |
||
228 | f_Settings->pb_K5->setValue(Parser::dataToInt(RX.decode, 10 ,true)); |
||
229 | f_Settings->pb_K6->setValue(Parser::dataToInt(RX.decode, 12,true)); |
||
230 | f_Settings->pb_K7->setValue(Parser::dataToInt(RX.decode, 14,true)); |
||
231 | f_Settings->pb_K8->setValue(Parser::dataToInt(RX.decode, 16,true));*/ |
||
232 | break; |
||
393 | Brean | 233 | // Settings lesen |
234 | case 'Q' : // DONE 0.71g |
||
449 | Brean | 235 | com->stop_resend(); |
236 | |||
237 | if (data[1] == VERSION_SETTINGS) |
||
393 | Brean | 238 | { |
449 | Brean | 239 | int Settings_ID = data[0]; |
240 | /*for (int a = 0; a < MaxParameter; a++) |
||
393 | Brean | 241 | { |
449 | Brean | 242 | FCSettings[a] = RX.decode[a + 2]; |
393 | Brean | 243 | } |
449 | Brean | 244 | f_Settings->show_FCSettings(Settings_ID, FCSettings); |
245 | f_Settings->pb_Read->setEnabled(true); |
||
246 | f_Settings->pb_Write->setEnabled(true);*/ |
||
247 | } |
||
248 | else |
||
249 | { |
||
250 | /*f_Settings->pb_Read->setDisabled(true); |
||
251 | f_Settings->pb_Write->setDisabled(true); |
||
393 | Brean | 252 | |
449 | Brean | 253 | QString name = QString("Versionen inkompatibel.\n") + |
254 | QString("Version von GroundStation benoetigt: ") + |
||
255 | QString(VERSION_SETTINGS) + |
||
256 | QString("\nVersion auf der FlightCtrl: ") + |
||
257 | QString(RX.decode[1]) + |
||
258 | QString("\nParameterbearbeitung nicht moeglich."); |
||
259 | QMessageBox::warning(this, QA_NAME, |
||
260 | name, QMessageBox::Ok);*/ |
||
393 | Brean | 261 | } |
449 | Brean | 262 | break; |
396 | Brean | 263 | // Settings written |
393 | Brean | 264 | case 'S' : // DONE 0.71g |
397 | Brean | 265 | com->stop_resend(); |
396 | Brean | 266 | //TODO: QMessagebox("settings written successful") ? |
449 | Brean | 267 | break; |
393 | Brean | 268 | } |
269 | |||
270 | case ADDRESS_NC : |
||
449 | Brean | 271 | switch(cmd) |
393 | Brean | 272 | { |
273 | // Navigationsdaten |
||
274 | case 'O' : // NOT DONE 0.12h |
||
449 | Brean | 275 | //new_NaviData(RX); |
276 | break; |
||
393 | Brean | 277 | } |
278 | // case ADDRESS_MK3MAG : |
||
279 | |||
280 | default : |
||
449 | Brean | 281 | switch(cmd) |
393 | Brean | 282 | { |
283 | // LCD-Anzeige |
||
284 | case 'L' : // DONE 0.71g |
||
449 | Brean | 285 | com->stop_resend(); |
393 | Brean | 286 | |
449 | Brean | 287 | /*int LCD[150]; |
288 | memcpy(LCD,RX.decode, sizeof(RX.decode)); |
||
393 | Brean | 289 | |
449 | Brean | 290 | f_LCD->show_Data(LCD); |
393 | Brean | 291 | |
449 | Brean | 292 | LCD_Page = RX.decode[0]; |
293 | LCD_MAX_Page = RX.decode[1]; |
||
294 | */ |
||
295 | break; |
||
393 | Brean | 296 | // Analoglabels |
297 | case 'A' : // DONE 0.71g |
||
449 | Brean | 298 | com->stop_resend(); |
393 | Brean | 299 | |
449 | Brean | 300 | //check position |
301 | if (data[0] != 31) { |
||
302 | /* |
||
303 | Settings->Analog1.Label[Position] = ToolBox::dataToQString(RX.decode,1,17).trimmed(); |
||
304 | if (Settings->Analog1.Label[Position] == "") |
||
393 | Brean | 305 | { |
449 | Brean | 306 | Settings->Analog1.Label[Position] = "A-" + QString("%1").arg(Position); |
393 | Brean | 307 | } |
449 | Brean | 308 | Position ++; |
309 | TX_Data[0] = Position; |
||
310 | o_Connection->send_Cmd('a', ADDRESS_ALL, TX_Data, 1, true);*/ |
||
311 | } else { |
||
312 | /* |
||
313 | for (int a = 0; a < MaxAnalog; a++) |
||
393 | Brean | 314 | { |
449 | Brean | 315 | lb_Analog[a]->setText(Settings->Analog1.Label[a]); |
393 | Brean | 316 | } |
449 | Brean | 317 | Settings->Analog1.Version = QString(Mode.Version); |
318 | Settings->write_Settings_AnalogLabels(HardwareID); |
||
319 | config_Plot();*/ |
||
393 | Brean | 320 | } |
449 | Brean | 321 | break; |
393 | Brean | 322 | // Debug-Daten |
323 | case 'D' : // DONE 0.71g |
||
449 | Brean | 324 | for (int i = 0; i < MaxAnalog; i++) { |
462 | Brean | 325 | std::cout << Parser::dataToInt(data, (i * 2) + 2) << std::endl; |
393 | Brean | 326 | } |
449 | Brean | 327 | //show_DebugData(); |
328 | break; |
||
393 | Brean | 329 | // Version |
330 | case 'V' : // DONE 0.71h |
||
449 | Brean | 331 | com->stop_resend(); |
332 | /* |
||
333 | Mode.ID = HardwareID; |
||
334 | Mode.VERSION_MAJOR = RX.decode[0]; |
||
335 | Mode.VERSION_MINOR = RX.decode[1]; |
||
336 | Mode.VERSION_PATCH = RX.decode[4]; |
||
337 | Mode.VERSION_SERIAL_MAJOR = RX.decode[2]; |
||
338 | Mode.VERSION_SERIAL_MINOR = RX.decode[3]; |
||
393 | Brean | 339 | |
449 | Brean | 340 | Mode.Hardware = HardwareType[Mode.ID]; |
341 | //TODO: Funktion im Handler get_version() oder sowas |
||
342 | QString version = QString("%1").arg(RX.decode[0]) + "." + |
||
343 | QString("%1").arg(RX.decode[1]) + |
||
344 | QString(RX.decode[4] + 'a'); |
||
345 | Mode.Version = version.toLatin1().data; |
||
346 | setWindowTitle(QA_NAME + " v" + QA_VERSION + " - " + |
||
347 | Mode.Hardware + " " + |
||
348 | Mode.Version); |
||
393 | Brean | 349 | |
449 | Brean | 350 | if (Mode.VERSION_SERIAL_MAJOR != VERSION_SERIAL_MAJOR) |
351 | { |
||
393 | Brean | 352 | // AllowSend = false; |
449 | Brean | 353 | QMessageBox::warning(this, QA_NAME, |
354 | tr("Serielles Protokoll Inkompatibel. \nBitte neue Programmversion installieren,"), QMessageBox::Ok); |
||
355 | } |
||
393 | Brean | 356 | |
449 | Brean | 357 | if (ac_NoDebug->isChecked()) |
358 | { |
||
359 | TX_Data[0] = 0; |
||
360 | } |
||
361 | else |
||
362 | if (ac_FastDebug->isChecked()) |
||
363 | { |
||
364 | TX_Data[0] = Settings->Data.Debug_Fast / 10; |
||
365 | } |
||
366 | else |
||
367 | { |
||
368 | TX_Data[0] = Settings->Data.Debug_Slow / 10; |
||
369 | } |
||
370 | |||
371 | o_Connection->send_Cmd('d', ADDRESS_ALL, TX_Data, 1, false); |
||
372 | |||
373 | // Wenn MK3MAG dann andauernd Daten neu anfragen. |
||
374 | if (Mode.ID == ADDRESS_MK3MAG) |
||
375 | { |
||
376 | TickerEvent[3] = true; |
||
377 | rb_SelMag->setChecked(true); |
||
378 | } |
||
379 | |||
380 | // Wenn NaviCtrl dann hier. |
||
381 | if (Mode.ID == ADDRESS_NC) |
||
382 | { |
||
383 | rb_SelNC->setChecked(true); |
||
384 | |||
385 | if (ac_NoNavi->isChecked()) |
||
393 | Brean | 386 | { |
387 | TX_Data[0] = 0; |
||
388 | } |
||
389 | else |
||
449 | Brean | 390 | if (ac_FastNavi->isChecked()) |
393 | Brean | 391 | { |
449 | Brean | 392 | TX_Data[0] = Settings->Data.Navi_Fast / 10; |
393 | Brean | 393 | } |
394 | else |
||
395 | { |
||
449 | Brean | 396 | TX_Data[0] = Settings->Data.Navi_Slow / 10; |
393 | Brean | 397 | } |
398 | |||
449 | Brean | 399 | o_Connection->send_Cmd('o', ADDRESS_NC, TX_Data, 1, false); |
400 | } |
||
393 | Brean | 401 | |
402 | |||
449 | Brean | 403 | // Wenn FlightCtrl dann Settings abfragen. |
404 | if (Mode.ID == ADDRESS_FC) |
||
405 | { |
||
406 | rb_SelFC->setChecked(true); |
||
393 | Brean | 407 | { |
449 | Brean | 408 | TX_Data[0] = 0xff; |
409 | TX_Data[1] = 0; |
||
393 | Brean | 410 | |
449 | Brean | 411 | // DEP: Raus wenn Resend implementiert. |
393 | Brean | 412 | // ToolBox::Wait(SLEEP); |
449 | Brean | 413 | o_Connection->send_Cmd('q', ADDRESS_FC, TX_Data, 1, true); |
414 | qDebug("FC - Get Settings"); |
||
393 | Brean | 415 | } |
449 | Brean | 416 | } |
417 | // Wenn nicht Lesen und Schreiben der Settings deaktivieren. |
||
418 | else |
||
419 | { |
||
420 | f_Settings->pb_Read->setDisabled(true); |
||
421 | f_Settings->pb_Write->setDisabled(true); |
||
422 | } |
||
393 | Brean | 423 | |
449 | Brean | 424 | Settings->read_Settings_Analog(HardwareID); |
425 | Settings->read_Settings_AnalogLabels(HardwareID); |
||
393 | Brean | 426 | |
449 | Brean | 427 | if (Settings->Analog1.Version != QString(Mode.Version)) |
428 | { |
||
429 | lb_Status->setText(tr("Analoglabel-Version unterschiedlich. Lese Analoglabels neu aus.")); |
||
430 | slot_ac_GetLabels(); |
||
393 | Brean | 431 | } |
449 | Brean | 432 | else |
433 | for (int a = 0; a < MaxAnalog; a++) |
||
434 | { |
||
435 | lb_Analog[a]->setText(Settings->Analog1.Label[a]); |
||
436 | } |
||
437 | config_Plot();*/ |
||
438 | break; |
||
393 | Brean | 439 | } |
440 | } |
||
391 | Brean | 441 | } |