forked from ZeroWeight/ZeroWeight
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCalcTime.cpp
58 lines (51 loc) · 1.3 KB
/
CalcTime.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <cmath> //math.h
#include<iostream>
using namespace std;
static const double A_max = 5.0;//the max. acc. of the vehicle
static const double A_max_ = 3.0;//the max. dec. of the vehicle
static const double V_min = 10.0;//the min. vec. of the vehicle
static const double V_max = 20.0;//the max ....
inline double CalMaxTime(double pos, double vec) {
//dec. to the min. vec.
//keep the max vec till aim
//
//pos>0 vec>0
double time;
double pos_cri= (vec*vec - V_min*V_min) / (2 * A_max_);
if (pos <= pos_cri)
{
double vec_ = sqrt(vec*vec - 2 * A_max_*pos);//vec at the terminal
time = (vec - vec_) / A_max_;
}
else
{
double time1= (vec - V_min) / A_max_;
double time2 = (pos - pos_cri) / V_min;
time = time1 + time2;
}
return time;
}
inline double CalMinTime(double pos, double vec) {
double time;
double pos_cri = (V_max*V_max - vec*vec ) / (2 * A_max);
if (pos <= pos_cri)
{
double vec_ = sqrt(vec*vec + 2 * A_max*pos);//vec at the terminal
time = (vec_ - vec) / A_max;
}
else
{
double time1 = (V_max - vec) / A_max;
double time2 = (pos-pos_cri) / V_max;
time = time1 + time2;
}
return time;
}
int main()
{
cout << CalMaxTime(10, 15) << endl;
cout << CalMaxTime(30, 15) << endl;
cout << CalMinTime(10, 15) << endl;
cout << CalMinTime(30, 15) << endl;
system("pause");
}