-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathODERobots.pas
More file actions
2251 lines (1912 loc) · 61.8 KB
/
ODERobots.pas
File metadata and controls
2251 lines (1912 loc) · 61.8 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
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
unit ODERobots;
{$MODE Delphi}
interface
uses
Windows, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, GLScene, GLObjects, {GLMisc,} GLLCLViewer, ODEImport, OpenGL1x,
GLVectorGeometry, GLGeomObjects, ExtCtrls, ComCtrls, GLTexture, GLGraphics,
keyboard, math, GLMaterial, GLVectorFileObjects, GLColor,
GLVectorLists, GLVectorTypes, GLTextureFormat;
const
//ODE world constants
MAX_CONTACTS = 7;
MaxJointSamples = 256;
MaxKeyVals = 8;
MaxAxis = 3;
const MaxDim = 8;
{const
skDefault = 0;
skOmniWheel = 1;
skMotorBelt = 2;}
type
TSolidKind = (skDefault, skOmniWheel, skOmniSurface, skMotorBelt, skPropeller, skFloor, skWall, skMecanumWheel_R, skMecanumWheel_L);
type
TControlMode = (cmPIDPosition, cmPIDSpeed, cmState);
const
ControlModeNames: array[TControlMode] of string = ('PIDPosition', 'PIDSpeed', 'State');
type
TFriction = record
Bv, Fc: double;
// CoulombLimit: double;
end;
TSpring = record
K, ZeroPos: double;
end;
TMotController = record
Ki, Kd, Kp, Kf: double;
Sek, ek_1: double;
y_sat: double;
Ticks, ControlPeriod: double;
ControlMode: TControlMode;
active: boolean;
end;
TEncoder = record
PPR: integer;
NoiseMean: double;
NoiseStDev: double;
end;
TMotor = record
Ri, Li, Ki, Vmax, Imax: double;
Im: double;
GearRatio: double;
JRotor, teta, w: double;
BRotor, QRotor: double;
KGearBox, BGearBox: double;
KGearBox2, BGearBox2: double;
Encoder: TEncoder;
Controller: TMotController;
voltage, PowerDrain, EnergyDrain: double;
active, simple: boolean;
end;
TMatterProperty = (smMetallic, smFerroMagnetic, smRFIDTag);
TMatterProperties = set of TMatterProperty;
TPaintMode = (pmPaint, pmHeatmap, pmResult);//pmOriginal
{ TSolid }
TVertex = record
pos: TVector3f;
normal: TVector3f;
triangles: array of integer;
paintMapColor: TVector;
paintHeatmapColor: TVector;
paintResultColor: TVector;
end;
TTriangle = record
vertexs: array [0..2] of ^TVertex;
center: TAffineVector;
normal: TAffineVector;
area: double;
neighbors: array of integer;
paintThickness: double;
end;
TSolid = class
Body: PdxBody;
Geom : PdxGeom;
GLObj, AltGLObj, ShadowGlObj, CanvasGLObj, extraGLObj: TGLSceneObject;
PaintBitmap: TBitmap;
PaintBitmapCorner: TdVector3;
isPaintTarget: bool;
initialColor: TVector4f;
paintGunMinDist: double;
//paintThickness: TDoubleList;
//paintHeatmap: TVectorList;
//paintmap: TVectorList;
paintMode: TPaintMode;
avgAreaPerVertex: double;
meshVertexs: array of TVertex;
meshTriangles: array of TTriangle;
paintZBitmap: array of array of double; // Z distance at polar coordinates (i,j)
paintZBitmapResolution: integer;
paintZBitmapOldTri: array of array of array of integer;
paintZBitmapOldQua: array of array of array of double;
kind: TSolidKind;
MatterProperties: TMatterProperties;
BeltSpeed: double;
ParSurface{, MaxParSurface} : TdSurfaceParameters;
ID, Tag: string;
BuoyantMass, Volume, Drag, StokesDrag, RollDrag: double;
BuoyanceCenter: TdVector3;
Thrust: double;
Ax, Ay, Az: double;
ZeroPosition: TdVector3;
ZeroRotation: TdMatrix3;
private
public
constructor Create;
destructor Destroy; override;
procedure SetPosition(posX, posY, posZ: double);
procedure SetLinSpeed(vX, vY, vZ: double);
procedure MovePosition(dX, dY, dZ: double);
procedure SetRotation(axisX, axisY, axisZ, rot_angle: double); overload;
procedure SetRotation(R: TdMatrix3); overload;
procedure SetZeroState;
procedure SetColor(R, G, B: single; A: single = 1);
procedure GetColor(out R, G, B, A: integer);
procedure SetColorRGB(RGB: TColor);
procedure SetTexture(TextureName: string; TextureScale: double);
procedure SetTag(newTag: string);
function GetTag: string;
function GetPosition: TdVector3;
function GetRotation: TdMatrix3;
function GetLinSpeed: TdVector3;
function GetAngularVel: TdVector3;
procedure SetAngularVel(wX, wY, wZ: double);
procedure SetSize(sizeX, sizeY, sizeZ: double);
procedure GetSize(out sizeX, sizeY, sizeZ: double);
procedure SetForce(FX, FY, FZ: double);
procedure SetSurfaceFriction(mu, mu2: double);
procedure SetSurfacePars(mu, mu2, softness, bounce, bounce_tresh: double);
procedure UpdateGLCanvas;
function CalculateHeatmapColor(paintThickness: double): TColorVector;
function FindVertexIndex(Coordinates: TVector3f): integer;
end;
TSolidList = class(TList)
private
protected
function GetItems(Index: Integer): TSolid;
procedure SetItems(Index: Integer; ASolid: TSolid);
public
function Add(ASolid: TSolid): Integer;
function Extract(Item: TSolid): TSolid;
function Remove(ASolid: TSolid): Integer;
function IndexOf(ASolid: TSolid): Integer;
function First: TSolid;
function Last: TSolid;
procedure Insert(Index: Integer; ASolid: TSolid);
property Items[Index: Integer]: TSolid read GetItems write SetItems; default;
procedure ClearAll;
function IndexFromID(aID: string): integer;
end;
TAxisTraj = class
public
pos, speed, t: double;
constructor Create;
destructor Destroy; override;
end;
TAxisTrajList = class(TList)
private
protected
function GetItems(Index: Integer): TAxisTraj;
procedure SetItems(Index: Integer; AAxisTraj: TAxisTraj);
public
function Add(AAxisTraj: TAxisTraj): Integer;
function Extract(Item: TAxisTraj): TAxisTraj;
function Remove(AAxisTraj: TAxisTraj): Integer;
function IndexOf(AAxisTraj: TAxisTraj): Integer;
function First: TAxisTraj;
function Last: TAxisTraj;
procedure Insert(Index: Integer; AAxisTraj: TAxisTraj);
property Items[Index: Integer]: TAxisTraj read GetItems write SetItems; default;
procedure ClearAll;
end;
TAxisGLPars = record
radius, height: double;
color: longWord;
end;
TAxisInputs = record
theta, w: double;
volts, Torque: double;
end;
TOdoState = record
Angle, LastAngle: double;
Residue: double;
Value, LastValue, AccValue: integer;
end;
TSolidLink = class;
TAxis = class
ParentLink: TSolidLink;
GLObj: TGLBaseSceneObject;
Friction: TFriction;
Spring: TSpring;
Motor: TMotor;
TrajectPoints, WayPoints: TAxisTrajList;
Odo: TOdoState;
ref: TAxisInputs;
TotalTorque: double;
cur_pos, last_pos, filt_speed: double;
canWrap: boolean;
private
public
constructor Create;
destructor Destroy; override;
procedure GetAnchor(var result: TdVector3);
procedure SetAnchor(const nAnchor: TdVector3);
procedure GetDir(var result: TdVector3);
procedure SetDir(const nDir: TdVector3);
function GetPos: double;
function GetSpeed(dt: double): double;
procedure AddTorque(Tq: Double);
procedure GLCreateCylinder(Parent: TGLBaseSceneObject; aRadius, aHeight: double);
procedure GLCreateBall(Parent: TGLBaseSceneObject; aRadius: double);
procedure GLSetPosition;
function isCircular: boolean;
procedure GetLimits(out MinLimit, Maxlimit: double);
end;
TAxisList = class(TList)
private
protected
function GetItems(Index: Integer): TAxis;
procedure SetItems(Index: Integer; AAxis: TAxis);
public
function Add(AAxis: TAxis): Integer;
function Extract(Item: TAxis): TAxis;
function Remove(AAxis: TAxis): Integer;
function IndexOf(AAxis: TAxis): Integer;
function First: TAxis;
function Last: TAxis;
procedure Insert(Index: Integer; AAxis: TAxis);
property Items[Index: Integer]: TAxis read GetItems write SetItems; default;
procedure ClearAll;
function IndexFromAxisID(aID: string; ith: integer): integer;
end;
TSolidLink = class
joint: TdJointID;
GLObj: TGLBaseSceneObject;
//Axis, Axis2: TAxis;
Axis: array[0..MaxAxis-1] of TAxis;
ID: string;
description: string;
public
constructor Create;
destructor Destroy; override;
end;
TSolidLinkList = class(TList)
private
protected
function GetItems(Index: Integer): TSolidLink;
procedure SetItems(Index: Integer; ASolidLink: TSolidLink);
public
function Add(ASolidLink: TSolidLink): Integer;
function Extract(Item: TSolidLink): TSolidLink;
function Remove(ASolidLink: TSolidLink): Integer;
function IndexOf(ASolidLink: TSolidLink): Integer; overload;
function First: TSolidLink;
function Last: TSolidLink;
procedure Insert(Index: Integer; ASolidLink: TSolidLink);
property Items[Index: Integer]: TSolidLink read GetItems write SetItems; default;
procedure ClearAll;
function IndexOf(aID: string): integer; overload;
end;
TWheelPars = record
offsetX, offsetY, offsetZ: double;
Radius, Width, mass, CenterDist, Angle: double;
Mu, Mu2, soft_cfm: double;
omni: boolean;
end;
TWheel = class
Pars: TWheelPars;
Tyre: TSolid; //Not owned by the class
Axle: TSolidLink; //Not owned by the class
active: boolean;
public
constructor Create;
destructor Destroy; override;
end;
TWheelList = class(TList)
private
protected
function GetItems(Index: Integer): TWheel;
procedure SetItems(Index: Integer; AWheel: TWheel);
public
function Add(AWheel: TWheel): Integer;
function Extract(Item: TWheel): TWheel;
function Remove(AWheel: TWheel): Integer;
function IndexOf(AWheel: TWheel): Integer;
function First: TWheel;
function Last: TWheel;
procedure Insert(Index: Integer; AWheel: TWheel);
property Items[Index: Integer]: TWheel read GetItems write SetItems; default;
procedure ClearAll;
end;
TSensor = class;
TSensorMeasure = class
value, dist: double;
pos, normal: TdVector3;
HitSolid: TSolid;
has_measure: boolean;
public
constructor Create;
destructor Destroy; override;
end;
TSensorRay = class
ParentSensor, TargetBeacon: TSensor;
Geom: PdxGeom;
Measure: TSensorMeasure;
public
constructor Create;
destructor Destroy; override;
procedure Place(posX, posY, posZ, angX, angY, AngZ: double; AbsoluteCoords: boolean = false);
end;
TSensorRayList = class(TList)
private
protected
function GetItems(Index: Integer): TSensorRay;
procedure SetItems(Index: Integer; ASensorRay: TSensorRay);
public
function Add(ASensorRay: TSensorRay): Integer;
function Extract(Item: TSensorRay): TSensorRay;
function Remove(ASensorRay: TSensorRay): Integer;
function IndexOf(ASensorRay: TSensorRay): Integer;
function First: TSensorRay;
function Last: TSensorRay;
procedure Insert(Index: Integer; ASensorRay: TSensorRay);
property Items[Index: Integer]: TSensorRay read GetItems write SetItems; default;
procedure ClearAll;
end;
TSensorNoise = record
var_k, var_d, offset, gain: double;
std_a, std_p: double;
active: boolean;
end;
TSensorKind = (skGeneric, skIR, skIRSharp, skSonar, skCapacitive, skInductive,
skBeacon, skFloorLine, skRanger2D, skIMU, skRFID, skPenTip, skSolenoid, skSprayGun);
TSensor = class
ID: string;
kind: TSensorKind;
Noise: TSensorNoise;
Rays: TSensorRayList;
GLObj: TGLSceneObject;
Period, TimeFromLastMeasure: double;
Tags: TStringlist;
Fmax, k1,k2: double;
Vin: double;
MaxDist, MinDist, StartAngle, EndAngle: double;
paintRate: single;
paintMaxAngle: single;
paintColor: TColorVector;
paintOn: boolean;
paintOcclusion: boolean;
private
function InsideGLPolygonsTaged(x, y: double; GLFloor: TGLBaseSceneObject): boolean;
procedure FreeMeasures;
protected
function GetMeasureCount: integer;
//function GetMeasure(Index: Integer): TSensorMeasure;
//procedure SetMeasure(Index: Integer; AMeasure: TSensorMeasure);
public
Measures: array of TSensorMeasure;
constructor Create(MeasuresCount: integer = 1);
destructor Destroy; override;
procedure SetColor(R, G, B: single; A: single = -1);
//property Measures[Index: Integer]: TSensorMeasure read GetMeasure write SetMeasure; default;
property MeasuresCount: integer read GetMeasureCount;
procedure PreProcess(dt: double);
procedure PostProcess;
procedure UpdateMeasures;
procedure NoiseModel;
procedure SetColorRGB(RGB: TColor);
function SwapRGB(RGB: TColor): TColor;
procedure SetVin(aVin: double);
procedure CreateMeasures(Count: integer = 1);
end;
const
SensorKindStrings: array[TSensorKind] of string =
('Generic', 'IR', 'IRSharp', 'Sonar', 'Capacitive', 'Inductive',
'Beacon', 'FloorLine', 'Ranger2D', 'IMU', 'RFID', 'PenTip', 'Solenoid', 'SprayGun');
type
TSensorList = class(TList)
private
protected
function GetItems(Index: Integer): TSensor;
procedure SetItems(Index: Integer; ASensor: TSensor);
public
function Add(ASensor: TSensor): Integer;
function Extract(Item: TSensor): TSensor;
function Remove(ASensor: TSensor): Integer;
function IndexOf(ASensor: TSensor): Integer;
function First: TSensor;
function Last: TSensor;
procedure Insert(Index: Integer; ASensor: TSensor);
property Items[Index: Integer]: TSensor read GetItems write SetItems; default;
procedure ClearAll;
function IndexFromID(aID: string): integer;
end;
TKeyVals = array[0..MaxKeyVals-1] of single; //TODO
TRobotKind = (rkUnknown, rkWheelChair, rkOmni3, rkOmni4, rkHumanoid, rkArm, rkConveyorBelt, rkOther);
TRobot = class
Solids: TSolidList;
Links: TSolidLinkList;
Axes: TAxisList; // TAxisList does not owns the axis
AxesWayPointsIDs: TStringlist;
MainBody: TSolid;
Shells: TSolidList;
Wheels: TWheelList;
Sensors: TSensorList;
Kind: TRobotKind;
//SamplesCount, DecPeriodSamples: integer;
//SecondsCount, DecPeriod: double;
ID: string;
ForceMoved: boolean;
public
constructor Create;
destructor Destroy; override;
procedure SetXYZTeta(new_x, new_y, new_z, new_teta: double);
procedure GetXYZTeta(out x, y, z, teta: double);
function CalcCenterOfMass: TdVector3;
procedure AddXY(add_x, add_y: double);
end;
TRobotList = class(TList)
private
protected
function GetItems(Index: Integer): TRobot;
procedure SetItems(Index: Integer; ARobot: TRobot);
public
function Add(ARobot: TRobot): Integer;
function Extract(Item: TRobot): TRobot;
function Remove(ARobot: TRobot): Integer;
function IndexOf(ARobot: TRobot): Integer;
function First: TRobot;
function Last: TRobot;
procedure Insert(Index: Integer; ARobot: TRobot);
property Items[Index: Integer]: TRobot read GetItems write SetItems; default;
procedure ClearAll;
function IndexFromID(aID: string): integer;
end;
procedure RFromZYXRotRel(var R: TdMatrix3; angX, angY, angZ: TDreal);
procedure RFromZYXRotRelRay(var R: TdMatrix3; angX, angY, angZ: TDreal);
implementation
uses Utils, Viewer;
procedure RFromZYXRotRel(var R: TdMatrix3; angX, angY, angZ: TDreal);
var Rx, Ry, Rz, Ryx: TdMatrix3;
begin
dRFromAxisAndAngle(Rz, 0, 0, 1, angZ);
dRFromAxisAndAngle(Ry, 0, 1, 0, angY);
dRFromAxisAndAngle(Rx, 1, 0, 0, angX);
dMULTIPLY0_333(Ryx, Ry, Rx);
dMULTIPLY0_333(R, Rz, Ryx);
end;
procedure RFromZYXRotRelRay(var R: TdMatrix3; angX, angY, angZ: TDreal);
var Rx, Ry, Rz, Raux1, Raux2, Ry2: TdMatrix3;
begin
dRFromAxisAndAngle(Rz, 0, 0, 1, angZ);
dRFromAxisAndAngle(Ry, 0, 1, 0, angY);
dRFromAxisAndAngle(Ry2, 0, 1, 0, pi/2);
dRFromAxisAndAngle(Rx, 1, 0, 0, angX);
dMULTIPLY0_333(Raux1, Rz, Ry2);
dMULTIPLY0_333(Raux2, Ry, Raux1);
dMULTIPLY0_333(R, Rx, Raux2);
end;
{ TRobot }
constructor TRobot.Create;
begin
Solids := TSolidList.Create;
Shells := TSolidList.Create;
Axes := TAxisList.Create;
AxesWayPointsIDs := TStringList.Create;
Links := TSolidLinkList.Create;
Wheels := TWheelList.Create;
Sensors := TSensorList.Create;
end;
destructor TRobot.Destroy;
begin
// Sensors.ClearAll;
Sensors.Free;
AxesWayPointsIDs.Free;
//Axes.ClearAll; // TAxisList does not owns the axis
Axes.Free;
Wheels.ClearAll;
Wheels.Free;
Links.ClearAll;
Links.Free;
Shells.ClearAll;
Shells.Free;
Solids.ClearAll;
Solids.free;
inherited;
end;
procedure TRobot.GetXYZTeta(out x, y, z, teta: double);
var v0, v1, v2: TdVector3;
begin
if (MainBody <> nil) and
(MainBody.Body <> nil) then begin
v0 := MainBody.ZeroPosition;
v1 := dBodyGetPosition(MainBody.Body)^;
dBodyGetRelPointPos(MainBody.Body, 1,0,0, v2);
x := v1[0] - V0[0];
y := v1[1] - V0[1];
z := v1[2] - V0[2];
teta := atan2(v2[1]-v1[1], v2[0]-v1[0]);
end;
end;
procedure TRobot.SetXYZTeta(new_x, new_y, new_z, new_teta: double);
var i, j, k, n: integer;
Rteta, Ra: TdMatrix3;
Pr, P0, Pd: TdVector3;
begin
if MainBody <> nil then begin
P0 := MainBody.ZeroPosition;
//P0 := Solids[0].ZeroPosition;
end else begin
P0 := Vector3Make(0, 0, 0);
end;
dRFromAxisAndAngle(Rteta, 0, 0, 1, new_teta);
for i := 0 to Solids.Count - 1 do begin
// Rotate to the original pose
dMULTIPLY0_333(Ra, Rteta, Solids[i].ZeroRotation);
dBodySetRotation(Solids[i].Body, Ra);
// Move to the original pose counting with the relative offset from the mainbody
Pd := Vector3SUB(Solids[i].ZeroPosition, P0);
dMULTIPLY0_331(Pr, Rteta, Pd);
dBodySetPosition(Solids[i].Body, Pr[0] + P0[0] + new_x, Pr[1] + P0[1] + new_y, Pr[2] + P0[2] + new_z);
// Nullify the velocities
dBodySetLinearVel(Solids[i].Body, 0, 0, 0);
dBodySetAngularVel(Solids[i].Body, 0, 0, 0);
end;
// Reset motor position
for j := 0 to Links.Count - 1 do begin
//for k := 0 to MaxAxis - 1 do begin
// Links[j].Axis[0].Motor.teta := 0;
n := 0;
if (dJointGetType(Links[j].joint) = ord(dJointTypeHinge)) or
(dJointGetType(Links[j].joint) = ord(dJointTypeSlider)) then n := 1
else if (dJointGetType(Links[j].joint) = ord(dJointTypeUniversal)) then n := 2;
for k := 0 to n - 1 do begin
Links[j].Axis[k].Motor.teta := 0;
end;
end;
// deal with links to the world
for i := 0 to Solids.Count - 1 do begin
for j := 0 to Links.Count - 1 do begin
if (dJointGetBody(Links[j].joint, 0) = Solids[i].Body) and (dJointGetBody(Links[j].joint, 1)= nil) then begin
{Links[j].Axis[0].GetAnchor(Pr);
Pd := Vector3SUB(Pr, P0);
dMULTIPLY0_331(Pr, Rteta, Pd);
Pd[0] := Pr[0] + P0[0];
Pd[1] := Pr[1] + P0[1];
Pd[2] := Pr[2] + P0[2];
Links[j].Axis[0].SetAnchor(Pd)}
{Pd := Vector3SUB(Pr, P0);
dMULTIPLY0_331(Pr, Rteta, Pd);
Pd[0] := Pr[0] + P0[0] + new_x;
Pd[1] := Pr[1] + P0[1] + new_y;
Pd[2] := Pr[2] + P0[2] + new_z;}
//Links[j].Axis[0].SetAnchor(Pd);
//Links[j].Axis[0].GetAnchor(Pr);
//dMULTIPLY0_331(Pd, Rteta, Pr);
//Pr[0] := Pr[0] + new_x;
//Pr[1] := Pr[1] + new_y;
//Pr[2] := Pr[2] + new_z;
//Links[j].Axis[0].SetAnchor(Pr);
Links[j].Axis[0].GetAnchor(Pr); // TWICE!!!
{ dMULTIPLY0_331(Pd, Rteta, Pr);
Pr[0] := Pd[0];// + new_x;
Pr[1] := Pd[1];// + new_y;
Pr[2] := Pd[2];// + new_z;}
Links[j].Axis[0].SetAnchor(Pr);
Links[j].Axis[0].GetAnchor(Pr);
Links[j].Axis[0].SetAnchor(Pr);
Links[j].Axis[0].GetDir(Pr);
dMULTIPLY0_331(Pd, Rteta, Pr); // Double angle!!!
dMULTIPLY0_331(Pr, Rteta, Pd);
{Pr[0] := Pd[0];// + new_x;
Pr[1] := Pd[1];// + new_y;
Pr[2] := Pd[2];// + new_z;}
Links[j].Axis[0].Setdir(Pr);
end;
end;
end;
ForceMoved := true;
end;
function TRobot.CalcCenterOfMass: TdVector3;
var v0, vi: TdVector3;
m0, mi: double;
i: integer;
begin
v0 := Vector3Make(0, 0, 0);
m0 := 0;
for i := 0 to Solids.Count - 1 do begin
vi := dBodyGetPosition(Solids[i].Body)^;
mi := Solids[i].Body.mass.mass;
v0 := Vector3ADD(v0, Vector3ScalarMul(vi, mi));
m0 := m0 + mi;
end;
if m0 > 0 then begin
v0 := Vector3ScalarMul(v0, 1/m0);
end;
result := v0;
end;
procedure TRobot.AddXY(add_x, add_y: double);
var i: integer;
Pr: TdVector3;
begin
for i := 0 to Solids.Count - 1 do begin
Pr := dBodygetPosition(Solids[i].Body)^;
dBodySetPosition(Solids[i].Body, Pr[0] + add_x, Pr[1] + add_y, Pr[2]);
//dBodySetPosition(Solids[i].Body, Pr[0], Pr[1], Pr[2]);
end;
end;
{ TRobotList }
function TRobotList.IndexFromID(aID: string): integer;
var i: integer;
begin
result := -1;
for i := 0 to Count - 1 do begin
if Items[i].ID = aID then begin
result := i;
exit;
end;
end;
end;
function TRobotList.Add(ARobot: TRobot): Integer;
begin
Result := inherited Add(ARobot);
end;
procedure TRobotList.ClearAll;
var i: integer;
begin
For i := 0 to count-1 do begin
GetItems(i).Free;
end;
clear;
end;
function TRobotList.Extract(Item: TRobot): TRobot;
begin
Result := TRobot(inherited Extract(Item));
end;
function TRobotList.First: TRobot;
begin
Result := TRobot(inherited First);
end;
function TRobotList.GetItems(Index: Integer): TRobot;
begin
Result := TRobot(inherited Items[Index]);
end;
function TRobotList.IndexOf(ARobot: TRobot): Integer;
begin
Result := inherited IndexOf(ARobot);
end;
procedure TRobotList.Insert(Index: Integer; ARobot: TRobot);
begin
inherited Insert(Index, ARobot);
end;
function TRobotList.Last: TRobot;
begin
Result := TRobot(inherited Last);
end;
function TRobotList.Remove(ARobot: TRobot): Integer;
begin
Result := inherited Remove(ARobot);
end;
procedure TRobotList.SetItems(Index: Integer; ARobot: TRobot);
begin
inherited Items[Index] := ARobot;
end;
{ TSolid }
constructor TSolid.Create;
begin
BeltSpeed := 0.5;
PaintBitmap := nil;
end;
destructor TSolid.Destroy;
begin
if assigned(PaintBitmap) then PaintBitmap.Free;
inherited;
end;
{ TSolidsList }
procedure TSolidList.ClearAll;
var i: integer;
begin
For i := 0 to count-1 do begin
GetItems(i).Free;
end;
clear;
end;
function TSolidList.Add(ASolid: TSolid): Integer;
begin
Result := inherited Add(ASolid);
end;
function TSolidList.Extract(Item: TSolid): TSolid;
begin
Result := TSolid(inherited Extract(Item));
end;
function TSolidList.First: TSolid;
begin
Result := TSolid(inherited First);
end;
function TSolidList.GetItems(Index: Integer): TSolid;
begin
Result := TSolid(inherited Items[Index]);
end;
function TSolidList.IndexOf(ASolid: TSolid): Integer;
begin
Result := inherited IndexOf(ASolid);
end;
procedure TSolidList.Insert(Index: Integer; ASolid: TSolid);
begin
inherited Insert(Index, ASolid);
end;
function TSolidList.Last: TSolid;
begin
Result := TSolid(inherited Last);
end;
function TSolidList.Remove(ASolid: TSolid): Integer;
begin
Result := inherited Remove(ASolid);
end;
procedure TSolidList.SetItems(Index: Integer; ASolid: TSolid);
begin
inherited Items[Index] := ASolid;
end;
function TSolid.GetPosition: TdVector3;
begin
result := dBodyGetPosition(Body)^;
end;
function TSolid.GetRotation: TdMatrix3;
begin
result := dBodyGetRotation(Body)^;
end;
procedure TSolid.MovePosition(dX, dY, dZ: double);
var Vpos: TdVector3;
begin
Vpos := dBodyGetPosition(Body)^;
dBodySetPosition(Body, Vpos[0] + dX, Vpos[1] + dY, Vpos[2] + dZ);
end;
procedure TSolid.SetColor(R, G, B, A: single);
begin
if GLObj = nil then exit;
if A < 1 then GLObj.Material.BlendingMode := bmTransparency;
GLObj.Material.FrontProperties.Diffuse.SetColor(R, G, B, A);
//GLObj.Material.FrontProperties.Ambient.SetColor(0.2 * R, 0.2 * G, 0.2 * B, A);
end;
procedure TSolid.SetColorRGB(RGB: TColor);
begin
if GLObj = nil then exit;
GLObj.Material.FrontProperties.Diffuse.AsWinColor := RGB;
end;
procedure TSolid.SetTag(newTag: string);
begin
Tag := newTag;
end;
function TSolid.GetTag: string;
begin
result := Tag;
end;
procedure TSolid.GetColor(out R, G, B, A: integer);
begin
if GLObj = nil then exit;
with GLObj.Material.FrontProperties.Diffuse do begin
R := round(255 * Red);
G := round(255 * green);
B := round(255 * Blue);
A := round(255 * Alpha);
end;
end;
procedure TSolid.SetPosition(posX, posY, posZ: double);
begin
dBodySetPosition(Body, posX, posY, posZ);
end;
procedure TSolid.SetRotation(axisX, axisY, axisZ, rot_angle: double);
var R: TdMatrix3;
begin
dRFromAxisAndAngle(R, axisX, axisY, axisZ, rot_angle);
dBodySetRotation(Body, R);
end;
procedure TSolid.SetRotation(R: TdMatrix3);
begin
dBodySetRotation(Body, R);
end;
procedure TSolid.SetTexture(TextureName: string; TextureScale: double);
var LM: TGLLibMaterial;
begin
//exit; //TODO
if GLObj = nil then exit;
//LM := GLObj.Material.MaterialLibrary.Materials.GetLibMaterialByName(TextureName);
LM := nil;//GLObj.Material.MaterialLibrary. GetLibMaterialByName(TextureName);
if LM <> nil then begin;
GLObj.Material.LibMaterialName := TextureName;
LM.TextureScale.x := TextureScale;
LM.TextureScale.y := TextureScale;
end else begin
GLObj.Material.TextureEx.Add;
GLObj.Material.TextureEx.Items[0].Texture.Image.LoadFromFile(TextureName);
with GLObj.Material.TextureEx.Items[0] do begin
TextureIndex := 0;
Texture.Disabled := false;
Texture.TextureMode := tmModulate;
//Texture.TextureMode := tmDecal;
//Texture.TextureMode := tmReplace;
//Texture.TextureMode := tmBlend;
Texture.MappingMode := tmmObjectLinear;
with Texture.MappingSCoordinates do begin
W := 0; X := 0; Y := 1; Z := 0;
end;
with Texture.MappingTCoordinates do begin
W := 0; X := -1; Y := 0; Z := 0;
end;
with TextureOffset do begin
X := 0.5; Y := 0.5; Z := 0;
end;
//Texture.TextureWrap := twNone;
Texture.FilteringQuality := tfAnisotropic;
end;
GLObj.Material.TextureEx.Items[0].TextureScale.x := TextureScale;
GLObj.Material.TextureEx.Items[0].TextureScale.y := TextureScale;
end;
end;
procedure TSolid.SetZeroState;
begin
ZeroPosition := dBodyGetPosition(Body)^;
ZeroRotation := dBodyGetRotation(Body)^;
end;
procedure TSolid.SetLinSpeed(vX, vY, vZ: double);
begin
dBodySetLinearVel(Body, vX, vY, vZ);
end;
procedure TSolid.SetAngularVel(wX, wY, wZ: double);
begin
dBodySetAngularVel(Body, wX, wY, wZ);
end;
procedure TSolid.SetSize(sizeX, sizeY, sizeZ: double);
begin
if dGeomGetClass(Geom) = dBoxClass then begin
dGeomBoxSetLengths(Geom, sizeX, sizeY, sizeZ);
with (GLObj as TGLCube) do begin
CubeDepth := sizeZ;
CubeHeight := sizeY;
CubeWidth := sizeX;
end;
end else if dGeomGetClass(Geom) = dCylinderClass then begin
dGeomCylinderSetParams(Geom, sizeX, sizeZ);
with (GLObj as TGLCylinder) do begin
TopRadius := sizeX;
BottomRadius := sizeX;
Height := sizeZ;
end;
end else if dGeomGetClass(Geom) = dSphereClass then begin
dGeomSphereSetRadius(Geom, sizeX);
with (GLObj as TGLSphere) do begin