source: flair-src/trunk/lib/FlairFilter/src/TrajectoryGenerator2DCircle_impl.cpp@ 10

Last change on this file since 10 was 10, checked in by Sanahuja Guillaume, 8 years ago

lic

File size: 4.6 KB
Line 
1// %flair:license{
2// This file is part of the Flair framework distributed under the
3// CECILL-C License, Version 1.0.
4// %flair:license}
5// created: 2013/04/08
6// filename: TrajectoryGenerator2DCircle_impl.cpp
7//
8// author: Guillaume Sanahuja
9// Copyright Heudiasyc UMR UTC/CNRS 7253
10//
11// version: $Id: $
12//
13// purpose: objet permettant la generation d'une trajectoire cercle
14//
15//
16/*********************************************************************/
17
18#include "TrajectoryGenerator2DCircle_impl.h"
19#include "TrajectoryGenerator2DCircle.h"
20#include <cvmatrix.h>
21#include <Layout.h>
22#include <GroupBox.h>
23#include <DoubleSpinBox.h>
24#include <cmath>
25
26#define PI ((float)3.14159265358979323846)
27
28using std::string;
29using namespace flair::core;
30using namespace flair::gui;
31using namespace flair::filter;
32
33TrajectoryGenerator2DCircle_impl::TrajectoryGenerator2DCircle_impl(TrajectoryGenerator2DCircle* self,const LayoutPosition* position,string name) {
34 first_update=true;
35 is_running=false;
36 is_finishing=false;
37
38 //init UI
39 GroupBox* reglages_groupbox=new GroupBox(position,name);
40 T=new DoubleSpinBox(reglages_groupbox->NewRow(),"period, 0 for auto"," s",0,1,0.01);
41 rayon=new DoubleSpinBox(reglages_groupbox->LastRowLastCol(),"R"," m",0,1000,.1);
42 veloctity=new DoubleSpinBox(reglages_groupbox->LastRowLastCol(),"velocity"," m/s",-10,10,1);
43 acceleration=new DoubleSpinBox(reglages_groupbox->LastRowLastCol(),"acceleration (absolute)"," m/s²",0,10,.1);
44
45 //init matrix
46 cvmatrix_descriptor* desc=new cvmatrix_descriptor(2,2);
47 desc->SetElementName(0,0,"pos.x");
48 desc->SetElementName(0,1,"pos.y");
49 desc->SetElementName(1,0,"vel.x");
50 desc->SetElementName(1,1,"vel.y");
51 output=new cvmatrix(self,desc,floatType,name);
52
53 output->SetValue(0,0,0);
54 output->SetValue(0,1,0);
55 output->SetValue(1,0,0);
56 output->SetValue(1,1,0);
57}
58
59
60TrajectoryGenerator2DCircle_impl::~TrajectoryGenerator2DCircle_impl() {
61 delete output;
62}
63
64void TrajectoryGenerator2DCircle_impl::StartTraj(const Vector2D &start_pos,float nb_lap) {
65 is_running=true;
66 first_update=true;
67 is_finishing=false;
68 this->nb_lap=nb_lap;
69
70 //configure trajectory
71 angle_off=atan2(start_pos.y-pos_off.y,start_pos.x-pos_off.x);
72 CurrentTime=0;
73}
74
75void TrajectoryGenerator2DCircle_impl::FinishTraj(void) {
76 if(!is_finishing) {
77 is_finishing=true;
78 FinishTime=CurrentTime;
79 }
80}
81
82void TrajectoryGenerator2DCircle_impl::Update(Time time) {
83 float delta_t;
84 float theta;
85 float V=veloctity->Value();
86 float A=acceleration->Value();
87 float R=rayon->Value();
88 Vector2D v;
89
90 if(V<0) A=-A;
91
92 if(T->Value()==0) {
93 if(first_update) {
94 first_update=false;
95 previous_time=time;
96 return;
97 } else {
98 delta_t=(float)(time-previous_time)/1000000000.;
99 }
100 } else {
101 delta_t=T->Value();
102 }
103 previous_time=time;
104 CurrentTime+=delta_t;
105
106 if(is_finishing && CurrentTime>FinishTime+V/A) is_running=false;
107
108 if(is_running) {
109 if(R==0) {
110 pos.x=0;
111 pos.y=0;
112 v.x=0;
113 v.y=0;
114 } else {
115 if(CurrentTime<V/A) {
116 theta=angle_off+A/2*CurrentTime*CurrentTime/R;
117 pos.x=R*cos(theta);
118 pos.y=R*sin(theta);
119 v.x=-A*CurrentTime*sin(theta);
120 v.y=A*CurrentTime*cos(theta);
121 } else {
122 if(!is_finishing) {
123 theta=angle_off+V*V/(2*A*R)+(CurrentTime-V/A)*V/R;
124 pos.x=R*cos(theta);
125 pos.y=R*sin(theta);
126 v.x=-V*sin(theta);
127 v.y=V*cos(theta);
128 } else {
129 theta=angle_off+V*V/(2*A*R)+(FinishTime-V/A)*V/R-A/2*(FinishTime-CurrentTime)*(FinishTime-CurrentTime)/R+V*(CurrentTime-FinishTime)/R;
130 pos.x=R*cos(theta);
131 pos.y=R*sin(theta);
132 v.x=-(V+A*(FinishTime-CurrentTime))*sin(theta);
133 v.y=(V+A*(FinishTime-CurrentTime))*cos(theta);
134 }
135 }
136 }
137
138 if(theta-angle_off>=nb_lap*2*PI-(-A/2*(V/A)*(V/A)/R+V*(V/A)/R) && nb_lap>0) {
139 FinishTraj();
140 }
141 } else {
142 v.x=0;
143 v.y=0;
144 }
145
146 //on prend une fois pour toute les mutex et on fait des accès directs
147 output->GetMutex();
148 output->SetValueNoMutex(0,0,pos.x+pos_off.x);
149 output->SetValueNoMutex(0,1,pos.y+pos_off.y);
150 output->SetValueNoMutex(1,0,v.x+vel_off.x);
151 output->SetValueNoMutex(1,1,v.y+vel_off.y);
152 output->ReleaseMutex();
153
154 output->SetDataTime(time);
155}
Note: See TracBrowser for help on using the repository browser.