diff --git a/prosthesis_controller/behaviour.cpp b/prosthesis_controller/behaviour.cpp index 4730949..2b89823 100644 --- a/prosthesis_controller/behaviour.cpp +++ b/prosthesis_controller/behaviour.cpp @@ -10,28 +10,13 @@ float matrix_weights_main[SERVO_COUNT_MAIN][WEIGHT_COUNT]; //mapping outputs, se float matrix_weights_peripheralL[SERVO_COUNT_PERIPHERAL][WEIGHT_COUNT]; float matrix_weights_peripheralR[SERVO_COUNT_PERIPHERAL][WEIGHT_COUNT]; -void updateMatrixWeights(unsigned long millis,bool body_present, bool contact_main, bool contact_peripheralL, bool contact_peripheralR, float pitch, float roll){ - mood.loneliness=1.0; +void updateMatrixWeights(unsigned long millis,bool bellyup, bool body_present, bool contact_main, bool contact_peripheralL, bool contact_peripheralR, float pitch, float roll){ + static unsigned long last_weights_update=0; if (millis-last_weights_update>WEIGHT_UPDATE_INTERVAL) { last_weights_update=millis; - static float contact_main_persist=0; - if (contact_main) { - contact_main_persist=1.0; - }else{ - if (contact_main_persist>0) { - contact_main_persist-=0.2*WEIGHT_UPDATE_INTERVAL/1000.0; - }else{ - contact_main_persist=0; - } - } - - //0=pitch - //1=roll - - //float map_mode=constrain(mapfloat(mood.wakefulness,0,0.2, 1,0),0,1); //0=pitchroll control, 1=noise //float map_mode=constrain(mapfloat(mood.shakiness,0,0.2, 1,0),0,1); //0=pitchroll control, 1=noise @@ -40,59 +25,95 @@ void updateMatrixWeights(unsigned long millis,bool body_present, bool contact_ma float map_mode_slither=0; float map_mode_sleeping=mood.loneliness;*/ + + static float map_mode_rollpitch=0; + static float map_mode_walking=0; + static float map_mode_sleeping=0; + static float map_mode_noise=0; - bool state_bellyup=false; - bool state_map_mode_detached=false; - bool state_map_mode_attached=false; + - float map_mode_rollpitch=0; - float map_mode_slither=0; - float map_mode_sleeping=0; + if (body_present) { + map_mode_rollpitch+=0.3 *WEIGHT_UPDATE_INTERVAL/1000.0; + map_mode_walking-=0.5 *WEIGHT_UPDATE_INTERVAL/1000.0; + map_mode_noise-=0.2 *WEIGHT_UPDATE_INTERVAL/1000.0; - - float contact_main_smooth=constrain(mapfloat(contact_main_persist,0.0,0.3, 0.0,1.0), 0.0,1.0); - float map_mode_noise=1.0-contact_main_smooth; //idle movement - float map_mode_scared=0.0+contact_main_smooth; //contracted position + }else{ + map_mode_rollpitch-=.2 *WEIGHT_UPDATE_INTERVAL/1000.0; + if (bellyup) { + map_mode_walking+=0.3 *WEIGHT_UPDATE_INTERVAL/1000.0; + map_mode_noise-=.2 *WEIGHT_UPDATE_INTERVAL/1000.0; + }else{ //sitting on a level survace + map_mode_walking-=0.1 *WEIGHT_UPDATE_INTERVAL/1000.0; + map_mode_noise+=0.3*WEIGHT_UPDATE_INTERVAL/1000.0; + } + } + map_mode_walking=constrain(map_mode_walking,0.0,1.0); + map_mode_rollpitch=constrain(map_mode_rollpitch,0.0,1.0); + map_mode_noise=constrain(map_mode_noise,0.0,1.0); + + Serial.print("Walking: \t"); Serial.println(map_mode_walking); + Serial.print("RollPitch: \t"); Serial.println(map_mode_rollpitch); + Serial.print("Noise: \t"); Serial.println(map_mode_noise); + Serial.println(); - float map_sum=map_mode_noise+map_mode_scared; + float map_sum=map_mode_walking+map_mode_rollpitch+map_mode_noise; + map_mode_walking/=map_sum; + map_mode_rollpitch/=map_sum; map_mode_noise/=map_sum; //divide mapping so the sum will be 1.0 - map_mode_scared/=map_sum; + + analogWrite(PIN_VIBRATION,contact_main*map_mode_noise*64); + Serial.print("Vibrt:"); Serial.println(contact_main*map_mode_noise*160); + + //reset all weights + for (uint8_t i=0;iSERVOWEIGHT_UPDATE_INTERVAL) { @@ -116,8 +137,6 @@ void updateServosByWeights(unsigned long millis){ #define MILLISADD_MAX 100 millis_add+=constrain(mapfloat(mood.shakiness, 0.05,1 ,0,MILLISADD_MAX),0,MILLISADD_MAX); - float pitch=0; - float roll=0; float vsin=sin((millis()+millis_add)/1000.0); float vcos=cos((millis()+millis_add)/1000.0); @@ -128,6 +147,9 @@ void updateServosByWeights(unsigned long millis){ float pnoise=sn.noise((millis()+millis_add)/5000.0,i*10); //simplexnoise -1 to 1 float pnoiseslow=sn.noise((millis()+millis_add)/50000.0,i*10); //simplexnoise -1 to 1 + + pitch=constrain(pitch,-80,80); + roll=constrain(roll,-80,80); float angle=0; @@ -155,7 +177,8 @@ void updateServosByWeights(unsigned long millis){ +pnoiseslow*matrix_weights_main[servoPosInMatrix][W_NOISESLOW] +vsin*matrix_weights_main[servoPosInMatrix][W_SIN] +vcos*matrix_weights_main[servoPosInMatrix][W_COS] - +lookdirection*matrix_weights_main[servoPosInMatrix][W_LOOKDIRECTION]; + +lookdirection*matrix_weights_main[servoPosInMatrix][W_LOOKDIRECTION] + +1.0*matrix_weights_main[servoPosInMatrix][W_OFFSET]; }else if(i