add more wagon parameters and change wrap around to loop

This commit is contained in:
interfisch 2018-04-10 21:17:45 +02:00
parent 0cc531c7b3
commit 6bb3993bf4
3 changed files with 153 additions and 62 deletions

View File

@ -11,8 +11,6 @@
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800); Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
long lastPixelUpdate=0; long lastPixelUpdate=0;
#define PIXELUPDATETIME 10 #define PIXELUPDATETIME 10
long lastRoutineUpdate=0; long lastRoutineUpdate=0;
@ -30,75 +28,131 @@ uint8_t maxid=0;
void setup() { void setup() {
Serial.begin(115200); Serial.begin(115200);
for (int i=0;i<NUMPIXELS;i++){
height[i]=0;
}
//Temporaer
int h=0;
for (int i=2;i<=30;i++){
height[i]=h*43/(30-2);
h++;
}
for (int i=31;i<(31+55);i++){ //gerade obene ebene
height[i]=43;
}
h=30;
for (int i=(31+55);i<(31+55+30);i++){
height[i]=h*43/(30);
h--;
}
h=0;
for (int i=(31+55+30+40);i<=(31+55+30+40+15);i++){
height[i]=h*23/(15);
h++;
}
for (int i=(31+55+30+40+15);i<=(31+55+30+40+15+55);i++){ //gerade mittlere ebene
height[i]=23;
}
h=20;
for (int i=(31+55+30+40+15+55);i<=(31+55+30+40+15+55+20);i++){ //gerade mittlere ebene
height[i]=h*23/20;
h--;
}
h=0;
for (int i=NUMPIXELS-30;i<NUMPIXELS;i++){
height[i]=h*40/30;
h++;
}
strip.begin(); strip.begin();
strip.setBrightness(150); strip.setBrightness(150);
strip.show(); // Initialize all pixels to 'off' strip.show(); // Initialize all pixels to 'off'
Serial.println("Started"); Serial.println("Started");
previewHeightmap(); for (int i=0;i<NUMPIXELS;i++){
height[i]=255;
}
//Temporaer
height[0]=46;
height[28]=46;
height[60]=3;
height[63]=3;
height[70]=4;
height[100]=0;
height[122]=0;
height[137]=12;
height[138]=12;
height[152]=0;
height[183]=0;
height[200]=21;
height[209]=26;
height[225]=26;
height[233]=23;
height[240]=23;
height[250]=21;
height[255]=20;
height[274]=46;
height[NUMPIXELS-1]=46;
//previewHeightmap(10000);
//interpolate every part with height value 255
for (int interpolateStartpos=0;interpolateStartpos<NUMPIXELS-1;interpolateStartpos++){
if (height[interpolateStartpos]==255){ //interpolation part starts
int interpolateEndpos=interpolateStartpos+1;
while (interpolateEndpos<NUMPIXELS && height[interpolateEndpos]==255){
interpolateEndpos++;
}
interpolateEndpos--;
//interpolateStartpos index of first 255 value
//interpolateEndpos index of last 255 value
uint8_t interpolateStartvalue=height[interpolateStartpos-1];
uint8_t interpolateEndvalue=height[interpolateEndpos+1];
int interpolateLength=interpolateEndpos-interpolateStartpos+1; //one 255 element -> length 1
float interpolateStep= ((int)(interpolateEndvalue)-(int)(interpolateStartvalue))*1.0 /(interpolateLength+1);
Serial.println();
Serial.print("interpolateStep=");
Serial.print("(");
Serial.print(interpolateEndvalue);
Serial.print("-");
Serial.print(interpolateStartvalue);
Serial.print(")/");
Serial.print(interpolateLength+1);
Serial.print("=");
Serial.println(interpolateStep);
int interpolateStepCounter=1;
Serial.println();
Serial.print("interpolateStartpos=");
Serial.println(interpolateStartpos);
Serial.print("interpolateEndpos=");
Serial.println(interpolateEndpos);
Serial.print("interpolateStartvalue=");
Serial.println(interpolateStartvalue);
Serial.print("interpolateEndvalue=");
Serial.println(interpolateEndvalue);
Serial.print("interpolateLength=");
Serial.println(interpolateLength);
Serial.print("interpolateStep=");
Serial.println(interpolateStep,6);
for (int setinti=interpolateStartpos;setinti<=interpolateEndpos;setinti++) { //for all coherent elements to interpolate
height[setinti]=height[interpolateStartpos-1]+(int)(interpolateStep*interpolateStepCounter);
/*Serial.print(height[interpolateStartpos-1]);
Serial.print("+(");
Serial.print(interpolateStep);
Serial.print("*");
Serial.print(interpolateStepCounter);
Serial.print(")=");
Serial.println(height[setinti]);*/
interpolateStepCounter++;
}
interpolateStartpos=interpolateEndpos;
}
}
Serial.println();
for (int i=0;i<NUMPIXELS;i++){
Serial.print(i);
Serial.print(": ");
Serial.println(height[i]);
}
previewHeightmap(2000);
spawnWagon(); spawnWagon();
spawnWagon(); spawnWagon();
} }
void previewHeightmap(){ void previewHeightmap(int waittime){
for (int i=0;i<NUMPIXELS;i++){ for (int i=0;i<NUMPIXELS;i++){
//uint32_t c=Wheel(height[i]*255/45); //uint32_t c=Wheel(height[i]*255/45);
uint8_t b=height[i]*255/MAXHEIGHT; uint8_t b=height[i]*255.0/MAXHEIGHT;
uint32_t c=strip.Color(255-b,b,0); uint32_t c=strip.Color(255-b,b,0);
if (height[i]==255){
c=strip.Color(0,0,0);
}
strip.setPixelColor(i,c); strip.setPixelColor(i,c);
} }
strip.show(); strip.show();
delay(3000); delay(waittime);
} }
void spawnWagon(){ 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
Wagon tmpr = Wagon(maxid++,NUMPIXELS,&strip, height, random(30, 200), 6, random(0.2, 10)/10.0,0); //spawn new wagon // pos, wagonlength, startvel, startacc, wagonmass, wagoncolor
Wagon tmpr = Wagon(maxid++,NUMPIXELS,&strip, height, random(0, 20), random(3,20), random(0.2, 50)/10.0, 0 , random(5,40) , Wheel(random(0,255))); //spawn new wagon
wagon_arr.push_back(tmpr); wagon_arr.push_back(tmpr);
Serial.println("Spawned Wagon"); Serial.println("Spawned Wagon");

View File

@ -1,7 +1,8 @@
#include "wagon.h" #include "wagon.h"
#define SLOWVELOCITY 0.05
Wagon::Wagon(int id,int numpixels, Adafruit_NeoPixel *strip,uint8_t *height,float pos, float wagonlength,float startvel,float startacc) Wagon::Wagon(int id,int numpixels, Adafruit_NeoPixel *strip,uint8_t *height,float pos, float wagonlength,float startvel,float startacc, float wagonmass, uint32_t wagoncolor)
{ {
_id = id; _id = id;
_numpixels=numpixels; _numpixels=numpixels;
@ -11,7 +12,10 @@ Wagon::Wagon(int id,int numpixels, Adafruit_NeoPixel *strip,uint8_t *height,floa
_height=height; _height=height;
_vel=startvel; _vel=startvel;
_acc=startacc; _acc=startacc;
_wagonmass=wagonmass;
_spawntime=millis(); _spawntime=millis();
_lasttimefast=millis();
_wagoncolor=wagoncolor;
} }
@ -44,7 +48,7 @@ void Wagon::updatePhysics(float updatedelayms)
//rho Massendichte luft 1,2041 //rho Massendichte luft 1,2041
//A = 1m^2 //A = 1m^2
float m=40/_wagonlength; //mass of part of a wagon float m=_wagonmass/_wagonlength; //mass of part of a wagon
_acc=0; _acc=0;
int cpos=(int)_pos; int cpos=(int)_pos;
@ -92,15 +96,34 @@ void Wagon::updatePhysics(float updatedelayms)
Serial.print(_vel); Serial.print(_vel);
Serial.print(" Acc="); Serial.print(" Acc=");
Serial.println(_acc); Serial.println(_acc);
float _testvel=_vel;
if (_testvel<0){
_testvel*=-1;
}
if (_testvel>SLOWVELOCITY){ //for despawn if slow
_lasttimefast=millis();
}
if (_pos>=_numpixels){ //Wrap around edges
_pos-=_numpixels;
}
if (_pos<0){ //warp around edges
_pos=_numpixels+_pos;
}
} }
float Wagon::getHeight(int p){ float Wagon::getHeight(int p){
if (p<0){ if (p<0){
//p=0; p=0; //straight edges
return (-p)*10; //return (-p)*10; //edges as wall
}else if(p>=_numpixels){ }else if(p>=_numpixels){
//p=_numpixels-1; p=_numpixels-1; //straight edges
return (p-_numpixels)*10; return (p-_numpixels)*10; //edges as wall
} }
return _height[p]; return _height[p];
} }
@ -126,12 +149,19 @@ void Wagon::updateGraphics()
//uint32_t c=Wheel(_height[i]/45.0*255,featherbrightness); //uint32_t c=Wheel(_height[i]/45.0*255,featherbrightness);
//uint8_t b=_height[i]*255/45; //uint8_t b=_height[i]*255/45;
uint8_t b=abs(_vel)*255.0; //uint8_t b=abs(_vel)*255.0;
uint32_t c=_strip->Color(b*featherbrightness,(255-b)*featherbrightness,0); //uint32_t c=_strip->Color(b*featherbrightness,(255-b)*featherbrightness,0);
uint32_t c=_wagoncolor;
uint8_t _r = (uint8_t)(c >> 16);
uint8_t _g = (uint8_t)(c >> 8);
uint8_t _b = (uint8_t)c;
_r*=featherbrightness;
_g*=featherbrightness;
_b*=featherbrightness;
_strip->setPixelColor(i,_r,_g,_b);
_strip->setPixelColor(i,c);
} }
} }
@ -156,7 +186,11 @@ bool Wagon::alive()
return false; return false;
}*/ }*/
if (millis()>_spawntime+30*1000){ //too old /*if (millis()>_spawntime+30*1000){ //too old
return false;
}*/
if (millis()>_lasttimefast+5000 ){ //too long too slow
return false; return false;
} }

View File

@ -7,7 +7,7 @@ class Wagon
{ {
public: public:
int _id; int _id;
Wagon(int id,int numpixels,Adafruit_NeoPixel *strip,uint8_t *height, float pos, float wagonlength, float startvel,float startacc); Wagon(int id,int numpixels,Adafruit_NeoPixel *strip,uint8_t *height, float pos, float wagonlength, float startvel,float startacc, float wagonmass, uint32_t wagoncolor);
Wagon(); Wagon();
bool operator==(const Wagon &r) const; bool operator==(const Wagon &r) const;
void updatePhysics(float updatedelayms); void updatePhysics(float updatedelayms);
@ -24,9 +24,12 @@ class Wagon
float _pos; float _pos;
float _vel; float _vel;
float _acc; float _acc;
float _wagonmass;
float _wagonlength; float _wagonlength;
uint8_t *_height; uint8_t *_height;
long _spawntime; long _spawntime;
long _lasttimefast;
uint32_t _wagoncolor;
}; };
#endif #endif