Roomba Controller Dashboard 1
A GUI and TCP client application that is used to control a Roomba for Computer Engineering 2880 @ Iowa State
Loading...
Searching...
No Matches
Pose2D.cpp
Go to the documentation of this file.
1//
2// Created by caleb on 10/24/2024
3//
4
5#include "Pose2D.hpp"
6
7Pose2D::Pose2D(double x, double y, double heading) {
8 this->x = x;
9 this->y = y;
10 this->heading = heading;
11}
12
13Pose2D::Pose2D(double x, double y) {
14 this->x = x;
15 this->y = y;
16}
17
19 this->x = 0;
20 this->y = 0;
21 this->heading = 0;
22}
23
24bool Pose2D::isOnLine(Pose2D LineEnd1, Pose2D LineEnd2) {
25 double y1 = LineEnd1.getY();
26 double x1 = LineEnd1.getX();
27
28 double y2 = LineEnd2.getY();
29 double x2 = LineEnd2.getX();
30
31 if (x1 > x2) {
32 // need to swap
33 double holder = x1;
34 x1 = x2;
35 x2 = holder;
36 holder = y1;
37 y1 = y2;
38 y2 = holder;
39 }
40
41 // base case of the line just being y =
42 if (std::abs(y1 - y2) < 0.1) {
43 // straight y = line
44 return (this->getX() < x2 && this->getX() > x1);
45 }
46
47 // case where the line is just x =
48 else if (std::abs(x1 - x2) < 0.1) {
49 return (this->getY() < std::fmax(y1, y2) && this->getY() > std::fmin(y1, y2));
50 }
51
52 // y = mx + b line can be formed
53 else {
54 double m, b;
55 // find the equation of the line and plugin this coordinates into it to see if it is close to on the line
56 m = (y2 - y1) / (x2 - x1);
57 b = y1 - m * x1; // B?
58
59 double wouldBeY = m * this->getX() + b;
60 return fabs(wouldBeY - this->getY()) < 0.1;
61 }
62}
63
64Pose2D::Pose2D(const Pose2D& position) {
65 this->x = position.x;
66 this->y = position.y;
67 this->heading = position.heading;
68}
69
70/*
71Pose2D::Pose2D(Pose2D* position) {
72 this->x = position->x;
73 this->y = position->y;
74 this->heading = position->heading;
75}
76*/
77
78double Pose2D::angleTo(const Pose2D& other) const {
79 return atan2(other.y - this->y, other.x - this->x);
80}
81
82void Pose2D::addAngle(double angle) {
83 this->heading += angle;
84}
85
86double Pose2D::distanceTo(const Pose2D& other) const {
87 return hypot(this->x - other.x, this->y - other.y);
88}
89
90double Pose2D::squareOfDistanceTo(const Pose2D& other) const {
91 return pow(other.x -this->x, 2) + pow(other.y - this->y, 2);
92}
93
94void Pose2D::transformForPose(const Pose2D& other) {
95 rotateByPose(other);
96 this->x += other.x;
97 this->y += other.y;
98}
99
100void Pose2D::plus(const Pose2D& other) {
101 this->x += other.x;
102 this->y += other.y;
103 this->heading += other.heading;
104}
105
107 return {this->x, this->y, this->heading};
108}
109
110void Pose2D::vecAdd(const double angle, const double magnitude) {
111 this->x += magnitude * cos(angle);
112 this->y += magnitude * sin(angle);
113}
114
115
116void Pose2D::rotateByAngle(double angle) {
117 double newX = this->x * cos(angle) - this->y * sin(angle);
118 this->y = this->x * sin(angle) + this->y * cos(angle);
119 this->x = newX;
120 this->heading += angle;
121}
122
123void Pose2D::plusCoord(const Pose2D& other) {
124 this->x += other.x;
125 this->y += other.y;
126}
127
128void Pose2D::rotateByPose(const Pose2D& rotation) {
129 rotateByAngle(rotation.heading);
130}
131
132void Pose2D::translateByPose(const Pose2D& translation) {
133 this->x += translation.x;
134 this->y += translation.y;
135}
136
137void Pose2D::translateByMagnitude(double magnitude) {
138 this->x += magnitude * cos(this->heading);
139 this->y += magnitude * sin(this->heading);
140}
141
142void Pose2D::transformPose(const Pose2D& modifier) {
143 translateByPose(modifier);
144 rotateByPose(modifier);
145}
146
147double Pose2D::getX() const {
148 return x;
149}
150
151double Pose2D::getY() const {
152 return y;
153}
154
155double Pose2D::getHeading() const {
156 return heading;
157}
158
159void Pose2D::setHeading(double angle) {
160 this->heading = angle;
161}
162
163void Pose2D::setX(double x) {
164 this->x = x;
165}
166
167void Pose2D::setY(double y) {
168 this->y = y;
169}
170
171uint8_t Pose2D::getQuadrant() const {
172 if (x == 0 || y == 0) {
173 return 0;
174 }
175 if (x < 0) {
176 if (y < 0) {
177 return 3;
178 }
179 return 2;
180 }
181 if (y > 0) {
182 return 1;
183 }
184 return 4;
185}
186
187void Pose2D::minus(const Pose2D other) {
188 this->x -= other.x;
189 this->y -= other.y;
190 this->heading -= other.heading;
191}
192
193Pose2D Pose2D::fromPolar(double magnitude, double angle) {
194 double x = magnitude * cos(angle);
195 double y = magnitude * sin(angle);
196 Pose2D pose(x, y, 0);
197 return pose;
198}
199
200double Pose2D::degreesToRadians(double degrees) {
201 return degrees * M_PI / 180.0;
202}
203
204Pose2D Pose2D::parseFromStream(std::istringstream& stream) {
205 double x, y, heading;
206 if (stream >> x >> y >> heading) {
207 return {x, y, heading};
208 }
209
210 return {0, 0};
211}
212
213Pose2D Pose2D::subtractBy(const Pose2D& other) const {
214 return {this->x - other.x, this->y - other.y};
215}
216
217Pose2D Pose2D::scaleBy(double scalar) const {
218 return {this->x * scalar, this->y * scalar};
219}
220
222 double squared = sqrt(pow(this->x, 2) + pow(this->y, 2));
223 return {this->x / squared, this->y / squared};
224}
225
226double Pose2D::dotProduct(const Pose2D& other) const {
227 return this->x * other.x + this->y * other.y;
228}
229
230std::ostream &operator<<(std::ostream &os, const Pose2D &d) {
231 os << "x: " << d.x << " y: " << d.y << " heading: " << d.heading;
232 return os;
233}
234
236 heading = fmod(heading, 2 * M_PI);
237
238 if (heading > M_PI) {
239 heading -= 2 * M_PI;
240 }
241
242 if (heading < - M_PI) {
243 heading += 2 * M_PI;
244 }
245}
246
247
248Rectangle makeRectangleFromLine(const Pose2D& L1, const Pose2D& L2, double width) {
249 double angleBetweenPoints = L1.angleTo(L2);
250 double lengthBetween = L1.distanceTo(L2);
251 Pose2D head(L1);
252 head.setHeading(angleBetweenPoints - M_PI);
253 head.translateByMagnitude(width);
254 Pose2D r1(head);
255 head.addAngle(M_PI);
256 head.translateByMagnitude(lengthBetween);
257 Pose2D r2(head);
258 head.addAngle(M_PI);
259 head.translateByMagnitude(width * 2);
260 Pose2D r3(head);
261 head.addAngle(M_PI);
262 head.translateByMagnitude(lengthBetween);
263
264 return (Rectangle) {r1, r2, r3, head};
265}
266
267void Pose2D::multiply(double d) {
268 this->x *= d;
269 this->y *= d;
270}
std::ostream & operator<<(std::ostream &os, const Pose2D &d)
Definition Pose2D.cpp:230
Rectangle makeRectangleFromLine(const Pose2D &L1, const Pose2D &L2, double width)
Definition Pose2D.cpp:248
void translateByPose(const Pose2D &translation)
Definition Pose2D.cpp:132
void setHeading(double angle)
Definition Pose2D.cpp:159
void multiply(double d)
Definition Pose2D.cpp:267
void rotateByAngle(double angle)
Definition Pose2D.cpp:116
Pose2D()
Definition Pose2D.cpp:18
double angleTo(const Pose2D &other) const
Definition Pose2D.cpp:78
void setY(double y)
Definition Pose2D.cpp:167
Pose2D clone() const
Definition Pose2D.cpp:106
static Pose2D fromPolar(double magnitude, double angle)
Definition Pose2D.cpp:193
double dotProduct(const Pose2D &other) const
Definition Pose2D.cpp:226
double heading
Definition Pose2D.hpp:25
bool isOnLine(Pose2D LineEnd1, Pose2D LineEnd2)
Definition Pose2D.cpp:24
double x
Definition Pose2D.hpp:25
void transformForPose(const Pose2D &other)
Definition Pose2D.cpp:94
void wrapHeading()
Definition Pose2D.cpp:235
void plus(const Pose2D &other)
Definition Pose2D.cpp:100
static Pose2D parseFromStream(std::istringstream &stream)
Definition Pose2D.cpp:204
void transformPose(const Pose2D &modifier)
Definition Pose2D.cpp:142
double squareOfDistanceTo(const Pose2D &other) const
Definition Pose2D.cpp:90
void addAngle(double angle)
Definition Pose2D.cpp:82
double y
Definition Pose2D.hpp:25
void translateByMagnitude(double magnitude)
Definition Pose2D.cpp:137
double distanceTo(const Pose2D &other) const
Definition Pose2D.cpp:86
uint8_t getQuadrant() const
Definition Pose2D.cpp:171
double getHeading() const
Definition Pose2D.cpp:155
void vecAdd(double angle, double magnitude)
Definition Pose2D.cpp:110
Pose2D scaleBy(double scaler) const
Definition Pose2D.cpp:217
Pose2D normalize() const
Definition Pose2D.cpp:221
void plusCoord(const Pose2D &other)
Definition Pose2D.cpp:123
double getY() const
Definition Pose2D.cpp:151
static double degreesToRadians(double degrees)
Definition Pose2D.cpp:200
Pose2D subtractBy(const Pose2D &other) const
Definition Pose2D.cpp:213
double getX() const
Definition Pose2D.cpp:147
void minus(Pose2D other)
Definition Pose2D.cpp:187
void rotateByPose(const Pose2D &rotation)
Definition Pose2D.cpp:128
void setX(double x)
Definition Pose2D.cpp:163