go away spammer

Author Topic: How to read servo pulses from receiver?  (Read 3710 times)

0 Members and 1 Guest are viewing this topic.

Offline Razor ConceptsTopic starter

  • Supreme Robot
  • *****
  • Posts: 1,856
  • Helpful? 53
    • RazorConcepts
How to read servo pulses from receiver?
« on: November 06, 2008, 08:40:51 PM »
Is there a simple way of reading servo pulses coming in from a RC receiver, and reading them in a timely fashion?
Here is some code for reading in two channels, but they use some pulseIn command I don't know of.

Offline airman00

  • Contest Winner
  • Supreme Robot
  • ****
  • Posts: 3,650
  • Helpful? 21
  • narobo.com
    • Narobo.com - Mechatronics and related
Re: How to read servo pulses from receiver?
« Reply #1 on: November 06, 2008, 08:43:55 PM »
PWM from the servos gives an analog signal. A simple ADC will be able to "read" a PWM.
Check out the Roboduino, Arduino-compatible board!


Link: http://curiousinventor.com/kits/roboduino

www.Narobo.com

Offline ArcMan

  • Supreme Robot
  • *****
  • Posts: 519
  • Helpful? 4
  • Mmmm... Plasma
Re: How to read servo pulses from receiver?
« Reply #2 on: November 06, 2008, 09:53:31 PM »
Here's how I do it...

Code: [Select]
// Wait for incoming steering pulse
set_timer1(0); // Start the timeout timer
while (get_timer1() < 62500) { // 50 ms timeout
if (input(STEERING_PULSE)) {
set_timer1(0); // Start pulse timer
break;
}
}
if (get_timer1() >= 62500) {
missed_pulse = TRUE;
missed_pulses++;
good_radio = 0;
}

// Measure steering pulse duration
while (get_timer1() < 3125) { // 2.5 ms timeout
if (!input(STEERING_PULSE)) {
steering_pw = get_timer1();
set_timer1(0); // Start pulse timer
break;
}
}
if ((steering_pw < (ST_LEFT-50)) || (steering_pw > (ST_RIGHT+50))) { // If the steering PW is outside the valid range
steering_pw = 0;
missed_pulse = TRUE;
missed_pulses++;
good_radio = 0;
}
else { // Radio signal is good.
if (++good_radio == 10) // Radio signal must be good for 10 pulses to establish a "good radio".
radio_signal_lost = FALSE;
missed_pulse = FALSE;
missed_pulses = 0;
}

There's alot of extra stuff in there for detecting missing pulses and such, but what it amounts to is waiting for the pulse to go TRUE, clearing the timer, wait for the pulse to go FALSE, and then read your timer.  The timer value is the measured duration of the pulse.  Easy breezy.



Offline Admin

  • Administrator
  • Supreme Robot
  • *****
  • Posts: 11,703
  • Helpful? 173
    • Society of Robots
Re: How to read servo pulses from receiver?
« Reply #3 on: November 09, 2008, 01:08:58 AM »
Well, the most proper way would be to use timer interrupts. You'd use a separate interrupt for each channel.

pseudocode:
set up timer interrupts to trigger on port change

reset timer

main()
when timer interrupt is triggered, store timer value, then reset timer

Offline mbateman

  • Full Member
  • ***
  • Posts: 82
  • Helpful? 0
    • Robotics 4 Fun
Re: How to read servo pulses from receiver?
« Reply #4 on: November 09, 2008, 07:44:12 AM »
See this post... http://www.societyofrobots.com/robotforum/index.php?topic=5708.msg43934#msg43934.
It has code for doing this with external interrupts and a running timer.

Offline HyperNerd

  • Robot Overlord
  • ****
  • Posts: 139
  • Helpful? 5
  • N3rd1n80r
Re: How to read servo pulses from receiver?
« Reply #5 on: November 16, 2008, 03:18:35 AM »
I'm sure I read this somewhere: (i'm not sure though :-\)

PWM------/\/\/\/--------+--------------------ANALOG OUT
                                  |
                                 ---
                                 ---
                                  |
GND----------------------+--------------------GND

Then connect the ANALOG OUT to the input of the MCU's ADC. The output will be directly proportional to the length of the PWM pulse.

As I said I'm not sure :-\

PS. I don't know the value of the components, just that the resistor and capacitor are connected like that... That probably makes this post useless, but oh well :)
There are 10 types of people in the world - those who understand binary, and those who don't.

Offline ArcMan

  • Supreme Robot
  • *****
  • Posts: 519
  • Helpful? 4
  • Mmmm... Plasma
Re: How to read servo pulses from receiver?
« Reply #6 on: November 16, 2008, 06:51:40 PM »
Not useless.  It illustrates an alternate method for measuring the duty cycle of a PWM signal.
However, it is a crude way of measuring pulse width.  It can be likened to measuring the size of a room by stepping it off instead of using a tape measure.
It's also not a good idea for measuring a servo pulse width, since the analog value will vary not just based on the pulse width, but also based on the pulse frequency (which may not always be 50 Hz).

 


Get Your Ad Here

data_list