versuch 1

Dependents:   last

Committer:
corsa1600
Date:
Mon Jun 24 05:34:33 2019 +0000
Revision:
0:1b5364ba80c0
Versuch 1

Who changed what in which revision?

UserRevisionLine numberNew contents of line
corsa1600 0:1b5364ba80c0 1 #include "mbed.h"
corsa1600 0:1b5364ba80c0 2
corsa1600 0:1b5364ba80c0 3 static float clamp(float value, float min, float max)
corsa1600 0:1b5364ba80c0 4 {
corsa1600 0:1b5364ba80c0 5 if(value < min)
corsa1600 0:1b5364ba80c0 6 {
corsa1600 0:1b5364ba80c0 7 return min;
corsa1600 0:1b5364ba80c0 8 }
corsa1600 0:1b5364ba80c0 9 else if(value > max)
corsa1600 0:1b5364ba80c0 10 {
corsa1600 0:1b5364ba80c0 11 return max;
corsa1600 0:1b5364ba80c0 12 }
corsa1600 0:1b5364ba80c0 13 else
corsa1600 0:1b5364ba80c0 14 {
corsa1600 0:1b5364ba80c0 15 return value;
corsa1600 0:1b5364ba80c0 16 }
corsa1600 0:1b5364ba80c0 17 }
corsa1600 0:1b5364ba80c0 18
corsa1600 0:1b5364ba80c0 19 Servo::Servo(PinName pin) : _pwm(pin)
corsa1600 0:1b5364ba80c0 20 {
corsa1600 0:1b5364ba80c0 21 calibrate();
corsa1600 0:1b5364ba80c0 22 write(0.5);
corsa1600 0:1b5364ba80c0 23 }
corsa1600 0:1b5364ba80c0 24
corsa1600 0:1b5364ba80c0 25 void Servo::write(float percent)
corsa1600 0:1b5364ba80c0 26 {
corsa1600 0:1b5364ba80c0 27 float offset = _range * 2.0 * (percent - 0.5);
corsa1600 0:1b5364ba80c0 28 _pwm.pulsewidth(0.0015 + clamp(offset, -_range, _range));
corsa1600 0:1b5364ba80c0 29 _p = clamp(percent, 0.0, 1.0);
corsa1600 0:1b5364ba80c0 30 }
corsa1600 0:1b5364ba80c0 31
corsa1600 0:1b5364ba80c0 32 void Servo::position(float degrees)
corsa1600 0:1b5364ba80c0 33 {
corsa1600 0:1b5364ba80c0 34 float offset = _range * (degrees / _degrees);
corsa1600 0:1b5364ba80c0 35 _pwm.pulsewidth(0.0015 + clamp(offset, -_range, _range));
corsa1600 0:1b5364ba80c0 36 }
corsa1600 0:1b5364ba80c0 37
corsa1600 0:1b5364ba80c0 38 void Servo::calibrate(float range, float degrees)
corsa1600 0:1b5364ba80c0 39 {
corsa1600 0:1b5364ba80c0 40 _range = range;
corsa1600 0:1b5364ba80c0 41 _degrees = degrees;
corsa1600 0:1b5364ba80c0 42 }
corsa1600 0:1b5364ba80c0 43
corsa1600 0:1b5364ba80c0 44 float Servo::read()
corsa1600 0:1b5364ba80c0 45 {
corsa1600 0:1b5364ba80c0 46 return _p;
corsa1600 0:1b5364ba80c0 47 }
corsa1600 0:1b5364ba80c0 48
corsa1600 0:1b5364ba80c0 49 Servo& Servo::operator= (float percent)
corsa1600 0:1b5364ba80c0 50 {
corsa1600 0:1b5364ba80c0 51 write(percent);
corsa1600 0:1b5364ba80c0 52 return *this;
corsa1600 0:1b5364ba80c0 53 }
corsa1600 0:1b5364ba80c0 54
corsa1600 0:1b5364ba80c0 55 Servo& Servo::operator= (Servo& rhs)
corsa1600 0:1b5364ba80c0 56 {
corsa1600 0:1b5364ba80c0 57 write(rhs.read());
corsa1600 0:1b5364ba80c0 58 return *this;
corsa1600 0:1b5364ba80c0 59 }
corsa1600 0:1b5364ba80c0 60
corsa1600 0:1b5364ba80c0 61 Servo::operator float()
corsa1600 0:1b5364ba80c0 62 {
corsa1600 0:1b5364ba80c0 63 return read();
corsa1600 0:1b5364ba80c0 64 }