-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
256 lines (221 loc) · 8.39 KB
/
main.ts
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
enum Command { STOP, FWD, BKW, LEFT, RIGHT, TURN_LEFT, TURN_RIGHT }
let speed = 100
// in aer = 0
// pe podea alb = 1, negru = 0
let left = -1
let right = -1
let newLeft = -1
let newRight = -1
let newLeftForDebouce = -1
let newLeftForDebounceInertia: number
let newRightForDebounce = -1
let newRightForDebounceInertia: number
let debounceInertiaMax: number;
let lastLateral: Command;
let running = false
let command: Command;
const inertiaMax = 20;
let inertia = 0;
enum TurnState { MOTOR_JUST_STARTED, SECOND_WENT_OFF, FIRST_ON }
let turnState: TurnState;
enum TurnCommand { OFF, TURN_LEFT, TURN_RIGHT };
let turnCommand = TurnCommand.OFF;
let runForDurationStart = 0;
let runForDuration = 0;
// main program
serial.setBaudRate(BaudRate.BaudRate115200);
serial.writeLine("salut")
bluetooth.startUartService()
sendMotorCommand(Command.STOP, 0);
// main loop
basic.forever(function on_forever() {
iter++;
const leftSensor = maqueen.readPatrol(maqueen.Patrol.PatrolLeft)
if (leftSensor != newLeftForDebouce) {
newLeftForDebouce = leftSensor;
newLeftForDebounceInertia = debounceInertiaMax;
}
// else
{
if (newLeftForDebounceInertia >= 0) { newLeftForDebounceInertia--; } // last execution => -1
if (newLeft < 0 || newLeftForDebounceInertia == 0) {
newLeft = newLeftForDebouce;
}
}
const rightSensor = maqueen.readPatrol(maqueen.Patrol.PatrolRight)
if (rightSensor != newRightForDebounce) {
newRightForDebounce = rightSensor;
newRightForDebounceInertia = debounceInertiaMax;
}
// else
{
if (newRightForDebounceInertia >= 0) { newRightForDebounceInertia--; } // last execution => -1
if (newRight < 0 || newRightForDebounceInertia == 0) {
newRight = newRightForDebounce;
}
}
// newLeft = maqueen.readPatrol(maqueen.Patrol.PatrolLeft)
// newRight = maqueen.readPatrol(maqueen.Patrol.PatrolRight)
// if (newLeft != left || newRight != right) {
// serial.writeNumber(iter);
// if (newLeft != left) { serial.writeString("l"); serial.writeNumber(newLeft); }
// if (newRight != right) { serial.writeString("r"); serial.writeNumber(newRight); }
// serial.writeLine("");
// }
if (runForDuration && control.millis() > runForDurationStart + runForDuration) {
runForDuration = 0;
sendMotorCommand(Command.STOP, 0);
}
if (running) {
loop_followLine();
} else if (turnCommand != TurnCommand.OFF) {
loop_turn();
}
left = newLeft;
right = newRight;
});
function sendMotorCommand(command: Command, speed: number) {
if (command == Command.STOP) {
maqueen.motorRun(maqueen.Motors.All, maqueen.Dir.CW, 0)
} else if (command == Command.FWD) {
maqueen.motorRun(maqueen.Motors.All, maqueen.Dir.CW, speed)
} else if (command == Command.BKW) {
maqueen.motorRun(maqueen.Motors.All, maqueen.Dir.CCW, speed)
} else if (command == Command.LEFT) {
maqueen.motorRun(maqueen.Motors.M1, maqueen.Dir.CW, speed)
maqueen.motorRun(maqueen.Motors.M2, maqueen.Dir.CW, 0)
} else if (command == Command.RIGHT) {
maqueen.motorRun(maqueen.Motors.M1, maqueen.Dir.CW, 0)
maqueen.motorRun(maqueen.Motors.M2, maqueen.Dir.CW, speed)
} else if (command == Command.TURN_LEFT) {
maqueen.motorRun(maqueen.Motors.M1, maqueen.Dir.CCW, speed)
maqueen.motorRun(maqueen.Motors.M2, maqueen.Dir.CW, speed)
} else if (command == Command.TURN_RIGHT) {
maqueen.motorRun(maqueen.Motors.M1, maqueen.Dir.CW, speed)
maqueen.motorRun(maqueen.Motors.M2, maqueen.Dir.CCW, speed)
}
}
function loop_turn() {
if (turnState == TurnState.MOTOR_JUST_STARTED) {
if (newLeft == 1 && newRight == 1
|| turnCommand == TurnCommand.TURN_RIGHT && (left == 0 && newLeft == 1) // left just exited the band
|| turnCommand == TurnCommand.TURN_LEFT && right == 0 && newRight == 1) {
turnState = TurnState.SECOND_WENT_OFF;
}
} else if (turnState == TurnState.SECOND_WENT_OFF) {
if (turnCommand == TurnCommand.TURN_RIGHT && newRight == 0 ||
turnCommand == TurnCommand.TURN_LEFT && newLeft == 0) {
// turnState = TurnState.FIRST_ON;
// sendMotorCommand(turnCommand == TurnCommand.TURN_RIGHT ? Command.TURN_RIGHT : Command.TURN_LEFT, speed / 4);
sendMotorCommand(Command.STOP, 0);
turnCommand = TurnCommand.OFF;
bluetooth.uartWriteString("turnEnd");
}
}
// else if (turnState == TurnState.FIRST_ON) {
// if (turnCommand == TurnCommand.TURN_RIGHT && newLeft == 0 ||
// turnCommand == TurnCommand.TURN_LEFT && newRight == 0) {
// sendMotorCommand(Command.STOP, 0);
// turnCommand = TurnCommand.OFF;
// }
// }
}
function loop_followLine() {
const now = control.millis();
if (command == Command.LEFT || command == Command.RIGHT) {
lastLateral = command
}
let newCommand = command;
if (command == Command.FWD // going forward
&& newLeft == 1 && newRight == 1) { // and left the track
if (newLeft != left
// && (lastLateral == Command.RIGHT || inertia == 0)
) {
// just exited w/ left sensor; so from now on go left to try
// to reenter the track
newCommand = Command.RIGHT
} else if (newRight != right
// && (lastLateral == Command.LEFT || inertia == 0)
) {
newCommand = Command.LEFT;
} else if (inertia > 0) {
newCommand = lastLateral
}
} else { // searching for track
if (newLeft == 1 && newRight == 1) {
// still out; so nothing to do
} else {
newCommand = Command.FWD;
}
}
// atentie: cred ca da stack overflow de la concatenare
// serial.writeLine("cmd=" + lastLateral + ",i=" + inertia);
if (newCommand == Command.FWD) {
if (inertia >= 1) { inertia--; }
} else if (newCommand != Command.BKW) {
if (inertia < inertiaMax) { inertia++; }
}
if (newCommand == command) { return; }
command = newCommand;
sendMotorCommand(command, speed);
// serial.writeString("New command: ");
// serial.writeNumber(command);
// serial.writeLine("");
}
input.onButtonPressed(Button.A, function () {
setRunning(!running)
// bluetooth.uartWriteString("turnEnd");
})
input.onButtonPressed(Button.B, function () {
setTurning(false);
})
function setTurning(isLeft: boolean) {
turnState = TurnState.MOTOR_JUST_STARTED;
turnCommand = !isLeft ? TurnCommand.TURN_RIGHT : TurnCommand.TURN_LEFT;
sendMotorCommand(!isLeft ? Command.TURN_RIGHT : Command.TURN_LEFT, speed);
// important, this way they are valid also for first loop
// left = maqueen.readPatrol(maqueen.Patrol.PatrolLeft)
// right = maqueen.readPatrol(maqueen.Patrol.PatrolRight)
left = newLeft
right = newRight
debounceInertiaMax = 1;
}
let iter = 0;
function setRunning(value: boolean) {
// serial.writeNumber(iter);
// serial.writeLine("setRunning");
iter = 0;
running = value
left = newLeft
right = newRight
inertia = 0
debounceInertiaMax = 2;
maqueen.motorRun(maqueen.Motors.All, maqueen.Dir.CW, value ? speed : 0)
sendMotorCommand(value ? Command.FWD : Command.STOP, speed);
}
bluetooth.onBluetoothConnected(function () {
basic.showString("C")
})
bluetooth.onBluetoothDisconnected(function () {
basic.showString("D")
})
bluetooth.onUartDataReceived(serial.delimiters(Delimiters.SemiColon), function () {
let str = bluetooth.uartReadUntil(serial.delimiters(Delimiters.SemiColon));
// serial.writeString("BLE: ");
// serial.writeLine(str);
if (str == "S") { setRunning(false); }
else if (str == "F") { setRunning(true); }
else if (str == "L") { setTurning(true); }
else if (str == "R") { setTurning(false); }
else if (str == "B") { sendMotorCommand(Command.BKW, speed); }
else if (str.indexOf("BD ") == 0) {
running = false;
const spl = str.split(" ");
// serial.writeLine("Will backward for ms = " + runForDuration);
sendMotorCommand(Command.BKW, speed);
runForDuration = parseInt(spl[1]);
runForDurationStart = control.millis();
return; // don't show on screen
}
basic.showString(str);
})