/
Turret.cpp
251 lines (184 loc) · 4.05 KB
/
Turret.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#include "Arduino.h"
#include "Turret.h"
Turret::Turret()
{
pinMode(AZIMUTH_DIR_PIN, OUTPUT);
pinMode(AZIMUTH_STEP_PIN, OUTPUT);
pinMode(ELEVATION_DIR_PIN, OUTPUT);
pinMode(ELEVATION_STEP_PIN, OUTPUT);
pinMode(AZIMUTH_CUTOUT_PIN, INPUT);
pinMode(ELEVATION_CUTOUT_PIN, INPUT);
pinMode(FIRE_PIN, OUTPUT);
pinMode(MOTOR_POWER_PIN, OUTPUT);
digitalWrite(FIRE_PIN, LOW);
digitalWrite(MOTOR_POWER_PIN, HIGH);
target = {0, 0};
_position = {0, 0};
_motorPowerState = false;
}
void Turret::goHome(void)
{
motors(ON);
reset();
_elevate(ELEVATION_HOME);
_rotate(AZIMUTH_HOME);
motors(OFF);
}
void Turret::reset(void)
{
motors(ON);
while(!_isElevationCutout())
_elevate(-1, true);
while(!_isAzimuthCutout())
_rotate(-1, true);
target = {0, 0};
_position = {0, 0};
_motorPowerState = true;
}
bool Turret::aim(void)
{
return _rotate(target.azimuth - _position.azimuth) && _elevate(target.elevation - _position.elevation);
}
bool Turret::aim(int a, int e)
{
setTarget(a, e);
return aim();
}
bool Turret::setTarget(int a, int e)
{
if(_isTargetElevationValid(e) && _isTargetAzimuthValid(a))
{
target.elevation = e;
target.azimuth = a;
return true;
}
else
{
return false;
}
}
void Turret::aimAndFire(int a, int e)
{
if(aim(a, e))
{
delay(500);
fire();
}
}
void Turret::fire(void)
{
digitalWrite(FIRE_PIN, HIGH);
delay(250);
digitalWrite(FIRE_PIN, LOW);
}
bool Turret::_elevate(int delta)
{
return _elevate(delta, false);
}
bool Turret::_elevate(int delta, bool overrideCutoutProtection)
{
_checkMotors();
_setElevationDirection(delta);
if(!overrideCutoutProtection && !_isTargetElevationValid(delta))
{
return false;
}
for(int k = 0; k < abs(delta); k++)
{
digitalWrite(ELEVATION_STEP_PIN, LOW);
delayMicroseconds(2);
digitalWrite(ELEVATION_STEP_PIN, HIGH);
delayMicroseconds(ELEVATION_MOTOR_DELAY);
_position.elevation += (delta < 0) ? -1 : 1;
if(_isElevationCutout()) return false;
}
return true;
}
bool Turret::_isTargetElevationValid(int delta)
{
return (_position.elevation + delta < EL_MAX && _position.elevation + delta > EL_MIN);
}
bool Turret::_isElevationCutout(void)
{
return digitalRead(ELEVATION_CUTOUT_PIN) == LOW;
}
void Turret::_setElevationDirection(int delta)
{
if(delta < 0)
{
digitalWrite(ELEVATION_DIR_PIN, HIGH);
}
else
{
digitalWrite(ELEVATION_DIR_PIN, LOW);
}
delay(1);
}
bool Turret::_rotate(int delta)
{
return _rotate(delta, false);
}
bool Turret::_rotate(int delta, bool overrideCutoutProtection)
{
_checkMotors();
_setAzimuthDirection(delta);
if(!overrideCutoutProtection && _isTargetAzimuthValid(delta))
{
return false;
}
for(int k = 0; k < abs(delta); k++)
{
digitalWrite(AZIMUTH_STEP_PIN, LOW);
delayMicroseconds(2);
digitalWrite(AZIMUTH_STEP_PIN,HIGH);
delayMicroseconds(AZIMUTH_MOTOR_DELAY);
_position.azimuth += (delta < 0) ? -1 : 1;
if(_isAzimuthCutout()) return false;
}
return true;
}
bool Turret::_isTargetAzimuthValid(int delta)
{
return (_position.azimuth + delta < AZ_MAX || _position.azimuth + delta > AZ_MIN);
}
bool Turret::_isAzimuthCutout(void)
{
return digitalRead(AZIMUTH_CUTOUT_PIN) == LOW;
}
void Turret::_setAzimuthDirection(int delta)
{
if(delta < 0)
{
digitalWrite(AZIMUTH_DIR_PIN, HIGH);
}
else
{
digitalWrite(AZIMUTH_DIR_PIN, LOW);
}
delay(1);
}
// If the motors have been off, then need to reset before we move.
// This ensures the system is in a known position state and avoids
// issues where the turret was moved manually whenthe motors were disengaged.
// It is assumed that when the motors are active that they can hold the system
// in place.
void Turret::_checkMotors(void)
{
if(!_motorPowerState) reset();
}
void Turret::motors(bool mode)
{
digitalWrite(MOTOR_POWER_PIN, mode);
if(!mode) _motorPowerState = false;
}
String Turret::toString(void)
{
String str = "Azimuth: ";
str.concat(_position.azimuth);
str.concat(" Elevation: ");
str.concat(_position.elevation);
str.concat(" Weapon: ");
str.concat(_motorPowerState);
return str;
}
Turret::~Turret(){} // Nothing to destruct