2024-09-11 09:40:46 +00:00
|
|
|
#include "Arduino.h"
|
|
|
|
#include "behaviour.h"
|
|
|
|
|
|
|
|
SimplexNoise sn;
|
|
|
|
|
|
|
|
|
|
|
|
Mood mood;
|
|
|
|
|
|
|
|
float matrix_weights_main[SERVO_COUNT_MAIN][WEIGHT_COUNT]; //mapping outputs, sensors/moods
|
|
|
|
float matrix_weights_peripheralL[SERVO_COUNT_PERIPHERAL][WEIGHT_COUNT];
|
|
|
|
float matrix_weights_peripheralR[SERVO_COUNT_PERIPHERAL][WEIGHT_COUNT];
|
|
|
|
|
2024-09-11 15:15:34 +00:00
|
|
|
void updateMatrixWeights(unsigned long millis,bool bellyup, bool body_present, bool contact_main, bool contact_peripheralL, bool contact_peripheralR, float pitch, float roll){
|
|
|
|
|
2024-09-11 09:40:46 +00:00
|
|
|
|
|
|
|
static unsigned long last_weights_update=0;
|
|
|
|
if (millis-last_weights_update>WEIGHT_UPDATE_INTERVAL) {
|
|
|
|
last_weights_update=millis;
|
|
|
|
|
|
|
|
//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
|
|
|
|
|
|
|
|
/*float map_mode_noise=constrain(mapfloat(mood.wakefulness,0,0.2, 0,1),0,1);
|
|
|
|
float map_mode_rollpitch=constrain(mapfloat(mood.shakiness,0,0.5, 0,1),0,1);
|
|
|
|
float map_mode_slither=0;
|
|
|
|
float map_mode_sleeping=mood.loneliness;*/
|
|
|
|
|
2024-09-11 15:15:34 +00:00
|
|
|
|
|
|
|
static float map_mode_rollpitch=0;
|
|
|
|
static float map_mode_walking=0;
|
|
|
|
static float map_mode_sleeping=0;
|
|
|
|
static float map_mode_noise=0;
|
2024-09-11 09:40:46 +00:00
|
|
|
|
2024-09-11 15:15:34 +00:00
|
|
|
|
2024-09-11 09:40:46 +00:00
|
|
|
|
2024-09-11 15:15:34 +00:00
|
|
|
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;
|
|
|
|
|
|
|
|
}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;
|
|
|
|
}
|
|
|
|
}
|
2024-09-11 09:40:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2024-09-11 15:15:34 +00:00
|
|
|
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);
|
|
|
|
|
2024-09-11 09:40:46 +00:00
|
|
|
|
2024-09-11 15:15:34 +00:00
|
|
|
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();
|
2024-09-11 09:40:46 +00:00
|
|
|
|
|
|
|
|
2024-09-11 15:15:34 +00:00
|
|
|
float map_sum=map_mode_walking+map_mode_rollpitch+map_mode_noise;
|
|
|
|
map_mode_walking/=map_sum;
|
|
|
|
map_mode_rollpitch/=map_sum;
|
2024-09-11 09:40:46 +00:00
|
|
|
map_mode_noise/=map_sum; //divide mapping so the sum will be 1.0
|
|
|
|
|
2024-09-11 15:15:34 +00:00
|
|
|
|
|
|
|
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;i<SERVO_COUNT_MAIN;i++) {
|
|
|
|
for (uint8_t w=0;w<WEIGHT_COUNT;w++) {
|
|
|
|
matrix_weights_main[i][w]=0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (uint8_t i=0;i<SERVO_COUNT_PERIPHERAL;i++) {
|
|
|
|
for (uint8_t w=0;w<WEIGHT_COUNT;w++) {
|
|
|
|
matrix_weights_peripheralL[i][w]=0;
|
|
|
|
matrix_weights_peripheralR[i][w]=0;
|
|
|
|
}
|
|
|
|
}
|
2024-09-11 09:40:46 +00:00
|
|
|
|
|
|
|
//Mode Noise
|
2024-09-11 15:15:34 +00:00
|
|
|
matrix_weights_main[0][W_NOISE]+=30.0 *map_mode_noise;
|
|
|
|
matrix_weights_main[0][W_NOISESLOW]+=0.0 *map_mode_noise;
|
2024-09-11 09:40:46 +00:00
|
|
|
|
2024-09-11 15:15:34 +00:00
|
|
|
matrix_weights_main[1][W_NOISE]+=30.0 *map_mode_noise;
|
|
|
|
matrix_weights_main[1][W_NOISESLOW]+=20.0 *map_mode_noise;
|
2024-09-11 09:40:46 +00:00
|
|
|
|
2024-09-11 15:15:34 +00:00
|
|
|
matrix_weights_main[2][W_NOISE]+=20.0 *map_mode_noise;
|
|
|
|
matrix_weights_main[2][W_NOISESLOW]+=30.0 *map_mode_noise;
|
2024-09-11 09:40:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//Mode Roll Pitch
|
2024-09-11 15:15:34 +00:00
|
|
|
matrix_weights_main[0][W_PITCH]+=1.0* map_mode_rollpitch;
|
|
|
|
matrix_weights_main[1][W_ROLL]+=1.0* map_mode_rollpitch;
|
|
|
|
matrix_weights_main[2][W_PITCH]+=1.0* map_mode_rollpitch;
|
|
|
|
|
|
|
|
//Mode Walking
|
|
|
|
matrix_weights_main[0][W_NOISESLOW]+=5 *map_mode_walking;
|
|
|
|
matrix_weights_main[0][W_SIN]+=10 *map_mode_walking;
|
|
|
|
matrix_weights_main[0][W_OFFSET]+=-60 *map_mode_walking;
|
|
|
|
matrix_weights_main[1][W_NOISESLOW]+=30 *map_mode_walking;
|
|
|
|
matrix_weights_main[2][W_COS]+=-20 *map_mode_walking;
|
|
|
|
matrix_weights_main[2][W_OFFSET]+=70 *map_mode_walking;
|
|
|
|
|
2024-09-11 09:40:46 +00:00
|
|
|
|
|
|
|
//Mode Sleeping
|
2024-09-11 15:15:34 +00:00
|
|
|
matrix_weights_main[0][W_NOISESLOW]+=20 *map_mode_sleeping;
|
|
|
|
matrix_weights_main[1][W_NOISESLOW]+=20 *map_mode_sleeping;
|
|
|
|
matrix_weights_main[2][W_NOISESLOW]+=30 *map_mode_sleeping;
|
2024-09-11 09:40:46 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
float sleeping=constrain(mapfloat(mood.wakefulness,0.005,0.1, 0,1),0,1);
|
|
|
|
for(int i = 0; i < SERVO_COUNT ; i++){
|
|
|
|
matrix_weights[i][W_PITCH]*=sleeping;
|
|
|
|
matrix_weights[i][W_ROLL]*=sleeping;
|
|
|
|
matrix_weights[i][W_NOISE]*=sleeping;
|
|
|
|
matrix_weights[i][W_NOISESLOW]*=constrain(sleeping,0.1,1);
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-09-11 15:15:34 +00:00
|
|
|
void updateServosByWeights(unsigned long millis,float pitch,float roll){
|
2024-09-11 09:40:46 +00:00
|
|
|
static unsigned long last_servoweights_update=0;
|
|
|
|
if (millis-last_servoweights_update>SERVOWEIGHT_UPDATE_INTERVAL)
|
|
|
|
{
|
|
|
|
last_servoweights_update=millis;
|
|
|
|
static unsigned long millis_add;
|
|
|
|
#define MILLISADD_MAX 100
|
|
|
|
millis_add+=constrain(mapfloat(mood.shakiness, 0.05,1 ,0,MILLISADD_MAX),0,MILLISADD_MAX);
|
|
|
|
|
|
|
|
|
|
|
|
float vsin=sin((millis()+millis_add)/1000.0);
|
|
|
|
float vcos=cos((millis()+millis_add)/1000.0);
|
|
|
|
|
|
|
|
for (uint8_t i=0;i<SERVO_COUNT;i++) {
|
|
|
|
uint8_t servoPosInMatrix=0;
|
|
|
|
|
|
|
|
|
|
|
|
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
|
2024-09-11 15:15:34 +00:00
|
|
|
|
|
|
|
pitch=constrain(pitch,-80,80);
|
|
|
|
roll=constrain(roll,-80,80);
|
2024-09-11 09:40:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
float angle=0;
|
|
|
|
|
|
|
|
float lookdirection=(servos[i].angle>=0)?1:-1; //if angle positive, 1, else 0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (i<SERVO_COUNT_MAIN) { //Main
|
|
|
|
/*if (i==0){
|
|
|
|
Serial.print("servoangle:");Serial.println(servos[i].angle);
|
|
|
|
Serial.print("lookdirection:");Serial.println(lookdirection);
|
|
|
|
Serial.print("weight:");Serial.println(matrix_weights_main[i][W_LOOKDIRECTION]);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}*/
|
|
|
|
|
|
|
|
servoPosInMatrix=i;
|
|
|
|
angle=
|
|
|
|
pitch*matrix_weights_main[servoPosInMatrix][W_PITCH]
|
|
|
|
+roll*matrix_weights_main[servoPosInMatrix][W_ROLL]
|
|
|
|
+pnoise*matrix_weights_main[servoPosInMatrix][W_NOISE]
|
|
|
|
+pnoiseslow*matrix_weights_main[servoPosInMatrix][W_NOISESLOW]
|
|
|
|
+vsin*matrix_weights_main[servoPosInMatrix][W_SIN]
|
|
|
|
+vcos*matrix_weights_main[servoPosInMatrix][W_COS]
|
2024-09-11 15:15:34 +00:00
|
|
|
+lookdirection*matrix_weights_main[servoPosInMatrix][W_LOOKDIRECTION]
|
|
|
|
+1.0*matrix_weights_main[servoPosInMatrix][W_OFFSET];
|
2024-09-11 09:40:46 +00:00
|
|
|
|
|
|
|
}else if(i<SERVO_COUNT_MAIN+SERVO_COUNT_PERIPHERAL) { //Peripheral Left
|
|
|
|
servoPosInMatrix=i-3;
|
|
|
|
}else if(i<SERVO_COUNT_MAIN+2*SERVO_COUNT_PERIPHERAL) { //Peripheral Right
|
|
|
|
servoPosInMatrix=i-2*3;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
servos[i].angle=angle;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
float mapfloat(float x, float in_min, float in_max, float out_min, float out_max)
|
|
|
|
{
|
|
|
|
return (float)(x - in_min) * (out_max - out_min) / (float)(in_max - in_min) + out_min;
|
|
|
|
}
|
|
|
|
|