Don't ad-block us - support your favorite websites. We have safe, unobstrusive, robotics related ads that you actually want to see - see here for more.
0 Members and 1 Guest are viewing this topic.
Anthing with 12 i/o pins or more should work for a 12 servo hexapod. pwm isnt strictly necessary, i would even edge away from it for servo control
#define SERVO_CONTROL1 pin_B0#define SERVO_CONTROL 2 pin_B1timer0 = 0; (increment us)int servo1starttime = 0; //what the timer value is when the last phase was issuedint servo1positiontime = 1500; //what the timer value is to set the position (1500)us for centered boolean servo1ishigh = false; //is the servo in a high state or a low state?int servo2starttime = 0;int servo2positiontime = 1500;boolean servo2ishigh = false;int allservolowtime = 20000 //as in they should all be low for this amount of time(us)void main(){ while(1){ setupServo's();//havent written this, but assign times to the servo's for how long they need to be high for changeStates(); }}void changeStates(){ if (servo1ishigh == false && (timer0-servo1starttime) >= allservolowtime){ servo1ishigh =false; servo1starttime = timer0; } if (servo1ishigh == true && (timer0-servo1starttime) >= servo1positiontime){ servo1ishigh =true; servo1starttime = timer0; } if (servo2ishigh == false && (timer0-servo2starttime) >= allservolowtime){ servo2ishigh =false; servo2starttime = timer0; } if (servo2ishigh == true && (timer0-servo2starttime) >= servo2positiontime){ servo2ishigh =true; servo2starttime = timer0; }}
Thats why pwm isnt recommended, you control the high pulse to set the servo's position (between 1 - 2 ms), but the low pulse becomes exactly the same length of the high pulse. The low pulse should be around 10ms - 20ms.As you can see even at the 2ms high pulse extreme, the standard pwm square wave would produce also a 2ms low. This is 8ms shorter than what it should be. You are not giving the servo a chance to do its before you send the next high pulse resulting in erratic behaviour and juddery movements.
I haven't done it myself, but in theory this is how you do it:1) Store the desired pulse time for each servo into an array.2) Sort the array from lowest time to highest time.3) reset timer to 04) command all servo pins as high5) set a timer interrupt to trigger at the first/next value in the array6) upon receiving an interrupt, command that servo pin as low7) repeat steps 5 and 6 until all servo pins go lowIn theory the entire process will take about 2ms . . . the problem however is what happens when servos share the same time . . .If you do the delay method on 18 servos . . . and you put 6 on PWM . . . that leaves 12 servos running an average of 1.5ms each, or 18ms. Even if it takes 50ms you probably won't notice the difference . . .