Control an H-Bridge using a PwmOut (enable) and two DigitalOuts (direction select)
Fork of Motor by
Motor.cpp@4:5370aae8ee3b, 2015-05-07 (annotated)
- Committer:
- miczyg
- Date:
- Thu May 07 09:47:58 2015 +0000
- Revision:
- 4:5370aae8ee3b
- Parent:
- 3:9572ea41a1b4
- Child:
- 5:71d7e9c17980
asd
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
simon | 0:a470311addc4 | 1 | /* mbed simple H-bridge motor controller |
miczyg | 4:5370aae8ee3b | 2 | * Copyright (c) 2007-2010, sford, http://mbed.org |
miczyg | 4:5370aae8ee3b | 3 | * |
miczyg | 4:5370aae8ee3b | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
miczyg | 4:5370aae8ee3b | 5 | * of this software and associated documentation files (the "Software"), to deal |
miczyg | 4:5370aae8ee3b | 6 | * in the Software without restriction, including without limitation the rights |
miczyg | 4:5370aae8ee3b | 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
miczyg | 4:5370aae8ee3b | 8 | * copies of the Software, and to permit persons to whom the Software is |
miczyg | 4:5370aae8ee3b | 9 | * furnished to do so, subject to the following conditions: |
miczyg | 4:5370aae8ee3b | 10 | * |
miczyg | 4:5370aae8ee3b | 11 | * The above copyright notice and this permission notice shall be included in |
miczyg | 4:5370aae8ee3b | 12 | * all copies or substantial portions of the Software. |
miczyg | 4:5370aae8ee3b | 13 | * |
miczyg | 4:5370aae8ee3b | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
miczyg | 4:5370aae8ee3b | 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
miczyg | 4:5370aae8ee3b | 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
miczyg | 4:5370aae8ee3b | 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
miczyg | 4:5370aae8ee3b | 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
miczyg | 4:5370aae8ee3b | 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
miczyg | 4:5370aae8ee3b | 20 | * THE SOFTWARE. |
miczyg | 4:5370aae8ee3b | 21 | */ |
simon | 0:a470311addc4 | 22 | |
simon | 0:a470311addc4 | 23 | #include "Motor.h" |
simon | 0:a470311addc4 | 24 | #include "mbed.h" |
simon | 0:a470311addc4 | 25 | |
miczyg | 4:5370aae8ee3b | 26 | #define AUTO 1 |
miczyg | 4:5370aae8ee3b | 27 | |
miczyg | 4:5370aae8ee3b | 28 | Motor::Motor(PinName pwm, PinName fwd, PinName rev) : |
miczyg | 4:5370aae8ee3b | 29 | _pwm(pwm), _fwd(fwd), _rev(rev), speedControl(K, Ti, Td, itv), encoder(CH_A, CH_B, NC, pPerRev) |
miczyg | 3:9572ea41a1b4 | 30 | { |
simon | 0:a470311addc4 | 31 | |
simon | 0:a470311addc4 | 32 | // Set initial condition of PWM |
simon | 0:a470311addc4 | 33 | _pwm.period(0.001); |
simon | 0:a470311addc4 | 34 | _pwm = 0; |
simon | 0:a470311addc4 | 35 | |
simon | 0:a470311addc4 | 36 | // Initial condition of output enables |
simon | 0:a470311addc4 | 37 | _fwd = 0; |
simon | 0:a470311addc4 | 38 | _rev = 0; |
miczyg | 4:5370aae8ee3b | 39 | |
miczyg | 4:5370aae8ee3b | 40 | //PID config |
miczyg | 4:5370aae8ee3b | 41 | speedControl.setInputLimits(0, 1); |
miczyg | 4:5370aae8ee3b | 42 | speedControl.setOutputLimits(0, 1); |
miczyg | 4:5370aae8ee3b | 43 | speedControl.setMode(AUTO); |
miczyg | 4:5370aae8ee3b | 44 | |
simon | 0:a470311addc4 | 45 | } |
simon | 0:a470311addc4 | 46 | |
miczyg | 4:5370aae8ee3b | 47 | void Motor::speed(float speed) { |
miczyg | 4:5370aae8ee3b | 48 | |
miczyg | 4:5370aae8ee3b | 49 | if (speed != set_speed){ |
miczyg | 4:5370aae8ee3b | 50 | speedControl.setSetPoint(speed); |
miczyg | 4:5370aae8ee3b | 51 | set_speed = speed; |
miczyg | 3:9572ea41a1b4 | 52 | } |
miczyg | 4:5370aae8ee3b | 53 | |
miczyg | 4:5370aae8ee3b | 54 | pulses = encoder.getPulses(); |
miczyg | 4:5370aae8ee3b | 55 | curr_speed = (pulses-prev_pulses) / max_pulse_rate; |
miczyg | 4:5370aae8ee3b | 56 | prev_pulses = pulses; |
miczyg | 4:5370aae8ee3b | 57 | |
miczyg | 4:5370aae8ee3b | 58 | speedControl.setProcessValue(curr_speed); |
miczyg | 4:5370aae8ee3b | 59 | speed = speedControl.compute(); |
miczyg | 4:5370aae8ee3b | 60 | |
miczyg | 4:5370aae8ee3b | 61 | |
miczyg | 4:5370aae8ee3b | 62 | _fwd = (speed > 0.0); |
miczyg | 4:5370aae8ee3b | 63 | _rev = (speed < 0.0); |
simon | 0:a470311addc4 | 64 | _pwm = abs(speed); |
simon | 0:a470311addc4 | 65 | } |
simon | 0:a470311addc4 | 66 | |
simon | 0:a470311addc4 | 67 | |
simon | 0:a470311addc4 | 68 |