[89] | 1 | // created: 2016/07/01
|
---|
| 2 | // filename: DemoGps.cpp
|
---|
| 3 | //
|
---|
| 4 | // author: Guillaume Sanahuja
|
---|
| 5 | // Copyright Heudiasyc UMR UTC/CNRS 7253
|
---|
| 6 | //
|
---|
| 7 | // version: $Id: $
|
---|
| 8 | //
|
---|
| 9 | // purpose: demo GPS
|
---|
| 10 | //
|
---|
| 11 | //
|
---|
| 12 | /*********************************************************************/
|
---|
| 13 |
|
---|
| 14 | #include "DemoGps.h"
|
---|
| 15 | #include <TargetController.h>
|
---|
| 16 | #include <Uav.h>
|
---|
| 17 | #include <GridLayout.h>
|
---|
| 18 | #include <PushButton.h>
|
---|
| 19 | #include <DataPlot1D.h>
|
---|
| 20 | #include <DataPlot2D.h>
|
---|
| 21 | #include <MetaDualShock3.h>
|
---|
| 22 | #include <FrameworkManager.h>
|
---|
| 23 | #include <TrajectoryGenerator2DCircle.h>
|
---|
| 24 | #include <cmath>
|
---|
| 25 | #include <Tab.h>
|
---|
| 26 | #include <Pid.h>
|
---|
| 27 | #include <Ahrs.h>
|
---|
| 28 | #include <AhrsData.h>
|
---|
| 29 | #include <RTDM_SerialPort.h>
|
---|
[186] | 30 | #include <Imu.h>
|
---|
| 31 | #include <NmeaGps.h>
|
---|
[89] | 32 |
|
---|
| 33 | using namespace std;
|
---|
| 34 | using namespace flair::core;
|
---|
| 35 | using namespace flair::gui;
|
---|
| 36 | using namespace flair::sensor;
|
---|
| 37 | using namespace flair::filter;
|
---|
| 38 | using namespace flair::meta;
|
---|
| 39 |
|
---|
[159] | 40 | DemoGps::DemoGps(TargetController* controller)
|
---|
| 41 | : UavStateMachine(controller)
|
---|
| 42 | , behaviourMode(BehaviourMode_t::Default) {
|
---|
| 43 | Uav* uav = GetUav();
|
---|
| 44 | startCircle = new PushButton(GetButtonsLayout()->NewRow(), "start_circle");
|
---|
| 45 | stopCircle = new PushButton(GetButtonsLayout()->LastRowLastCol(), "stop_circle");
|
---|
[89] | 46 |
|
---|
[185] | 47 | circle = new TrajectoryGenerator2DCircle(uav->GetGps()->GetLayout()->NewRow(), "circle");
|
---|
[159] | 48 | // todo: add graphs in gps plot
|
---|
| 49 | /*
|
---|
| 50 | uav->GetVrpnObject()->xPlot()->AddCurve(circle->Matrix()->Element(0,0),DataPlot::Blue);
|
---|
| 51 | uav->GetVrpnObject()->yPlot()->AddCurve(circle->Matrix()->Element(0,1),DataPlot::Blue);
|
---|
| 52 | uav->GetVrpnObject()->VxPlot()->AddCurve(circle->Matrix()->Element(1,0),DataPlot::Blue);
|
---|
| 53 | uav->GetVrpnObject()->VyPlot()->AddCurve(circle->Matrix()->Element(1,1),DataPlot::Blue);
|
---|
| 54 | uav->GetVrpnObject()->XyPlot()->AddCurve(circle->Matrix()->Element(0,1),circle->Matrix()->Element(0,0),DataPlot::Blue,"circle");*/
|
---|
[89] | 55 |
|
---|
[159] | 56 | uX = new Pid(setupLawTab->At(1, 0), "u_x");
|
---|
| 57 | uX->UseDefaultPlot(graphLawTab->NewRow());
|
---|
| 58 | uY = new Pid(setupLawTab->At(1, 1), "u_y");
|
---|
| 59 | uY->UseDefaultPlot(graphLawTab->LastRowLastCol());
|
---|
[89] | 60 |
|
---|
[159] | 61 | customReferenceOrientation = new AhrsData(this, "reference");
|
---|
| 62 | uav->GetAhrs()->AddPlot(customReferenceOrientation, DataPlot::Yellow);
|
---|
| 63 | AddDataToControlLawLog(customReferenceOrientation);
|
---|
| 64 |
|
---|
| 65 | customOrientation = new AhrsData(this, "orientation");
|
---|
[89] | 66 | }
|
---|
| 67 |
|
---|
| 68 | DemoGps::~DemoGps() {
|
---|
| 69 | }
|
---|
| 70 |
|
---|
[159] | 71 | AhrsData* DemoGps::GetReferenceOrientation(void) {
|
---|
[167] | 72 | Vector2Df pos_err, vel_err; // in Uav coordinate system
|
---|
[159] | 73 | float yaw_ref;
|
---|
| 74 | Euler refAngles;
|
---|
[89] | 75 |
|
---|
[159] | 76 | PositionValues(pos_err, vel_err, yaw_ref);
|
---|
[89] | 77 |
|
---|
[159] | 78 | refAngles.yaw = yaw_ref;
|
---|
[89] | 79 |
|
---|
[159] | 80 | uX->SetValues(pos_err.x, vel_err.x);
|
---|
| 81 | uX->Update(GetTime());
|
---|
| 82 | refAngles.pitch = uX->Output();
|
---|
[89] | 83 |
|
---|
[159] | 84 | uY->SetValues(pos_err.y, vel_err.y);
|
---|
| 85 | uY->Update(GetTime());
|
---|
| 86 | refAngles.roll = -uY->Output();
|
---|
[89] | 87 |
|
---|
[167] | 88 | customReferenceOrientation->SetQuaternionAndAngularRates(refAngles.ToQuaternion(), Vector3Df(0, 0, 0));
|
---|
[89] | 89 |
|
---|
[159] | 90 | return customReferenceOrientation;
|
---|
[89] | 91 | }
|
---|
| 92 |
|
---|
[167] | 93 | void DemoGps::PositionValues(Vector2Df& pos_error, Vector2Df& vel_error, float& yaw_ref) {
|
---|
| 94 | Vector3Df uav_pos, uav_vel;
|
---|
| 95 | Vector2Df uav_2Dpos, uav_2Dvel;
|
---|
[89] | 96 |
|
---|
[159] | 97 | // TODO GPS position and circle center
|
---|
| 98 | // GetUav()->GetVrpnObject()->GetPosition(uav_pos);
|
---|
| 99 | // GetUav()->GetVrpnObject()->GetSpeed(uav_vel);
|
---|
[89] | 100 |
|
---|
[159] | 101 | uav_pos.To2Dxy(uav_2Dpos);
|
---|
| 102 | uav_vel.To2Dxy(uav_2Dvel);
|
---|
[89] | 103 |
|
---|
[159] | 104 | if(behaviourMode == BehaviourMode_t::PositionHold) {
|
---|
| 105 | pos_error = uav_2Dpos - posHold;
|
---|
| 106 | vel_error = uav_2Dvel;
|
---|
| 107 | yaw_ref = yawHold;
|
---|
| 108 | } else { // Circle
|
---|
[167] | 109 | Vector2Df circle_pos, circle_vel;
|
---|
| 110 | Vector2Df target_2Dpos;
|
---|
[89] | 111 |
|
---|
[159] | 112 | circle->SetCenter(target_2Dpos);
|
---|
[89] | 113 |
|
---|
[159] | 114 | // circle reference
|
---|
| 115 | circle->Update(GetTime());
|
---|
| 116 | circle->GetPosition(circle_pos);
|
---|
| 117 | circle->GetSpeed(circle_vel);
|
---|
[89] | 118 |
|
---|
[159] | 119 | // error in optitrack frame
|
---|
| 120 | pos_error = uav_2Dpos - circle_pos;
|
---|
| 121 | vel_error = uav_2Dvel - circle_vel;
|
---|
| 122 | yaw_ref = atan2(target_2Dpos.y - uav_pos.y, target_2Dpos.x - uav_pos.x);
|
---|
| 123 | }
|
---|
[89] | 124 |
|
---|
[159] | 125 | // error in uav frame
|
---|
| 126 | Quaternion currentQuaternion = GetCurrentQuaternion();
|
---|
| 127 | Euler currentAngles; // in vrpn frame
|
---|
| 128 | currentQuaternion.ToEuler(currentAngles);
|
---|
| 129 | pos_error.Rotate(-currentAngles.yaw);
|
---|
| 130 | vel_error.Rotate(-currentAngles.yaw);
|
---|
[89] | 131 | }
|
---|
| 132 |
|
---|
| 133 | void DemoGps::SignalEvent(Event_t event) {
|
---|
[159] | 134 | UavStateMachine::SignalEvent(event);
|
---|
| 135 | switch(event) {
|
---|
| 136 | case Event_t::TakingOff:
|
---|
| 137 | behaviourMode = BehaviourMode_t::Default;
|
---|
| 138 | break;
|
---|
| 139 | case Event_t::EnteringControlLoop:
|
---|
| 140 | if((behaviourMode == BehaviourMode_t::Circle) && (!circle->IsRunning())) {
|
---|
| 141 | GpsPositionHold();
|
---|
[89] | 142 | }
|
---|
[159] | 143 | break;
|
---|
| 144 | case Event_t::EnteringFailSafeMode:
|
---|
| 145 | behaviourMode = BehaviourMode_t::Default;
|
---|
| 146 | break;
|
---|
| 147 | }
|
---|
[89] | 148 | }
|
---|
| 149 |
|
---|
| 150 | void DemoGps::ExtraSecurityCheck(void) {
|
---|
| 151 | }
|
---|
| 152 |
|
---|
| 153 | void DemoGps::ExtraCheckPushButton(void) {
|
---|
[159] | 154 | if(startCircle->Clicked() && (behaviourMode != BehaviourMode_t::Circle)) {
|
---|
| 155 | StartCircle();
|
---|
| 156 | }
|
---|
| 157 | if(stopCircle->Clicked() && (behaviourMode == BehaviourMode_t::Circle)) {
|
---|
| 158 | StopCircle();
|
---|
| 159 | }
|
---|
[89] | 160 | }
|
---|
| 161 |
|
---|
| 162 | void DemoGps::ExtraCheckJoystick(void) {
|
---|
[159] | 163 | // R1 and Circle
|
---|
[314] | 164 | if(GetTargetController()->IsButtonPressed(9) && GetTargetController()->IsButtonPressed(4) &&
|
---|
[159] | 165 | (behaviourMode != BehaviourMode_t::Circle)) {
|
---|
| 166 | StartCircle();
|
---|
| 167 | }
|
---|
[89] | 168 |
|
---|
[159] | 169 | // R1 and Cross
|
---|
[314] | 170 | if(GetTargetController()->IsButtonPressed(9) && GetTargetController()->IsButtonPressed(5) &&
|
---|
[159] | 171 | (behaviourMode == BehaviourMode_t::Circle)) {
|
---|
| 172 | StopCircle();
|
---|
| 173 | }
|
---|
[89] | 174 | }
|
---|
| 175 |
|
---|
| 176 | void DemoGps::StartCircle(void) {
|
---|
[159] | 177 | if(SetOrientationMode(OrientationMode_t::Custom)) {
|
---|
| 178 | Thread::Info("DemoGps: start circle\n");
|
---|
| 179 | } else {
|
---|
| 180 | Thread::Warn("DemoGps: could not start circle\n");
|
---|
| 181 | return;
|
---|
| 182 | }
|
---|
[167] | 183 | Vector3Df uav_pos;
|
---|
| 184 | Vector2Df uav_2Dpos, target_2Dpos;
|
---|
[89] | 185 |
|
---|
[159] | 186 | circle->SetCenter(target_2Dpos);
|
---|
[89] | 187 |
|
---|
[159] | 188 | // todo get uav and circle pos by gps
|
---|
| 189 | uav_pos.To2Dxy(uav_2Dpos);
|
---|
| 190 | circle->StartTraj(uav_2Dpos);
|
---|
[89] | 191 |
|
---|
[159] | 192 | uX->Reset();
|
---|
| 193 | uY->Reset();
|
---|
| 194 | behaviourMode = BehaviourMode_t::Circle;
|
---|
[89] | 195 | }
|
---|
| 196 |
|
---|
| 197 | void DemoGps::StopCircle(void) {
|
---|
[159] | 198 | circle->FinishTraj();
|
---|
| 199 | // GetJoystick()->Rumble(0x70);
|
---|
| 200 | Thread::Info("DemoGps: finishing circle\n");
|
---|
[89] | 201 | }
|
---|
| 202 |
|
---|
| 203 | void DemoGps::GpsPositionHold(void) {
|
---|
| 204 |
|
---|
[159] | 205 | // tood set yawHold and posHold
|
---|
[89] | 206 |
|
---|
[159] | 207 | uX->Reset();
|
---|
| 208 | uY->Reset();
|
---|
| 209 | behaviourMode = BehaviourMode_t::PositionHold;
|
---|
| 210 | SetOrientationMode(OrientationMode_t::Custom);
|
---|
| 211 | Thread::Info("DemoGps: holding position\n");
|
---|
[89] | 212 | }
|
---|