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: LSM9DS1 RangeFinder FastPWM
Dependents: PM2_Example_PES_board PM2_Example_PES_board PM2_Example_PES_board PM2_Example_PES_board ... more
Servo.cpp@18:21c0a669d6ef, 2022-03-22 (annotated)
- Committer:
- pmic
- Date:
- Tue Mar 22 08:33:25 2022 +0000
- Revision:
- 18:21c0a669d6ef
- Parent:
- 16:69911e81dfd4
- Child:
- 19:518ed284d98b
Introduced min and max value for input in Servo class (bugfix)
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| pmic | 0:86129f1b4a93 | 1 | #include "Servo.h" |
| pmic | 4:9c003c402033 | 2 | |
| pmic | 18:21c0a669d6ef | 3 | const float Servo::MIN_INPUT = 0.01f; |
| pmic | 18:21c0a669d6ef | 4 | const float Servo::MAX_INPUT = 0.99f; |
| pmic | 18:21c0a669d6ef | 5 | |
| pmic | 4:9c003c402033 | 6 | Servo::Servo(PinName Pin) : ServoPin(Pin) {} |
| pmic | 0:86129f1b4a93 | 7 | |
| pmic | 10:fe74e8909d3f | 8 | /** |
| pmic | 16:69911e81dfd4 | 9 | * Sets the desired angle. |
| pmic | 16:69911e81dfd4 | 10 | * @_Input a value between 0...1. |
| pmic | 10:fe74e8909d3f | 11 | */ |
| pmic | 16:69911e81dfd4 | 12 | void Servo::SetPosition(float _Input) |
| pmic | 4:9c003c402033 | 13 | { |
| pmic | 18:21c0a669d6ef | 14 | if (_Input < MIN_INPUT) _Input = MIN_INPUT; |
| pmic | 18:21c0a669d6ef | 15 | if (_Input > MAX_INPUT) _Input = MAX_INPUT; |
| pmic | 16:69911e81dfd4 | 16 | Position = static_cast<int>(_Input * static_cast<float>(Period)); |
| pmic | 18:21c0a669d6ef | 17 | printf("Position: %d\r\n", Position); |
| pmic | 4:9c003c402033 | 18 | } |
| pmic | 0:86129f1b4a93 | 19 | |
| pmic | 4:9c003c402033 | 20 | void Servo::StartPulse() |
| pmic | 4:9c003c402033 | 21 | { |
| pmic | 4:9c003c402033 | 22 | ServoPin = 1; |
| pmic | 10:fe74e8909d3f | 23 | PulseStop.attach(callback(this, &Servo::EndPulse), std::chrono::microseconds{static_cast<long int>(Position)}); |
| pmic | 4:9c003c402033 | 24 | } |
| pmic | 0:86129f1b4a93 | 25 | |
| pmic | 4:9c003c402033 | 26 | void Servo::EndPulse() |
| pmic | 4:9c003c402033 | 27 | { |
| pmic | 4:9c003c402033 | 28 | ServoPin = 0; |
| pmic | 4:9c003c402033 | 29 | } |
| pmic | 0:86129f1b4a93 | 30 | |
| pmic | 10:fe74e8909d3f | 31 | /** |
| pmic | 16:69911e81dfd4 | 32 | * Enables the servo with start angle and period. |
| pmic | 16:69911e81dfd4 | 33 | * @_StartInput a value between 0...1. |
| pmic | 16:69911e81dfd4 | 34 | * @_Period period in mus. |
| pmic | 10:fe74e8909d3f | 35 | */ |
| pmic | 16:69911e81dfd4 | 36 | void Servo::Enable(float _StartInput, int _Period) |
| pmic | 4:9c003c402033 | 37 | { |
| pmic | 18:21c0a669d6ef | 38 | if (_StartInput < MIN_INPUT) _StartInput = MIN_INPUT; |
| pmic | 18:21c0a669d6ef | 39 | if (_StartInput > MAX_INPUT) _StartInput = MAX_INPUT; |
| pmic | 16:69911e81dfd4 | 40 | Period = _Period; |
| pmic | 16:69911e81dfd4 | 41 | Position = static_cast<int>(_StartInput * static_cast<float>(Period)); |
| pmic | 18:21c0a669d6ef | 42 | printf("Position: %d\r\n", Position); |
| pmic | 10:fe74e8909d3f | 43 | Pulse.attach(callback(this, &Servo::StartPulse), std::chrono::microseconds{static_cast<long int>(Period)}); |
| pmic | 4:9c003c402033 | 44 | } |
| pmic | 0:86129f1b4a93 | 45 | |
| pmic | 10:fe74e8909d3f | 46 | /** |
| pmic | 10:fe74e8909d3f | 47 | * Disables the servo. |
| pmic | 10:fe74e8909d3f | 48 | */ |
| pmic | 4:9c003c402033 | 49 | void Servo::Disable() |
| pmic | 4:9c003c402033 | 50 | { |
| pmic | 4:9c003c402033 | 51 | Pulse.detach(); |
| pmic | 4:9c003c402033 | 52 | } |
