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@16:69911e81dfd4, 2022-03-21 (annotated)
- Committer:
- pmic
- Date:
- Mon Mar 21 13:47:46 2022 +0000
- Revision:
- 16:69911e81dfd4
- Parent:
- 10:fe74e8909d3f
- Child:
- 18:21c0a669d6ef
Changed Servo class for normilized input.
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 | 4:9c003c402033 | 3 | Servo::Servo(PinName Pin) : ServoPin(Pin) {} |
| pmic | 0:86129f1b4a93 | 4 | |
| pmic | 10:fe74e8909d3f | 5 | /** |
| pmic | 16:69911e81dfd4 | 6 | * Sets the desired angle. |
| pmic | 16:69911e81dfd4 | 7 | * @_Input a value between 0...1. |
| pmic | 10:fe74e8909d3f | 8 | */ |
| pmic | 16:69911e81dfd4 | 9 | void Servo::SetPosition(float _Input) |
| pmic | 4:9c003c402033 | 10 | { |
| pmic | 16:69911e81dfd4 | 11 | Position = static_cast<int>(_Input * static_cast<float>(Period)); |
| pmic | 4:9c003c402033 | 12 | } |
| pmic | 0:86129f1b4a93 | 13 | |
| pmic | 4:9c003c402033 | 14 | void Servo::StartPulse() |
| pmic | 4:9c003c402033 | 15 | { |
| pmic | 4:9c003c402033 | 16 | ServoPin = 1; |
| pmic | 10:fe74e8909d3f | 17 | PulseStop.attach(callback(this, &Servo::EndPulse), std::chrono::microseconds{static_cast<long int>(Position)}); |
| pmic | 4:9c003c402033 | 18 | } |
| pmic | 0:86129f1b4a93 | 19 | |
| pmic | 4:9c003c402033 | 20 | void Servo::EndPulse() |
| pmic | 4:9c003c402033 | 21 | { |
| pmic | 4:9c003c402033 | 22 | ServoPin = 0; |
| pmic | 4:9c003c402033 | 23 | } |
| pmic | 0:86129f1b4a93 | 24 | |
| pmic | 10:fe74e8909d3f | 25 | /** |
| pmic | 16:69911e81dfd4 | 26 | * Enables the servo with start angle and period. |
| pmic | 16:69911e81dfd4 | 27 | * @_StartInput a value between 0...1. |
| pmic | 16:69911e81dfd4 | 28 | * @_Period period in mus. |
| pmic | 10:fe74e8909d3f | 29 | */ |
| pmic | 16:69911e81dfd4 | 30 | void Servo::Enable(float _StartInput, int _Period) |
| pmic | 4:9c003c402033 | 31 | { |
| pmic | 16:69911e81dfd4 | 32 | Period = _Period; |
| pmic | 16:69911e81dfd4 | 33 | Position = static_cast<int>(_StartInput * static_cast<float>(Period)); |
| pmic | 10:fe74e8909d3f | 34 | Pulse.attach(callback(this, &Servo::StartPulse), std::chrono::microseconds{static_cast<long int>(Period)}); |
| pmic | 4:9c003c402033 | 35 | } |
| pmic | 0:86129f1b4a93 | 36 | |
| pmic | 10:fe74e8909d3f | 37 | /** |
| pmic | 10:fe74e8909d3f | 38 | * Disables the servo. |
| pmic | 10:fe74e8909d3f | 39 | */ |
| pmic | 4:9c003c402033 | 40 | void Servo::Disable() |
| pmic | 4:9c003c402033 | 41 | { |
| pmic | 4:9c003c402033 | 42 | Pulse.detach(); |
| pmic | 4:9c003c402033 | 43 | } |
