Page 286 - Programming Microcontrollers in C
P. 286

Timer Operations    271

                          prior to the first TOI, plus 65535 times the number of TOIs that
                          have occurred, plus the time indicated in the input capture register.
                          With a scheme of this nature, there is essentially no limit to the length
                          of time that can be measured accurately. The resolution of this
                          measurement is the time increment of the prescaler output.
                              A velocity servo is a particularly difficult application. Remember
                          that velocity or motor speed is the derivative of position. Any derivative
                          operation is prone to noise, and the successful servo must be carefully
                          put together. For example, the equation calculated above to determine
                          the feedback term is essential to the proper operation of the system. An
                          interesting challenge is to try to duplicate the operation of the system
                          described here with a normal gain type feedback. Even though the program
                          seems to be messy, this approach will work, and most other approaches
                          give you a wildly hunting or oscillating system. The calculation of the
                          proper feedback value will avoid many of these problems.
                              Another problem area will be found. If the above FOREVER loop is
                          allowed to run without any time control, the computer will be calculating
                          a new feedback value many times before the effect of an earlier
                          calculation will be seen in the system performance. This operation again
                          causes the system to become unstable, and usually the system never
                          finds the final value but hunts over some wide range around it. This
                          operation can be corrected by slowing the rate at which feedback
                          corrections are calculated. In particular, several rates have been tested,
                          and it was found that a stable, accurate system would be obtained with
                          a new feedback value calculated each quarter of a second. This time
                          control is shown in the listing below. The main application program is
                          broken into two parts: the first part is the code that will be executed
                          whenever the motor speed is changed and the second part is the code
                          that will alter the PWM_count which is the feedback portion of the
                          program. The first portion is executed under the control of an if statement

                   if(old_motor_speed!=motor_speed)
                              There is no means to change the motor speed in this program, and
                          this change will be introduced in the next section. Therefore, it is
                          expected that the portion of the code controlled by this if statement
                          will execute the first time the program is executed, and it should never
                          be executed again until the system is reset. The second if statement

                   if(tick)
   281   282   283   284   285   286   287   288   289   290   291