forked from JakobHeller/NatureInspiredComputing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpeed.cpp
195 lines (161 loc) · 3.97 KB
/
Speed.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#include <math.h>
#include "Common.h"
#include "Speed.h"
#include "KheperaInterface.h" // for Exception. TODO: move into separate file.
#define MIN_SPEED 5
#define MAX_SPEED 30
CSpeed::CSpeed()
{
m_VelocityFlipped = false;
SetAngularComponents(0, 0);
}
CSpeed::CSpeed(double velocity, double angle)
{
m_VelocityFlipped;
SetAngularComponents(velocity, angle);
}
double CSpeed::Velocity() const
{
return m_VelocityFlipped ? -m_Velocity : m_Velocity;
}
void CSpeed::SetVelocity(double v)
{
if (isnan(v)) throw Exception("Attempted to set NaN velocity!", -1);
m_Velocity = m_VelocityFlipped ? -v : v;
Limit();
}
void CSpeed::IncreaseVelocity(double v)
{
SetVelocity(m_Velocity + v);
}
double CSpeed::Angle() const
{
return m_Angle;
}
void CSpeed::SetAngle(double a)
{
if (isnan(a)) throw Exception("Attempted to set NaN angle!", -1);
double simple = a;
while (simple > PI) simple -= 2 * PI;
while (simple < -PI) simple += 2 * PI;
if (abs(simple) > PI / 2)
{
m_VelocityFlipped = !m_VelocityFlipped;
simple += simple < 0 ? PI : -PI;
}
m_Angle = simple;
Limit();
}
void CSpeed::IncreaseAngle(double a)
{
SetAngle(m_Angle + a);
}
double CSpeed::Left() const
{
double val = Velocity() * (cos(Angle()) - sin(Angle()));
if (isnan(val))
{
printf("Speed side val is NaN. V = %f, A = %f\n", m_Velocity, m_Angle);
}
return val;
}
double CSpeed::Right() const
{
double val = Velocity() * (cos(Angle()) + sin(Angle()));
if (isnan(val))
{
printf("Speed side val is NaN. V = %f, A = %f\n", m_Velocity, m_Angle);
}
return val;
}
void CSpeed::SetComponents(double left, double right)
{
if (left == 0 && right == 0)
{
SetAngularComponents(0, 0);
return;
}
double straight = (left + right) / 2; // cos(a)*v
double turn = right - straight; // sin(a)*v
double a = atan2(turn, straight);
if (isnan(a)) throw Exception("Resulting angle is NaN. Left: " + std::to_string(left) + ", Right: " + std::to_string(left), 87);
double v = straight / cos(a);
if (v == 0) v = turn / sin(a);
if (isnan(v)) throw Exception("Resulting velocity is NaN. Left: " + std::to_string(left) + ", Right: " + std::to_string(left), 87);
SetAngularComponents(v, a);
}
void CSpeed::SetAngularComponents(double v, double a)
{
SetVelocity(v);
SetAngle(a);
}
void CSpeed::Limit()
{
if (m_VelocityFlipped)
{
m_Velocity = -m_Velocity;
m_VelocityFlipped = false;
}
if (m_Velocity < 0)
{
// m_Velocity = fmin(m_Velocity, -MIN_SPEED);
m_Velocity = fmax(m_Velocity, -MAX_SPEED);
return;
}
m_Velocity = fmin(m_Velocity, MAX_SPEED);
// m_Velocity = fmax(m_Velocity, MIN_SPEED);
}
CSpeed CSpeed::operator+(CSpeed other)
{
CSpeed sum;
double left = this->Left() + other.Left();
double right = this->Right() + other.Right();
sum.SetComponents(left, right);
return sum;
}
CSpeed CSpeed::operator-(CSpeed other)
{
CSpeed diff;
double left = this->Left() - other.Left();
double right = this->Right() - other.Right();
diff.SetComponents(left, right);
return diff;
}
CSpeed CSpeed::operator*(double factor)
{
CSpeed prod;
prod.SetAngle(this->Angle());
prod.SetVelocity(this->Velocity()*factor);
return prod;
}
CSpeed CSpeed::operator/(double factor)
{
CSpeed div;
div.SetAngle(this->Angle());
div.SetVelocity(this->Velocity()/factor);
return div;
}
CSpeed & CSpeed::operator+=(CSpeed other)
{
SetComponents(this->Left() + other.Left(), this->Right() + other.Right());
return *this;
}
CSpeed & CSpeed::operator-=(CSpeed other)
{
SetComponents(this->Left() - other.Left(), this->Right() - other.Right());
return *this;
}
CSpeed & CSpeed::operator*=(double factor)
{
SetVelocity(this->Velocity()*factor);
return *this;
}
CSpeed & CSpeed::operator/=(double factor)
{
SetVelocity(this->Velocity()/factor);
return *this;
}
bool CSpeed::operator<(const CSpeed & other) const
{
return this->Velocity() < other.Velocity();
}