Control an H-Bridge using a PwmOut (enable) and two DigitalOuts (direction select)

Fork of Motor by Simon Ford

Committer:
michal0074
Date:
Sat May 09 11:04:49 2015 +0000
Revision:
6:a321d28cce9a
Parent:
5:71d7e9c17980
Child:
7:d25073d59616
2165;

Who changed what in which revision?

UserRevisionLine numberNew 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 Motor::Motor(PinName pwm, PinName fwd, PinName rev) :
michal0074 6:a321d28cce9a 27 _pwm(pwm), _fwd(fwd), _rev(rev), 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;
simon 0:a470311addc4 33
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 4:5370aae8ee3b 39 speedControl.setInputLimits(0, 1);
miczyg 4:5370aae8ee3b 40 speedControl.setOutputLimits(0, 1);
miczyg 4:5370aae8ee3b 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) {
michal0074 6:a321d28cce9a 46 /*
miczyg 4:5370aae8ee3b 47 if (speed != set_speed){
miczyg 4:5370aae8ee3b 48 speedControl.setSetPoint(speed);
miczyg 4:5370aae8ee3b 49 set_speed = speed;
miczyg 3:9572ea41a1b4 50 }
miczyg 4:5370aae8ee3b 51
miczyg 4:5370aae8ee3b 52 pulses = encoder.getPulses();
miczyg 4:5370aae8ee3b 53 curr_speed = (pulses-prev_pulses) / max_pulse_rate;
miczyg 4:5370aae8ee3b 54 prev_pulses = pulses;
miczyg 4:5370aae8ee3b 55
miczyg 4:5370aae8ee3b 56 speedControl.setProcessValue(curr_speed);
miczyg 4:5370aae8ee3b 57 speed = speedControl.compute();
michal0074 6:a321d28cce9a 58 */
miczyg 4:5370aae8ee3b 59
miczyg 4:5370aae8ee3b 60 _fwd = (speed > 0.0);
miczyg 4:5370aae8ee3b 61 _rev = (speed < 0.0);
simon 0:a470311addc4 62 _pwm = abs(speed);
simon 0:a470311addc4 63 }