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