<?xml version="1.0" encoding="UTF-8"?>
<!-- generator="bbPress/1.0.2" -->
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
		<title>LeafLabs Garden &#187; Topic: RC receiver CPPM decode</title>
		<link>http://forums.leaflabs.com/topic.php?id=10470</link>
		<description>A place to share, learn, and grow...</description>
		<language>en-US</language>
		<pubDate>Fri, 22 Jan 2016 00:04:40 +0000</pubDate>
		<generator>http://bbpress.org/?v=1.0.2</generator>
		<textInput>
			<title><![CDATA[Search]]></title>
			<description><![CDATA[Search all topics from these forums.]]></description>
			<name>q</name>
			<link>http://forums.leaflabs.com/search.php</link>
		</textInput>
		<atom:link href="http://forums.leaflabs.com/rss.php?topic=10470" rel="self" type="application/rss+xml" />

		<item>
			<title>Twinkle on "RC receiver CPPM decode"</title>
			<link>http://forums.leaflabs.com/topic.php?id=10470#post-25257</link>
			<pubDate>Thu, 25 Apr 2013 05:32:56 +0000</pubDate>
			<dc:creator>Twinkle</dc:creator>
			<guid isPermaLink="false">25257@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;Yeah..it was a timer issue ! I had to declare a separate timer for pwm generation. Thank you guys !
&#60;/p&#62;</description>
		</item>
		<item>
			<title>mlundinse on "RC receiver CPPM decode"</title>
			<link>http://forums.leaflabs.com/topic.php?id=10470#post-25099</link>
			<pubDate>Mon, 22 Apr 2013 12:24:38 +0000</pubDate>
			<dc:creator>mlundinse</dc:creator>
			<guid isPermaLink="false">25099@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;You are using pins 3 and 4 for pwm output,  pin 3 is pwm output connected to Timer2, and pin4 is not a pwm output pin.&#60;/p&#62;
&#60;p&#62;Timer2 is used for the command generation timer so this might interfere with the pwm generation.&#60;/p&#62;
&#60;p&#62;You also declare HardwareTimer timer2(2) twice, once globally and once in the setup function.
&#60;/p&#62;</description>
		</item>
		<item>
			<title>Twinkle on "RC receiver CPPM decode"</title>
			<link>http://forums.leaflabs.com/topic.php?id=10470#post-25092</link>
			<pubDate>Mon, 22 Apr 2013 01:26:50 +0000</pubDate>
			<dc:creator>Twinkle</dc:creator>
			<guid isPermaLink="false">25092@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;The values of rc[1] and rc[3] are good enough. I checked them over the serial monitor. Also the values respond to the transmitter signals. But, the pwmWrite doesnt write the values of rc[1] and rc[3] for some reason.
&#60;/p&#62;</description>
		</item>
		<item>
			<title>mlundinse on "RC receiver CPPM decode"</title>
			<link>http://forums.leaflabs.com/topic.php?id=10470#post-25064</link>
			<pubDate>Sat, 20 Apr 2013 14:54:39 +0000</pubDate>
			<dc:creator>mlundinse</dc:creator>
			<guid isPermaLink="false">25064@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;what vaules do you read for outputPWMsteering = rc[1] and outputPWMthrottle = rc[3] ?&#60;/p&#62;
&#60;p&#62;Try sending them over the serial port, an oscilloscope or logical analyzer is a superb&#60;br /&#62;
tool for sebugging comms problems. They cost a bit but if you play with these things often&#60;br /&#62;
they with say you lots of frustration.
&#60;/p&#62;</description>
		</item>
		<item>
			<title>Twinkle on "RC receiver CPPM decode"</title>
			<link>http://forums.leaflabs.com/topic.php?id=10470#post-25053</link>
			<pubDate>Sat, 20 Apr 2013 14:29:28 +0000</pubDate>
			<dc:creator>Twinkle</dc:creator>
			<guid isPermaLink="false">25053@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;Thanks for the help! The cppm decode part in the code worked!  &#60;/p&#62;
&#60;p&#62;But I am still unable to control a servo using the Futuba transmitter. The decode part seems correct; Somehow, the 'pwmWrite' operation is not being performed. The code prints 'valid' in serial monitor, which indicates that cppm is being received. But, the servo doesnt move at all through the remote(Tx). That means, i guess, pwmWrite is not happening in the code. Cant understand why !&#60;/p&#62;
&#60;pre&#62;&#60;code&#62;volatile unsigned short rc_old = 0; // modified within the cppm_decode() handler
volatile unsigned short rc_new = 0;
volatile int channel = 0;
volatile int rc[8]; //Size of this array depends on the number of channels of the transmitter.
volatile int looper=0;
volatile int rc_valid = 0;
HardwareTimer timer(3);

