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

122     Bu il d  Y o ur  O w n  Q u a d c o p t e r


                                I found the meaning of this magic number in the website http://propeller.wikispaces
                             .com/Full+Duplex+Serial. The 0 in the argument list represents the operating mode for the
                             full duplex serial object. Below is the wiki documentation regarding the mode:

                                 .start(rxpin, txpin, mode, baudrate)

                                   Start serial driver—starts a cog
                                   mode bit 0 = invert rx
                                   mode bit 1 = invert tx
                                   mode bit 2 = open-drain/source tx
                                   mode bit 3 = ignore tx echo on rx

                                I guess it is just the teacher in me, but I would prefer to eliminate all magic numbers and
                             instead modify this code to include a constant definition as follows:

                                  In the CON section add

                             mode = 0
                                  and in the PUB section modify

                             fdx.start(rxPin, txPin, mode, baud)     ‘start serial driver to PC
                                You see how much clearer it all becomes, although it is true that you will have to go back
                             into the documentation to discover exactly how the mode works. Please refer to my rant in
                             the previous chapter regarding magic numbers. Now back to the analysis.
                                There is a “repeat – until” loop checking for a character from the PC. This code snippet
                             is shown below:

                               repeat
                                 ch := fdx.rx
                               until ch := “ ”

                                All you need to do is press the space bar in order to enable you to enter the desired
                             power level. The motor always starts at the 0 power level meaning, no rotation. The Spin
                             program will then launch a new cog after the initial position value (80,000 in this case) is
                             stored. The following code snippet creates this new cog object:

                             cognew(@SingleServo,@position)

                                This statement  creates a new cog that runs code  beginning at the symbolic address
                             “SingleServo,” which is also the start of an assembly language program. The statement
                             also instructs the cog to load, at boot time, the value stored in the hub RAM location named
                             “position” into its “PAR” special purpose register (SPR). I introduced the SPRs at the end of
                             Chapter 4, but I did not really elaborate on them. The PAR SPR is the so-called boot register,
                             which means that a value will be stored in it when the cog is created and/or rebooted when
                             it is executing assembly code. You should note that creating a cog that is designated to
                             execute assembly language is substantially different from creating a cog to run a method
                             in  another  Spin object,  which  was  the  situation  in  all  of  the  Chapter  4 examples.  The
   138   139   140   141   142   143   144   145   146   147   148