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

Last change on this file since 131 was 131, checked in by Sanahuja Guillaume, 7 years ago

error message

File size: 6.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 <CvtColor.h>
18#include <OpticalFlow.h>
19#include <OpticalFlowSpeed.h>
20#include <LowPassFilter.h>
21#include <cvmatrix.h>
22#include <GridLayout.h>
23#include <DataPlot1D.h>
24#include <Tab.h>
25#include <TabWidget.h>
26#include <GroupBox.h>
27#include <DoubleSpinBox.h>
28#include <PushButton.h>
29#include <FrameworkManager.h>
30#include <MetaDualShock3.h>
31#include <Vector2D.h>
32#include <AhrsData.h>
33#include <Ahrs.h>
34#include <Pid.h>
35
36#include <stdio.h>
37
38using namespace std;
39using namespace flair::core;
40using namespace flair::gui;
41using namespace flair::filter;
42using namespace flair::meta;
43using namespace flair::sensor;
44
45DemoOpticalFlow::DemoOpticalFlow(TargetController *controller): UavStateMachine(controller) {
46 Uav* uav=GetUav();
47 if (uav->GetVerticalCamera() == NULL) {
48 Err("no vertical camera found\n");
49 exit(1);
50 }
51uav->GetVerticalCamera()->SetLogFormat(Camera::LogFormat::JPG);
52getFrameworkManager()->AddDeviceToLog(uav->GetVerticalCamera());
53
54 startOpticalflow=new PushButton(GetButtonsLayout()->NewRow(),"start optical flow");
55
56 greyCameraImage=new CvtColor(uav->GetVerticalCamera(),"gray",CvtColor::Conversion_t::ToGray);
57
58 uav->GetVerticalCamera()->UseDefaultPlot(greyCameraImage->Output()); // Le defaultPlot de la caméra peut afficher n'importe quoi?
59
60 //optical flow stack
61 opticalFlow=new OpticalFlow(greyCameraImage,uav->GetVerticalCamera()->GetLayout()->NewRow(),"flux optique");
62 opticalFlowSpeed=new OpticalFlowSpeed(opticalFlow,"vitesse du flux optique");
63
64 cvmatrix_descriptor* desc=new cvmatrix_descriptor(2,1);
65 for(int i=0;i<2;i++) {
66 desc->SetElementName(i,0,opticalFlowSpeed->Output()->Name(i,0));
67 }
68 cvmatrix* prev_value=new cvmatrix((const Thread*)this,desc,floatType,uav->ObjectName()); // diamond inheritance
69 for(int i=0;i<2;i++) {
70 prev_value->SetValue(i,0,0);
71 }
72
73 opticalFlowSpeedFiltered=new LowPassFilter(opticalFlowSpeed,uav->GetVerticalCamera()->GetLayout()->NewRow(),"passe bas",prev_value);
74// delete prev_value?
75
76 getFrameworkManager()->AddDeviceToLog(opticalFlowSpeed);
77
78 Tab* opticalFlowTab=new Tab(getFrameworkManager()->GetTabWidget(),"flux optique");
79 DataPlot1D* xVelocityPlot=new DataPlot1D(opticalFlowTab->NewRow(),"x_velocity",-5,5);
80 DataPlot1D* yVelocityPlot=new DataPlot1D(opticalFlowTab->LastRowLastCol(),"y_velocity",-5,5);
81
82 xVelocityPlot->AddCurve(opticalFlowSpeed->Output()->Element(0,0));
83 xVelocityPlot->AddCurve(opticalFlowSpeedFiltered->Matrix()->Element(0,0),DataPlot::Blue);
84 yVelocityPlot->AddCurve(opticalFlowSpeed->Output()->Element(1,0));
85 yVelocityPlot->AddCurve(opticalFlowSpeedFiltered->Matrix()->Element(1,0),DataPlot::Blue);
86
87 u_x=new Pid(setupLawTab->At(1,0),"u_x");
88 u_x->UseDefaultPlot(graphLawTab->NewRow());
89 u_y=new Pid(setupLawTab->At(1,1),"u_y");
90 u_y->UseDefaultPlot(graphLawTab->LastRowLastCol());
91
92 opticalFlowGroupBox=new GroupBox(GetJoystick()->GetTab()->NewRow(),"consignes fo");
93 maxXSpeed=new DoubleSpinBox(opticalFlowGroupBox->NewRow(),"debattement x"," m/s",-5,5,0.1,1);
94 maxYSpeed=new DoubleSpinBox(opticalFlowGroupBox->LastRowLastCol(),"debattement y"," m/s",-5,5,0.1,1);
95
96 opticalFlowReference=new cvmatrix((const Thread*)this,2,1,floatType);
97 xVelocityPlot->AddCurve(opticalFlowReference->Element(0,0),DataPlot::Green,"consigne");
98 yVelocityPlot->AddCurve(opticalFlowReference->Element(1,0),DataPlot::Green,"consigne");
99
100 customReferenceOrientation= new AhrsData(this,"reference");
101 uav->GetAhrs()->AddPlot(customReferenceOrientation,DataPlot::Yellow);
102 AddDataToControlLawLog(customReferenceOrientation);
103}
104
105void DemoOpticalFlow::SignalEvent(Event_t event) {
106 switch(event) {
107 case Event_t::EnteringControlLoop:
108 opticalFlowReference->SetValue(0,0,GetJoystick()->GetAxisValue(1)*maxXSpeed->Value());//joy axis 0 maps to x displacement
109 opticalFlowReference->SetValue(1,0,GetJoystick()->GetAxisValue(0)*maxYSpeed->Value());//joy axis 1 maps to y displacement
110 break;
111 }
112}
113
114void DemoOpticalFlow::StartOpticalFlow(void) {
115 if (SetOrientationMode(OrientationMode_t::Custom)) {
116 Thread::Info("(Re)entering optical flow mode\n");
117 u_x->Reset();
118 u_y->Reset();
119 } else {
120 Thread::Warn("Could not enter optical flow mode\n");
121 }
122}
123
124void DemoOpticalFlow::ExtraCheckPushButton(void) {
125 if(startOpticalflow->Clicked()) {
126 //StartOpticalFlow();
127 GetUav()->GetVerticalCamera()->SavePictureToFile("./toto.jpg");
128
129 }
130}
131
132void DemoOpticalFlow::ExtraCheckJoystick(void) {
133 static bool wasOpticalFlowModeButtonPressed=false;
134 // controller button R1 enters optical flow mode
135 if(GetJoystick()->IsButtonPressed(9)) { // R1
136 if (!wasOpticalFlowModeButtonPressed) {
137 wasOpticalFlowModeButtonPressed=true;
138 StartOpticalFlow();
139 }
140 } else {
141 wasOpticalFlowModeButtonPressed=false;
142 }
143}
144
145const AhrsData *DemoOpticalFlow::GetReferenceOrientation(void) {
146 Euler refAngles=GetDefaultReferenceOrientation()->GetQuaternion().ToEuler();//to keep default yaw reference
147
148 // /!\ in this demo, the target value is a speed (in pixel/s). As a consequence the error is the difference between the current speed and the target speed
149 Vector2D error, errorVariation; // in Uav coordinate system
150
151 //opticalFlow= matrice de déplacements en pixels entre 2 images consécutives
152 //opticalFlowSpeed=vitesse de déplacement en pixel par seconde (moyenne sur tous les points et division par le delta T)
153 error.x=opticalFlowSpeedFiltered->Output(0,0)-opticalFlowReference->Value(0,0);
154 error.y=opticalFlowSpeedFiltered->Output(1,0)-opticalFlowReference->Value(1,0);
155
156 //la dérivée est à la fréquence de la loi de commande ("rapide") alors que le flux optique est à la fréquence de la caméra
157 // fréquemment la dérivée car le signal n'a pas bougé -> dérivée super crade
158 //gsanahuj: brancher un eulerderivative derriere le opticalFlowSpeedFiltered pour avoir la derivee
159 //opticalFlowSpeed doit etre renomme car finalement ce n'est pas une vitesse mais un deplacement
160 errorVariation.x=0;
161 errorVariation.y=0;
162
163 u_x->SetValues(error.x, errorVariation.x);
164 u_x->Update(GetTime());
165 refAngles.pitch=u_x->Output();
166
167 u_y->SetValues(error.y, errorVariation.y);
168 u_y->Update(GetTime());
169 refAngles.roll=-u_y->Output();
170
171 customReferenceOrientation->SetQuaternionAndAngularRates(refAngles.ToQuaternion(),Vector3D(0,0,0));
172
173 return customReferenceOrientation;
174}
175
176DemoOpticalFlow::~DemoOpticalFlow() {
177}
Note: See TracBrowser for help on using the repository browser.