Werkende servoaansturing, smooth met sinus
Dependencies: mbed Servo MODSERIAL FastPWM
Diff: main.cpp
- Revision:
- 5:74962b191242
- Parent:
- 4:8ae0c82c2d60
--- a/main.cpp Fri Oct 11 09:57:47 2019 +0000 +++ b/main.cpp Fri Oct 11 11:06:43 2019 +0000 @@ -1,21 +1,21 @@ #include "mbed.h" -#include "Servo.h" +//#include "Servo.h" +//#include "FastPWM.h" +#include <math.h> Serial pc(USBTX, USBRX); -Servo myservo(D5); +PwmOut myservo(D5); int main() { - while(1) { - for(int i=0; i<100; i++) { - myservo = i/100.0; - wait(0.01); - pc.printf("Naar voren\n\r"); - } - for(int i=100; i>0; i--) { - myservo = i/100.0; - wait(0.01); - pc.printf("Naar achteren\n\r"); - } - } + myservo.period(0.02f); + float t = 0; + while (true) + { + float ref = sin(t); + float pwm = 0.05 + 0.03 * ref; // maximale bereik: allebei 0.05 + myservo.write(pwm); + t+=0.02; // nooit groter dan periode servomotor, zelfde geldt voor regel hieronder + wait(0.02); + } } \ No newline at end of file