source: flair-src/branches/sanscv/demos/OpticalFlow/uav/src/DemoOpticalFlow.cpp@ 324

Last change on this file since 324 was 324, checked in by Sanahuja Guillaume, 5 years ago

removing opencv dependency

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