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.
Motor.cpp@2:11b3ef6b937d, 2017-10-29 (annotated)
- Committer:
- calmantara186
- Date:
- Sun Oct 29 06:04:49 2017 +0000
- Revision:
- 2:11b3ef6b937d
- Parent:
- 1:c75b234558af
- Child:
- 3:143494304b66
UPDATE
Who changed what in which revision?
| User | Revision | Line number | New contents of line | 
|---|---|---|---|
| aberk | 0:f05e09f8f5d9 | 1 | /* mbed simple H-bridge motor controller | 
| aberk | 0:f05e09f8f5d9 | 2 | * Copyright (c) 2007-2010, sford | 
| aberk | 0:f05e09f8f5d9 | 3 | * | 
| aberk | 0:f05e09f8f5d9 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy | 
| aberk | 0:f05e09f8f5d9 | 5 | * of this software and associated documentation files (the "Software"), to deal | 
| aberk | 0:f05e09f8f5d9 | 6 | * in the Software without restriction, including without limitation the rights | 
| aberk | 0:f05e09f8f5d9 | 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | 
| aberk | 0:f05e09f8f5d9 | 8 | * copies of the Software, and to permit persons to whom the Software is | 
| aberk | 0:f05e09f8f5d9 | 9 | * furnished to do so, subject to the following conditions: | 
| aberk | 0:f05e09f8f5d9 | 10 | * | 
| aberk | 0:f05e09f8f5d9 | 11 | * The above copyright notice and this permission notice shall be included in | 
| aberk | 0:f05e09f8f5d9 | 12 | * all copies or substantial portions of the Software. | 
| aberk | 0:f05e09f8f5d9 | 13 | * | 
| aberk | 0:f05e09f8f5d9 | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | 
| aberk | 0:f05e09f8f5d9 | 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | 
| aberk | 0:f05e09f8f5d9 | 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | 
| aberk | 0:f05e09f8f5d9 | 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | 
| aberk | 0:f05e09f8f5d9 | 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | 
| aberk | 0:f05e09f8f5d9 | 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | 
| aberk | 0:f05e09f8f5d9 | 20 | * THE SOFTWARE. | 
| aberk | 0:f05e09f8f5d9 | 21 | */ | 
| aberk | 0:f05e09f8f5d9 | 22 | |
| aberk | 0:f05e09f8f5d9 | 23 | #include "Motor.h" | 
| aberk | 0:f05e09f8f5d9 | 24 | |
| aberk | 0:f05e09f8f5d9 | 25 | Motor::Motor(PinName pwm, PinName fwd, PinName rev): | 
| aberk | 0:f05e09f8f5d9 | 26 | _pwm(pwm), _fwd(fwd), _rev(rev) { | 
| aberk | 0:f05e09f8f5d9 | 27 | |
| aberk | 0:f05e09f8f5d9 | 28 | // Set initial condition of PWM | 
| aberk | 0:f05e09f8f5d9 | 29 | _pwm.period(0.001); | 
| aberk | 0:f05e09f8f5d9 | 30 | _pwm = 0; | 
| aberk | 0:f05e09f8f5d9 | 31 | |
| aberk | 0:f05e09f8f5d9 | 32 | // Initial condition of output enables | 
| aberk | 0:f05e09f8f5d9 | 33 | _fwd = 0; | 
| aberk | 0:f05e09f8f5d9 | 34 | _rev = 0; | 
| aberk | 0:f05e09f8f5d9 | 35 | } | 
| aberk | 0:f05e09f8f5d9 | 36 | |
| aberk | 0:f05e09f8f5d9 | 37 | void Motor::speed(float speed) { | 
| calmantara186 | 2:11b3ef6b937d | 38 | _fwd = (speed > (float)0.0); | 
| calmantara186 | 2:11b3ef6b937d | 39 | _rev = (speed < (float)0.0); | 
| aberk | 0:f05e09f8f5d9 | 40 | _pwm = abs(speed); | 
| aberk | 0:f05e09f8f5d9 | 41 | } | 
| aberk | 0:f05e09f8f5d9 | 42 | |
| aberk | 0:f05e09f8f5d9 | 43 | void Motor::period(float period){ | 
| aberk | 0:f05e09f8f5d9 | 44 | |
| aberk | 0:f05e09f8f5d9 | 45 | _pwm.period(period); | 
| aberk | 0:f05e09f8f5d9 | 46 | |
| aberk | 0:f05e09f8f5d9 | 47 | } | 
| aberk | 1:c75b234558af | 48 | |
| aberk | 1:c75b234558af | 49 | void Motor::brake(int highLow){ | 
| aberk | 1:c75b234558af | 50 | |
| aberk | 1:c75b234558af | 51 | if(highLow == BRAKE_HIGH){ | 
| aberk | 1:c75b234558af | 52 | _fwd = 1; | 
| aberk | 1:c75b234558af | 53 | _rev = 1; | 
| aberk | 1:c75b234558af | 54 | } | 
| aberk | 1:c75b234558af | 55 | else if(highLow == BRAKE_LOW){ | 
| aberk | 1:c75b234558af | 56 | _fwd = 0; | 
| aberk | 1:c75b234558af | 57 | _rev = 0; | 
| aberk | 1:c75b234558af | 58 | } | 
| aberk | 1:c75b234558af | 59 | |
| aberk | 1:c75b234558af | 60 | } |