There are probably some others up on Trossens forum that use the Arbotix board more than I do and have more experience than I do with the Atmega644p and can answer some specific questions about the process better than I can. But as I am more or less the owner of the Phantom Phoenix code base. I will give you some hints on what I would try.
Timer Interrupts: If you wish to go this way. Look at the AVR ATMega 644p reference Manual (pdf) at the timers chapters. In the PDF I have this is chapters 12-14. This will show you the register names and values and the like. I would then look at examples of the code that use these. Example the servo library as an example…
However I personally would not go down that route as I think this will cause your Phantom to not move smoothly. Why? Most code that I have seen does not use the firmware of the servos to do the timed moves from a current location to a new location. In theory you can do this using the speed settings, but I never got this to work well for me. (Note: KevinO up on Trossen and here has had some better luck but that is not in this code base). So instead I use a slightly modified version the bioloid library Interpolate functions. (BioloidControllerEx::interpolateSetup and int BioloidControllerEx::interpolateStep(boolean fWait). In particular my version of the library decides if it will wait for the next step time or not.
So in order for the Bioloid interpolation to work smoothly, it needs to be called at the correct times, for it to calculate the next place the servos are supposed to be and to then output the commands to update the goal positions for each of the servos. So suppose that you create an interrupt for lets say Timer2, while walking and you try to do the function you showed from the ISR. You will run into lots of issues.
- Timing for the next cycle(s) can and likely to be screwed up.
- If you do your function from the ISR - by default interrupts are disabled in the ISR, and the default AX library uses interrupts to receive characters. So likely your ax12GetRegister will hang.
- You might be able to enable interrupts again within your ISR, but SoftwareSerial disables Interrupts when it sends and as it’s ISR does not enable interrupts, during the reception of a character). So depending on other things, you can loose interrupts.
I could go into more on this part if that would help.
What I would try is: To handle the periodic updates of servos, I added some optional code into the servo drivers of the Arduino Phoenix code base, which is enabled on the PhantomX. Not sure which version of the code base you are using. But if you are using the self contained one (Phantom_Phoenix project up on Github?) Look at the _Phoenix_Driver_AX12.h file. If you are using my in parts project (drop the leading _).
If you look at the function: void ServoDriver::BackgroundProcess(void)
You will see that I call off to the Bioloid code to output the servos. Also my more recent versions shows calling out to a servo to get it’s battery voltage at a time that should not interfere with the timings.
So I would do yours the same way. That is I would add it to this function and/or a call of to your function, that checks to see if it is time to do something…
I would also try to decide if I really needed to ask the servo for it’s position (as that takes sending out a packet and waiting for a response). I would probably look at using the function:
int BioloidControllerEx::getCurPose(int id)
To retrieve the position I last output, knowing that we are only doing very fine adjustments. However if you really need the actual servo position for things like detecting obstructions or the like, well you can do.
Also I would try to break this up. That is requesting data from all 18 servos and then bitbanging the data back to other processor will take time and will probably again interfere with the next bioloid time frame. So you then decide to get the state of one servo per cycle and see if this gets fast enough response for you. Or you can try 2 or 3 and see if this is still working OK.
At least that is how I would try it out on the Arbotix. Personally I am having more fun using things like the Teensy 3.1 that has 4 UARTS (including USB) and on this one I would probably use an Interval timer. Would still want to be smart about not interfering with the updates for the interpolation…
Hope that helps
Kurt