Page 142 - Build Your Own Quadcopter_ Power Up Your Designs with the Parallax Elev-8
P. 142

Chapter 5: Quadcopter Propulsors           121


                                 Software is furnished to do so, subject to the following conditions: The above copyright notice and
                                 this permission notice shall be included in all copies or substantial portions of the Software. The
                                 software is provided as is, without warranty of any kind, express or implied, including but not
                                 limited to the warranties of non-infringement.
                                   In no event shall the author or copyright holder be liable for any claim, damages or other
                                 liability, out of or in connection with the software or the use or other dealings in the software.


                             Notes
                             This program demonstrates how to control a single R/C servomotor by dedicating cog-to-
                             output signal pulses, using a simple assembly program. Once the assembly program is loaded
                             into a new cog, it continuously checks the value of the “position” variable in the main RAM
                             (the value of which can be changed at any time by code running on any other cog) and creates
                             a steady stream of signal pulses with a high part that is equal to the value of the “position”
                             variable times the clock period (1/80 MHz) in length and a low part that is 10 ms in length.
                             (This low part may need to be changed to 20 ms depending on the brand of motor being used,
                             but 10 ms seems to work fine for Parallax/Futaba Standard Servos and gives a quicker
                             response time than 20 ms.) With an 80-MHz system clock, the servo-signal’s pulse resolution
                             is between 12.5–50 ns; however, the control circuitry inside most analog servomotors probably
                             will not be able to distinguish between such small changes in the signal.
                                To use the above code in your own Spin code, simply declare a “position” variable as a
                             long, start the assembly code running in a cog with the “cognew(@SingleServo,@
                             position) line, and copy and paste my DAT section into the DAT section of your own code.
                             Note that you must change the number “7” in the ServoPin constant declaration in the
                             assembly code to select a pin other than Pin 7 to be the output pin for the servo signal.
                                If you are using a Parallax/Futaba Standard Servo, the range of signal-pulse widths is
                             typically between 0.5–2.25 ms, which corresponds to “position” values between 40_000 (full
                             clockwise) and 180_000 (full counterclockwise). In theory, this provides you with 140_000
                             units of "position" resolution across the full range of motion. You may need to experiment
                             with changing the “position” values a little to take advantage of the full range of motion for
                             the specific R/C servo motor that you are using. However, you must be careful not to force
                             the servo to try to move beyond its mechanical stops. If you find that your propeller chip or
                             servomotor stops working for no apparent reason, it could be that the motor is sending
                             inductive spikes back into the power supply or it is simply drawing too much current and
                             resetting the propeller chip. Adding a large capacitor (e.g.,1000 uF) across the power leads of
                             the servo motor, or using separate power sources for the propeller chip’s 3.3-V regulator and
                             the servomotor’s power supply will help to fix this.
                                The Spin portion of the program uses the FullDuplexSerial object that sets up a means
                             by which the user can input data via the keyboard as well as view data on the PC screen.
                             The PSerT program must first be running on the PC, as was discussed above, and then
                             the FullDuplexSerial object will take over all the duties of communicating between the
                             program and the PC. The FullDuplexSerial object has the local reference name of  fdx,
                             which is short for full duplex. The fdx object is preset for a 115,200 baud rate as well as
                             using the normally designated Prop chip pins 31 for receive and 32 for transmit. These pin
                             designations are standard on all Parallax Propeller development boards and match the
                             Prop chip's functions.
                                I would like to point out the presence of a “magic number” in the Spin statement that
                             starts the fdx object:

                             fdx.start(rxPin,txPin,0,baud)      ‘start serial driver to PC
   137   138   139   140   141   142   143   144   145   146   147