Files
darkwater_cplus_escape/examples/PPM-decoder/PPM.cpp
shrkey d4fff8f253 grr
2016-10-17 16:56:02 +01:00

121 lines
4.0 KiB
C++

/*
Example code is placed under the BSD license.
Written by Team Dark Water (team@darkwater.io) - based on original by Emlid
Copyright (c) 2014, Emlid Limited. All rights reserved.
Written by Mikhail Avkhimenia (mikhail.avkhimenia@emlid.com)
twitter.com/emlidtech || www.emlid.com || info@emlid.com
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the Emlid Limited nor the names of its contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL EMLID LIMITED BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <pigpio.h>
#include <stdio.h>
#include <unistd.h>
#include "darkwater/DWESCAPE.h"
#include "darkwater/Util.h"
#include <stdlib.h>
//================================ Options =====================================
unsigned int samplingRate = 1; // 1 microsecond (can be 1,2,4,5,10)
unsigned int ppmInputGpio = 4; // PPM input on Navio's 2.54 header
unsigned int ppmSyncLength = 4000; // Length of PPM sync pause
unsigned int ppmChannelsNumber = 8; // Number of channels packed in PPM
unsigned int servoFrequency = 50; // Servo control frequency
bool verboseOutputEnabled = true; // Output channels values to console
//============================ Objects & data ==================================
using namespace DarkWater;
DWESCAPE *dw;
DW_Servo *servos[6];
float channels[8];
//============================== PPM decoder ===================================
unsigned int currentChannel = 0;
unsigned int previousTick;
unsigned int deltaTime;
void ppmOnEdge(int gpio, int level, uint32_t tick)
{
if (level == 0) {
deltaTime = tick - previousTick;
previousTick = tick;
if (deltaTime >= ppmSyncLength) { // Sync
currentChannel = 0;
// RC output
for (int i = 0; i < ppmChannelsNumber; i++)
servos[i]->setPWMuS( channels[i] );
// Console output
if (verboseOutputEnabled) {
printf("\n");
for (int i = 0; i < ppmChannelsNumber; i++)
printf("%4.f ", channels[i]);
}
}
else
if (currentChannel < ppmChannelsNumber)
channels[currentChannel++] = deltaTime;
}
}
//================================== Main ======================================
int main(int argc, char *argv[])
{
if (check_apm()) {
return 1;
}
dw = new DWESCAPE();
dw->initialize();
dw->setFrequency(servoFrequency);
servos[0] = dw->getServo(1);
servos[1] = dw->getServo(2);
servos[2] = dw->getServo(3);
servos[3] = dw->getServo(4);
servos[4] = dw->getServo(5);
servos[5] = dw->getServo(6);
// GPIO setup
gpioCfgClock(samplingRate, PI_DEFAULT_CLK_PERIPHERAL, 0); /* last parameter is deprecated now */
gpioInitialise();
previousTick = gpioTick();
gpioSetAlertFunc(ppmInputGpio, ppmOnEdge);
// Infinite sleep - all action is now happening in ppmOnEdge() function
while(1)
sleep(10);
return 0;
}