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