2

I am working on comparing autopilots like Ardupilot, CC3D, KK2.1.5. The comparision is about system reactions/behaviors that it gives during flight. For this purpose, I need to read the PWM duty cycle (like osilascopes - the PWM frequency can be vary from 50 to 430 Hz) values from autopilot card. There are 4 command input PWMs and 4 esc control PWMs for a typical quadrotor. I have to read 8 duty cycle values with digital/analog input pins. How do I measure duty cycle of 8 PWM signals? Also, is digital or analogue measurement precise and faster?

Note about attachInterrupt command: According to arduino.cc AtMega328 series have 2 interrupts, Arduino ATmega 2560 series have 7 interrupts.

acs
  • 235
  • 5
  • 16

2 Answers2

2

The code shown in my answer to question 18183, “Read RC receiver channels using Interrupt instead of PulseIn”. That answer has code to read four channels of RC PWM. It can easily be adapted to work with eight channels. It has an ISR that runs when selected pins change. It sorts out which channel(s) changed, and stores the value of micros() at each change.

Also see my answer to question 19106, “Can external interrupts be OR'd together on the '328 (Uno)?” That answer includes a sketch for generating appropriate ISR code. My answer to question 32572, “How to detect the direction of a rotary encoder?”, shows another instance.

Digital input, as used in the code mentioned above, takes under a microsecond ten microseconds in the ISR. As noted in dannyf's comment, unrolling the for loop and (where possible) using constants instead of variables can reduce the in-ISR time to 3.4 μs. Note, ISR entry and exit still add several microseconds.

10-bit analog input, on the other hand, will take around 104 microseconds per reading, a time comparable to some of the pulse widths you'll be measuring. So it isn't fast enough. If degraded ADC accuracy is acceptable, decreasing the ADC-clock scale factor as explained in a webpage at microsmart.co.za can get AD conversion time down to 20 μs. This is around three times as long as digital input takes, so rather slow.

James Waldby - jwpat7
  • 8,920
  • 3
  • 21
  • 33
0

if done in analog, run the pwm through a lpf and perform an adc on the output;

if done in digital, use a timer's capture function, or use external interrupt / pin change interrupt to capture the timing off a time base.

dannyf
  • 2,813
  • 11
  • 13