close Warning: Can't use blame annotator:
svn blame failed on trunk/demos/OpticalFlow/uav/src/DemoOpticalFlow.cpp: 200029 - Couldn't perform atomic initialization

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

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

modifs

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