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