source: flair-src/trunk/demos/OpticalFlow/uav/src/DemoOpticalFlow.cpp@ 160

Last change on this file since 160 was 156, checked in by Bayard Gildas, 8 years ago

Additionnal graphs

File size: 9.4 KB
RevLine 
[122]1// created: 2011/05/01
2// filename: DemoOpticalFlow.cpp
3//
4// author: Guillaume Sanahuja
5// Copyright Heudiasyc UMR UTC/CNRS 7253
6//
7// version: $Id: $
8//
9// purpose: demo optical flow
10//
11//
12/*********************************************************************/
13
14#include "DemoOpticalFlow.h"
15#include <Uav.h>
16#include <Camera.h>
17#include <CvtColor.h>
18#include <OpticalFlow.h>
[155]19#include <OpticalFlowCompensated.h>
[122]20#include <OpticalFlowSpeed.h>
21#include <LowPassFilter.h>
[142]22#include <EulerDerivative.h>
[122]23#include <cvmatrix.h>
24#include <GridLayout.h>
25#include <DataPlot1D.h>
26#include <Tab.h>
27#include <TabWidget.h>
28#include <GroupBox.h>
29#include <DoubleSpinBox.h>
[124]30#include <PushButton.h>
[122]31#include <FrameworkManager.h>
32#include <MetaDualShock3.h>
33#include <Vector2D.h>
34#include <AhrsData.h>
35#include <Ahrs.h>
36#include <Pid.h>
37
38#include <stdio.h>
39
40using namespace std;
41using namespace flair::core;
42using namespace flair::gui;
43using namespace flair::filter;
44using namespace flair::meta;
45using namespace flair::sensor;
46
47DemoOpticalFlow::DemoOpticalFlow(TargetController *controller): UavStateMachine(controller) {
48 Uav* uav=GetUav();
49 if (uav->GetVerticalCamera() == NULL) {
[131]50 Err("no vertical camera found\n");
[122]51 exit(1);
52 }
[124]53
54 startOpticalflow=new PushButton(GetButtonsLayout()->NewRow(),"start optical flow");
[122]55
[124]56 greyCameraImage=new CvtColor(uav->GetVerticalCamera(),"gray",CvtColor::Conversion_t::ToGray);
[122]57
[124]58 uav->GetVerticalCamera()->UseDefaultPlot(greyCameraImage->Output()); // Le defaultPlot de la caméra peut afficher n'importe quoi?
[122]59
[124]60 //optical flow stack
[147]61 //opticalFlow= matrice de déplacements en pixels entre 2 images consécutives
62 opticalFlow=new OpticalFlow(greyCameraImage,uav->GetVerticalCamera()->GetLayout()->NewRow(),"flux optique");
[156]63 opticalFlowCompensated=new OpticalFlowCompensated(opticalFlow,uav->GetAhrs(),uav->GetVerticalCamera()->GetLayout()->NewRow(),"flux optique compense");
[155]64 opticalFlowSpeedRaw=new OpticalFlowSpeed(opticalFlowCompensated,"vitesse du flux optique");
[147]65 //opticalFlowSpeed=vitesse de déplacement en pixels par seconde (moyenne sur tous les points et division par le delta T)
[155]66 cvmatrix* twoByOneOFS=new cvmatrix((const Thread*)this,2,1,floatType);
67 cvmatrix* twoByOneOFAR=new cvmatrix((const Thread*)this,2,1,floatType);
68 cvmatrix* twoByOneOFA=new cvmatrix((const Thread*)this,2,1,floatType);
69 opticalFlowSpeed=new LowPassFilter(opticalFlowSpeedRaw,uav->GetVerticalCamera()->GetLayout()->NewRow(),"Speed lowPass",twoByOneOFS);
70 opticalFlowAccelerationRaw=new EulerDerivative(opticalFlowSpeed,uav->GetVerticalCamera()->GetLayout()->NewRow(),"derivative",twoByOneOFAR);
71 opticalFlowAcceleration=new LowPassFilter(opticalFlowAccelerationRaw,uav->GetVerticalCamera()->GetLayout()->NewRow(),"Acceleration lowPass",twoByOneOFA);
[147]72
73 getFrameworkManager()->AddDeviceToLog(opticalFlowSpeedRaw);
[122]74
[155]75 Tab* opticalFlowTab=new Tab(getFrameworkManager()->GetTabWidget(),"flux optique");
76 DataPlot1D* xVelocityPlot=new DataPlot1D(opticalFlowTab->NewRow(),"x speed (px/s)",-250,250);
77 DataPlot1D* yVelocityPlot=new DataPlot1D(opticalFlowTab->LastRowLastCol(),"y speed (px/s)",-250,250);
78 DataPlot1D* xFirstPointPlot=new DataPlot1D(opticalFlowTab->NewRow(),"x movement first point",-25,25);
79 DataPlot1D* yFirstPointPlot=new DataPlot1D(opticalFlowTab->LastRowLastCol(),"y movement first point",-25,25);
80// DataPlot1D* xAccelerationPlot=new DataPlot1D(opticalFlowTab->NewRow(),"x_acceleration",-250,250);
81// DataPlot1D* yAccelerationPlot=new DataPlot1D(opticalFlowTab->LastRowLastCol(),"y_acceleration",-250,250);
[122]82
[155]83 xVelocityPlot->AddCurve(opticalFlowSpeedRaw->Output()->Element(0,0));
84 xVelocityPlot->AddCurve(opticalFlowSpeed->Matrix()->Element(0,0),DataPlot::Blue);
85 yVelocityPlot->AddCurve(opticalFlowSpeedRaw->Output()->Element(1,0));
86 yVelocityPlot->AddCurve(opticalFlowSpeed->Matrix()->Element(1,0),DataPlot::Blue);
87 xFirstPointPlot->AddCurve(opticalFlowCompensated->GetFirstPointDisplacement()->Element(0,0));
88 xFirstPointPlot->AddCurve(opticalFlowCompensated->GetFirstPointDisplacement()->Element(1,0),DataPlot::Blue);
89 xFirstPointPlot->AddCurve(opticalFlowCompensated->GetFirstPointDisplacement()->Element(2,0),DataPlot::Green);
[156]90 yFirstPointPlot->AddCurve(opticalFlowCompensated->GetFirstPointDisplacement()->Element(0,1));
91 yFirstPointPlot->AddCurve(opticalFlowCompensated->GetFirstPointDisplacement()->Element(1,1),DataPlot::Blue);
92 yFirstPointPlot->AddCurve(opticalFlowCompensated->GetFirstPointDisplacement()->Element(2,1),DataPlot::Green);
[155]93// xAccelerationPlot->AddCurve(opticalFlowAccelerationRaw->Matrix()->Element(0,0));
94// xAccelerationPlot->AddCurve(opticalFlowAcceleration->Matrix()->Element(0,0),DataPlot::Blue);
95// yAccelerationPlot->AddCurve(opticalFlowAccelerationRaw->Matrix()->Element(1,0));
96// yAccelerationPlot->AddCurve(opticalFlowAcceleration->Matrix()->Element(1,0),DataPlot::Blue);
[122]97
[155]98 u_x=new Pid(setupLawTab->At(1,0),"u_x");
99 u_x->UseDefaultPlot(graphLawTab->NewRow());
100 u_y=new Pid(setupLawTab->At(1,1),"u_y");
101 u_y->UseDefaultPlot(graphLawTab->LastRowLastCol());
[122]102
[155]103 opticalFlowGroupBox=new GroupBox(GetJoystick()->GetTab()->NewRow(),"consignes fo");
104 maxXSpeed=new DoubleSpinBox(opticalFlowGroupBox->NewRow(),"debattement x"," m/s",-5,5,0.1,1);
105 maxYSpeed=new DoubleSpinBox(opticalFlowGroupBox->LastRowLastCol(),"debattement y"," m/s",-5,5,0.1,1);
[154]106
107 Tab* opticalFlowRealTab=new Tab(getFrameworkManager()->GetTabWidget(),"real speed");
108 opticalFlowRealSpeed=new cvmatrix((const Thread*)this,2,1,floatType);
109 opticalFlowReference=new cvmatrix((const Thread*)this,2,1,floatType);
110 opticalFlowRealAcceleration=new cvmatrix((const Thread*)this,2,1,floatType);
111 DataPlot1D* xRealVelocityPlot=new DataPlot1D(opticalFlowRealTab->NewRow(),"x speed (m/s)",-2,2);
[155]112 DataPlot1D* yRealVelocityPlot=new DataPlot1D(opticalFlowRealTab->LastRowLastCol(),"y speed (m/s)",-2,2);
[154]113 DataPlot1D* xRealAccelerationPlot=new DataPlot1D(opticalFlowRealTab->NewRow(),"x acceleration (m/s2)",-2,2);
[155]114 DataPlot1D* yRealAccelerationPlot=new DataPlot1D(opticalFlowRealTab->LastRowLastCol(),"y acceleration (m/s2)",-2,2);
[154]115 xRealVelocityPlot->AddCurve(opticalFlowRealSpeed->Element(0));
[155]116 xRealVelocityPlot->AddCurve(opticalFlowReference->Element(0),DataPlot::Blue,"consigne");
117 yRealVelocityPlot->AddCurve(opticalFlowRealSpeed->Element(1));
[154]118 yRealVelocityPlot->AddCurve(opticalFlowReference->Element(1),DataPlot::Blue,"consigne");
119 xRealAccelerationPlot->AddCurve(opticalFlowRealAcceleration->Element(0));
120 yRealAccelerationPlot->AddCurve(opticalFlowRealAcceleration->Element(1));
[122]121
[155]122 customReferenceOrientation= new AhrsData(this,"reference");
123 uav->GetAhrs()->AddPlot(customReferenceOrientation,DataPlot::Yellow);
124 AddDataToControlLawLog(customReferenceOrientation);
[122]125}
126
127void DemoOpticalFlow::SignalEvent(Event_t event) {
128 switch(event) {
129 case Event_t::EnteringControlLoop:
130 opticalFlowReference->SetValue(0,0,GetJoystick()->GetAxisValue(1)*maxXSpeed->Value());//joy axis 0 maps to x displacement
131 opticalFlowReference->SetValue(1,0,GetJoystick()->GetAxisValue(0)*maxYSpeed->Value());//joy axis 1 maps to y displacement
[154]132 float focal=271.76;
133 float z,dz;
134 AltitudeValues(z, dz);
135 float scale=z/focal;
136 opticalFlowRealSpeed->SetValue(0,0,opticalFlowSpeed->Output(0,0)*scale);
137 opticalFlowRealSpeed->SetValue(1,0,opticalFlowSpeed->Output(1,0)*scale);
138 opticalFlowRealAcceleration->SetValue(0,0,opticalFlowAcceleration->Output(0,0)*scale);
139 opticalFlowRealAcceleration->SetValue(1,0,opticalFlowAcceleration->Output(1,0)*scale);
[122]140 break;
141 }
142}
143
[124]144void DemoOpticalFlow::StartOpticalFlow(void) {
145 if (SetOrientationMode(OrientationMode_t::Custom)) {
146 Thread::Info("(Re)entering optical flow mode\n");
147 u_x->Reset();
148 u_y->Reset();
149 } else {
150 Thread::Warn("Could not enter optical flow mode\n");
151 }
152}
153
154void DemoOpticalFlow::ExtraCheckPushButton(void) {
155 if(startOpticalflow->Clicked()) {
[142]156 StartOpticalFlow();
[124]157 }
158}
159
[122]160void DemoOpticalFlow::ExtraCheckJoystick(void) {
161 static bool wasOpticalFlowModeButtonPressed=false;
162 // controller button R1 enters optical flow mode
163 if(GetJoystick()->IsButtonPressed(9)) { // R1
164 if (!wasOpticalFlowModeButtonPressed) {
165 wasOpticalFlowModeButtonPressed=true;
[124]166 StartOpticalFlow();
[122]167 }
168 } else {
169 wasOpticalFlowModeButtonPressed=false;
170 }
171}
172
173const AhrsData *DemoOpticalFlow::GetReferenceOrientation(void) {
174 Euler refAngles=GetDefaultReferenceOrientation()->GetQuaternion().ToEuler();//to keep default yaw reference
175
[143]176 // /!\ in this demo, the target value is a speed (in m/s). As a consequence the error is the difference between the current speed and the target speed
[122]177 Vector2D error, errorVariation; // in Uav coordinate system
[154]178
179 error.x=opticalFlowRealSpeed->Value(0,0)-opticalFlowReference->Value(0,0);
180 error.y=opticalFlowRealSpeed->Value(1,0)-opticalFlowReference->Value(1,0);
181 errorVariation.x=opticalFlowRealAcceleration->Value(0,0);
182 errorVariation.y=opticalFlowRealAcceleration->Value(1,0);
[143]183//Printf("Altitude=%f, Error=(%f,%f)\n",z,error.x,error.y);
[122]184
185 u_x->SetValues(error.x, errorVariation.x);
186 u_x->Update(GetTime());
187 refAngles.pitch=u_x->Output();
188
189 u_y->SetValues(error.y, errorVariation.y);
190 u_y->Update(GetTime());
191 refAngles.roll=-u_y->Output();
192
193 customReferenceOrientation->SetQuaternionAndAngularRates(refAngles.ToQuaternion(),Vector3D(0,0,0));
194
195 return customReferenceOrientation;
196}
197
198DemoOpticalFlow::~DemoOpticalFlow() {
199}
Note: See TracBrowser for help on using the repository browser.