Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: wifi-ism43362
Dependents: DigitalOut DigitalOut
Servo/Servo.cpp@16:0a60a1530987, 2020-05-26 (annotated)
- Committer:
- marcel1691
- Date:
- Tue May 26 17:34:16 2020 +0000
- Revision:
- 16:0a60a1530987
- Parent:
- 14:b0a7f17c5a29
Ohne BMP180 Wrapper
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| marcel1691 | 14:b0a7f17c5a29 | 1 | /* mbed simple Servo controller |
| marcel1691 | 14:b0a7f17c5a29 | 2 | * Copyright (c) 2019, marcel.bernet@ch-open.ch |
| marcel1691 | 14:b0a7f17c5a29 | 3 | * |
| marcel1691 | 14:b0a7f17c5a29 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| marcel1691 | 14:b0a7f17c5a29 | 5 | * of this software and associated documentation files (the "Software"), to deal |
| marcel1691 | 14:b0a7f17c5a29 | 6 | * in the Software without restriction, including without limitation the rights |
| marcel1691 | 14:b0a7f17c5a29 | 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| marcel1691 | 14:b0a7f17c5a29 | 8 | * copies of the Software, and to permit persons to whom the Software is |
| marcel1691 | 14:b0a7f17c5a29 | 9 | * furnished to do so, subject to the following conditions: |
| marcel1691 | 14:b0a7f17c5a29 | 10 | * |
| marcel1691 | 14:b0a7f17c5a29 | 11 | * The above copyright notice and this permission notice shall be included in |
| marcel1691 | 14:b0a7f17c5a29 | 12 | * all copies or substantial portions of the Software. |
| marcel1691 | 14:b0a7f17c5a29 | 13 | * |
| marcel1691 | 14:b0a7f17c5a29 | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| marcel1691 | 14:b0a7f17c5a29 | 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| marcel1691 | 14:b0a7f17c5a29 | 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| marcel1691 | 14:b0a7f17c5a29 | 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| marcel1691 | 14:b0a7f17c5a29 | 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| marcel1691 | 14:b0a7f17c5a29 | 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| marcel1691 | 14:b0a7f17c5a29 | 20 | * THE SOFTWARE. |
| marcel1691 | 14:b0a7f17c5a29 | 21 | */ |
| marcel1691 | 14:b0a7f17c5a29 | 22 | |
| marcel1691 | 14:b0a7f17c5a29 | 23 | #include "mbed.h" |
| marcel1691 | 14:b0a7f17c5a29 | 24 | #include "Servo.h" |
| marcel1691 | 14:b0a7f17c5a29 | 25 | |
| marcel1691 | 14:b0a7f17c5a29 | 26 | |
| marcel1691 | 14:b0a7f17c5a29 | 27 | |
| marcel1691 | 14:b0a7f17c5a29 | 28 | Servo::Servo(PinName pin) : _pwm(pin) |
| marcel1691 | 14:b0a7f17c5a29 | 29 | { |
| marcel1691 | 14:b0a7f17c5a29 | 30 | _pwm.period_ms(20); |
| marcel1691 | 14:b0a7f17c5a29 | 31 | write(0.5); |
| marcel1691 | 14:b0a7f17c5a29 | 32 | } |
| marcel1691 | 14:b0a7f17c5a29 | 33 | |
| marcel1691 | 14:b0a7f17c5a29 | 34 | void Servo::write(float percent) |
| marcel1691 | 14:b0a7f17c5a29 | 35 | { |
| marcel1691 | 14:b0a7f17c5a29 | 36 | _p = percent; |
| marcel1691 | 14:b0a7f17c5a29 | 37 | float p = percent * (SERVO_MAX-SERVO_MIN) + SERVO_MIN; |
| marcel1691 | 14:b0a7f17c5a29 | 38 | p = (p < SERVO_MIN) ? SERVO_MIN : p; |
| marcel1691 | 14:b0a7f17c5a29 | 39 | p = (p > SERVO_MAX) ? SERVO_MAX : p; |
| marcel1691 | 14:b0a7f17c5a29 | 40 | _pwm.pulsewidth_us( (int) p ); |
| marcel1691 | 14:b0a7f17c5a29 | 41 | } |
| marcel1691 | 14:b0a7f17c5a29 | 42 | |
| marcel1691 | 14:b0a7f17c5a29 | 43 | float Servo::read() |
| marcel1691 | 14:b0a7f17c5a29 | 44 | { |
| marcel1691 | 14:b0a7f17c5a29 | 45 | return _p; |
| marcel1691 | 14:b0a7f17c5a29 | 46 | } |
| marcel1691 | 14:b0a7f17c5a29 | 47 | |
| marcel1691 | 14:b0a7f17c5a29 | 48 | Servo& Servo::operator= (float percent) |
| marcel1691 | 14:b0a7f17c5a29 | 49 | { |
| marcel1691 | 14:b0a7f17c5a29 | 50 | write(percent); |
| marcel1691 | 14:b0a7f17c5a29 | 51 | return *this; |
| marcel1691 | 14:b0a7f17c5a29 | 52 | } |
| marcel1691 | 14:b0a7f17c5a29 | 53 | |
| marcel1691 | 14:b0a7f17c5a29 | 54 | Servo& Servo::operator= (Servo& rhs) |
| marcel1691 | 14:b0a7f17c5a29 | 55 | { |
| marcel1691 | 14:b0a7f17c5a29 | 56 | write(rhs.read()); |
| marcel1691 | 14:b0a7f17c5a29 | 57 | return *this; |
| marcel1691 | 14:b0a7f17c5a29 | 58 | } |
| marcel1691 | 14:b0a7f17c5a29 | 59 | |
| marcel1691 | 14:b0a7f17c5a29 | 60 | Servo::operator float() |
| marcel1691 | 14:b0a7f17c5a29 | 61 | { |
| marcel1691 | 14:b0a7f17c5a29 | 62 | return read(); |
| marcel1691 | 14:b0a7f17c5a29 | 63 | } |
| marcel1691 | 14:b0a7f17c5a29 | 64 | |
| marcel1691 | 14:b0a7f17c5a29 | 65 | |
| marcel1691 | 14:b0a7f17c5a29 | 66 |