ppm

Dependents:   Autonomous_quadcopter

Fork of PPM by Eduard Medla

Files at this revision

API Documentation at this revision

Comitter:
edy05
Date:
Fri Sep 22 11:28:39 2017 +0000
Parent:
1:3649456c67ef
Child:
3:2d4d05f95c1a
Commit message:
1 release, bugs

Changed in this revision

PPMIn.cpp Show annotated file Show diff for this revision Revisions of this file
PPMIn.h Show annotated file Show diff for this revision Revisions of this file
PPMOut.cpp Show annotated file Show diff for this revision Revisions of this file
PPMOut.h Show annotated file Show diff for this revision Revisions of this file
--- a/PPMIn.cpp	Fri Jun 09 08:34:42 2017 +0000
+++ b/PPMIn.cpp	Fri Sep 22 11:28:39 2017 +0000
@@ -1,29 +1,42 @@
 #include "mbed.h"
 #include "PPMIn.h"
 
-PpmIn::PpmIn(PinName pin): ppm(pin), pc_com(USBTX, USBRX)
+PpmIn::PpmIn(PinName pin): ppm(pin)
 {
-    pc_com.printf("Constructor say hi!");
+    //__disable_irq();
     current_channel = 0;
     state = false;
     timer.start();
-    ppm.rise(this, &PpmIn::rise);
-       
+    ppm.fall(callback(this, &PpmIn::rise));
+    
+    //__enable_irq(); 
 }
 
-uint16_t* PpmIn::getPpm()
+void PpmIn::getAllChannels(uint16_t all_channels[])
 {
-    uint16_t* p_out[CHANNELS+2];
-    *p_out = &channels[0];
-    return *p_out;            
+    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();
     
-    //pc_com.printf("Rise say hi!");
+    rise_value = true;
+    
     // we are in synchro zone
     if(time > 2500)
     {
@@ -40,4 +53,5 @@
     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 Jun 09 08:34:42 2017 +0000
+++ b/PPMIn.h	Fri Sep 22 11:28:39 2017 +0000
@@ -1,30 +1,30 @@
 #ifndef CH_PPM_IN
 #define CH_PPM_IN
 
-
-
 class PpmIn
 {
     public:
     
-        static const uint8_t CHANNELS = 8;
-    
+        static const uint8_t CHANNELS = 6;
         uint16_t period;
         uint16_t channels[CHANNELS+2]; 
         bool state;
         
         PpmIn(PinName pin);
         
-        uint16_t* getPpm();
+        void getAllChannels(uint16_t all_channels[]);
         void rise();
         
+        bool rise_captured(void);
+        
  
-    protected:
+    private:
         InterruptIn ppm;
-        Serial pc_com;
         Timer timer;
         uint8_t current_channel;
         
+        volatile bool rise_value;
+        
         
 };
 
--- a/PPMOut.cpp	Fri Jun 09 08:34:42 2017 +0000
+++ b/PPMOut.cpp	Fri Sep 22 11:28:39 2017 +0000
@@ -1,44 +1,93 @@
 #include "mbed.h"
 #include "PPMOut.h"
 
-PpmOut::PpmOut(PinName pin, uint8_t channel_number): ppm(pin) {
+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 = 1;
+    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] = 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];
+        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;
     
@@ -48,6 +97,8 @@
     if(current_dot == channel_number*2+2) { // 2 for FRAME_SYNC
         current_dot = 0;
     }
+     
+    //__enable_irq();
 }
 
 void PpmOut::resetChannels() {
@@ -55,6 +106,7 @@
     
     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;
--- a/PPMOut.h	Fri Jun 09 08:34:42 2017 +0000
+++ b/PPMOut.h	Fri Sep 22 11:28:39 2017 +0000
@@ -6,17 +6,28 @@
         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 = 1980; // us
-        static const uint16_t MIN_CHANNEL_VALUE = 1020; 
+        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 */
@@ -27,7 +38,11 @@
         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();