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