-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmotorsManual.cpp
More file actions
170 lines (141 loc) · 4.89 KB
/
motorsManual.cpp
File metadata and controls
170 lines (141 loc) · 4.89 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#include "motorsManual.h"
#include "encoderStuff.h"
#include <Zumo32U4.h>
Zumo32U4Motors motors;
motorsManual::motorsManual() {
double speed = 1;
double speedRight;
double speedLeft;
// const int MIN_SPEED = -400;
// const int MAX_SPEED = 400;
// the minimum is 1 because we are going to multiplied
// const int minimumTuriningValue = 1;
// const int maximumTurningValue = 6;
//intensity of steering changes
// const double steerIntensity = 1.3;
// }
// determines if the robot should move or pause
bool allowDrive = true;
//default steer value. Value is later on multiplied with so the default is 1
double steerRight = minimumTuriningValue;
//default steer value. Value is later on multiplied with so the default is 1
double steerLeft = minimumTuriningValue;
/// change the turning left speed by the steerIntensity
void motorsManual::moveLeft() {
encoder.resetEncoderCounts();
if (!(steerRight < maximumTurningValue)) {
return;
}
if (steerLeft < minimumTuriningValue) {
steerRight *= steerIntensity; //increase right wheel speed
} else {
steerLeft /= steerIntensity; //reduce left wheel speed if condition not met
}
}
/// change the turning right speed by the steerIntensity
void motorsManual::moveRight() {
encoder.resetEncoderCounts();
if (steerLeft < maximumTurningValue) {
if (steerRight < minimumTuriningValue) {
steerLeft *= steerIntensity; //increase left wheel speed
} else {
steerRight /= steerIntensity; //decrease right wheel speed if condition not met
}
}
}
// move slower by 10 %
void motorsManual::moveSlower() {
encoder.resetEncoderCounts();
if (speed > -10) //checks if speed is above mimimum allowed value (NOTE: speed within functions isn't the actual motor speed.
{ //The actual speeds for the motorsManual are calculated at the end using a formula)
speed -= 1;
Serial1.println((String) "Speed: " + speed + " --");
}
}
/// does the opposite as moveSlower(), to move faster
void motorsManual::moveFaster() {
encoder.resetEncoderCounts();
if (speed < 10) //checks if speed is below maximum allowed value
{
speed += 1;
Serial1.println((String) "Speed: " + speed + " ++");
}
}
/// set speed to 8
void motorsManual::moveToMaxSpeed() {
encoder.resetEncoderCounts();
speed = 8; //set speed to the max
Serial1.println((String) "Speed: " + speed + " ++");
}
/// set speed to 1 (standing still)
void motorsManual::resetSpeed() {
encoder.resetEncoderCounts();
speed = 1;
//This stops the robot because opposite steer is subtracted from speed in the formula that determines the final speed for both motorsManual (1-1=0)
steerLeft = 1;
steerRight = 1;
speedLeft = 0;
speedRight = 0;
//sets drive t true so robot can immediately start to drive again when input it given in case the robot was paused before reset was pressed
allowDrive = true;
}
// reset rotational movement
void motorsManual::resetRotationalMovement() {
encoder.resetEncoderCounts();
//resets steer values but not the overal speed so the robots starts driving straight forward
steerLeft = 1;
steerRight = 1;
}
/// switches the allowDrive flag
void motorsManual::stopContinue() {
encoder.resetEncoderCounts();
allowDrive = !allowDrive;
}
/// rotate param degrees
void motorsManual::rotateDeg(int deg) {
encoder.resetEncoderCounts();
deg *= 10.6;
encoder.setExpectedLeftEncoderCount(-deg);
encoder.setExpectedRightEncoderCount(deg);
}
void motorsManual::setAndNormalizeMotorValues() {
speedLeft = (speed * steerLeft - steerRight) * 50;
speedRight = (speed * steerRight - steerLeft) * 50;
//prints the left/right motor speed and the two steer values
Serial1.println((String) "\nLEFT: " + speedLeft + " steerLeft: " + steerLeft + "\nRIGHT: " + speedRight + " steerRight: " + steerRight);
//these two formulas determine the final speeds for the left and right motor
if (!allowDrive) {
speedLeft = 0; //sets the motor speeds to 0 to stop them. When robot is allowed to drive again, it will continue at the last set speed instead of resetting its speed.
speedRight = 0;
Serial1.println("STOPPED");
}
}
/// set the pins of the motor so it changes the speed
void motorsManual::applyMotorValues() {
encoder.correctOffset(speedLeft, speedRight);
encoder.printCorrectionValues();
motors.setLeftSpeed(speedLeft + encoder.getCorrectLeft());
motors.setRightSpeed(speedRight + encoder.getCorrectRight());
}
// if the motor speed is 0
const bool motorsManual::isStandingStill() {
return (speedLeft == 0 && speedRight == 0);
}
// is the allowDrive flag is set
const bool motorsManual::isAllowDrive() {
return allowDrive;
}
// get motorSpeedLeft
const double motorsManual::getSpeedLeft() {
return speedLeft;
}
// get motorSpeedRight
const double motorsManual::getSpeedRight() {
return speedRight;
}
void motorsManual::setSpeedLeft(double s) {
speedLeft = s;
}
void motorsManual::setSpeedRight(double s) {
speedRight = s;
}