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