Tuesday 25 December 2012

Receiving Info From the Servo Motors

      Since we successfully sent commands to the controller using the sliders, our next endeavour was to get feedback on the actual position of the servos as they move, using the commands Q & QP. The format of the Q command is "#(servo) Q", and it will send back an ASCII '+' or '.'. The plus means that the servo is currently in motion, while the period means the servo is stopped. The QP command takes the form of "QP(servo)", and will return the pulse width that the servo has been delivered. Using these 2 commands, we can track the servo as it moves.


      We added a text box to the right of slider 4 in our control window and modified our code to have the program show the current pulse width as the slider and the servo both move.
     Our first attempt at coding this was to add a subroutine called “getPulseWidth” that sent the SCC-32 the QP command for servo 4, then read the response and put it in the text box. This operated on roughly the same lines as how we sent sent the move commands to the arm, and then it used the 'read' command to get the signal that was sent back. Because the SSC-32 can sometimes take a few milliseconds to respond, we included a timer that pauses the program for 6 milliseconds to account for that. The program then converts the response to to an integer, and places it in the text box.


      This worked, but we wanted something that would constantly display the pulse width as it changed, not just once when the slider was moved. To do this, we used a loop to call “getPulseWidth” multiple times, constantly updating the text box. It kept getting the value but introduced a few other problems. Since that the loop was constantly running, it became somewhat of a power-hog, and was using processing power when we didn't need it to. This caused large amounts of lag, slowing down our program.

          To fix this,we used a special timer that would trigger every 5th of a second and check the pulse width. This worked perfectly but the timer was still running. We fixed this with the “Q” command, so that the timer when the servo stopped moving. It works by sending the Q command to the arm, reading the response, and if it's a period, meaning that the arm has stopped, it kills the timer.


      We also did one last check of the pulse when the arm had stopped to make sure we got a final position value. This ensured that the position in the text box was correct. We also had to add a couple of lines to kill the timer before we started it just to make sure that the timer was completely reset.




Here's a video showing the final program with the pulse value being updated as the servo moves:




No comments:

Post a Comment