[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>
|
---|
| 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>
|
---|
[124] | 28 | #include <PushButton.h>
|
---|
[122] | 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 |
|
---|
| 38 | using namespace std;
|
---|
| 39 | using namespace flair::core;
|
---|
| 40 | using namespace flair::gui;
|
---|
| 41 | using namespace flair::filter;
|
---|
| 42 | using namespace flair::meta;
|
---|
| 43 | using namespace flair::sensor;
|
---|
| 44 |
|
---|
| 45 | DemoOpticalFlow::DemoOpticalFlow(TargetController *controller): UavStateMachine(controller) {
|
---|
| 46 | Uav* uav=GetUav();
|
---|
| 47 | if (uav->GetVerticalCamera() == NULL) {
|
---|
[131] | 48 | Err("no vertical camera found\n");
|
---|
[122] | 49 | exit(1);
|
---|
| 50 | }
|
---|
| 51 | uav->GetVerticalCamera()->SetLogFormat(Camera::LogFormat::JPG);
|
---|
| 52 | getFrameworkManager()->AddDeviceToLog(uav->GetVerticalCamera());
|
---|
[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
|
---|
| 61 | opticalFlow=new OpticalFlow(greyCameraImage,uav->GetVerticalCamera()->GetLayout()->NewRow(),"flux optique");
|
---|
| 62 | opticalFlowSpeed=new OpticalFlowSpeed(opticalFlow,"vitesse du flux optique");
|
---|
[122] | 63 |
|
---|
[124] | 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);
|
---|
[122] | 74 | // delete prev_value?
|
---|
| 75 |
|
---|
[124] | 76 | getFrameworkManager()->AddDeviceToLog(opticalFlowSpeed);
|
---|
[122] | 77 |
|
---|
[124] | 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);
|
---|
[122] | 81 |
|
---|
[124] | 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);
|
---|
[122] | 86 |
|
---|
[124] | 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());
|
---|
[122] | 91 |
|
---|
[124] | 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);
|
---|
[122] | 95 |
|
---|
[124] | 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");
|
---|
[122] | 99 |
|
---|
[124] | 100 | customReferenceOrientation= new AhrsData(this,"reference");
|
---|
| 101 | uav->GetAhrs()->AddPlot(customReferenceOrientation,DataPlot::Yellow);
|
---|
| 102 | AddDataToControlLawLog(customReferenceOrientation);
|
---|
[122] | 103 | }
|
---|
| 104 |
|
---|
| 105 | void 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 |
|
---|
[124] | 114 | void 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 |
|
---|
| 124 | void DemoOpticalFlow::ExtraCheckPushButton(void) {
|
---|
| 125 | if(startOpticalflow->Clicked()) {
|
---|
| 126 | //StartOpticalFlow();
|
---|
| 127 | GetUav()->GetVerticalCamera()->SavePictureToFile("./toto.jpg");
|
---|
| 128 |
|
---|
| 129 | }
|
---|
| 130 | }
|
---|
| 131 |
|
---|
[122] | 132 | void 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;
|
---|
[124] | 138 | StartOpticalFlow();
|
---|
[122] | 139 | }
|
---|
| 140 | } else {
|
---|
| 141 | wasOpticalFlowModeButtonPressed=false;
|
---|
| 142 | }
|
---|
| 143 | }
|
---|
| 144 |
|
---|
| 145 | const 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 |
|
---|
| 176 | DemoOpticalFlow::~DemoOpticalFlow() {
|
---|
| 177 | }
|
---|