-3
tcommand=(pwm_value-480.0)/8.22;
flapmag=(pwm_value-880)/41.0+10;

In this piece of code used in arduino, I don't understand why PWM value is subtracted and divided like this. This is not my code. Any explanation would be of great help. I have read about PWM but all have different explanations.

  • 1
    You provide no context for these two lines. How should we understand them? This is just a calculation. That one involved variable is named pwm_value does not make the calculation about PWM in general. We cannot know, what tcommand and flapmag should be. Please provide the full code. Then we might be able to understand it – chrisl Jan 24 at 7:58
  • 1
    Is this code for receiving radio control communication and controlling a model plane or something? – Majenko Jan 24 at 9:01
  • As @chrisl said, we need enough context to understand what those values mean. This is a fine example why magic numbers are discouraged in any code style. – the busybee Jan 24 at 9:21
  • You need to fix your question to provide more information, or delete it. – Duncan C Jan 24 at 19:22
1

There is not enough context to be certain, but given the variable names I can assume that this is code from a sketch that receives information from an RC controller and is intended to control parts of some form of flying craft.

So with that in mind, let's take this line:

flapmag=(pwm_value-880)/41.0+10;

I assume that this is to calculate the amount the flaps should be extended or retracted.

RC controllers, or rather the receivers that pick up the signals, usually output a PWM signal (square wave with a variable duty cycle) where the duty cycle is directly related to the position of the joystick.

A joystick is usually zero in the center and goes plus or minus for up and down (or right and left). But you can't have a negative PWM value, since that makes no sense (how can you have a signal that is off for less that 0% of the time?!). So the output is "offset" by a certain amount (that's not quite true - actually the joystick is 0 for fully down, and 100% for fully up, with "0" being somewhere in the middle, so the offset is applied by the joystick, not the receiver, but that really doesn't matter for us right now).

So we have a signal that is at any time somewhere between 0% and 100%, with "zero" being somewhere in the middle. What actual value the "100%" is, or exactly where "zero" is, can vary depending on many many factors, so we'll just use "100" for the maximum, and "50" for the middle "zero" offset.

The first thing you want to do, then, is remove that "zero" offset. And that is as simple as subtracting it. With our example numbers above, if you subtract the "zero" offset of "50" from the incoming signal it now goes between -50 and +50 instead of 0 and 100. That now relates much better to how a joystick appears - 0-50 in an upwards direction (+) and 0-50 in a downwards direction (-).

And that is exactly what is happening with pwm_value-880 in your code. The middle "zero" value of 880 is being subtracted from the incoming joystick position to give you an "up" or "down" value that is positive or negative respectively.

Next comes the division. This scales the incoming value to the right size for the device you are driving. In our example we have a ±50 signal. But what if our flaps require a ±10 signal to give the correct movement range. So simply dividing the value by 5 will change ±50 into ±10. That's exactly what the /41.0 is doing - scaling the result to fit the range of the target device.

Then there's one final +10 in there. This simply adds a small offset to the result. You can think of this as a "trim" value. It sets the actual "zero" point of the target device - in this case when the joystick is centered it means the flaps are set to a +10 position.

As to how the pwm_value itself is created, that is usually done through capturing the rising and falling edges of the incoming waveform from the receiver and timing how long the signal is in the HIGH or LOW state. That may be performed by some unspecified library.

  • Actually, this is what I was asking. Thanks for the information. – Shivam Goswami Jan 24 at 18:27

Not the answer you're looking for? Browse other questions tagged or ask your own question.