Eduard Medla / PPM2

Dependents:   Autonomous_quadcopter

Fork of PPM by Eduard Medla

Files at this revision

API Documentation at this revision

Comitter:
edy05
Date:
Wed Sep 27 17:05:59 2017 +0000
Parent:
2:64f90dca7f36
Child:
4:380e7c35e466
Commit message:
Class PpmRegen which read PPM signal and generate PwmOut

Changed in this revision

PPMIn.cpp Show diff for this revision Revisions of this file
PPMIn.h Show diff for this revision Revisions of this file
PPMOut.cpp Show diff for this revision Revisions of this file
PPMOut.h Show diff for this revision Revisions of this file
PpmRegen.cpp Show annotated file Show diff for this revision Revisions of this file
PpmRegen.h Show annotated file Show diff for this revision Revisions of this file
--- a/PPMIn.cpp	Fri Sep 22 11:28:39 2017 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,57 +0,0 @@
-#include "mbed.h"
-#include "PPMIn.h"
-
-PpmIn::PpmIn(PinName pin): ppm(pin)
-{
-    //__disable_irq();
-    current_channel = 0;
-    state = false;
-    timer.start();
-    ppm.fall(callback(this, &PpmIn::rise));
-    
-    //__enable_irq(); 
-}
-
-void PpmIn::getAllChannels(uint16_t all_channels[])
-{
-    for(int i = 0; i < CHANNELS; i++){
-        all_channels[i] = channels[i];   
-    }           
-    
-}
-
-bool PpmIn::rise_captured(){
-    if(rise_value){
-        rise_value = false;
-        return true;
-    }
-    else{
-        return false;
-    }
-}
-            
-void PpmIn::rise()
-{
-    //__disable_irq();
-    uint16_t time = timer.read_us();
-    
-    rise_value = true;
-    
-    // we are in synchro zone
-    if(time > 2500)
-    {
-       current_channel = 0;
-       // return values 
-       state = true;
-    }
-    else
-    {
-        channels[current_channel] = timer.read_us();  
-        current_channel += 1;     
-    }
-    
-    timer.reset();
-    
-    //if (current_channel > (CHANNELS + 2 - 1)); //+frame and - 1 indexing of channels list
-    //__enable_irq();
-}
\ No newline at end of file
--- a/PPMIn.h	Fri Sep 22 11:28:39 2017 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,31 +0,0 @@
-#ifndef CH_PPM_IN
-#define CH_PPM_IN
-
-class PpmIn
-{
-    public:
-    
-        static const uint8_t CHANNELS = 6;
-        uint16_t period;
-        uint16_t channels[CHANNELS+2]; 
-        bool state;
-        
-        PpmIn(PinName pin);
-        
-        void getAllChannels(uint16_t all_channels[]);
-        void rise();
-        
-        bool rise_captured(void);
-        
- 
-    private:
-        InterruptIn ppm;
-        Timer timer;
-        uint8_t current_channel;
-        
-        volatile bool rise_value;
-        
-        
-};
-
-#endif
\ No newline at end of file
--- a/PPMOut.cpp	Fri Sep 22 11:28:39 2017 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,115 +0,0 @@
-#include "mbed.h"
-#include "PPMOut.h"
-
-PpmOut::PpmOut(PinName pin, uint8_t channel_number): ppm(pin){
-    //__disable_irq();
-    if(channel_number > MAX_CHANNELS){
-        this->channel_number = MAX_CHANNELS;
-    }
-    this->channel_number = channel_number;
-    resetChannels();
-    pulse_out = 0;
-    ppm = pulse_out;
-    current_dot = 0;
-    timeout.attach_us(this, &PpmOut::attimeout, FRAME_LEN);
-    //__enable_irq(); 
-}
-
-void PpmOut::setChannel(int channel_no, uint16_t value) {
-    //__disable_irq();     // Disable Interrupts
-    
-    //pc.printf("channel was set \r\n ");
-    
-    if(channel_no > channel_number-1){
-        return;
-    }
-    if(value > MAX_CHANNEL_VALUE){
-        value = MAX_CHANNEL_VALUE;
-    }
-    if(value < MIN_CHANNEL_VALUE){
-        value = MIN_CHANNEL_VALUE;
-    }
-    
-    dots[channel_no*2] = CHANNEL_SYNC;
-    //dots[channel_no*2+1] = CHANNEL_PAD_SYNC + value;
-    dots[channel_no*2+1] = value - CHANNEL_SYNC + channel_correction[channel_no] - GENERAL_CORRECTION;
-    //dots[channel_no*2+1] = CHANNEL_MID_VALUE - CHANNEL_SYNC;
-
-    setFrameSync();
-    //__enable_irq();     // Enable Interrupts
-}
-
-void PpmOut::setAllChannels(uint16_t new_channels[MAX_CHANNELS], int channels){
-    for(int channel_no = 0; channel_no < channels; channel_no++){
-        dots[channel_no*2] = CHANNEL_SYNC;    
-        dots[channel_no*2+1] = new_channels[channel_no] - CHANNEL_SYNC + channel_correction[channel_no] - GENERAL_CORRECTION;
-    }   
-    
-    setFrameSync();
-}
-
-void PpmOut::setChannelCorrection(int channel_no, uint16_t value){
-    
-    channel_correction[channel_no] = value;
-        
-}
-
-void PpmOut::getAllChannels(uint16_t all_channels[]){
-    
-       
-    for(uint8_t channel = 0; channel < channel_number; channel++) {
-        all_channels[channel] = dots[channel*2+1] + channel_correction[channel];
-    }   
-    all_channels[0] = 1000;
-    //return all_channels;
-
-}
-
-
-void PpmOut::setFrameSync(){
-    
-    //pc.printf("Frame synchronization \r\n ");
-    //__disable_irq();
-    
-    uint16_t sum_channels = 0;
-    for(uint8_t channel = 0; channel < channel_number; channel++) {
-        sum_channels += dots[channel*2+1] + channel_correction[channel];
-    }
-    sum_channels += channel_number*CHANNEL_SYNC;
-    dots[channel_number*2] = CHANNEL_SYNC;
-    dots[channel_number*2+1] = FRAME_LEN - CHANNEL_SYNC - sum_channels;
-    
-    //__enable_irq();
-     
-}
-
-void PpmOut::attimeout() {
-    
-    //pc.printf("PPM attimeout \r\n ");
-    //__disable_irq();
-    
-    pulse_out = !pulse_out;
-    ppm = pulse_out;
-    
-    timeout.attach_us(this, &PpmOut::attimeout, dots[current_dot]);
-    current_dot++;
-
-    if(current_dot == channel_number*2+2) { // 2 for FRAME_SYNC
-        current_dot = 0;
-    }
-     
-    //__enable_irq();
-}
-
-void PpmOut::resetChannels() {
-    int8_t channel;
-    
-    current_dot = 0;
-    memset(dots, 0x00, DOTS);
-    memset(channel_correction, 0x00, MAX_CHANNELS); 
-    for(channel = 0; channel < channel_number; channel++) {
-        dots[channel*2] = CHANNEL_SYNC;
-        dots[channel*2+1] = CHANNEL_PAD_SYNC;
-    }
-    setFrameSync();
-}
\ No newline at end of file
--- a/PPMOut.h	Fri Sep 22 11:28:39 2017 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,51 +0,0 @@
-#ifndef CH_PPM_OUT
-#define CH_PPM_OUT
-
-class PpmOut{
-    public:
-        static const uint8_t MAX_CHANNELS = 8;
-        static const uint16_t CHANNEL_SYNC = 300; // us
-        static const uint16_t CHANNEL_PAD_SYNC = 1000 - CHANNEL_SYNC; // us
-        static const uint16_t CHANNEL_MID_VALUE = 1500; // us
-        static const uint16_t FRAME_SYNC = 5000; // us
-        static const uint16_t FRAME_LEN = 20000; // us
-        static const uint16_t MAX_CHANNEL_VALUE = 2000; // us
-        static const uint16_t MIN_CHANNEL_VALUE = 1010; 
-        static const uint16_t DOTS = MAX_CHANNELS*2+2; // two dots per channel + FRAME_SYNC
-        // general channel correction - difference between assigned value a result value on fly controller
-        static const uint16_t GENERAL_CORRECTION = 26; // us
-        
-        /* Will start the PPM output */
-        PpmOut(PinName pin, uint8_t channel_number);
-        /* Values go from MIN_CHANNEL_VALUE to MAX_CHANNEL_VALUE */
-        void setChannel(int channel_no, uint16_t value);
-        /* Set all channels */
-        void PpmOut::setAllChannels(uint16_t new_channels[MAX_CHANNELS], int channels);
-        /* Set channels correction at the start */
-        void setChannelCorrection(int channel_no, uint16_t value);
-        
-        // Get all channels value
-        void getAllChannels(uint16_t all_channels[]);
-        
-        
-    private:
-        /* These are the time dots where the signal changes the value 
-           from 0 to 1 and in reverse */
-        uint16_t dots[DOTS];
-        Timeout timeout;
-        DigitalOut ppm;
-        uint8_t current_dot;
-        uint8_t channel_number;
-        uint16_t frame_length;
-        uint16_t pulse_out;
-        // correction for every single channel
-        uint16_t channel_correction[MAX_CHANNELS];
-        
-        //uint16_t* all_channels[MAX_CHANNELS];
-        
-        void attimeout();
-        inline void resetChannels();
-        inline void setFrameSync();
-};
-
-#endif // CH_PPM_OUT
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/PpmRegen.cpp	Wed Sep 27 17:05:59 2017 +0000
@@ -0,0 +1,59 @@
+#include "mbed.h"
+#include "PpmRegen.h"
+
+PpmRegen::PpmRegen(PinName InterruptPort, PinName roll, PinName pitch, PinName yaw, PinName throttle, PinName aux1, PinName aux2): 
+ppmPin(InterruptPort), _roll(roll), _pitch(pitch), _yaw(yaw), _throttle(throttle), _aux1(aux1), _aux2(aux2){
+    current_channel = 0;
+    first_reading = true;
+    timer.start();
+    ppmPin.fall(this, &PpmRegen::fall);
+}
+    
+
+void PpmRegen::getAllChannels(uint16_t all_channels[]){
+    for(int i = 0; i < CHANNELS; i++){
+        all_channels[i] = channels[i];   
+    }           
+    
+}
+            
+void PpmRegen::fall(){
+    //__disable_irq();
+    uint16_t time = timer.read_us();
+    
+    // we are in synchro zone
+    if(time > 2500)
+    {
+        if(first_reading){
+            //channel_correction();   
+            first_reading = false;     
+        }
+        
+        current_channel = 0;
+        
+        _roll.pulsewidth_us(    channels[0]);
+        _pitch.pulsewidth_us(   channels[1]);
+        _yaw.pulsewidth_us(     channels[2]);
+        _throttle.pulsewidth_us(channels[3]);
+        _aux1.pulsewidth_us(    channels[4]);
+        _aux2.pulsewidth_us(    channels[5]);
+    }
+    else
+    {
+        channels[current_channel] = timer.read_us();  
+        current_channel += 1;     
+    }
+    
+    timer.reset();
+    
+    //__enable_irq();
+}
+
+void PpmRegen::channel_correction(){
+    corrections[0] = 1500 - channels[0];    
+    corrections[1] = 1500 - channels[1];
+    corrections[2] = 1500 - channels[2];
+    corrections[3] = 1010 - channels[3];
+    corrections[4] = 1010 - channels[4];
+    corrections[5] = 1010 - channels[5];
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/PpmRegen.h	Wed Sep 27 17:05:59 2017 +0000
@@ -0,0 +1,36 @@
+#ifndef CH_PPM_IN
+#define CH_PPM_IN
+
+#include "mbed.h"
+
+class PpmRegen
+{
+    public:
+    
+        static const uint8_t CHANNELS = 6;
+        uint16_t period;
+        uint16_t channels[CHANNELS+2]; 
+        uint16_t corrections[CHANNELS];
+        
+        PpmRegen(PinName InterruptPort, PinName roll, PinName pitch, PinName yaw, PinName throttle, PinName aux1, PinName aux2);
+        
+        void getAllChannels(uint16_t all_channels[]);
+        void fall();
+        void channel_correction(void);
+        
+    private:
+        InterruptIn ppmPin;
+        Timer timer;
+        uint8_t current_channel;
+        bool first_reading;
+        
+        PwmOut              _roll;
+        PwmOut              _pitch;
+        PwmOut              _yaw;
+        PwmOut              _throttle;
+        PwmOut              _aux1;
+        PwmOut              _aux2;
+        
+};
+
+#endif
\ No newline at end of file