-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLEGORobot.pas
More file actions
648 lines (553 loc) · 19.6 KB
/
Copy pathLEGORobot.pas
File metadata and controls
648 lines (553 loc) · 19.6 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
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
// ToDo Обработка событий в отдельных потоках -
// с callback'ами или без них
unit LEGORobot;
interface
{$reference 'NKH.MindSqualls.dll'}
uses
NKH.MindSqualls, Timers;
// Предварительное описание класса "робот" - для определения процедурного
// типа данных
type MyRobot = class;
// Процедурный тип - для обработки событий
RoboProc = procedure(Robot : MyRobot);
SensorType = (sNone, sUltrasonic, sTouch, sLight, sColor);
MotorPort = (PortA, PortB, PortC);
/// <summary>
/// Класс мотора
/// </summary>
RoboMotor = class(NKH.MindSqualls.NxtMotor)
private
/// <summary>
/// Объект для представления мотора
/// </summary>
// motor : NKH.MindSqualls.NxtMotor;
/// <summary>
/// Контроллер робота
/// </summary>
//brick : NKH.MindSqualls.NxtBrick;
/// <summary>
/// Порт мотора
/// </summary>
//port : NKH.MindSqualls.NxtMotorPort;
/// <summary>
/// Количество оборотов мотора
/// </summary>
/// <returns>Число оборотов, совершенное мотором</returns>
function GetTachoCount : integer;
/// <summary>
/// Проверка состояния мотора
/// </summary>
/// <returns>True, если мотор вращается</returns>
procedure SetBrick(brick : NKH.MindSqualls.NxtBrick);
begin
Self.Brick := brick;
end;
procedure SetPort(port : NKH.MindSqualls.NxtMotorPort);
begin
Self.Port := port;
end;
public
constructor Create;
function IsRunning : boolean;
function Status : string;
procedure Run(power : shortint; tachoLimit : longword) ;override;
procedure Stop;
procedure Coast;
procedure Brake;
procedure Reset;
procedure WaitFor;
property TachoCount : integer read GetTachoCount;
property Running : boolean read IsRunning;
end;
{Ultrasonic = class(NKH.Mindsqualls.NxtUltrasonicSensor)
public
Constructor Create;
end;}
/// <summary>
/// Класс для работы с роботом LEGO Mindstorms NXT
/// </summary>
MyRobot = class
public
// Моторы - по именам
motA, motB, motC : RoboMotor;
// Таймер для обработки событий
readTimer : Timer;
// Список обработчиков событий
handlers : array of RoboProc; // массив обработчиков - событийный
// Массив признаков для инициализации сенсоров
sns : array [1..4] of SensorType;
// "Кирпич" - основной объект для работы с роботом
// Экземпляр создается в методе connect, иначе с портами проблема
brick : NxtBrick;
motorRunning : boolean;
procedure setAction(v: RoboProc); // добавление обработчика
procedure executeActions;
//procedure addMotorA(m : RoboMotor);
function getMotorA : RoboMotor;
//procedure addMotorB(m : RoboMotor);
function getMotorB : RoboMotor;
//procedure addMotorC(m : RoboMotor);
function getMotorC : RoboMotor;
function getDistance : byte;
function getLight : byte;
function getColor : string;
// Инициализирует сенсоры по массиву sns
procedure InitSensors;
public
property MotorA : RoboMotor read getMotorA;
property MotorB : RoboMotor read getMotorB;
property MotorC : RoboMotor read getMotorC;
// Свойство "действие" - позволяет заполнять массив
property Action : RoboProc write setAction;
property Distance : byte read getDistance;
property Light : byte read getLight;
property Color : string read getColor;
constructor Create;
destructor Destroy;
begin
brick.Disconnect;
writeln('Disconnect command');
end;
function Connect(SerialPortNumber : byte := 0) : boolean;
function Disconnect : boolean;
procedure Stop;
//procedure CheckMotor;
// Добавляет сенсор
procedure AddSensor(sType : sensorType; sPort : integer);
// Добавляет моторчик на указанный порт
//procedure AddMotor(mPort : motorPort);
// Устанавливает интервал опроса сенсоров
procedure SetInterval(interval : integer);
end;
implementation
uses {System, System.Text,}System.IO.Ports;
//----------------Реализация методов мотора-------------------------------------
constructor RoboMotor.Create;
begin
// Пока мотор не "подцеплен" к кирпичу - внутренний объект пуст
// создание объекта происходит при привязке к собственно роботу
//motor := nil;
//brick := nil;
//port := NKH.MindSqualls.NxtMotorPort.All;
end;
procedure RoboMotor.Run(power : shortint; tachoLimit : longword);
begin
inherited Run(power, tachoLimit);
end;
procedure RoboMotor.Stop;
begin
Coast;
Idle
end;
procedure RoboMotor.Coast;
begin
inherited Coast;
end;
procedure RoboMotor.Brake;
begin
inherited Brake;
end;
procedure RoboMotor.Reset;
begin
ResetMotorPosition(false);
end;
function RoboMotor.GetTachoCount : integer;
begin
var motorState := Brick.CommLink.GetOutPutstate(port);
if (brick <> nil) and motorState.HasValue then
Result := motorState.Value.RotationCount
else
Result := -MaxInt;
end;
function RoboMotor.IsRunning : boolean;
begin
Result := false;
if (brick<>nil) then
begin
var statusValues := brick.CommLink.GetOutputState(port);
if statusValues.HasValue and
(statusValues.Value.runState = NKH.MindSqualls.NxtMotorRunState.MOTOR_RUN_STATE_RUNNING)
//(statusValues.Value.mode.HasFlag(NKH.MindSqualls.NxtMotorMode.MOTORON))
//(statusValues.Value.regulationMode <> NKH.MindSqualls.NxtMotorRegulationMode.REGULATION_MODE_IDLE)
then
Result := true;
//writeln('MA : ',statusValues.ToString);
{writeln('MA : ',statusValues.Value.mode);
writeln('MA : ',port); if statusValues.HasValue and
(statusValues.Value.runState = NKH.MindSqualls.NxtMotorRunState.MOTOR_RUN_STATE_RUNNING) and
(statusValues.Value.mode = NKH.MindSqualls.NxtMotorMode.MOTORON) then
}
//writeln;writeln;writeln;writeln;writeln;writeln;
end;
end;
procedure RoboMotor.WaitFor;
begin
// Попробовать исправить!!!!!!
//brick.CommLink.GetOutputState
while IsRunning do;
//sleep(10);
end;
function RoboMotor.Status : string;
begin
Result := brick.CommLink.GetOutputState(port).ToString;
end;
//------------------------Реализация методов робота-----------------------------
constructor MyRobot.Create;
begin
brick := nil;
readTimer := nil;
// readTimer := new Timer(50,executeActions);
// Заполняем поля - пока что пустые, автоматически определить
// наличие сенсора или мотора сложно или невозможно
MotA := nil;
MotB := nil;
MotC := nil;
for var i:=1 to 4 do
sns[i] := sNone;
end;
function getPortNumberFromString(portName : string) : byte;
var port : integer;
begin
while (portName.Length>0) and ((portName[1]<'0') or (portName[1]>'9')) do
Delete(portName,1,1);
if TryStrToInt(portName,port) then
Result := port
else
Result := -1;
end;
function MyRobot.Connect(SerialPortNumber : byte) : boolean;
begin
Result := false;
if SerialPortNumber <> 0 then
// Если указан номер порта для подключения
try
brick := new NxtBrick(NxtCommLinkType.Bluetooth, SerialPortNumber);
//brick.Sensor4 := new NxtUltrasonicSensor(); brick.Sensor4.PollInterval := 500; sns[4] := sUltrasonic;
InitSensors;
writeln('Пытаюсь подключиться к указанному порту - ',SerialPortNumber.ToString);
brick.Connect;
if brick.IsConnected then
begin
writeln('Получилось 1 - ',SerialPortNumber.ToString);
writeln('Возвращаем true...');
Result := true;
end;
except
brick := nil;
end
else
begin
// Иначе пробуем найти порт для подключения самостоятельно - перебором
writeln('Пытаемся найти порты...');
var ports := SerialPort.GetPortNames();
for var i := ports.Length-1 downto 0 do
try
var s1 := getPortNumberFromString(ports[i]);
writeln(s1);
brick := new NxtBrick(NxtCommLinkType.Bluetooth, getPortNumberFromString(ports[i]));
//brick.Sensor4 := new NxtUltrasonicSensor(); brick.Sensor4.PollInterval := 500; sns[4] := sUltrasonic;
writeln('Пытаюсь подключиться!!!!!!!!!!!!!!!!!!!!! - ',getPortNumberFromString(ports[i]));
InitSensors;
brick.Connect;
if brick.IsConnected then
begin
writeln('Получилось - '+ports[i]);
Result := true;
end;
except
brick := nil;
end;
end;
writeln('Проходим точку : 123');
if Result then
begin
// Если подключились, то создаем таймер для обработки событий
writeln('Все хорошо!');
readTimer := new Timer(100,executeActions);
readTimer.Start;
end;
end;
function MyRobot.Disconnect : boolean;
begin
brick.Disconnect;
Result := true;
end;
procedure MyRobot.Stop;
begin
if MotA <> nil then
MotA.Stop;
if MotB <> nil then
MotB.Stop;
if MotC <> nil then
MotC.Stop;
end;
procedure MyRobot.executeActions;
begin
for var i := 0 to Length(handlers) do
handlers[i](self);
end;
procedure MyRobot.setAction(v: RoboProc); // добавление обработчика
begin
SetLength(handlers,Length(handlers)+1);
handlers[Length(handlers)-1] := v;
end;
{procedure MyRobot.addMotorA(m : RoboMotor);
begin
MotA := m;
brick.MotorA := NxtMotor.Create();
brick.MotorA.PollInterval := 50;
m.motor := brick.MotorA;
m.brick := brick;
end;}
function MyRobot.getMotorA : RoboMotor;
begin
if MotA = nil then
begin
MotA := RoboMotor.Create();
brick.MotorA := MotA;
brick.MotorA.PollInterval := 50;
brick.MotorA.Coast;
brick.MotorA.ResetMotorPosition(false);
(brick.MotorA as Robomotor).SetBrick(brick);
(brick.MotorA as Robomotor).SetPort(NxtMotorPort.PortA);
end;
Result := MotA;
end;
{procedure MyRobot.addMotorB(m : RoboMotor);
begin
MotB := m;
brick.MotorB := NxtMotor.Create();
brick.MotorB.PollInterval := 50;
m.motor := brick.MotorB;
m.brick := brick;
end;}
function MyRobot.getMotorB : RoboMotor;
begin
if MotB = nil then
begin
MotB := RoboMotor.Create();
brick.MotorB := MotB;
brick.MotorB.PollInterval := 50;
brick.MotorB.ResetMotorPosition(false);
(brick.MotorB as Robomotor).SetBrick(brick);
(brick.MotorB as Robomotor).SetPort(NxtMotorPort.PortB);
end;
Result := MotB;
end;
{procedure MyRobot.addMotorC(m : RoboMotor);
begin
MotC := m;
brick.MotorC := NxtMotor.Create();
brick.MotorC.PollInterval := 50;
brick.MotorC.ResetMotorPosition(false);
m.motor := brick.MotorC;
m.brick := brick;
end;}
function MyRobot.getMotorC : RoboMotor;
begin
if MotC = nil then
begin
MotC := RoboMotor.Create();
brick.MotorC := MotC;
brick.MotorC.PollInterval := 50;
brick.MotorC.ResetMotorPosition(false);
(brick.MotorC as Robomotor).SetBrick(brick);
(brick.MotorC as Robomotor).SetPort(NxtMotorPort.PortC);
end;
Result := MotC;
end;
{function MyRobot.GetMotorState : string;
begin
Result := 'Состояние мотора : ';
if brick.MotorB.TachoCount.HasValue then
Result := Result + IntToStr(brick.MotorB.TachoCount.Value)
else
Result := Result + 'значение отсутвует';
{if ((brick.MotorB) as RoboMotor).IsRunning then
Result := Result + ' работаем'
else
Result := Result + ' молчим';}
{ if brick.MotorB.
Result += IntToStr(brick.MotorB.TachoCount.Value)+'\n'
else
Result += 'значение отсутвует\n';
end;}
// Запоминание сенсора для последующей инициализации
procedure MyRobot.AddSensor(sType : sensorType; sPort : integer);
begin
sns[sPort] := sType;
end;
procedure MyRobot.InitSensors;
begin
for var sPort:=1 to 4 do
case sns[sPort] of
sUltrasonic :
case sPort of
1 : begin brick.Sensor1 := new NxtUltrasonicSensor(); brick.Sensor1.PollInterval := 50; sns[1] := sUltrasonic end;
2 : begin brick.Sensor2 := new NxtUltrasonicSensor(); brick.Sensor2.PollInterval := 50; sns[2] := sUltrasonic end;
3 : begin brick.Sensor3 := new NxtUltrasonicSensor(); brick.Sensor3.PollInterval := 50; sns[3] := sUltrasonic end;
4 : begin brick.Sensor4 := new NxtUltrasonicSensor(); brick.Sensor4.PollInterval := 500; sns[4] := sUltrasonic end;
end;
sLight :
case sPort of
1 : begin brick.Sensor1 := new Nxt2ColorSensor; Nxt2ColorSensor(brick.Sensor1).SetLightSensorMode(Nxt2Color.Red); brick.Sensor1.PollInterval := 100; sns[1] := sLight end;
2 : begin brick.Sensor2 := new Nxt2ColorSensor; Nxt2ColorSensor(brick.Sensor2).SetLightSensorMode(Nxt2Color.Red); brick.Sensor2.PollInterval := 100; sns[2] := sLight end;
3 : begin brick.Sensor3 := new Nxt2ColorSensor; Nxt2ColorSensor(brick.Sensor3).SetLightSensorMode(Nxt2Color.Red); brick.Sensor3.PollInterval := 100; sns[3] := sLight end;
4 : begin brick.Sensor4 := new Nxt2ColorSensor; Nxt2ColorSensor(brick.Sensor4).SetLightSensorMode(Nxt2Color.Red); brick.Sensor4.PollInterval := 100; sns[4] := sLight end;
end;
sColor :
case sPort of
1 : begin brick.Sensor1 := new Nxt2ColorSensor; Nxt2ColorSensor(brick.Sensor1).SetColorDetectorMode(); brick.Sensor1.PollInterval := 100; sns[1] := sColor end;
2 : begin brick.Sensor2 := new Nxt2ColorSensor; Nxt2ColorSensor(brick.Sensor2).SetColorDetectorMode(); brick.Sensor2.PollInterval := 100; sns[2] := sColor end;
3 : begin brick.Sensor3 := new Nxt2ColorSensor; Nxt2ColorSensor(brick.Sensor3).SetColorDetectorMode(); brick.Sensor3.PollInterval := 100; sns[3] := sColor end;
4 : begin brick.Sensor4 := new Nxt2ColorSensor; Nxt2ColorSensor(brick.Sensor4).SetColorDetectorMode(); brick.Sensor4.PollInterval := 100; sns[4] := sColor end;
end;
end;
end;
function MyRobot.getDistance : byte;
begin
if sns[4] = sUltrasonic then
begin
if NxtUltrasonicSensor(brick.Sensor4).DistanceCm.HasValue then
Result := byte(NxtUltrasonicSensor(brick.Sensor4).DistanceCm)
else
Result := 0;
exit;
end;
if sns[2] = sUltrasonic then
begin
Result := byte(NxtUltrasonicSensor(brick.Sensor2).DistanceCm);
exit;
end;
if sns[3] = sUltrasonic then
begin
Result := byte(NxtUltrasonicSensor(brick.Sensor3).DistanceCm);
exit;
end;
if sns[1] = sUltrasonic then
begin
Result := byte(NxtUltrasonicSensor(brick.Sensor1).DistanceCm);
exit;
end;
Result := 0;
end;
function MyRobot.getColor : string;
begin
if sns[3] = sColor then
begin
Result := Nxt2ColorSensor(brick.Sensor3).Color.ToString;
exit;
end;
if sns[4] = sColor then
begin
Result := Nxt2ColorSensor(brick.Sensor4).Color.ToString;
exit;
end;
if sns[1] = sColor then
begin
Result := Nxt2ColorSensor(brick.Sensor1).Color.ToString;
exit;
end;
if sns[2] = sColor then
begin
Result := Nxt2ColorSensor(brick.Sensor2).Color.ToString;
exit;
end;
Result := 'No sensor!';
end;
function MyRobot.getLight : byte;
begin
if sns[3] = sLight then
begin
Result := byte(Nxt2ColorSensor(brick.Sensor3).Intensity.Value);
exit;
end;
if sns[4] = sLight then
begin
Result := byte(Nxt2ColorSensor(brick.Sensor4).Intensity.Value);
exit;
end;
if sns[1] = sLight then
begin
Result := byte(Nxt2ColorSensor(brick.Sensor1).Intensity.Value);
exit;
end;
if sns[2] = sLight then
begin
Result := byte(Nxt2ColorSensor(brick.Sensor2).Intensity.Value);
exit;
end;
Result := 0;
end;
{procedure MyRobot.AddMotor(mPort : motorPort);
begin
case mPort of
PortA : begin brick.MotorA := NxtMotor.Create;
motA := new RoboMotor;
motA.motor := brick.MotorA;
brick.MotorA.PollInterval := 50; end;
PortB : begin brick.MotorB := NxtMotor.Create;
motB := new RoboMotor;
motB.motor := brick.MotorB;
brick.MotorB.PollInterval := 50; end;
PortC : begin brick.MotorC := NxtMotor.Create;
motC := new RoboMotor;
motC.motor := brick.MotorC;
brick.MotorC.PollInterval := 50; end;
end;
end;}
procedure MyRobot.SetInterval(interval : integer);
begin
readTimer.Interval := interval;
end;
end.
{
procedure touchSensor_OnPolled(polledItem: NxtPollable);
begin
touchSensor := NxtTouchSensor(polledItem);
// необходимо преобразовывать к boolean так как touchSensor.IsPressed возвращает Nullable<bollean>
isPressed := boolean(touchSensor.IsPressed);
if (isPressed) then
Writeln('Touch sensor pressed');
end;
procedure UltrasonicSensor_OnPolled(polledItem: NxtPollable);
begin
UltrasonicSensor := NxtUltrasonicSensor(polledItem);
// необходимо преобразовывать к byte так как UltrasonicSensor.DistanceCm возвращает Nullable<byte>
distanse := byte(UltrasonicSensor.DistanceCm);
//WriteLn(distanse);
end;
begin
isPressed:=false;
// Создаем touch sensor.
touchSensor := new NxtTouchSensor();
// Создаем Ultrasonic sensor.
UltrasonicSensor := new NxtUltrasonicSensor();
// Присоединяем моторы к порту А и С на NXT.
brick.MotorA := new NxtMotor();
brick.MotorB := new NxtMotor();
brick.MotorC := new NxtMotor();
// присоединяем touchSensor к порту 1.
brick.Sensor1 := touchSensor;
// Присоединяем Ультразвуковой сенсор к порту 4.
brick.Sensor4 := UltrasonicSensor;
// Устанавливаем интервал опроса сенсоров 50 милисекунд.
touchSensor.PollInterval := 50;
UltrasonicSensor.PollInterval := 50;
// Обрабатываем событие опроса сенсоров.
touchSensor.OnPolled += touchSensor_OnPolled;
UltrasonicSensor.OnPolled += UltrasonicSensor_OnPolled;
// Соединяемся NXT.
brick.Connect();
// Ждем для обработки событий
Writeln('Press any key to stop.');
sleep(3000);
//for var i:=1 to 100 do
writeln(distanse);
readln();
// Запускаем моторы с мощьностью 75%, на 1600 градксов.
brick.MotorA.Run(75, 1600);
brick.MotorC.Run(75, 1600);
// Отсоединяемся от NXT.
brick.Disconnect();
end.}