[44] | 1 | // created: 2016/06/01
|
---|
| 2 | // filename: PidUav.cpp
|
---|
| 3 | //
|
---|
| 4 | // author: Gildas Bayard
|
---|
| 5 | // Copyright Heudiasyc UMR UTC/CNRS 7253
|
---|
| 6 | //
|
---|
| 7 | // version: $Id: $
|
---|
| 8 | //
|
---|
| 9 | // purpose: Uav class showing the incidence of pid parameters choice
|
---|
| 10 | //
|
---|
| 11 | //
|
---|
| 12 | /*********************************************************************/
|
---|
| 13 |
|
---|
| 14 | #include "PidUav.h"
|
---|
| 15 | #include <TargetController.h>
|
---|
| 16 | #include <cvmatrix.h>
|
---|
| 17 | #include <GridLayout.h>
|
---|
| 18 | #include <DataPlot1D.h>
|
---|
| 19 | #include <Tab.h>
|
---|
| 20 | #include <TabWidget.h>
|
---|
| 21 | #include <GroupBox.h>
|
---|
| 22 | #include <DoubleSpinBox.h>
|
---|
| 23 | #include <FrameworkManager.h>
|
---|
| 24 | #include <MetaDualShock3.h>
|
---|
| 25 | #include <Pid.h>
|
---|
| 26 | #include <AhrsData.h>
|
---|
| 27 |
|
---|
| 28 | #include <stdio.h>
|
---|
| 29 |
|
---|
| 30 | using namespace std;
|
---|
[105] | 31 | using namespace flair::core;
|
---|
| 32 | using namespace flair::sensor;
|
---|
| 33 | using namespace flair::gui;
|
---|
| 34 | using namespace flair::filter;
|
---|
| 35 | using namespace flair::meta;
|
---|
[44] | 36 |
|
---|
| 37 | /*
|
---|
| 38 | ** ===================================================================
|
---|
| 39 | ** constructor and destructor
|
---|
| 40 | **
|
---|
| 41 | ** ===================================================================
|
---|
| 42 | */
|
---|
[105] | 43 | PidUav::PidUav(Uav *uav, TargetController *controller)
|
---|
| 44 | : behaviourMode(BehaviourMode_t::Default),
|
---|
[236] | 45 | UavStateMachine(controller) {
|
---|
[105] | 46 | my_uRoll = new Pid(setupLawTab->At(1, 1), "custom uRoll");
|
---|
| 47 | my_uRoll->UseDefaultPlot(graphLawTab->LastRowLastCol());
|
---|
| 48 | my_uPitch = new Pid(setupLawTab->At(1, 0), "custom uPitch");
|
---|
| 49 | my_uPitch->UseDefaultPlot(graphLawTab->NewRow());
|
---|
[44] | 50 | }
|
---|
| 51 |
|
---|
| 52 | void PidUav::SignalEvent(Event_t event) {
|
---|
[105] | 53 | UavStateMachine::SignalEvent(event);
|
---|
| 54 | switch (event) {
|
---|
| 55 | case Event_t::TakingOff:
|
---|
| 56 | // always take off in default mode
|
---|
| 57 | behaviourMode = BehaviourMode_t::Default;
|
---|
| 58 | break;
|
---|
| 59 | case Event_t::EnteringFailSafeMode:
|
---|
| 60 | // return to default mode
|
---|
| 61 | Thread::Info("Entering failSafe mode\n");
|
---|
| 62 | behaviourMode = BehaviourMode_t::Default;
|
---|
| 63 | break;
|
---|
| 64 | }
|
---|
[44] | 65 | }
|
---|
| 66 |
|
---|
| 67 | bool PidUav::StartCustomMode() {
|
---|
[105] | 68 | // ask UavStateMachine to enter in custom torques
|
---|
| 69 | if (SetTorqueMode(TorqueMode_t::Custom)) {
|
---|
| 70 | Thread::Info("CustomTorques: start\n");
|
---|
| 71 | my_uPitch->Reset();
|
---|
| 72 | my_uRoll->Reset();
|
---|
| 73 | return true;
|
---|
| 74 | } else {
|
---|
| 75 | Thread::Warn("CustomTorques: could not start\n");
|
---|
| 76 | return false;
|
---|
| 77 | }
|
---|
[44] | 78 | }
|
---|
| 79 |
|
---|
| 80 | void PidUav::StartOscillatingMode() {
|
---|
[105] | 81 | if (behaviourMode == BehaviourMode_t::Default) {
|
---|
| 82 | if (!StartCustomMode())
|
---|
| 83 | return;
|
---|
| 84 | }
|
---|
| 85 | behaviourMode = BehaviourMode_t::Oscillating;
|
---|
| 86 | Thread::Info("Entering oscillating mode\n");
|
---|
| 87 | // remove the 'D' effect with a strong 'P'
|
---|
[44] | 88 | }
|
---|
| 89 |
|
---|
| 90 | void PidUav::ExtraCheckJoystick(void) {
|
---|
[105] | 91 | static bool wasOscillatingModeButtonPressed = false;
|
---|
| 92 | // controller button R1 enters optical flow mode
|
---|
[314] | 93 | if (GetTargetController()->IsButtonPressed(9)) { // R1
|
---|
[105] | 94 | if (!wasOscillatingModeButtonPressed) {
|
---|
| 95 | wasOscillatingModeButtonPressed = true;
|
---|
| 96 | if (behaviourMode != BehaviourMode_t::Oscillating)
|
---|
| 97 | StartOscillatingMode();
|
---|
[44] | 98 | }
|
---|
[105] | 99 | } else {
|
---|
| 100 | wasOscillatingModeButtonPressed = false;
|
---|
| 101 | }
|
---|
[44] | 102 | }
|
---|
| 103 |
|
---|
| 104 | void PidUav::ComputeCustomTorques(Euler &torques) {
|
---|
[105] | 105 | ComputeDefaultTorques(torques);
|
---|
| 106 | // overload default torques calculation for pitch and roll
|
---|
| 107 | const AhrsData *refOrientation = GetDefaultReferenceOrientation();
|
---|
| 108 | Quaternion refQuaternion;
|
---|
[236] | 109 | Vector3Df refAngularRates;
|
---|
[105] | 110 | refOrientation->GetQuaternionAndAngularRates(refQuaternion, refAngularRates);
|
---|
| 111 | Euler refAngles = refQuaternion.ToEuler();
|
---|
[44] | 112 |
|
---|
[105] | 113 | const AhrsData *currentOrientation = GetDefaultOrientation();
|
---|
| 114 | Quaternion currentQuaternion;
|
---|
[236] | 115 | Vector3Df currentAngularRates;
|
---|
[105] | 116 | currentOrientation->GetQuaternionAndAngularRates(currentQuaternion,
|
---|
| 117 | currentAngularRates);
|
---|
| 118 | Euler currentAngles = currentQuaternion.ToEuler();
|
---|
[44] | 119 |
|
---|
[105] | 120 | my_uRoll->SetValues(currentAngles.roll - refAngles.roll,
|
---|
| 121 | currentAngularRates.x);
|
---|
| 122 | my_uRoll->Update(GetTime());
|
---|
| 123 | torques.roll = my_uRoll->Output();
|
---|
[44] | 124 |
|
---|
[105] | 125 | my_uPitch->SetValues(currentAngles.pitch - refAngles.pitch,
|
---|
| 126 | currentAngularRates.y);
|
---|
| 127 | my_uPitch->Update(GetTime());
|
---|
| 128 | torques.pitch = my_uPitch->Output();
|
---|
[44] | 129 | }
|
---|
| 130 |
|
---|
[105] | 131 | PidUav::~PidUav() {}
|
---|