-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbiped.cpp
More file actions
executable file
·2466 lines (2040 loc) · 60.1 KB
/
biped.cpp
File metadata and controls
executable file
·2466 lines (2040 loc) · 60.1 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
/*
Biped HyperNEAT project
Copyright 2013 Randal S. Olson, Joel Lehman, Kenneth Stanley.
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.
*/
// Test for cylinder vs sphere, by Bram Stolk
#include <ode/odeconfig.h>
#include <assert.h>
#include <vector>
#include <iostream>
#include <fstream>
#include <math.h>
#include <unistd.h>
#include "rtneat/neat.h"
#include "rtneat/organism.h"
#include "rtneat/noveltyset.h"
#include "rtneat/datarec.h"
#include "simplega/ga.h"
#include "simplega/ConfigFile.h"
static char startgenes_fn[100]="bipedstartgenes";
static char substrate_fn[100] = "substrategenes";
static int novelty_function = 0;
//#define RIGIDITY
#define NF_COG 14
#define NF_COGSAMP 15
#define NF_LEG 16
#define NF_LEGSAMP 17
#define NF_SS 18
#define NF_FITCU 0
#define NF_FITCUSAMP 1
#define NF_COGCU 2
#define NF_COGCUSAMP 3
#define NF_LEGCU 4
#define NF_LEGCUSAMP 5
#define NF_COGSQ 6
#define NF_COGSAMPSQ 7
#define NF_LEGSQ 8
#define NF_LEGSAMPSQ 9
#define NF_FITSQ 10
#define NF_FITSQSAMP 11
#define NF_FIT 12
#define NF_FITSAMP 13
using namespace std;
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <ode/ode.h>
#ifdef _MSC_VER
#pragma warning(disable:4244 4305) // for VC++, no precision loss complaints
#endif
#ifdef GRAPHICS
#include <drawstuff/drawstuff.h>
#include "texturepath.h"
#endif
class WalkerDomain;
static WalkerDomain *domain;
static SGA::Population *pop;
static SGA::GA *ga;
static dReal params[]={ 0.2, 3.0, 0.1, 0.15, 0.0, 0.1 , 0.2,0.3,0.35};
class SineController;
class DummyController;
class Controller;
static Controller* controller;
//NEAT + NS stuff
inline float dist(float x1, float y1, float x2, float y2)
{
float xd = x1-x2;
float yd = y1-y2;
return xd*xd+yd*yd;
}
static void calculate_delta(dVector3 v1, dVector3 v2, dVector3 o)
{
for(int x=0;x<3;x++)
o[x]=v2[x]-v1[x];
}
static void calculate_power(dVector3 v,int pow)
{
for(int x=0;x<3;x++)
{
float temp=v[x];
bool sign=false;
if(temp<0.0)
sign=true;
for(int k=1;k<pow;k++)
v[x]*=temp;
if(sign)
v[x]=(-v[x]);
}
}
float feet_distance2(vector<float>& t1, vector<float>& x1, vector<float>& y1, vector<float>& t2, vector<float> &x2, vector<float> &y2)
{
float td=0.0;
float dummy_distance=0.05;
int c1=0;
int c2=0;
int size1 = x1.size();
int size2 = x2.size();
int loopsize=0;
int bigsize=0;
float step_factor=1.0;
float inc_factor=1.0;
//cout << x1.size() << " " << y1.size() << " " << x2.size() << " " << y2.size() << endl;
if(size1<size2)
{
loopsize=size1;
bigsize=size2;
}
else
{
loopsize=size2;
bigsize=size1;
}
for(int x=0;x<loopsize;x++)
{
// cout << x << " " << y1.size() << " " << y2.size() << endl;
td+=step_factor*dist(x1[x],y1[x],x2[x],y2[x]);
//step_factor*=inc_factor;
}
for(int x=loopsize;x<bigsize;x++)
{
// cout << x << " " << loopsize << " " << bigsize << endl;
if(size1<size2)
td+=step_factor*dist(x2[x],y2[x],0.0,0.0);
else
td+=step_factor*dist(x1[x],y1[x],0.0,0.0);
//step_factor*=inc_factor;
}
return td;
}
float feet_distance(vector<float>& t1, vector<float>& x1, vector<float>& y1, vector<float>& t2, vector<float> &x2, vector<float> &y2)
{
float td=0.0;
int c1=0;
int c2=0;
int size1 = x1.size();
int size2 = x2.size();
int time=0;
float step_factor=1.0;
int step_cnt=0;
float dummy_distance=0.05;
float inc_factor=1.00;
while(c1<size1 && c2<size2)
{
if(t1[c1+1]<t2[c2+1])
{
td+=step_factor*dist(x1[c1],y1[c1],x2[c2],y2[c2])*(t1[c1+1]-time);
time=t1[c1+1];
c1++;
}
else if (t2[c2+1]<t1[c1+1])
{
td+=step_factor*dist(x1[c1],y1[c1],x2[c2],y2[c2])*(t2[c2+1]-time);
time=t2[c2+1];
c2++;
}
else
{
td+=step_factor*dist(x1[c1],y1[c1],x2[c2],y2[c2])*(t2[c2+1]-time);
time=t2[c2+1];
c1++;
c2++;
}
if(c1 > step_cnt)
{
step_cnt=c1;
step_factor*=inc_factor;
}
else if(c2>step_cnt)
{
step_cnt=c2;
step_factor*=inc_factor;
}
}
vector<float>* t;
vector<float>* x;
vector<float>* y;
int c;
if (t1[t1.size()-1] < t2[t2.size()-1])
{
t=&t2;
x=&x2;
y=&y2;
c=c2;
}
else
{
t=&t1;
x=&x1;
y=&y1;
c=c1;
}
step_cnt=c;
step_factor=pow(inc_factor,c);
for(int k=0;k<t->size()-1;k++)
{
td+=step_factor*dummy_distance*((*t)[k+1]-time);
time=(*t)[k+1];
step_factor*=inc_factor;
}
if(td<0.0)
{
cout << "Wtf" << endl;
return 0.001;
}
return td;
}
//novelty metric for maze simulation
float walker_novelty_metric(noveltyitem* x,noveltyitem* y)
{
float dist=0.0;
if(novelty_function!=NF_SS)
{
int size = x->data[0].size();
for(int k=0;k<size;k++)
{
float delta = x->data[0][k]-y->data[0][k];
dist+=delta*delta;
}
return dist;
}
else
{
float left_feet;
float right_feet;
left_feet = feet_distance2(x->data[0],x->data[1],x->data[2],y->data[0],y->data[1],y->data[2]);
right_feet = feet_distance2(x->data[3],x->data[4],x->data[5],y->data[3],y->data[4],y->data[5]);
return left_feet+right_feet;
}
}
void biped_epoch(NEAT::Population *pop,bool novelty=false);
void biped_realtime_loop(NEAT::Population *pop,bool novelty=false);
NEAT::Population *biped_realtime(bool novelty=false);
noveltyitem* biped_evaluate(NEAT::Organism* org,data_record* data=NULL);
//globals
static NEAT::Population *neatpop;
static noveltyarchive archive(1.0,*walker_novelty_metric);
static data_rec Record; //stores run information
static int indiv_counter=0;
static bool Novelty=false;
static char outdir[50]="";
class Controller
{
public:
vector<dReal> outs;
int size;
bool scale;
bool debug;
ofstream* dbgfile;
Controller() { }
Controller(int s,bool deb=false)
{
debug=deb;
scale=true;
size=s;
for(int x=0;x<size;x++)
outs.push_back(0.0);
if (debug)
dbgfile=new ofstream("controller.log");
}
virtual void update(double time,vector<dReal> sensors) {
if(debug)
{
for(int x=0;x<size;x++)
{
*dbgfile << outs[x] << " ";
}
*dbgfile << "\n";
}
}
virtual vector<dReal>* get_outputs()
{
return &outs;
}
virtual ~Controller()
{
if(debug)
delete dbgfile;
}
};
class DummyController: public Controller
{
public:
DummyController():Controller(6,false)
{
scale=false;
for(int x=0;x<size;x++)
outs[x]=0.0;
}
};
class CTRNNController: public Controller
{
public:
NEAT::Network* net;
CTRNNController(NEAT::Network* n,bool dbg=false):Controller(6,dbg)
{
scale=true;
net=n;
n->init_ctrnn();
for(int x=0;x<50;x++)
net->activate_ctrnn(0.02);
for(int x=0;x<size;x++)
outs[x]=net->outputs[x]->output;
}
virtual void update(double time,vector<dReal> sensors)
{
double sens[20];
for(int x=0;x<sensors.size();x++)
sens[x]=sensors[x];
net->load_sensors(sens);
net->activate_ctrnn(time);
for(int x=0;x<6;x++)
{
outs[x]=net->outputs[x]->output;
}
Controller::update(time,sensors);
}
};
class SineController: public Controller
{
public:
double cum_time;
vector<dReal> params;
SineController(int size):Controller(size)
{
scale=false;
cum_time=0.0;
}
void load_params(dReal *p)
{
for(int x=0;x<(size/2)*3;x++)
{
params.push_back(p[x]);
}
}
virtual void update(double time,vector<dReal> sensors)
{
for(int x=0;x<size;x+=2)
{
double constant=params[3*(x/2)];
double phase=params[3*(x/2)+1];
double amplitude=params[3*(x/2)+2];
outs[x]=constant+amplitude*sin(3.14*2.0*cum_time+phase);
outs[x+1]=constant+amplitude*sin(3.14*2.0*cum_time+phase+3.14);
}
cum_time+=time;
}
};
class Creature
{
public:
bool movie_rec;
bool movie_play;
ofstream* movie;
ofstream* movie_rot;
ifstream* movie_in;
ifstream* movie_rot_in;
Controller* controller;
vector<dGeomID> geoms;
vector<dBodyID> bodies;
vector<bool> onground;
vector<dJointID> joints;
vector<dReal> current_angles;
vector<dReal> desired_angles;
vector<dReal> lo_limit;
vector<dReal> hi_limit;
vector<dReal> sensors;
vector<dReal> desired_angvel;
vector<dReal> delta_angles;
vector<dReal> p_terms;
vector<dReal> d_terms;
dWorldID world;
dSpaceID space;
dVector3 pos;
Creature(bool mov=false,bool play=false) {
if(play)
mov=false;
movie_play=play;
movie_rec=mov;
if(play)
{
movie_in=new ifstream("movie_pos.dat");
movie_rot_in=new ifstream("movie_rot.dat");
}
if(mov)
{
movie=new ofstream("movie_pos.dat");
movie_rot=new ofstream("movie_rot.dat");
}
}
virtual void Update(double timestep)
{
if(movie_rec)
{
for(int x=0;x<geoms.size();x++)
{
dQuaternion rot;
const dReal* pos=dGeomGetPosition(geoms[x]);
dGeomGetQuaternion(geoms[x],rot);
*movie << pos[0] << " " << pos[1] << " " << pos[2] << " ";
*movie_rot << rot[0] << " " << rot[1] << " " << rot[2] << " " << rot[3] << " ";
}
*movie << endl;
*movie_rot << endl;
}
if(movie_play)
{
for(int k=0;k<geoms.size();k++)
{
dReal x,y,z;
dQuaternion q;
*movie_in >> x >> y >> z;
*movie_rot_in >> q[0] >> q[1] >> q[2] >> q[3];
dGeomSetPosition(geoms[k],x,y,z);
dGeomSetQuaternion(geoms[k],q);
}
}
}
dReal TotalMass()
{
dReal total_mass=0.0;
for(int x=0;x<bodies.size();x++)
{
dMass m;
dBodyGetMass(bodies[x],&m);
total_mass+=m.mass;
}
return total_mass;
}
void CenterOfMass(dVector3 center)
{
dReal total_mass=0.0;
dVector3 accum={0.0,0.0,0.0};
const dReal* bpos;
for(int x=0;x<bodies.size();x++)
{
dMass m;
dBodyGetMass(bodies[x],&m);
bpos=dBodyGetPosition(bodies[x]);
total_mass+=m.mass;
for(int y=0;y<3;y++)
accum[y]+=m.mass*bpos[y];
}
for(int x=0;x<3;x++)
center[x]=accum[x]/total_mass;
}
virtual void Create(dWorldID worldi, dSpaceID spacei, dVector3 posi,Controller* cont)
{
controller=cont;
world=worldi;
space=spacei;
pos[0]=posi[0];
pos[1]=posi[1];
pos[2]=posi[2];
}
virtual void Destroy()
{
for(int x=0;x<geoms.size();x++)
dGeomDestroy(geoms[x]);
}
virtual bool abort()=0;
virtual dReal fitness()=0;
int add_fixed(int b1,int b2)
{
dBodyID bd1=bodies[b1];
dBodyID bd2;
if (b2!=(-1))
{
bd2=bodies[b2];
}
else
{
bd2=0;
}
dJointID tempjoint = dJointCreateFixed(world,0);
dJointAttach(tempjoint,bd1,bd2);
dJointSetFixed(tempjoint);
joints.push_back(tempjoint);
return joints.size()-1;
}
int add_hinge(int b1,int b2,dVector3 anchor,dVector3 axis,dReal lostop, dReal histop, dReal fmax)
{
dJointID tempjoint = dJointCreateHinge(world,0);
dJointAttach(tempjoint,bodies[b1],bodies[b2]);
dJointSetHingeAnchor(tempjoint,pos[0]+anchor[0],pos[1]+anchor[1],pos[2]+anchor[2]);
dJointSetHingeAxis(tempjoint,axis[0],axis[1],axis[2]);
dJointSetHingeParam(tempjoint,dParamLoStop,lostop);
dJointSetHingeParam(tempjoint,dParamHiStop,histop);
dJointSetHingeParam(tempjoint,dParamFMax,fmax);
joints.push_back(tempjoint);
return joints.size()-1;
}
int add_universal(int b1,int b2,dVector3 anchor, dVector3 axis1, dVector3 axis2, dReal lostop1, dReal histop1, dReal lostop2, dReal histop2, dReal fmax1, dReal fmax2)
{
dJointID tempjoint = dJointCreateUniversal(world,0);
dJointAttach(tempjoint,bodies[b1],bodies[b2]);
dJointSetUniversalAnchor(tempjoint,pos[0]+anchor[0],pos[1]+anchor[1],pos[2]+anchor[2]);
dJointSetUniversalAxis1(tempjoint,axis1[0],axis1[1],axis1[2]);
dJointSetUniversalAxis2(tempjoint,axis2[0],axis2[1],axis2[2]);
dJointSetUniversalParam(tempjoint,dParamLoStop,lostop1);
dJointSetUniversalParam(tempjoint,dParamHiStop,histop1);
dJointSetUniversalParam(tempjoint,dParamLoStop2,lostop2);
dJointSetUniversalParam(tempjoint,dParamHiStop2,histop2);
dJointSetUniversalParam(tempjoint,dParamFMax,fmax1);
dJointSetUniversalParam(tempjoint,dParamFMax2,fmax2);
joints.push_back(tempjoint);
return joints.size()-1;
}
int add_box(dReal density, dReal lx, dReal ly, dReal lz, const dVector3 p)
{
dBodyID tempbody;
dGeomID tempgeom;
dMass m;
tempbody = dBodyCreate (world);
dMassSetBoxTotal(&m,density,lx,ly,lz);
tempgeom = dCreateBox(0,lx,ly,lz);
dGeomSetBody(tempgeom,tempbody);
dBodySetPosition(tempbody,pos[0]+p[0],pos[1]+p[1],pos[2]+p[2]);
dSpaceAdd(space,tempgeom);
bodies.push_back(tempbody);
geoms.push_back(tempgeom);
onground.push_back(false);
return bodies.size()-1;
}
int add_sphere(dReal density, dReal radius, const dVector3 p)
{
dBodyID tempbody;
dGeomID tempgeom;
dMass m;
tempbody = dBodyCreate (world);
dMassSetSphereTotal(&m,density,radius);
tempgeom = dCreateSphere(0,radius);
dGeomSetBody(tempgeom,tempbody);
dBodySetMass(tempbody,&m);
dBodySetPosition(tempbody,pos[0]+p[0],pos[1]+p[1],pos[2]+p[2]);
dSpaceAdd(space,tempgeom);
bodies.push_back(tempbody);
geoms.push_back(tempgeom);
onground.push_back(false);
return bodies.size()-1;
}
int add_cylinder(int axis, dReal density,dReal length, dReal radius, const dVector3 p,dBodyID* k=NULL)
{
dReal a[]={0.0,0.0,0.0};
dBodyID tempbody;
dGeomID tempgeom;
dMass m;
tempbody = dBodyCreate (world);
if(k!=NULL)
(*k)=tempbody;
dQuaternion q;
if (axis==1)
{
a[1]=1.0;
}
else if (axis==2)
{
a[0]=1.0;
}
else
{
a[2]=1.0;
}
dQFromAxisAndAngle (q,a[0],a[1],a[2], M_PI * 0.5);
dBodySetQuaternion (tempbody,q);
dMassSetCylinderTotal (&m,density,axis,radius,length);
dBodySetMass (tempbody,&m);
tempgeom = dCreateCylinder(0, radius, length);
dGeomSetBody (tempgeom,tempbody);
dBodySetPosition (tempbody, pos[0]+p[0],pos[1]+p[1], pos[2]+p[2]);
dSpaceAdd (space, tempgeom);
geoms.push_back(tempgeom);
bodies.push_back(tempbody);
onground.push_back(false);
return bodies.size()-1;
}
virtual ~Creature()
{
if(movie_rec)
{
delete movie;
delete movie_rot;
cout << "terminating.." << endl;
}
if(movie_play)
{
delete movie_in;
delete movie_rot_in;
}
}
};
//BIPED PARAMETERS
static dReal SCALE_FACTOR = 3;
static dReal FOOTX_SZ =1.0/SCALE_FACTOR;
static dReal FOOTY_SZ =0.5/SCALE_FACTOR;
static dReal FOOTZ_SZ =1.0/SCALE_FACTOR;
static dReal LLEG_LEN =1.0/SCALE_FACTOR;
static dReal LLEG_RAD =0.2/SCALE_FACTOR;
static dReal ULEG_LEN =1.0/SCALE_FACTOR;
static dReal ULEG_RAD =0.2/SCALE_FACTOR;
static dReal TORSO_LEN =1.0/SCALE_FACTOR;
static dReal TORSO_RAD =0.3/SCALE_FACTOR;
static dReal ORIG_HEIGHT= (TORSO_RAD/2.0+ULEG_LEN+LLEG_LEN+FOOTZ_SZ);
static dReal DENSITY=0.5;
static dReal TORSO_DENSITY=1.0;
static dReal FOOT_DENSITY=0.1;
static dReal MAXTORQUE_FOOT= 10.0;
static dReal MAXTORQUE_KNEE= 5.0;
static dReal MAXTORQUE_HIPMINOR= 5.0;
static dReal MAXTORQUE_HIPMAJOR= 5.0;
static dReal P_CONSTANT= 9.0;
static dReal D_CONSTANT= 0.0;
static dReal FOOTFACTOR= 5.0;
static bool bDisplay = false;
static bool bDoEvolution = true;
static bool bSlowDown = false;
#ifdef GRAPHICS
static bool bNoVis=true;//false;
#else
static bool bNoVis=true;
#endif
static bool bMoviePlay=false;
void load_parameters()
{
ConfigFile config( "config.txt" );
DENSITY=config.read<dReal>("DENSITY");
P_CONSTANT=config.read<dReal>("P_CONSTANT");
D_CONSTANT=config.read<dReal>("D_CONSTANT");
MAXTORQUE_KNEE=config.read<dReal>("MAXTORQUE_KNEE");
MAXTORQUE_HIPMINOR=config.read<dReal>("MAXTORQUE_HIPMINOR");
MAXTORQUE_HIPMAJOR=config.read<dReal>("MAXTORQUE_HIPMAJOR");
MAXTORQUE_FOOT=config.read<dReal>("MAXTORQUE_FOOT");
//FOOT_P_CONSTANT=config.read<dReal>("FOOT_P_CONSTANT");
//FOOT_D_CONSTANT=config.read<dReal>("FOOT_D_CONSTANT");
int novis=config.read<int>("NOVIS");
if(novis==0)
bNoVis=false;
else
bNoVis=true;
bMoviePlay=(bool)config.read<int>("MOVIE");
}
dReal scaleval(dReal v, dReal min, dReal max)
{
if(v<min)
return 0.0;
if(v>max)
return 1.0;
return (v-min)/(max-min);
}
class Biped: public Creature
{
public:
int step;
dVector3 orig_com;
dVector3 orig_left;
dVector3 orig_right;
dVector3 curr_com;
bool log;
ofstream* logfile;
dJointFeedback feedback[6];
//keeping track of foot positionxorz
bool leftdown;
bool rightdown;
bool leftrigid;
bool rightrigid;
int lastdown; //which foot was last down
vector<float> lft; //left foot time
vector<float> lfx; //x
vector<float> lfy; //y
vector<float> rft; //right foot time
vector<float> rfx; //x
vector<float> rfy; //y
Biped(bool logging=false,bool movie=false):Creature(logging,movie) {
step=0;
leftdown=false;
rightdown=false;
leftrigid=false;
rightrigid=false;
lastdown=0;
log=logging;
if(log)
{
logfile=new ofstream("log.dat");
}
for(int x=0;x<6;x++)
{
p_terms.push_back(P_CONSTANT);
d_terms.push_back(D_CONSTANT);
desired_angles.push_back(0.0);
current_angles.push_back(0.0);
delta_angles.push_back(0.0);
desired_angvel.push_back(0.0);
lo_limit.push_back(0.0);
hi_limit.push_back(0.0);
}
for(int x=0;x<8;x++)
sensors.push_back(0.0);
}
int add_foot(dReal density, dReal radius, const dVector3 p)
{
dBodyID tempbody;
dGeomID maingeom[3];
dGeomID tempgeom[3];
dMass m;
tempbody = dBodyCreate (world);
dMassSetSphereTotal(&m,density,radius);
dBodySetPosition(tempbody,pos[0]+p[0],pos[1]+p[1],pos[2]+p[2]);
bodies.push_back(tempbody);
onground.push_back(false);
for(int x=0;x<3;x++)
{
maingeom[x] = dCreateGeomTransform(space);
tempgeom[x] = dCreateSphere(0,radius);
dGeomSetBody(maingeom[x],tempbody);
dGeomTransformSetGeom(maingeom[x],tempgeom[x]);
dGeomTransformSetCleanup(maingeom[x],1);
dGeomTransformSetInfo(maingeom[x],1);
geoms.push_back(maingeom[x]);
if(x==0)
dGeomSetPosition(tempgeom[x],0.07,-0.08,0.0);
if(x==1)
dGeomSetPosition(tempgeom[x],0.07,0.08,0.0);
if(x==2)
dGeomSetPosition(tempgeom[x],-0.20,0.0,0.0);
}
return bodies.size()-1;
}
virtual dReal fitness()
{
dVector3 new_com;
CenterOfMass(new_com);
double fitness=0.0;
for(int x=0;x<2;x++)
{
double delta=new_com[x]-orig_com[x];
delta*=delta;
fitness+=delta;
}
return sqrt(fitness);
}
~Biped()
{
if(log)
delete logfile;
}
virtual bool abort()
{
const dReal* torsoPos;
torsoPos=dBodyGetPosition(bodies[6]);
if (torsoPos[2]< 0.5*(ORIG_HEIGHT))
return true;
return false;
}
void create_leg(dVector3 offset)
{
dVector3 xAxis={1.0,0.0,0.0};
dVector3 yAxis={0.0,-1.0,0.0};
dVector3 zAxis={0.0,0.0,1.0};
dVector3 p={offset[0],offset[1],offset[2]};
//dVector3 foot_pos = {p[0]-0.3*FOOTX_SZ,p[1],p[2]+(FOOTZ_SZ/2.0)};
//int foot=add_box(DENSITY,FOOTX_SZ,FOOTY_SZ,FOOTZ_SZ,foot_pos);
dVector3 foot_pos = {p[0],p[1],p[2]+(FOOTZ_SZ/2.0)};
int foot=add_sphere(FOOT_DENSITY,FOOTZ_SZ/2.0,foot_pos);
dVector3 lower_pos = {p[0],p[1],p[2]+FOOTZ_SZ+LLEG_LEN/2.0};
int lowerleg = add_cylinder(3,DENSITY,LLEG_LEN,LLEG_RAD,lower_pos);
dVector3 upper_pos = {p[0],p[1],p[2]+FOOTZ_SZ+LLEG_LEN+ULEG_LEN/2.0};
int upperleg = add_cylinder(3,DENSITY,ULEG_LEN,ULEG_RAD,upper_pos);
dVector3 foot_joint_a = {p[0],p[1],p[2]+(FOOTZ_SZ)};
dVector3 knee_joint_a = {p[0],p[1],p[2]+FOOTZ_SZ+LLEG_LEN};
add_fixed(foot,lowerleg);
//add_universal(foot,lowerleg,foot_joint_a,xAxis,yAxis,-0.1,0.1,-0.1,0.1,MAXTORQUE_FOOT,MAXTORQUE_FOOT);
add_hinge(lowerleg,upperleg,knee_joint_a,yAxis,-1.4,0.0,MAXTORQUE_KNEE);
}
virtual void Create(dWorldID worldi, dSpaceID spacei, dVector3 posi,Controller* cont)
{
dVector3 xAxis={1.0,0.0,0.0};
dVector3 nxAxis={-1.0,0.0,0.0};
dVector3 yAxis={0.0,1.0,0.0};
dVector3 zAxis={0.0,0.0,1.0};
Creature::Create(worldi,spacei,posi,cont);
dVector3 leftLegPos={0.0,0.0,0.0};
dVector3 rightLegPos={0.0,TORSO_LEN+ULEG_RAD,0.0};
//dVector3 torsoPos={0.0,(TORSO_LEN+ULEG_RAD)/2.0,ULEG_LEN+LLEG_LEN+FOOTZ_SZ};
dVector3 torsoPos={0.0,(TORSO_LEN+ULEG_RAD)/2.0,ULEG_LEN+LLEG_LEN+FOOTZ_SZ};
dVector3 leftHip={leftLegPos[0],leftLegPos[1]+ULEG_RAD,torsoPos[2]};
dVector3 rightHip={rightLegPos[0],rightLegPos[1]-ULEG_RAD,torsoPos[2]};
create_leg(leftLegPos);
create_leg(rightLegPos);
int torso=add_cylinder(2,TORSO_DENSITY,TORSO_LEN,TORSO_RAD,torsoPos);
//-1.4,1.4
add_universal(torso,2,leftHip,xAxis,yAxis,-0.8,0.8,-1.3,1.6,MAXTORQUE_HIPMINOR,MAXTORQUE_HIPMAJOR);
add_universal(torso,5,rightHip,nxAxis,yAxis,-0.8,0.8,-1.3,1.6,MAXTORQUE_HIPMINOR,MAXTORQUE_HIPMAJOR);
lo_limit[0]=dJointGetHingeParam(joints[1],dParamLoStop);
lo_limit[1]=dJointGetHingeParam(joints[3],dParamLoStop);
hi_limit[0]=dJointGetHingeParam(joints[1],dParamHiStop);
hi_limit[1]=dJointGetHingeParam(joints[3],dParamHiStop);
lo_limit[2]=dJointGetUniversalParam(joints[4],dParamLoStop);
lo_limit[3]=dJointGetUniversalParam(joints[5],dParamLoStop);
hi_limit[2]=dJointGetUniversalParam(joints[4],dParamHiStop);
hi_limit[3]=dJointGetUniversalParam(joints[5],dParamHiStop);
lo_limit[4]=dJointGetUniversalParam(joints[4],dParamLoStop2);
lo_limit[5]=dJointGetUniversalParam(joints[5],dParamLoStop2);
hi_limit[4]=dJointGetUniversalParam(joints[4],dParamHiStop2);
hi_limit[5]=dJointGetUniversalParam(joints[5],dParamHiStop2);
/*
lo_limit[6]=dJointGetUniversalParam(joints[0],dParamLoStop);
lo_limit[7]=dJointGetUniversalParam(joints[2],dParamLoStop);
hi_limit[6]=dJointGetUniversalParam(joints[0],dParamHiStop);
hi_limit[7]=dJointGetUniversalParam(joints[2],dParamHiStop);
lo_limit[8]=dJointGetUniversalParam(joints[0],dParamLoStop2);
lo_limit[9]=dJointGetUniversalParam(joints[2],dParamLoStop2);
hi_limit[8]=dJointGetUniversalParam(joints[0],dParamHiStop2);
hi_limit[9]=dJointGetUniversalParam(joints[2],dParamHiStop2);
*/
dJointSetFeedback(joints[1],&feedback[0]);
dJointSetFeedback(joints[3],&feedback[1]);
dJointSetFeedback(joints[4],&feedback[2]);
dJointSetFeedback(joints[5],&feedback[3]);
dJointSetFeedback(joints[0],&feedback[4]);
dJointSetFeedback(joints[2],&feedback[5]);
//add_fixed(torso,-1);
CenterOfMass(orig_com);
CenterOfMass(curr_com);
orig_left[0]=dBodyGetPosition(bodies[0])[0];
orig_right[0]=dBodyGetPosition(bodies[0])[1];
orig_left[1]=dBodyGetPosition(bodies[3])[0];
orig_right[1]=dBodyGetPosition(bodies[3])[1];
orig_left[2]=0.0;
orig_right[2]=0.0;
}
void print_behavior()
{
cout << "LEFTFOOTSTEPS: " << lft.size() << endl;
cout << "RIGHTFOOTSTEPS: " << rft.size() << endl;
for(int x=0;x<lft.size();x++)
{
cout << "LFT " << x << " time: " << lft[x] << " x: " << lfx[x] << " y: " << lfy[x] << endl;
}
for(int x=0;x<rft.size();x++)
{
cout << "RFT " << x << " time: " << rft[x] << " x: " << rfx[x] << " y: " << rfy[x] << endl;
}
}
// 2 input
virtual void Update(double timestep)
{
Creature::Update(timestep);
if (movie_play)
return;
dReal old_angles[10];
step++;
for(int x=0;x<6;x++)