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

Chapter 6: Radio-Controlled Systems and Telemetr y             147


                             OBJ
                               sig: “PWM2C_SIGEngine.spin”
                               com: “RS232_COMEngine.spin”    ‘not discussed – for serial comm
                               str: “ASCII0_STREngine.spin”   ‘not discussed – for text
                                                                  manipulation

                             PUB demo
                               ifnot( com.COMEngineStart(_receiverPin, _transmitterPin,
                             _baudRateSpeed) and { } sig.SIGEngineStart(_leftServoPin,
                             _rightServoPin, 100))
                                 reboot

                               repeat
                                 com.transmitString(string(“Left: “))
                                 com.transmitString(1 + str.integerToDecimal(sig.
                                      leftPulseLength, 10))
                                 com.transmitString(string(_clearToEndOfLineCharacter,
                                      _newLineCharacter, “Right: “))
                                 com.transmitString(1 + str.integerToDecimal(sig.
                                      rightPulseLength, 10))
                                 com.transmitString(string(_clearToEndOfLineCharacter,
                                      _homeCursorCharacter, “Servo Pulse Length’s”,
                                      _newLineCharacter))

                                This program is the driver that uses an “engine” object to perform the pulse-width
                             measurement and then reports the results back to the PSerT application for user display.
                             The heart of this program is the forever-repeating loop that sends a series of strings over
                             to the PSerT. The pulse-length values are acquired in the two object-method calls
                             sig.leftPulseLength and  sig.rightPulseLength. The  sig object is a reference
                             to a program named PWM2C_SIGEngine, which is listed below in an abbreviated manner.
                             I did add some comments to key code lines to help explain what was happening in
                             the code.

                             VAR
                               long leftLength, rightLength, stack[7]
                               byte cogNumber, leftPinNumber, rightPinNumber, timeoutPeriod

                             PUB leftPulseLength ‘’ 3 Stack Longs          ‘ Returns the servo

                                                                          microseconds.
                               return leftLength

                             PUB rightPulseLength ‘’ 3 Stack Longs         ‘ Returns the servo

                                                                          microseconds.
                               return rightLength
   163   164   165   166   167   168   169   170   171   172   173