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