-
Notifications
You must be signed in to change notification settings - Fork 0
/
Talas.h
296 lines (270 loc) · 6.85 KB
/
Talas.h
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/**
* \file
* \brief Talas library main header file.
* \author Vasiliy Polyakov <vp@psu.ru>
* \author RobotPSU https://robot.psu.ru/
* \date 2017-2018
*/
#ifndef Talas_h
#define Talas_h
#include <Arduino.h>
#include "Turret.h"
#ifdef DOXYGEN
# define USE_AT_MODE ///< Whether to build AT-commands mode support (boolean).
#else
//# define USE_AT_MODE true
#endif
#define TALAS_MAX_NAME_LENGTH 24 ///< Maximum name length (characters).
#define TALAS_MAX_HEALTH 3 ///< Maximum health (1..3).
#define TALAS_START_SPEED 150 ///< Start speed value (0..255).
#define TALAS_LIGHT_SAMPLES 100 ///< Number of light samples for illuminance check.
#define TALAS_LIGHT_DELAY 20 ///< Delay between light samples (ms).
#define TALAS_TIMER_FREQ 1000 ///< Hit detector timer frequency (Hz).
#define TALAS_DISPATCH_DELAY 10 ///< Delay between dispatch iterations (ms).
#define IMPULSE_VARIANCE 50 ///< Impulse length variance (s).
#define SERIAL_SPEED 9600 ///< Serial port speed rate (baud).
#define BT_SPEED 38400 ///< Bluetooth speed rate (baud).
#define BT_TIMEOUT 100 ///< Bluetooth timeout (ms).
#define BT_STR_TERM '\n' ///< Bluetooth string terminator.
/**
* Impulse data.
* Contains information about received laser impulses.
*/
typedef struct {
uint16_t n; ///< Impulse number.
uint16_t length; ///< Impulse length (ms).
uint16_t value; ///< Impulse value (\c HIGH or \c LOW).
} Impulse;
/**
* Main TALAS class.
* Laser tank (or basin) controlling code.
* \nosubgrouping
*/
class Talas
{
public:
/**
* Constructor.
* \param n TALAS name.
*/
Talas(String n);
/**
* \name Device Attachments
* For use in \c setup() function.
* \{
*/
/**
* Attach health indicator.
* Indicator uses digital pins for 4 LEDs.
* \param pin0 pin for red LED 'dead'.
* \param pin1 pin for green LED '1 life left'.
* \param pin2 pin for green LED '2 lives left'.
* \param pin3 pin for green LED '3 lives left'.
*/
void attachHealth(uint8_t pin0, uint8_t pin1, uint8_t pin2, uint8_t pin3);
/**
* Attach DC motors driver with constant full speed.
* \note Driver pins ENA and ENB must be closed by jumpers to use this method.
* \param pinIn1,pinIn2,pinIn3,pinIn4 Arduino pins for driver input.
*/
void attachDriver(uint8_t pinIn1, uint8_t pinIn2, uint8_t pinIn3, uint8_t pinIn4);
/**
* Attach DC motors driver with speed control.
* \note There must be no jumpers on driver pins ENA and ENB to use this method.
* \param pinIn1,pinIn2,pinIn3,pinIn4 Arduino pins for driver input.
* \param pinEnA,pinEnB Arduino pins for driver "enable" pins. Must be PWM-enabled.
*/
void attachDriver(uint8_t pinIn1, uint8_t pinIn2, uint8_t pinIn3, uint8_t pinIn4,
uint8_t pinEnA, uint8_t pinEnB);
/**
* Attach turret with laser gun only.
* \param pinLaser pin for laser gun.
*/
void attachTurret(uint8_t pinLaser);
/**
* Attach movable turret with laser gun.
* \param pinLaser pin for laser gun.
* \param pinH pin for horizontal servo.
* \param pinV pin for vertical servo.
*/
void attachTurret(uint8_t pinLaser, uint8_t pinH, uint8_t pinV);
/**
* Attach hit detector.
* \param pinHit Arduino pin for hit detector. Must be analog.
*/
void attachHitDetector(uint8_t pinHit);
/**
* Attach Bluetooth communication module.
* \note TALAS uses Serial1 for Bluetooth on Arduino Mega.
*/
void attachBluetooth();
/** \} */
/**
* \name Name
* \{
*/
/**
* Set name.
* \param n new name.
*/
void name(String n);
/**
* Get name.
* \return Name.
*/
String name();
/** \} */
/**
* \name Health Indicator
* \{
*/
/**
* Get health.
* \return Health.
*/
uint8_t health();
/**
* Reset health (set health to a maximum value).
*/
void resetHealth();
/** \} */
/**
* \name Hit Detector
* \{
*/
/**
* Handle timer interrupt and detect laser impulses.
* Usage in Arduino sketch:
\code
ISR(TIMER3_COMPB_vect) {
talas.handleImpulse();
}
\endcode
*/
void handleImpulse();
/**
* Handle hits.
* Decrease health and check if dead already.
*/
void hit();
/** \} */
/**
* \name Movement
* \{
*/
/**
* Move forward.
*/
void forward();
/**
* Move backward.
*/
void backward();
/**
* Turn left.
*/
void left();
/**
* Turn right.
*/
void right();
/**
* Stop.
*/
void stop();
/**
* Set speed.
* \param s speed value.
*/
void speed(uint8_t s);
/**
* Get speed.
* \return Speed.
*/
uint8_t speed();
/**
* Move slower.
* Decrease speed.
*/
void slower();
/**
* Move faster.
* Decrease. speed.
*/
void faster();
/** \} */
/**
* \name Turret
* See Turret class reference.
* \{
*/
void setTurretRange(uint8_t, uint8_t, uint8_t, uint8_t);
void setTurretDelta(uint8_t, uint8_t);
void resetTurret();
uint8_t getTurretH(); // Turret horizontal angle
uint8_t getTurretV(); // Turret vertical angle
void turretUp();
void turretDown();
void turretLeft();
void turretRight();
/** \} */
/**
* \name Laser Gun
* \{
*/
/**
* Fire laser gun.
*/
void fire();
/** \} */
/**
* \name Bluetooth Commands
* \{
*/
/**
* Dispatch Bluetooth commands.
* Usage in Arduino sketch:
* \code
* void loop() {
* talas.dispatch();
* }
* \endcode
*/
void dispatch();
/** \} */
private:
static const size_t _impulses[];
static const size_t _impulseMax;
/// L289N driver input pins
uint8_t _pinRight1, _pinRight2, _pinLeft1, _pinLeft2;
/// L289N driver enable pins
uint8_t _pinRightEn, _pinLeftEn;
/// Health indicator (LED) control pins
uint8_t _pinHealth[TALAS_MAX_HEALTH + 1];
/// Hit sensor pin
uint8_t _pinHit;
String _name; ///< Name.
volatile uint8_t _health; ///< Health (0..3).
unsigned _lux; ///< Illuminance.
volatile Impulse _impulse; ///< Received impulse data.
// Motors speed
uint8_t _speedRight, _speedLeft;
Turret _turret;
void showHealth();
void move(uint8_t);
int getLight();
void checkLight();
void setImpulseTimer();
#if defined(SERIAL_PORT_HARDWARE_OPEN) && defined(USE_AT_MODE)
// HC-05 Bluetooth module AT-commands mode
static const unsigned long _rates[]; ///< List of supported speed rates.
static const size_t _numRates; ///< Number of supported speed rates.
static const char *_cmds[]; ///< Info commands list.
static const size_t _numCmds; ///< Number of info commands.
unsigned long _atMode; ///< Actual speed rate (baud).
unsigned long inCmdMode(); ///< Whether module is in AT-commands mode.
void printBtInfo(); ///< Send information about module.
#endif
};
#endif
/* vim: set ft=arduino et sw=2 ts=2: */