From 6ee6000302aad2f703ded778ee6caee6d1000afb Mon Sep 17 00:00:00 2001 From: jasonshort Date: Sun, 27 Mar 2011 23:34:50 +0000 Subject: [PATCH] Temp revert to previous version, while I hunt down a bug. git-svn-id: https://arducopter.googlecode.com/svn/trunk@1822 f9c3cf11-9bcb-44bc-f272-b75c42450872 --- libraries/APM_RC/APM_RC.cpp | 188 ++++++++++++++++++------------------ 1 file changed, 94 insertions(+), 94 deletions(-) diff --git a/libraries/APM_RC/APM_RC.cpp b/libraries/APM_RC/APM_RC.cpp index 03a29168f3..deb36e87bf 100644 --- a/libraries/APM_RC/APM_RC.cpp +++ b/libraries/APM_RC/APM_RC.cpp @@ -3,9 +3,9 @@ Code by Jordi Muņoz and Jose Julio. DIYDrones.com This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Lesser General Public - License as published by the Free Software Foundation; either - version 2.1 of the License, or (at your option) any later version. + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. RC Input : PPM signal on IC4 pin RC Output : 11 Servo outputs (standard 20ms frame) @@ -15,7 +15,7 @@ OutpuCh(ch,pwm) : Output value to servos (range : 900-2100us) ch=0..10 InputCh(ch) : Read a channel input value. ch=0..7 GetState() : Returns the state of the input. 1 => New radio frame to process - Automatically resets when we call InputCh to read channels + Automatically resets when we call InputCh to read channels */ #include "APM_RC.h" @@ -42,20 +42,20 @@ ISR(TIMER4_CAPT_vect) unsigned int Pulse_Width; Pulse=ICR4; - if (Pulse8000) // SYNC pulse? - PPM_Counter=0; + Pulse_Width=Pulse-ICR4_old; //Calculating pulse + if (Pulse_Width>8000) // SYNC pulse? + PPM_Counter=0; else - { - if (PPM_Counter < (sizeof(PWM_RAW) / sizeof(PWM_RAW[0]))) { - PWM_RAW[PPM_Counter++]=Pulse_Width; //Saving pulse. + { + if (PPM_Counter < (sizeof(PWM_RAW) / sizeof(PWM_RAW[0]))) { + PWM_RAW[PPM_Counter++]=Pulse_Width; //Saving pulse. if (PPM_Counter >= NUM_CHANNELS) radio_status = 1; - } - } + } + } ICR4_old = Pulse; } @@ -69,102 +69,102 @@ APM_RC_Class::APM_RC_Class() // Public Methods ////////////////////////////////////////////////////////////// void APM_RC_Class::Init(void) { - // Init PPM input - pinMode(49, INPUT); // ICP4 pin (PL0) (PPM input) + // Init PWM Timer 1 + pinMode(11,OUTPUT); // (PB5/OC1A) + pinMode(12,OUTPUT); //OUT2 (PB6/OC1B) + pinMode(13,OUTPUT); //OUT3 (PB7/OC1C) - // Init PWM Timer 1 - pinMode(11,OUTPUT); // (PB5/OC1A) - pinMode(12,OUTPUT); // OUT3 (PB6/OC1B) - pinMode(13,OUTPUT); // OUT4 (PB7/OC1C) - // Init PWM Timer 3 - pinMode(2,OUTPUT); // OUT8 (PE4/OC3B) - pinMode(3,OUTPUT); // OUT7 (PE5/OC3C) - pinMode(4,OUTPUT); // (PE3/OC3A) - // Init PWM Timer 4 - // not avail // (PH3/OC4A) - pinMode(7,OUTPUT); // OUT6 (PH4/OC4B) - pinMode(8,OUTPUT); // OUT5 (PH5/OC4C) + //Remember the registers not declared here remains zero by default... + TCCR1A =((1<> 1; // Because timer runs at 0.5us we need to do value / 2 - result2 = PWM_RAW[ch] >> 1; + // Because servo pulse variables are 16 bits and the interrupts are running values could be corrupted. + // We dont want to stop interrupts to read radio channels so we have to do two readings to be sure that the value is correct... + result = PWM_RAW[ch]>>1; // Because timer runs at 0.5us we need to do value/2 + result2 = PWM_RAW[ch]>>1; + if (result != result2) + result = PWM_RAW[ch]>>1; // if the results are different we make a third reading (this should be fine) - if (result != result2) - result = PWM_RAW[ch] >> 1; // if the results are different we make a third reading (this should be fine) - - // Limit values to a valid range - result = constrain(result, MIN_PULSEWIDTH, MAX_PULSEWIDTH); - radio_status = 0; // Radio channel read - return(result); + // Limit values to a valid range + result = constrain(result,MIN_PULSEWIDTH,MAX_PULSEWIDTH); + radio_status=0; // Radio channel read + return(result); } unsigned char APM_RC_Class::GetState(void) @@ -176,20 +176,20 @@ unsigned char APM_RC_Class::GetState(void) // This function forces the PWM output (reset PWM) on Out0 and Out1 (Timer5). For quadcopters use void APM_RC_Class::Force_Out0_Out1(void) { - if (TCNT5>5000) // We take care that there are not a pulse in the output - TCNT5=39990; // This forces the PWM output to reset in 5us (10 counts of 0.5us). The counter resets at 40000 + if (TCNT5>5000) // We take care that there are not a pulse in the output + TCNT5=39990; // This forces the PWM output to reset in 5us (10 counts of 0.5us). The counter resets at 40000 } // This function forces the PWM output (reset PWM) on Out2 and Out3 (Timer1). For quadcopters use void APM_RC_Class::Force_Out2_Out3(void) { - if (TCNT1>5000) - TCNT1=39990; + if (TCNT1>5000) + TCNT1=39990; } // This function forces the PWM output (reset PWM) on Out6 and Out7 (Timer3). For quadcopters use void APM_RC_Class::Force_Out6_Out7(void) { - if (TCNT3>5000) - TCNT3=39990; + if (TCNT3>5000) + TCNT3=39990; } // allow HIL override of RC values @@ -199,7 +199,7 @@ void APM_RC_Class::setHIL(int16_t v[NUM_CHANNELS]) { for (unsigned char i=0; i