-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathPosition.cpp
More file actions
148 lines (128 loc) · 3.84 KB
/
Position.cpp
File metadata and controls
148 lines (128 loc) · 3.84 KB
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
/*
* Position.cpp
*
* Created on: Feb 13, 2016
* Author: Noah Zbozny
*/
#include "Position.h"
#include "math.h"
#define PI 3.14159265
Position::Position():
mxp(I2C::Port::kMXP, 50)
{
mxp.ZeroYaw();
xPos = Constants::xStartPos;
yPos = Constants::yStartPos;
angle = mxp.GetYaw();
pitch = mxp.GetPitch();
encoderTicks = 0;
}
void Position::TrackX(bool movingForward, int encoderTickValue) {
float distance;
float xDistance;
angle = GetAngle();
pitch = GetPitch();
distance = encoderTicks / Constants::ticksPerRotation * Constants::quadratureEncoderFactor * 2 * PI * wheelRadius;
xDistance = distance * cos(angle) * cos(pitch);
/*
* you're basically just rotating the 2D plane to be using the Z and X axes, so using cosine of pitch
* will work to give you the correct value. I can draw it out for anyone who wants
*/
if (movingForward) {
xPos = xPos + xDistance;
} else {
xPos = xPos - xDistance;
}
}
void Position::TrackY(bool movingForward, int encoderTickValue) {
float distance;
float yDistance;
angle = GetAngle();
pitch = GetPitch();
distance = encoderTicks / Constants::ticksPerRotation * Constants::quadratureEncoderFactor * 2 * PI * wheelRadius;
yDistance = distance * sin(angle) * cos(pitch);
/*
* you're basically just rotating the 2D plane to be using the Z and Y axes, so using the cosine of pitch
* will work to give you the correct value. I can draw it out for anyone who wants
*/
if (movingForward) {
yPos = yPos + yDistance;
} else {
yPos = yPos - yDistance;
}
}
void Position::Update(bool movingForward, int encoderTickValue) {
encoderTicks = encoderTickValue - encoderTicksOffset;
encoderTicksOffset = encoderTicksOffset + encoderTickValue;
TrackX(movingForward, encoderTicks);
TrackY(movingForward, encoderTicks);
}
/* void Position::Calibrate() {
int nearestObstacle = NearestObstacle();
xPos = obstacleXPos[nearestObstacle];
yPos = obstacleYPos[nearestObstacle];
}*/
int Position::NearestObstacle() {
double obstacleDistance [5] = {
sqrt(pow(xPos - obstacleXPos[0], 2) + pow(yPos - obstacleYPos[0], 2)),
sqrt(pow(xPos - obstacleXPos[1], 2) + pow(yPos - obstacleYPos[1], 2)),
sqrt(pow(xPos - obstacleXPos[2], 2) + pow(yPos - obstacleYPos[2], 2)),
sqrt(pow(xPos - obstacleXPos[3], 2) + pow(yPos - obstacleYPos[3], 2)),
sqrt(pow(xPos - obstacleXPos[4], 2) + pow(yPos - obstacleYPos[4], 2))
};
int nearestObstacle = 0;
int minDistance = obstacleDistance[0];
for (int i = 0; i < 5; i++) {
if (obstacleDistance[i] < minDistance) {
minDistance = obstacleDistance[i];
nearestObstacle = i;
}
}
return nearestObstacle;
}
float Position::GetX() {
return xPos;
}
float Position::GetY() {
return yPos;
}
float Position::AngleToTower() {
float theta = mxp.GetAngle();
float xToTower = Constants::towerX - xPos;
float yToTower = Constants::towerY - yPos;
float dotProduct;
float uLength;
float vLength;
float angleToTower;
dotProduct = (-1 * xPos) * (xToTower) + (-1 * xPos * tan(90 - theta)) * (yToTower);
uLength = sqrt(pow(-1 * xPos, 2) + pow(-1 * xPos * tan(90 - theta), 2));
vLength = sqrt(pow(xToTower, 2) + pow(yToTower, 2));
angleToTower = acos(dotProduct/(uLength * vLength)); //linear algebra
return angleToTower;
}
float Position::DistanceToTower() {
float xPart;
float yPart;
float distance;
xPart = Constants::towerX - xPos;
yPart = Constants::towerY - yPos;
distance = sqrt(pow(xPart, 2) + pow(yPart, 2));
return distance;
}
float Position::GetAngle() {
return fmod((mxp.GetYaw() + Constants::gyroOffset), 360) * PI / 180;
}
float Position::GetAngleDegrees()
{
return mxp.GetYaw();
}
float Position::GetPitch() {
return mxp.GetPitch() * PI / 180.0;
}
bool Position::IsTurning() {
return mxp.IsRotating();
}
void Position::ZeroYaw()
{
mxp.ZeroYaw();
}