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

Fork of Motor by Simon Ford

Committer:
miczyg
Date:
Tue May 19 16:39:03 2015 +0000
Revision:
17:26edc972222e
Parent:
15:025c8e571ff6
Wzmocnienie, period itd ok, wersja P dzia?aj?ca.

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 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
miczyg 17:26edc972222e 31 _pwm.period_us(40);
miczyg 7:d25073d59616 32 _stdby = 1;
simon 0:a470311addc4 33 // Initial condition of output enables
simon 0:a470311addc4 34 _fwd = 0;
simon 0:a470311addc4 35 _rev = 0;
miczyg 4:5370aae8ee3b 36
miczyg 4:5370aae8ee3b 37 //PID config
miczyg 8:d192b38e0d5c 38 //speedControl.setInputLimits(0, 1);
miczyg 8:d192b38e0d5c 39 //speedControl.setOutputLimits(0, 1);
miczyg 8:d192b38e0d5c 40 //speedControl.setMode(AUTO);
miczyg 4:5370aae8ee3b 41
simon 0:a470311addc4 42 }
simon 0:a470311addc4 43
miczyg 4:5370aae8ee3b 44 void Motor::speed(float speed) {
miczyg 7:d25073d59616 45
miczyg 7:d25073d59616 46 /*PID do enkoderow i utrzymywania stalej predkosci*/
michal0074 6:a321d28cce9a 47 /*
miczyg 4:5370aae8ee3b 48 if (speed != set_speed){
miczyg 4:5370aae8ee3b 49 speedControl.setSetPoint(speed);
miczyg 4:5370aae8ee3b 50 set_speed = speed;
miczyg 3:9572ea41a1b4 51 }
miczyg 4:5370aae8ee3b 52
miczyg 4:5370aae8ee3b 53 pulses = encoder.getPulses();
miczyg 4:5370aae8ee3b 54 curr_speed = (pulses-prev_pulses) / max_pulse_rate;
miczyg 4:5370aae8ee3b 55 prev_pulses = pulses;
miczyg 4:5370aae8ee3b 56
miczyg 4:5370aae8ee3b 57 speedControl.setProcessValue(curr_speed);
miczyg 4:5370aae8ee3b 58 speed = speedControl.compute();
michal0074 6:a321d28cce9a 59 */
miczyg 4:5370aae8ee3b 60
miczyg 4:5370aae8ee3b 61 _fwd = (speed > 0.0);
miczyg 4:5370aae8ee3b 62 _rev = (speed < 0.0);
simon 0:a470311addc4 63 _pwm = abs(speed);
simon 0:a470311addc4 64 }