forked from pieterbl/ElegooCarV3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ElegooCarV3.h
191 lines (155 loc) · 4.48 KB
/
ElegooCarV3.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
#ifndef __ELEGOO_CAR_V3_H__
#define __ELEGOO_CAR_V3_H__
#include <Arduino.h>
#include "ElegooConstants.h"
#include "ElegooCarConfig.h"
#include "ElegooDistanceUnit.h"
#include "ElegooMotorUnit.h"
#include "ElegooInfraredReceiver.h"
#include "ElegooBluetoothReceiver.h"
#include "ElegooCommandReader.h"
#include "ElegooManualDriver.h"
#include "ElegooManualDriver2.h"
#include "ElegooAutomaticDriver1.h"
#include "ElegooAutomaticDriver2.h"
#include "ElegooLineTrackingDriver.h"
class ElegooCarV3
{
private:
ElegooCarConfig * carConfig;
ElegooDistanceUnit distUnit;
ElegooMotorUnit motorUnit;
ElegooInfraredReceiver infraredReceiver;
ElegooBluetoothReceiver bluetoothReceiver;
ElegooCommandReader commandReader;
ElegooDriverBase * drivers[5];
ElegooDriverBase * currentDriver = 0;
int safetyDistanceInCM;
public:
ElegooCarV3(ElegooCarConfig * pCarConfig) :
carConfig(pCarConfig), //
distUnit(carConfig->distanceUnitConfig), //
motorUnit(carConfig->motorUnitConfig), //
infraredReceiver(carConfig->infraredReceiverConfig), //
bluetoothReceiver(carConfig->bluetoothReceiverConfig), //
commandReader(infraredReceiver, bluetoothReceiver), //
safetyDistanceInCM(carConfig->SAFETY_DISTANCE_CM)
{
}
int setup()
{
Serial.begin(carConfig->serialConfig.BAUD_RATE);
distUnit.setup();
distUnit.registerCommandReader(&commandReader);
motorUnit.setup();
motorUnit.registerCommandReader(&commandReader);
infraredReceiver.setup();
bluetoothReceiver.setup();
initializeDrivers();
selectManualDriver();
return ElegooConstants::OK;
}
private:
void initializeDrivers()
{
drivers[ElegooCommand::MANUAL_DRIVER_1] = //
new ElegooManualDriver(safetyDistanceInCM, distUnit, motorUnit);
drivers[ElegooCommand::MANUAL_DRIVER_2] = //
new ElegooManualDriver2(safetyDistanceInCM, distUnit, motorUnit);
drivers[ElegooCommand::AUTO_DRIVER_1] = //
new ElegooAutomaticDriver1(safetyDistanceInCM, distUnit, motorUnit);
drivers[ElegooCommand::AUTO_DRIVER_2] = //
new ElegooAutomaticDriver2(safetyDistanceInCM, distUnit, motorUnit);
drivers[ElegooCommand::LINE_TRACKING_DRIVER] = //
new ElegooLineTrackingDriver(safetyDistanceInCM, distUnit, motorUnit);
}
bool isDriver(ElegooCommand possibleDriver)
{
switch (possibleDriver)
{
case ElegooCommand::MANUAL_DRIVER_1:
case ElegooCommand::MANUAL_DRIVER_2:
case ElegooCommand::AUTO_DRIVER_1:
case ElegooCommand::AUTO_DRIVER_2:
case ElegooCommand::LINE_TRACKING_DRIVER:
return true;
default:
return false;
}
}
int selectDriver(ElegooCommand newDriver)
{
if (isDriver(newDriver))
{
currentDriver = drivers[newDriver];
}
return ElegooConstants::OK;
}
bool usingManualDriver()
{
return (currentDriver == drivers[ElegooCommand::MANUAL_DRIVER_1] || //
currentDriver == drivers[ElegooCommand::MANUAL_DRIVER_2]);
}
public:
int selectManualDriver()
{
return selectDriver(ElegooCommand::MANUAL_DRIVER_1);
}
void registerInfraredConfig(ElegooInfraredConfigInterface * infraredConfig)
{
infraredReceiver.registerInfraredConfig(infraredConfig);
}
void registerBluetoothConfig(ElegooBluetoothConfigInterface * bluetoothConfig)
{
bluetoothReceiver.registerBluetoothConfig(bluetoothConfig);
}
int drive()
{
ElegooCommand cmd = commandReader.readCommand();
if (cmd == ElegooCommand::STOP_MOVING)
{
motorUnit.stopMoving();
selectManualDriver();
return ElegooConstants::OK;
}
if (!usingManualDriver() && cmd != ElegooCommand::NO_COMMAND)
{
motorUnit.stopMoving();
selectManualDriver();
// continue processing the given command (button press) below
}
if (usingManualDriver())
{
if (isDriver(cmd))
{
motorUnit.stopMoving();
return selectDriver(cmd);
}
else
{
// manual drivers do correctly handle UNK_COMMAND and even rely on NO_COMMAND
return currentDriver->processCommand(cmd);
}
}
// automatic drivers will/must not listen to commands
// they must just get "re-triggered"
return currentDriver->processCommand(ElegooCommand::NO_COMMAND);
}
void testDistanceUnit()
{
Serial.println("Test Distance Unit");
distUnit.frontDistance();
distUnit.test();
distUnit.frontDistance();
Serial.println();
}
void testInfrared()
{
commandReader.testInfrared();
}
void testBluetooth()
{
commandReader.testBluetooth();
}
};
#endif