Matthias Grob
/
FlyBed2
My fully self designed first stable working Quadrocopter Software.
Servo_PWM/Servo_PWM.cpp@10:14390c90c3f5, 2015-08-31 (annotated)
- Committer:
- maetugr
- Date:
- Mon Aug 31 20:20:50 2015 +0000
- Revision:
- 10:14390c90c3f5
- Parent:
- 5:06e978fd147a
before changing to MPU9250
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
maetugr | 5:06e978fd147a | 1 | #include "Servo_PWM.h" |
maetugr | 5:06e978fd147a | 2 | #include "mbed.h" |
maetugr | 5:06e978fd147a | 3 | |
maetugr | 5:06e978fd147a | 4 | Servo_PWM::Servo_PWM(PinName Pin, int frequency) : ServoPin(Pin) { |
maetugr | 5:06e978fd147a | 5 | SetFrequency(frequency); |
maetugr | 5:06e978fd147a | 6 | ServoPin = 0; |
maetugr | 5:06e978fd147a | 7 | // initialize ESC |
maetugr | 5:06e978fd147a | 8 | SetPosition(0); // zero throttle |
maetugr | 5:06e978fd147a | 9 | } |
maetugr | 5:06e978fd147a | 10 | |
maetugr | 5:06e978fd147a | 11 | void Servo_PWM::SetFrequency(int frequency) { |
maetugr | 5:06e978fd147a | 12 | ServoPin.period(1.0/frequency); |
maetugr | 5:06e978fd147a | 13 | } |
maetugr | 5:06e978fd147a | 14 | |
maetugr | 5:06e978fd147a | 15 | void Servo_PWM::SetPosition(int position) { |
maetugr | 5:06e978fd147a | 16 | if (position > 1000) |
maetugr | 5:06e978fd147a | 17 | position = 1000; |
maetugr | 5:06e978fd147a | 18 | if (position < 0) |
maetugr | 5:06e978fd147a | 19 | position = 0; |
maetugr | 5:06e978fd147a | 20 | ServoPin.pulsewidth_us(position+1000); |
maetugr | 5:06e978fd147a | 21 | } |
maetugr | 5:06e978fd147a | 22 | |
maetugr | 5:06e978fd147a | 23 | void Servo_PWM::operator=(int position) { |
maetugr | 5:06e978fd147a | 24 | SetPosition(position); |
maetugr | 5:06e978fd147a | 25 | } |