4tronix / PiconZero

Initial commit
13 stars 11 forks source link

Read PWM Input #4

Closed mjhenkes closed 7 years ago

mjhenkes commented 7 years ago

Problem: My current project requires the ability to read input from a RC receiver. The RC receiver's output is in the for of a pulse which currently can't be read through the piconZeros Inputs.

Proposed Solution: Update the PiconZero to have a 5th input configuration to return the duty cycle for the pulse. Additional config should be added to set the expected period for the input so the duty cycle can be calculated. To read the pulse width we will need to enable interrupts for the rising and falling edge of the pulse and measure the time between the edges.

I've put together a branch but wanted to have a discussion before submitting a pull request. https://github.com/mjhenkes/piconZero/tree/read_pwm_input

Since pins A0-A3 do not have hardware interrupts I've pulled in the EnableInterrupt library. This library allows us to fire a method on any pin when a voltage change is detected. Since pins A0-A3 all share the same subroutine, I've made the function executed on change as minimal as possible. The method gathers the information needed to calculate the pulse width which is then calculated in the main loop function. I've tested with a pulse generated by the piconzero piped back into 3 inputs on the picon zero and each input is able to report the same duty cycle for all inputs. With a previous method the inputs were off by 200 ms.

Question 1: Do you want to add the ability to read PWM input to the PiconZero?

Question 2: Do you want to reserve input 4 for DHT11 or should PWM input be input config 4.

Question 3: Do you want the PWM input returned as a duty cycle? We could return the actual pulse width, but we run into strange cases at 0% and 100% duty cycle. They are both the absence of a pulse. If we return the duty cycle we can represent the high/low value in the output.

Question 4: How would you recommend passing in the period to calculate the duty cycle? We could add it to the inputConfig method as an additional default parameter or have a specific function for it. I think I prefer adding it to the inputConfig function.

Question 5: Would this change bump the version of the piconzero to 9? How would you like me to structure that?

Question 6: What would your recommend as a default period? 2000 micro seconds?

4tronix commented 7 years ago

Hi,

Wow that’s some good work.

  1. Yes, it seems a good idea. It is only activated if people select that option so shouldn’t cause issues elsewhere

  2. Let’s keep config 4 for DHT11 and use config 5 for this

  3. I return duty cycle in other similar things I have done, so let’s keep that. A separate config 6 perhaps could return frequency

  4. I agree it should form part of input config, but I do not want to change any existing calls. They must remain as is otherwise you are making every existing customer change their code

  5. Yes, version 9 would be required

Best regards,

Gareth

From: Matt Henkes [mailto:notifications@github.com] Sent: 10 December 2016 02:21 To: 4tronix/PiconZero Subject: [4tronix/PiconZero] Read PWM Input (#4)

Problem: My current project requires the ability to read input from a RC receiver. The RC receiver's output is in the for of a pulse which currently can't be read through the piconZeros Inputs.

Proposed Solution: Update the PiconZero to have a 5th input configuration to return the duty cycle for the pulse. Additional config should be added to set the expected period for the input so the duty cycle can be calculated. To read the pulse width we will need to enable interrupts for the rising and falling edge of the pulse and measure the time between the edges.

I've put together a branch but wanted to have a discussion before submitting a pull request. https://github.com/mjhenkes/piconZero/tree/read_pwm_input

Since pins A0-A3 do not have hardware interrupts I've pulled in the EnableInterrupt https://github.com/GreyGnome/EnableInterrupt library. This library allows us to fire a method on any pin when a voltage change is detected. Since pins A0-A3 all share the same subroutine, I've made the function executed on change as minimal as possible. The method gathers the information needed to calculate the pulse width which is then calculated in the main loop function. I've tested with a pulse generated by the piconzero piped back into 3 inputs on the picon zero and each input is able to report the same duty cycle for all inputs. With a previous method the inputs were off by 200 ms.

Question 1: Do you want to add the ability to read PWM input to the PiconZero?

Question 2: Do you want to reserve input 4 for DHT11 or should PWM input be input config 4.

Question 3: Do you want the PWM input returned as a duty cycle? We could return the actual pulse width, but we run into strange cases at 0% and 100% duty cycle. They are both the absence of a pulse. If we return the duty cycle we can represent the high/low value in the output.

Question 4: How would you recommend passing in the period to calculate the duty cycle? We could add it to the inputConfig method as an additional default parameter or have a specific function for it. I think I prefer adding it to the inputConfig function.

Question 5: Would this change bump the version of the piconzero to 9? How would you like me to structure that?

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/4tronix/PiconZero/issues/4 , or mute the thread https://github.com/notifications/unsubscribe-auth/AFjlrnviaGxcBevtH1tn5uG-cOIe9i2nks5rGgxugaJpZM4LJh2f . https://github.com/notifications/beacon/AFjlrtPj10DcJZUtx2UrYPyGmFz81RLUks5rGgxugaJpZM4LJh2f.gif