-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtread_controller.cpp
executable file
·176 lines (147 loc) · 5.34 KB
/
tread_controller.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
#include "tread_controller.h"
Tread_Controller::Tread_Controller() {
}
void Tread_Controller::move_forward() {
leftMotor->setSpeed(150);
rightMotor->setSpeed(150);
leftMotor->run(FORWARD);
rightMotor->run(FORWARD);
}
void Tread_Controller::rotate_left() {
leftMotor->setSpeed(150);
rightMotor->setSpeed(150);
leftMotor->run(BACKWARD);
rightMotor->run(FORWARD);
}
void Tread_Controller::rotate_right() {
leftMotor->setSpeed(150);
rightMotor->setSpeed(150);
leftMotor->run(FORWARD);
rightMotor->run(BACKWARD);
}
void Tread_Controller::move_backwards() {
leftMotor->setSpeed(150);
rightMotor->setSpeed(150);
leftMotor->run(FORWARD);
rightMotor->run(FORWARD);
}
void Tread_Controller::halt() {
leftMotor->run(RELEASE);
rightMotor->run(RELEASE);
}
/**************************************************************************/
/*
Calculate the intermediate distance using accelerometer values
*/
/**************************************************************************/
float Tread_Controller::calc_dist() {
bno = Adafruit_BNO055(55);
initialVelocity.first = 0.0;
initialVelocity.second = 0.0;
imu::Vector<3> acc = bno.getVector(Adafruit_BNO055::VECTOR_LINEARACCEL); //in m/s2
float ax = acc.x(); // in meters/second^2
float ay = acc.y(); // in meters/second^2
ux = velocities.first; // in meters/second
uy = velocities.second; // in meters/second
// calculate the change in velocities in x and y axes
// velocity for the next timestep
vx = ux + (ax * (interval * 0.001)); // in meters/second
vy = uy + (ay * (interval * 0.001)); // in meters/second
// find the distance in x and y axes
float sx = (ux * (interval * 0.001)) + (0.5 * ax * (pow((interval * 0.001), 2)));
float sy = (uy * (interval * 0.001)) + (0.5 * ay * (pow((interval * 0.001), 2)));
float s = sqrt(pow(sx, 2) + pow(sy, 2));
// store the final velocities to update the initial velocities for next timestep
velocities.first = vx;
velocities.second = vy;
return s; // in meters
}
/**************************************************************************/
/*
Forward drive distance accumulation
*/
/**************************************************************************/
void Tread_Controller::robot_drive(float distanceToTravel) {
// current_Millis = millis();
while ((distanceToTravel - totalDistance) > distTolerance) {
current_Millis = millis();
// For timing the measurements for correct interval
if (current_Millis - previousMillis > interval) {
move_forward();
Serial.print("td: "); Serial.println(totalDistance);
delay(10);
totalDistance += calc_dist();
previousMillis = current_Millis;
Serial.print("previousMillis: "); Serial.println(previousMillis);
delay(10);
}
}
halt(); // stop the robot
Serial.println("Reached");
totalDistance = 0.0; // reset the interative distance variable
}
/**************************************************************************/
/*
Clockwise roatation of robot until specified angle
*/
/**************************************************************************/
void Tread_Controller::robot_rotate_CW(float rotateAngle) {
if (isFirstTime == true) {
imu::Vector<3> eulerAngle = bno.getVector(Adafruit_BNO055::VECTOR_EULER);
yaw = eulerAngle.x(); // store first degree in variable yaw
isFirstTime = false;
}
// calculate the relative angle to rotate
final_yaw = (((int)yaw + (int)rotateAngle) % 360);
while (curr_yaw > ( (float)final_yaw + rotationTolerance / 2.0 ) ||
curr_yaw < ( (float)final_yaw - rotationTolerance / 2.0 )) {
rotate_right(); // rotate right
imu::Vector<3> eulerAngle = bno.getVector(Adafruit_BNO055::VECTOR_EULER);
curr_yaw = eulerAngle.x(); // store current degrees every timestep
Serial.print(curr_yaw);
Serial.print(", ");
Serial.print(final_yaw);
Serial.print(", ");
Serial.println(rotationTolerance);
delay(10);
}
halt(); // stop the robot
//yaw = curr_yaw;
imu::Vector<3> eulerAngle = bno.getVector(Adafruit_BNO055::VECTOR_EULER);
yaw = eulerAngle.x();
}
/**************************************************************************/
/*
Counter-Clockwise roatation of robot until specified angle
*/
/**************************************************************************/
void Tread_Controller::robot_rotate_CCW(float rotateAngle) {
if (isFirstTime == true) {
imu::Vector<3> eulerAngle = bno.getVector(Adafruit_BNO055::VECTOR_EULER);
yaw = eulerAngle.x(); // store first degree in variable yaw
isFirstTime = false;
}
// calculate the relative angle to rotate
final_yaw = ((int)yaw - (int)rotateAngle);
Serial.println("I'm here");
delay(10);
if (final_yaw < 0) {
final_yaw = 360 + final_yaw;
}
curr_yaw = yaw;
while (curr_yaw < ( (float)final_yaw - rotationTolerance / 2.0 ) ||
curr_yaw > ( (float)final_yaw + rotationTolerance / 2.0 )) {
rotate_left(); // rotate left
imu::Vector<3> eulerAngle = bno.getVector(Adafruit_BNO055::VECTOR_EULER);
curr_yaw = eulerAngle.x(); // store current degrees every timestep
Serial.print(curr_yaw);
Serial.print(", ");
Serial.print(final_yaw);
Serial.print(", ");
Serial.println(rotationTolerance);
delay(10);
}
halt(); // stop the robot
imu::Vector<3> eulerAngle = bno.getVector(Adafruit_BNO055::VECTOR_EULER);
yaw = eulerAngle.x();
}