Control an H-Bridge using a PwmOut (enable) and two DigitalOuts (direction select)
Fork of Motor by
Motor.cpp@9:9b00a28bc790, 2015-05-15 (annotated)
- Committer:
- miczyg
- Date:
- Fri May 15 13:44:38 2015 +0000
- Revision:
- 9:9b00a28bc790
- Parent:
- 8:d192b38e0d5c
- Child:
- 10:f22e6393af83
jk
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 | 7:d25073d59616 | 26 | Motor::Motor(PinName pwm, PinName fwd, PinName rev, PinName stby) : |
miczyg | 8:d192b38e0d5c | 27 | _pwm(pwm), _fwd(fwd), _rev(rev), _stdby(stby)//, speedControl(K, Ti, Td, itv)//, encoder(CH_A, CH_B, NC, pPerRev) |
miczyg | 3:9572ea41a1b4 | 28 | { |
simon | 0:a470311addc4 | 29 | |
simon | 0:a470311addc4 | 30 | // Set initial condition of PWM |
simon | 0:a470311addc4 | 31 | _pwm.period(0.001); |
simon | 0:a470311addc4 | 32 | _pwm = 0; |
miczyg | 7:d25073d59616 | 33 | _stdby = 1; |
simon | 0:a470311addc4 | 34 | // Initial condition of output enables |
simon | 0:a470311addc4 | 35 | _fwd = 0; |
simon | 0:a470311addc4 | 36 | _rev = 0; |
miczyg | 4:5370aae8ee3b | 37 | |
miczyg | 4:5370aae8ee3b | 38 | //PID config |
miczyg | 8:d192b38e0d5c | 39 | //speedControl.setInputLimits(0, 1); |
miczyg | 8:d192b38e0d5c | 40 | //speedControl.setOutputLimits(0, 1); |
miczyg | 8:d192b38e0d5c | 41 | //speedControl.setMode(AUTO); |
miczyg | 4:5370aae8ee3b | 42 | |
simon | 0:a470311addc4 | 43 | } |
simon | 0:a470311addc4 | 44 | |
miczyg | 4:5370aae8ee3b | 45 | void Motor::speed(float speed) { |
miczyg | 7:d25073d59616 | 46 | |
miczyg | 7:d25073d59616 | 47 | /*PID do enkoderow i utrzymywania stalej predkosci*/ |
michal0074 | 6:a321d28cce9a | 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(); |
michal0074 | 6:a321d28cce9a | 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 | } |