Unstable result when passing servo pulse through Arduino
Posted: 20 May 2019, 12:07
Hi all,
Some time ago a clubmate asked me if I could develop something in Arduino to avoid a clash between his retracting landing gear and the closing L/G doors. He had installed a pneumatic retract gear operated by a valve system, in turn operated by a servo. The system also operated the gear doors, but these moved much faster than the gear itself, risking a collision on retracting. The servo operating the valve needed to be operated in stages, low PWM to extend gear and open doors, mid PWM to retract gear, wait for 4 seconds, high PWM to close doors.
Now, I do not remember exactly how the gear doors were operated, but my clubmate preferred to maintain the possibility to trim the end positions of the valve servo from his Tx to finetune the doors. He operated the system with a 3-position switch which worked ok, but his fear was that someday he might skip the mid position by mistake and have the doors being damaged by the retracting gear legs.
So I set about to write some code to detect a sudden change in PWM value, coming from the low PWM position, and add a 4 seconds delay in the mid position irrespective from the PWM value coming from the Rx. This worked as intended. Outside the delay time, I just "passed through" the incoming PWM.
That is where a problem surfaced, the output to the servo showed a noticeable jitter (outside the delay period, where the PWM value output was a constant). Looking at the results from the serial monitor I could see a frequent drop in the incoming PWM of about 10-15 microseconds. I used the pulseIn() to read the PWM, but I have also tried another way by timing interrupts on the rising and falling edges of the pulse. Not much difference there. I ended up in putting out fixed PWM values for all stages, foregoing the request for trimmable end points.
The whole setup works fine and my clubmate is happy. But I would like to know if anyone has an idea as to the cause of this instability, and how to avoid it. I am attaching the code as it was initially. I am using the Servo.h library to generate the PWM output (sorry Phil ).
Cheers,
Max.
Sorry for the long story, which is a bit outside the main topics of this forum, but I figured you might be interested in the background. It boils down to the question why the PWM from a stable input source is not read as a stable number.
Some time ago a clubmate asked me if I could develop something in Arduino to avoid a clash between his retracting landing gear and the closing L/G doors. He had installed a pneumatic retract gear operated by a valve system, in turn operated by a servo. The system also operated the gear doors, but these moved much faster than the gear itself, risking a collision on retracting. The servo operating the valve needed to be operated in stages, low PWM to extend gear and open doors, mid PWM to retract gear, wait for 4 seconds, high PWM to close doors.
Now, I do not remember exactly how the gear doors were operated, but my clubmate preferred to maintain the possibility to trim the end positions of the valve servo from his Tx to finetune the doors. He operated the system with a 3-position switch which worked ok, but his fear was that someday he might skip the mid position by mistake and have the doors being damaged by the retracting gear legs.
So I set about to write some code to detect a sudden change in PWM value, coming from the low PWM position, and add a 4 seconds delay in the mid position irrespective from the PWM value coming from the Rx. This worked as intended. Outside the delay time, I just "passed through" the incoming PWM.
That is where a problem surfaced, the output to the servo showed a noticeable jitter (outside the delay period, where the PWM value output was a constant). Looking at the results from the serial monitor I could see a frequent drop in the incoming PWM of about 10-15 microseconds. I used the pulseIn() to read the PWM, but I have also tried another way by timing interrupts on the rising and falling edges of the pulse. Not much difference there. I ended up in putting out fixed PWM values for all stages, foregoing the request for trimmable end points.
The whole setup works fine and my clubmate is happy. But I would like to know if anyone has an idea as to the cause of this instability, and how to avoid it. I am attaching the code as it was initially. I am using the Servo.h library to generate the PWM output (sorry Phil ).
Cheers,
Max.
Sorry for the long story, which is a bit outside the main topics of this forum, but I figured you might be interested in the background. It boils down to the question why the PWM from a stable input source is not read as a stable number.