From 224a6bb3f756d8c8a15bbfc77e59b3ef485d0382 Mon Sep 17 00:00:00 2001 From: Fisch Date: Wed, 16 May 2018 20:48:55 +0200 Subject: [PATCH] improve wagon resistances --- achterbahn.ino | 69 ++++++++++------- .../achterbahnconfig/achterbahnconfig.pde | 6 +- wagon.cpp | 77 +++++++++++-------- 3 files changed, 87 insertions(+), 65 deletions(-) diff --git a/achterbahn.ino b/achterbahn.ino index 63f68e5..fd795e7 100644 --- a/achterbahn.ino +++ b/achterbahn.ino @@ -20,6 +20,9 @@ long lastCheckspawn=0; #define SPAWNCHANCE 20 //1 out of x times wagon will spawn #define SPAWNCHANCEDOUBLE 5 //change of spawning a two trains simultaneously +#define BRIGHTNESS_RUN 200 +#define BRIGHTNESS_DEBUG 150 + long loopmillis=0; uint8_t height[NUMPIXELS]; @@ -44,38 +47,43 @@ void setup() { Serial.begin(115200); strip.begin(); - strip.setBrightness(150); + strip.setBrightness(BRIGHTNESS_RUN); //150 strip.show(); // Initialize all pixels to 'off' Serial.println("Started"); resetHeightmap(); //fixed heightmap - heightraw[0]=254; - heightraw[43]=254; - heightraw[69]=200; - heightraw[95]=149; - heightraw[114]=132; - heightraw[137]=128; - heightraw[195]=128; - heightraw[226]=150; - heightraw[276]=139; - heightraw[303]=150; - heightraw[337]=131; - heightraw[354]=129; - heightraw[368]=131; - heightraw[405]=172; - heightraw[419]=147; - heightraw[435]=117; - heightraw[446]=105; - heightraw[458]=96; - heightraw[472]=77; - heightraw[503]=35; - heightraw[523]=0; - heightraw[554]=0; - heightraw[562]=8; - heightraw[577]=34; - heightraw[599]=67; + heightraw[0]=7; +heightraw[25]=1; +heightraw[89]=102; +heightraw[140]=159; +heightraw[163]=166; +heightraw[184]=162; +heightraw[229]=131; +heightraw[252]=95; +heightraw[266]=73; +heightraw[282]=65; +heightraw[295]=73; +heightraw[305]=82; +heightraw[321]=107; +heightraw[334]=114; +heightraw[343]=117; +heightraw[363]=107; +heightraw[380]=81; +heightraw[397]=53; +heightraw[409]=35; +heightraw[420]=21; +heightraw[429]=11; +heightraw[444]=1; +heightraw[489]=1; +heightraw[508]=10; +heightraw[524]=27; +heightraw[540]=59; +heightraw[599]=141; + + + @@ -117,6 +125,7 @@ void printHeightmapRaw() { Serial.print("]="); Serial.print(heightraw[i]); Serial.println(";"); + delay(10); } } } @@ -202,9 +211,11 @@ void previewHeightmap(int waittime){ void spawnWagon(){ //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,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); Serial.println("Spawned Wagon"); @@ -256,8 +267,10 @@ void checkSerial(){ if (serialstring.equals("run")){ + strip.setBrightness(BRIGHTNESS_RUN); configmode=false; }else if (serialstring.equals("debug")){ + strip.setBrightness(BRIGHTNESS_DEBUG); configmode=true; }else if (serialstring.equals("print")){ printHeightmapRaw(); diff --git a/achterbahnconfig/achterbahnconfig/achterbahnconfig.pde b/achterbahnconfig/achterbahnconfig/achterbahnconfig.pde index 0dbfe4c..8b0c122 100644 --- a/achterbahnconfig/achterbahnconfig/achterbahnconfig.pde +++ b/achterbahnconfig/achterbahnconfig/achterbahnconfig.pde @@ -97,13 +97,13 @@ void setup() { slSpawnLength = cp5.addSlider("spawnlength") .setRange(0,maxspawnlength) - .setValue(5) + .setValue(30) .setPosition(220,80+15*1) .setSize(maxspawnlength,10); slSpawnStartvel = cp5.addSlider("spawnstartvel") .setRange(0,maxspawnstartvel) - .setValue(10) + .setValue(70) .setPosition(220,80+15*2) .setSize(maxspawnstartvel,10); @@ -121,7 +121,7 @@ void setup() { slSpawnColor = cp5.addSlider("spawncolor") .setRange(0,maxspawncolor) - .setValue(0) + .setValue(150) .setPosition(220,80+15*5) .setSize(maxspawncolor,10); diff --git a/wagon.cpp b/wagon.cpp index 56f0821..1974d7c 100644 --- a/wagon.cpp +++ b/wagon.cpp @@ -2,6 +2,8 @@ #define SLOWVELOCITY 0.1 +#define WAGONLENGTH 5 + #define EDGE_WALL //#define EDGE_WRAP @@ -45,7 +47,7 @@ void Wagon::updatePhysics(float updatedelayms) }*/ #define CONST_G 9.81 #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 AIRRES 0.01 //for slipstream at second wagon #define AIRRESMUL 0.7 //how much of air resistance the next wagon has @@ -53,47 +55,54 @@ void Wagon::updatePhysics(float updatedelayms) //rho Massendichte luft 1,2041 //A = 1m^2 - float m=_wagonmass/_trainlength; //mass of part of a wagon + float m=_wagonmass; //mass of part of a wagon _acc=0; int cpos=(int)_pos; uint8_t wagonnumber=0; for (int cpos=(int)_pos;cpos>(int)(_pos-_trainlength);cpos--){ //for each wagon - - float hdiff=getHeight((int) (cpos-0.5)) - getHeight((int)(cpos+0.5)); - - //Serial.print("hdiff="); - //Serial.print(hdiff); + if (wagonnumber%WAGONLENGTH==0) { //every n-th pixel is a wagon - float beta=atan2(PIXELDISTANCE, hdiff); - - //Serial.print(" beta="); - //Serial.println(beta); + float hdiff=getHeight((int) (cpos-0.5)) - getHeight((int)(cpos+0.5)); - //_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 bb=C_ROLL*CONST_G*updatedelayms/1000*sin(beta); //roll resistance - if (_vel<0){ - bb*=-1; - } - float cc=0; - if (wagonnumber==0){ //first wagon - cc=AIRRESFIRST/m*pow(_vel,2); //air resistance for first wagon - }else{ - cc=AIRRES/m*pow(_vel,2) *pow(AIRRESMUL,wagonnumber-1); //air resistance - - } - if (_vel<0){ - cc*=-1; - } - - //Serial.print("aa="); Serial.print(aa); - //Serial.print(" bb="); Serial.print(bb); - //Serial.print(" cc="); Serial.println(cc); + //Serial.print("hdiff="); + //Serial.print(hdiff); - - _acc += aa - bb - cc; + float beta=atan2(PIXELDISTANCE, hdiff); + + //Serial.print(" beta="); + //Serial.println(beta); + + //_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 + + //Roll Resistance + float bb=C_ROLL*CONST_G*updatedelayms/1000*sin(beta); //roll resistance + + if (_vel<0){ + bb*=-1; + } + + //Air Resistance + float cc=0; + if (wagonnumber==0){ //first wagon + cc=AIRRESFIRST/m*pow(_vel,2); //air resistance for first wagon + }else { + cc=AIRRES/m*pow(_vel,2) *pow(AIRRESMUL,wagonnumber-1); //air resistance + } + + if (_vel<0){ + cc*=-1; + } + + //Serial.print("aa="); Serial.print(aa); + //Serial.print(" bb="); Serial.print(bb); + //Serial.print(" cc="); Serial.println(cc); + + + _acc += aa - bb - cc; + } wagonnumber++; }