const int pwmOutPin_steering = 4;    // PWM pin for servo
const int pwmOutPin_throttle = 3;   // PWM pin for throttle
volatile int outputPWMsteering = 0;
volatile int outputPWMthrottle = 0;

HardwareTimer timer2(2);
volatile int check;

void setup()
{

  pinMode(15, INPUT_PULLDOWN);
  attachInterrupt(15, cppm_decode, RISING);
  pinMode(pwmOutPin_steering, PWM);
  pinMode(pwmOutPin_throttle, PWM);
  pinMode(10,OUTPUT);
  timer.setPrescaleFactor(72);
  timer.setOverflow(20000); 

  timer2.pause();
  timer2.setPeriod(20000); // in microseconds
  timer2.setChannel1Mode(TIMER_OUTPUT_COMPARE);
  timer2.setCompare(TIMER_CH1, 1);  // Interrupt 1 count after each update
  timer2.attachCompare1Interrupt(command_generation);
  timer2.refresh();
  timer2.resume();

  HardwareTimer timer2(2);
  volatile int check;
}

 void loop(){
 //CONTROL LOOP: Control Decisions are taken in every Twenty Milliseconds

 if(check)
  {

   //SerialUSB.println(&#38;quot;Timer Overflow&#38;quot;);

   if(rc_valid)
    {
    SerialUSB.println(&#38;quot;valid&#38;quot;);
    outputPWMsteering = rc[1];
    outputPWMthrottle = rc[3];
    pwmWrite(pwmOutPin_steering, outputPWMsteering);
    pwmWrite(pwmOutPin_throttle, outputPWMthrottle);
    //pwmWrite(4, rc[1]);
    //pwmWrite(3, rc[3]);

    delay(1000);
    }

   check = 0;
  }

}

void command_generation(){
check = 1; //Whenever the 20ms timer overflows, the flag &#38;#39;check&#38;#39; is raised.
}

//Interrupt function to decode CPPM

void cppm_decode() {

  if(channel==0)
  {
    timer.setCount(0);
    rc_old = 0;
  }

  rc_old = rc_new;
  rc_new = timer.getCount();

  if((rc_new-rc_old) &#38;lt; 600)
    rc_valid = 0; //Indicates the Tx is OFF.

  if((rc_new-rc_old) &#38;gt; 2500)
   {
   timer.setCount(0);
   channel  = 1;
   rc_old = 0;
   rc_new = 0;
   rc_valid = 1; //Indicates the Tx is ON.
   }
  else
   {
    rc[channel] = rc_new-rc_old;

    channel = (channel + 1)%7 ;
   }
 }&#60;/code&#62;&#60;/pre&#62;</description>
		</item>
		<item>
			<title>ala42 on "RC receiver CPPM decode"</title>
			<link>http://forums.leaflabs.com/topic.php?id=10470#post-24342</link>
			<pubDate>Tue, 09 Apr 2013 11:52:50 +0000</pubDate>
			<dc:creator>ala42</dc:creator>
			<guid isPermaLink="false">24342@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;As gbulmer wrote before, you must change c = c + 1 to c = (c + 1) % 7 or better to c = (c + 1) % 4, as you print out just four values or you overwrite the memory.&#60;br /&#62;
Also change the declaration of c1 and c2 to unsigned short as this the the type of the counter value. Otherwise c2 - c1 can get negative and does not contain the distance of the last two values.
&#60;/p&#62;</description>
		</item>
		<item>
			<title>gbulmer on "RC receiver CPPM decode"</title>
			<link>http://forums.leaflabs.com/topic.php?id=10470#post-24340</link>
			<pubDate>Tue, 09 Apr 2013 10:06:11 +0000</pubDate>
			<dc:creator>gbulmer</dc:creator>
			<guid isPermaLink="false">24340@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;Twinkle - There is no shame in making your life easier (I think it is a mark of a smart person!-)&#60;/p&#62;
&#60;p&#62;IMHO it might be quicker to debug a polling version, then convert that to interrupt driven code (knowing that it works), than debug an interrupt driven version. I think you would need debugging hardware, e.g. a JTAG, or SWD debugger like an ST-LINK or STM32xDiscovery board, and gdb skills, or a logic analyser to be more productive.&#60;/p&#62;
&#60;p&#62;Also polling on a Maple, just using digitalRead, should be quite quick, so the error should be smaller than 0.5%, which is as good as many servos anyway. If you used a fast read approach (on several threads), then the poll loop could take a lot less than 1us.&#60;/p&#62;
&#60;p&#62;Further, USBSerial might be a higher priority interrupt than the pin, so the interrupt driven code might suffer significant jitter.
&#60;/p&#62;</description>
		</item>
		<item>
			<title>Twinkle on "RC receiver CPPM decode"</title>
			<link>http://forums.leaflabs.com/topic.php?id=10470#post-24339</link>
			<pubDate>Tue, 09 Apr 2013 09:07:27 +0000</pubDate>
			<dc:creator>Twinkle</dc:creator>
			<guid isPermaLink="false">24339@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;Thanks gbulmer..&#60;/p&#62;
&#60;p&#62;You're right, as soon as the LED starts to throb, it stops printing on the serial monitor. But, I am really not able to figure out where could the program be broken. Also, as long as I dont turn the futuba transmitter, whatever garbage Rx picks up on interrupt gets printed. The moment (or may be after a few seconds) the Tx is on, LED starts throbbing and, thus, printing stops.&#60;/p&#62;
&#60;p&#62;Before moving to polling, i really want to give a good try on interrupt.
&#60;/p&#62;</description>
		</item>
		<item>
			<title>gbulmer on "RC receiver CPPM decode"</title>
			<link>http://forums.leaflabs.com/topic.php?id=10470#post-24320</link>
			<pubDate>Mon, 08 Apr 2013 06:20:27 +0000</pubDate>
			<dc:creator>gbulmer</dc:creator>
			<guid isPermaLink="false">24320@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;Twinkle - Does the LED look like the effect described here: &#60;a href=&#34;http://leaflabs.com/docs/troubleshooting.html#the-led-is-throbbing-and-my-program-is-stopped&#34; rel=&#34;nofollow&#34;&#62;http://leaflabs.com/docs/troubleshooting.html#the-led-is-throbbing-and-my-program-is-stopped&#60;/a&#62;&#60;/p&#62;
&#60;p&#62;If that is the case, then the LED is signalling that the program is broken. If this is the case, then it won't print anything.&#60;/p&#62;
&#60;p&#62;Have you tried something simpler? For example 'polling' the pin using digitalRead in a loop, to detect when the pin has changed, and using micros() to measure the time? &#60;/p&#62;
&#60;p&#62;AFAICT &#60;code&#62;decode()&#60;/code&#62; could fill the &#60;code&#62;value&#60;/code&#62; array after 7 pulses, and then start over-writing memory, which could cause  the 'throbbing blue LED' state.&#60;/p&#62;
&#60;p&#62;The code &#60;code&#62;c = c + 1;&#60;/code&#62; could be changed to &#60;code&#62;c = (c + 1) % 7;&#60;/code&#62; to avoid that problem.&#60;/p&#62;
&#60;p&#62;(Full disclosure: I am not a member of LeafLabs staff.)
&#60;/p&#62;</description>
		</item>
		<item>
			<title>Twinkle on "RC receiver CPPM decode"</title>
			<link>http://forums.leaflabs.com/topic.php?id=10470#post-24318</link>
			<pubDate>Mon, 08 Apr 2013 05:07:16 +0000</pubDate>
			<dc:creator>Twinkle</dc:creator>
			<guid isPermaLink="false">24318@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;Hi all..I am trying to decode CPPM for rc receiver(futuba). When I upload this code on maple board..it just flashes streaks of blue light continuously..does not show anything on the serial monitor..&#60;/p&#62;
&#60;pre&#62;&#60;code&#62;volatile int state = LOW; // must declare volatile, since it&#38;#39;s
volatile int c1 = 0; // modified within the blink() handler
volatile int c2 = 0;
volatile int c = 0;
volatile int value[7];
volatile int looper=0;
HardwareTimer timer(3);

void setup() {
  //pinMode(BOARD_LED_PIN, OUTPUT);
  pinMode(6, INPUT_PULLDOWN);
  attachInterrupt(6, decode, RISING);
  timer.setPrescaleFactor(72);
  timer.setOverflow(50000);
}

void loop() {
  delay(100);
  for(looper=0;looper&#38;lt;4;looper++)
    SerialUSB.println(value[looper]);

}

void decode() {
  volatile int x;
  if(c==0)
  {
    timer.setCount(0);
    c1 = 0;
  }

  c1 = c2;
  c2 = timer.getCount();
  if((c2-c1) &#38;gt; 2500)
  {
   timer.setCount(0);
   c=0;
  }
  else
  {
    value[c] = c2 - c1;
    c = c + 1;
  }
}&#60;/code&#62;&#60;/pre&#62;</description>
		</item>

	</channel>
</rss>
