improve wagon resistances

This commit is contained in:
interfisch 2018-05-16 20:48:55 +02:00
parent 0b1c282921
commit 224a6bb3f7
3 changed files with 87 additions and 65 deletions

View File

@ -20,6 +20,9 @@ long lastCheckspawn=0;
#define SPAWNCHANCE 20 //1 out of x times wagon will spawn #define SPAWNCHANCE 20 //1 out of x times wagon will spawn
#define SPAWNCHANCEDOUBLE 5 //change of spawning a two trains simultaneously #define SPAWNCHANCEDOUBLE 5 //change of spawning a two trains simultaneously
#define BRIGHTNESS_RUN 200
#define BRIGHTNESS_DEBUG 150
long loopmillis=0; long loopmillis=0;
uint8_t height[NUMPIXELS]; uint8_t height[NUMPIXELS];
@ -44,38 +47,43 @@ void setup() {
Serial.begin(115200); Serial.begin(115200);
strip.begin(); strip.begin();
strip.setBrightness(150); strip.setBrightness(BRIGHTNESS_RUN); //150
strip.show(); // Initialize all pixels to 'off' strip.show(); // Initialize all pixels to 'off'
Serial.println("Started"); Serial.println("Started");
resetHeightmap(); resetHeightmap();
//fixed heightmap //fixed heightmap
heightraw[0]=254; heightraw[0]=7;
heightraw[43]=254; heightraw[25]=1;
heightraw[69]=200; heightraw[89]=102;
heightraw[95]=149; heightraw[140]=159;
heightraw[114]=132; heightraw[163]=166;
heightraw[137]=128; heightraw[184]=162;
heightraw[195]=128; heightraw[229]=131;
heightraw[226]=150; heightraw[252]=95;
heightraw[276]=139; heightraw[266]=73;
heightraw[303]=150; heightraw[282]=65;
heightraw[337]=131; heightraw[295]=73;
heightraw[354]=129; heightraw[305]=82;
heightraw[368]=131; heightraw[321]=107;
heightraw[405]=172; heightraw[334]=114;
heightraw[419]=147; heightraw[343]=117;
heightraw[435]=117; heightraw[363]=107;
heightraw[446]=105; heightraw[380]=81;
heightraw[458]=96; heightraw[397]=53;
heightraw[472]=77; heightraw[409]=35;
heightraw[503]=35; heightraw[420]=21;
heightraw[523]=0; heightraw[429]=11;
heightraw[554]=0; heightraw[444]=1;
heightraw[562]=8; heightraw[489]=1;
heightraw[577]=34; heightraw[508]=10;
heightraw[599]=67; heightraw[524]=27;
heightraw[540]=59;
heightraw[599]=141;
@ -117,6 +125,7 @@ void printHeightmapRaw() {
Serial.print("]="); Serial.print("]=");
Serial.print(heightraw[i]); Serial.print(heightraw[i]);
Serial.println(";"); Serial.println(";");
delay(10);
} }
} }
} }
@ -204,7 +213,9 @@ void spawnWagon(){
//Wagon tmpr = Wagon(maxid++,NUMPIXELS,&strip, height, 35, 6, 0.5,0); //spawn new wagon //Wagon tmpr = Wagon(maxid++,NUMPIXELS,&strip, height, 35, 6, 0.5,0); //spawn new wagon
// pos, wagonlength, startvel, startacc, trainmass, wagoncolor // pos, wagonlength, startvel, startacc, trainmass, wagoncolor
//Wagon tmpr = Wagon(maxid++,NUMPIXELS,&strip, height, random(0, 20), random(3,20), random(0.2, 50)/10.0, 0 , random(5,100) , Wheel(random(0,256))); //spawn new wagon //Wagon tmpr = Wagon(maxid++,NUMPIXELS,&strip, height, random(0, 20), random(3,20), random(0.2, 50)/10.0, 0 , random(5,100) , Wheel(random(0,256))); //spawn new wagon
Wagon tmpr = Wagon(maxid++,NUMPIXELS,&strip, height, random(0, 20), random(3,40), random(1, 70)/10.0, 0 , random(50,200) , Wheel(random(0,256))); //spawn new wagon int _randomlength=random(3,40); //3-> minimum vel 10, 40 -> minium vel 30
// pos, wagonlength, startvel , startacc, trainmass, wagoncolor
Wagon tmpr = Wagon(maxid++,NUMPIXELS,&strip, height, random(0, 20), _randomlength, random(map(_randomlength,3,40,10,30), map(_randomlength,3,40, 20,60))/10.0 , 0 , 20 , Wheel(random(0,256))); //spawn new wagon
wagon_arr.push_back(tmpr); wagon_arr.push_back(tmpr);
Serial.println("Spawned Wagon"); Serial.println("Spawned Wagon");
@ -256,8 +267,10 @@ void checkSerial(){
if (serialstring.equals("run")){ if (serialstring.equals("run")){
strip.setBrightness(BRIGHTNESS_RUN);
configmode=false; configmode=false;
}else if (serialstring.equals("debug")){ }else if (serialstring.equals("debug")){
strip.setBrightness(BRIGHTNESS_DEBUG);
configmode=true; configmode=true;
}else if (serialstring.equals("print")){ }else if (serialstring.equals("print")){
printHeightmapRaw(); printHeightmapRaw();

View File

@ -97,13 +97,13 @@ void setup() {
slSpawnLength = cp5.addSlider("spawnlength") slSpawnLength = cp5.addSlider("spawnlength")
.setRange(0,maxspawnlength) .setRange(0,maxspawnlength)
.setValue(5) .setValue(30)
.setPosition(220,80+15*1) .setPosition(220,80+15*1)
.setSize(maxspawnlength,10); .setSize(maxspawnlength,10);
slSpawnStartvel = cp5.addSlider("spawnstartvel") slSpawnStartvel = cp5.addSlider("spawnstartvel")
.setRange(0,maxspawnstartvel) .setRange(0,maxspawnstartvel)
.setValue(10) .setValue(70)
.setPosition(220,80+15*2) .setPosition(220,80+15*2)
.setSize(maxspawnstartvel,10); .setSize(maxspawnstartvel,10);
@ -121,7 +121,7 @@ void setup() {
slSpawnColor = cp5.addSlider("spawncolor") slSpawnColor = cp5.addSlider("spawncolor")
.setRange(0,maxspawncolor) .setRange(0,maxspawncolor)
.setValue(0) .setValue(150)
.setPosition(220,80+15*5) .setPosition(220,80+15*5)
.setSize(maxspawncolor,10); .setSize(maxspawncolor,10);

View File

@ -2,6 +2,8 @@
#define SLOWVELOCITY 0.1 #define SLOWVELOCITY 0.1
#define WAGONLENGTH 5
#define EDGE_WALL #define EDGE_WALL
//#define EDGE_WRAP //#define EDGE_WRAP
@ -45,7 +47,7 @@ void Wagon::updatePhysics(float updatedelayms)
}*/ }*/
#define CONST_G 9.81 #define CONST_G 9.81
#define PIXELDISTANCE 1.6666667 // 1/60.0 * 100 #define PIXELDISTANCE 1.6666667 // 1/60.0 * 100
#define C_ROLL 0.001 // = Croll * G https://de.wikipedia.org/wiki/Rollwiderstand 0.001 #define C_ROLL 0.001 // = Croll * G https://de.wikipedia.org/wiki/Rollwiderstand 0.001 (zug)
#define AIRRESFIRST 0.18 //C_w*A*0.5*rho Air resistance: C_w * A * 0.5 * rho (.... *v^2) #define AIRRESFIRST 0.18 //C_w*A*0.5*rho Air resistance: C_w * A * 0.5 * rho (.... *v^2)
#define AIRRES 0.01 //for slipstream at second wagon #define AIRRES 0.01 //for slipstream at second wagon
#define AIRRESMUL 0.7 //how much of air resistance the next wagon has #define AIRRESMUL 0.7 //how much of air resistance the next wagon has
@ -53,12 +55,13 @@ void Wagon::updatePhysics(float updatedelayms)
//rho Massendichte luft 1,2041 //rho Massendichte luft 1,2041
//A = 1m^2 //A = 1m^2
float m=_wagonmass/_trainlength; //mass of part of a wagon float m=_wagonmass; //mass of part of a wagon
_acc=0; _acc=0;
int cpos=(int)_pos; int cpos=(int)_pos;
uint8_t wagonnumber=0; uint8_t wagonnumber=0;
for (int cpos=(int)_pos;cpos>(int)(_pos-_trainlength);cpos--){ //for each wagon for (int cpos=(int)_pos;cpos>(int)(_pos-_trainlength);cpos--){ //for each wagon
if (wagonnumber%WAGONLENGTH==0) { //every n-th pixel is a wagon
float hdiff=getHeight((int) (cpos-0.5)) - getHeight((int)(cpos+0.5)); float hdiff=getHeight((int) (cpos-0.5)) - getHeight((int)(cpos+0.5));
@ -73,17 +76,22 @@ void Wagon::updatePhysics(float updatedelayms)
//_acc += CONST_G * cos(beta) - C_ROLLG*sin(beta) - AIRRES/m*_vel*_vel; //_acc += CONST_G * cos(beta) - C_ROLLG*sin(beta) - AIRRES/m*_vel*_vel;
float aa=CONST_G * cos(beta) *updatedelayms/1000; //Gravity and m/s^2 time correction float aa=CONST_G * cos(beta) *updatedelayms/1000; //Gravity and m/s^2 time correction
//Roll Resistance
float bb=C_ROLL*CONST_G*updatedelayms/1000*sin(beta); //roll resistance float bb=C_ROLL*CONST_G*updatedelayms/1000*sin(beta); //roll resistance
if (_vel<0){ if (_vel<0){
bb*=-1; bb*=-1;
} }
//Air Resistance
float cc=0; float cc=0;
if (wagonnumber==0){ //first wagon if (wagonnumber==0){ //first wagon
cc=AIRRESFIRST/m*pow(_vel,2); //air resistance for first wagon cc=AIRRESFIRST/m*pow(_vel,2); //air resistance for first wagon
}else{ }else {
cc=AIRRES/m*pow(_vel,2) *pow(AIRRESMUL,wagonnumber-1); //air resistance cc=AIRRES/m*pow(_vel,2) *pow(AIRRESMUL,wagonnumber-1); //air resistance
} }
if (_vel<0){ if (_vel<0){
cc*=-1; cc*=-1;
} }
@ -94,6 +102,7 @@ void Wagon::updatePhysics(float updatedelayms)
_acc += aa - bb - cc; _acc += aa - bb - cc;
}
wagonnumber++; wagonnumber++;
} }