I used the servo library to generate the PWM signal. I want to invert the PWM signal by using programming. But I don't know how can I modify my program.
My program is shown below: #include <Servo.h>
Servo myservo[12];
#define SERVO_TO_PIN(x) (x+2)
int pos = 5; //Beginning Degree
int RotateDirection = 0;
for(int ServoNum = 0; ServoNum <= 11; ServoNum++)
{
myservo[ServoNum].write(pos);// tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
Serial.print("The angle of the servo");
Serial.print(ServoNum);
Serial.print(" is ");
Serial.print(myservo[ServoNum].read());
Serial.print('\n');
delay(500);
}
}
I understand I should modify this command "myservo[ServoNum].write(pos);", but what should I add?
Thank you very much.
I haven't looked into this deeply, but couldn't you just 'rescale' the driving value so that what is normally the 'minimum' value becomes the 'maximum' value, and what is normally the 'maximum' value is the minimum? So if the PWM output ranges from 0 to 100, just use: pwmValue = 100 - desiredValue
That way, if you desire a value of 0, the pwm is set to 100, and if you desire a value of 100, the pwm output is set to 0. That should "invert" the pwm waveform.
Blessings in abundance, all the best, & ENJOY!
Art in Carlisle, PA USA
That might work for a PWM signal used for, say, dimming a LED, but not a servo PWM, where the library is mapping an "angle" to a short 1 to 2ms range.
If you want the complementary angle, then that method would work.
Which is why you'd have to modify the library to invert the signal.
I used the servo library to generate the PWM signal. I want to invert the PWM signal by using programming. But I don't know how can I modify my program.
Hardware wise, you could probably use a transistor/resistor setup or a logic chip.
Thanks AWOL ...
I wasn't aware of any functions (mapping) performed on the servo data. I was 'assuming' passing data directly to the PWM hardware register. Thanks for the correction.
AWOL:
The angle of the servo (or speed of the ESC) is proportional to the width of the pulse.
It is PWM.
A multichannel stream is encoded as PPM between transmitter and receiver, but once past the decoder, it reverts to being PWM.
That makes sense, and it also accounts for the confusion, because whether the signal is PWM or PPM depends on the context which will often not be obvious.
The difference between PPM and PWM is that PWM has a constant frequency and a variable duty cycle, where as PPM has a variable duty cycle and variable frequency, that are interdependent.
Grumpy_Mike:
The difference between PPM and PWM is that PWM has a constant frequency and a variable duty cycle, where as PPM has a variable duty cycle and variable frequency, that are interdependent.
Well I would describe the servo signal as output by the servo library as a variable pulse width (1-2msec) repeated at a constant frequency (40Hz). That is, leading positive edge to leading positive edge is always sent at a fixed period but the width of the positive pulse is variable within in it's pulse width range.
I guess it's sort of like the story of the many blind men describing an elephant.
Well I would describe the servo signal as output by the servo library as a variable pulse width (1-2msec) repeated at a constant frequency (40Hz).
That's a definition of the PWM produced by an analogWrite, the only difference for R/C is the frequency, and the range of duty cycle.
(R/C standard is 50Hz)
I have no problem calling a servo signal a pwm signal, but just that it's a specific pwm signal with limited duty cycle values allowed, unlike the classic pwm signal that allows for full 0 to 100% duty cycle variation.
And of course a classic pwm signal at either 0% or 100% duty cycle has a switching frequency of 0, so even the classic pwm signal doesn't have a constant switching frequency at it's 'edge' conditions.
I don't see the problem in describing 100% or 5% modulation as PWM.
You don't complain when your 90MHz FM signal is only modulated by a few tens of kHz, to give whatever they call AOR these days.
It's the proportionality that's important, not the absolute percentage.
And of course a classic pwm signal at either 0% or 100% duty cycle has a switching frequency of 0, so even the classic pwm signal doesn't have a constant switching frequency at it's 'edge' conditions
A PWM signal at 100% or 0% is not a PWM signal at all is it, it is a constant level. Therefore the definition of PWM can not include these modulation levels.
You don't complain when your 90MHz FM signal is only modulated by a few tens of kHz
FM is something different as is phase modulation but in the limit all these things look very similar. For example phase modulation is almost identical to low deviation frequency modulation.