Monday, April 30, 2007

Probability

Here is the beginning of adding some nuance to my code, rather than just using constant numbers or randoms. I'm working in some probabilities to modulate the motion so that it has a more organic feel.

This function is a loop that slowly moves my cables up and down. Without the probability in the foreloop, the motion looks very machine-like and repetitive. With the addition of the probability, the movement is more fluid. Much more refinement is needed, and I hope to add this sort of logic into other places in my code.

void HoldServo(){

for (int m = 0; m < servo_num; m ++) {
randomNum = random(0,11);
if(randomNum < 1){
speed = 40;
}
else if(randomNum < 9){
speed = 10;
}
else{
speed = 1;
}

rArray[m] = speed;
pulseH[m] = pulseH[m] - (rArray[m] * UorD[m]);

if (millis() - lastPulse[m] >= refreshTime) { // pulse the servo again if the
// refresh time (20 ms) have
// passed:

digitalWrite((2*(m+1)), HIGH); // Turn each motor on, tricky math
// to skip pins, ie 2,4,6,8
delayMicroseconds(pulseH[m]); // Length of the pulse sets the
// motor position
digitalWrite((2*(m+1)), LOW); // Turn the motors off
lastPulse[m] = millis(); // save the time of the last pulse
}

if (pulseH[m] < 1600){ // if it gets too low, flip it so it
UorD[m] = -1; // heads back up
}
else if(pulseH[m] > 2400){ // if it gets too high,flip it so it
UorD[m] = 1; // heads back down
}
}
}

No comments: