L'Hexapod: Extending the servo controller

Previously published

This article was previously published on lhexapod.com as part of my journey of discovery into robotics and embedded assembly programming. A full index of these articles can be found here.

To be able to implement the new commands for the servo controller I need to adjust the data that we use to control the servos. Right now we have a single byte per servo and that byte contains a value between 0 and 254 which represents the length of the pulse sent to the servo. To be able to implement the delayed or progressive move command we’ll need to know the current servo position and the desired servo position. We’ll also need to know how often we move the current position towards the desired position and by how much we move it.

So, for example, if we’re currently at position 0 and we want to move to position 150 and we want to do over a period of 3 seconds we could move at a rate of 1 every cycle; that is one step of one every 20ms. Each time the PWM signal for this servo is refreshed we would increment the actual position value until we reach the desired position.

As soon as we change the step rate from 1 we need another byte of control data to keep track of when we should step next. For example, for less fluid movement we might want to move from 0 to 150 in steps of 5. If we wanted to do that in the same time as before, i.e. over a period of 3 seconds, we would need to step every 5 cycles (10 times a second). So we’d set a desired position of 150, a step size of 5 and a step rate of 5. Every five times the PWM signal is refreshed we would increment the actual position by 5 until we reach the desired position. To keep track of when to increment we’d have a step count value which we’d initially set to the step rate then then decrement each time we refresh until it reaches zero at which point we’d increment the actual position and set the step count back to 5.

All of the planned new servo commands can be implemented in terms of these control values. The problem is that we now need 5 bytes of control data per servo and that restricts the number of servos that we can support with an ATTiny2313 to 18; which is just about enough…I expect that I’ll implement these features on the ATTiny2313 and then switch to the ATMega168 if I need more channels or more functionality.