Gaëtan Andrieu / Motordriver_Tennis
Committer:
Gaetan_Andrieu
Date:
Fri Jun 14 09:04:03 2019 +0000
Revision:
6:c411a45697f6
Parent:
5:3110b9209d3c
Robot joueur de tennis

Who changed what in which revision?

UserRevisionLine numberNew contents of line
littlexc 1:3da7302dc9ae 1 /*motor driver libary modified from the following libary,
littlexc 1:3da7302dc9ae 2 *
littlexc 1:3da7302dc9ae 3 * mbed simple H-bridge motor controller
littlexc 1:3da7302dc9ae 4 * Copyright (c) 2007-2010, sford
littlexc 1:3da7302dc9ae 5 *
littlexc 1:3da7302dc9ae 6 * by Christopher Hasler.
littlexc 1:3da7302dc9ae 7 *
littlexc 1:3da7302dc9ae 8 * from sford's libary,
littlexc 1:3da7302dc9ae 9 *
littlexc 1:3da7302dc9ae 10 * Permission is hereby granted, free of charge, to any person obtaining a copy
littlexc 1:3da7302dc9ae 11 * of this software and associated documentation files (the "Software"), to deal
littlexc 1:3da7302dc9ae 12 * in the Software without restriction, including without limitation the rights
littlexc 1:3da7302dc9ae 13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
littlexc 1:3da7302dc9ae 14 * copies of the Software, and to permit persons to whom the Software is
littlexc 1:3da7302dc9ae 15 * furnished to do so, subject to the following conditions:
littlexc 1:3da7302dc9ae 16 *
littlexc 1:3da7302dc9ae 17 * The above copyright notice and this permission notice shall be included in
littlexc 1:3da7302dc9ae 18 * all copies or substantial portions of the Software.
littlexc 1:3da7302dc9ae 19 *
littlexc 1:3da7302dc9ae 20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
littlexc 1:3da7302dc9ae 21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
littlexc 1:3da7302dc9ae 22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
littlexc 1:3da7302dc9ae 23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
littlexc 1:3da7302dc9ae 24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
littlexc 1:3da7302dc9ae 25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
littlexc 1:3da7302dc9ae 26 * THE SOFTWARE.
littlexc 1:3da7302dc9ae 27 */
littlexc 0:edc152f119b7 28
littlexc 0:edc152f119b7 29 #include "motordriver.h"
littlexc 1:3da7302dc9ae 30
littlexc 0:edc152f119b7 31 #include "mbed.h"
littlexc 1:3da7302dc9ae 32
littlexc 0:edc152f119b7 33 Motor::Motor(PinName pwm, PinName fwd, PinName rev, int brakeable):
littlexc 0:edc152f119b7 34 _pwm(pwm), _fwd(fwd), _rev(rev) {
littlexc 0:edc152f119b7 35
littlexc 0:edc152f119b7 36 // Set initial condition of PWM
littlexc 0:edc152f119b7 37 _pwm.period(0.001);
littlexc 0:edc152f119b7 38 _pwm = 0;
littlexc 0:edc152f119b7 39
littlexc 0:edc152f119b7 40 // Initial condition of output enables
littlexc 0:edc152f119b7 41 _fwd = 0;
littlexc 0:edc152f119b7 42 _rev = 0;
littlexc 1:3da7302dc9ae 43
littlexc 0:edc152f119b7 44 //set if the motor dirver is capable of braking. (addition)
littlexc 0:edc152f119b7 45 Brakeable= brakeable;
littlexc 1:3da7302dc9ae 46 sign = 0;//i.e nothing.
littlexc 0:edc152f119b7 47 }
Gaetan_Andrieu 6:c411a45697f6 48 float Motor::speed(float speed) {
Gaetan_Andrieu 6:c411a45697f6 49 float temp = 0;
Gaetan_Andrieu 6:c411a45697f6 50 _fwd = (speed > 0.0);
Gaetan_Andrieu 6:c411a45697f6 51 _rev = (speed < 0.0);
Gaetan_Andrieu 6:c411a45697f6 52 temp = abs(speed);
Gaetan_Andrieu 6:c411a45697f6 53 _pwm = temp;
Gaetan_Andrieu 6:c411a45697f6 54 return temp;
Gaetan_Andrieu 6:c411a45697f6 55 }
Gaetan_Andrieu 6:c411a45697f6 56 /*
littlexc 2:2dc873322032 57 float Motor::speed(float speed) {
littlexc 2:2dc873322032 58 float temp = 0;
littlexc 1:3da7302dc9ae 59 if (sign == 0) {
littlexc 1:3da7302dc9ae 60 _fwd = (speed > 0.0);
littlexc 1:3da7302dc9ae 61 _rev = (speed < 0.0);
littlexc 2:2dc873322032 62 temp = abs(speed);
littlexc 2:2dc873322032 63 _pwm = temp;
littlexc 1:3da7302dc9ae 64 } else if (sign == 1) {
littlexc 1:3da7302dc9ae 65 if (speed < 0) {
littlexc 1:3da7302dc9ae 66 _fwd = (speed > 0.0);
littlexc 1:3da7302dc9ae 67 _rev = (speed < 0.0);
littlexc 1:3da7302dc9ae 68 _pwm = 0;
littlexc 2:2dc873322032 69 temp = 0;
littlexc 4:5fb1296c0d60 70 } else {
littlexc 1:3da7302dc9ae 71 _fwd = (speed > 0.0);
littlexc 1:3da7302dc9ae 72 _rev = (speed < 0.0);
littlexc 2:2dc873322032 73 temp = abs(speed);
littlexc 2:2dc873322032 74 _pwm = temp;
littlexc 1:3da7302dc9ae 75 }
littlexc 1:3da7302dc9ae 76 } else if (sign == -1) {
littlexc 1:3da7302dc9ae 77 if (speed > 0) {
littlexc 1:3da7302dc9ae 78 _fwd = (speed > 0.0);
littlexc 1:3da7302dc9ae 79 _rev = (speed < 0.0);
littlexc 1:3da7302dc9ae 80 _pwm = 0;
littlexc 2:2dc873322032 81 temp = 0;
littlexc 1:3da7302dc9ae 82 } else {
littlexc 1:3da7302dc9ae 83 _fwd = (speed > 0.0);
littlexc 1:3da7302dc9ae 84 _rev = (speed < 0.0);
littlexc 2:2dc873322032 85 temp = abs(speed);
littlexc 2:2dc873322032 86 _pwm = temp;
littlexc 1:3da7302dc9ae 87 }
littlexc 1:3da7302dc9ae 88 }
littlexc 1:3da7302dc9ae 89 if (speed > 0)
littlexc 1:3da7302dc9ae 90 sign = 1;
littlexc 1:3da7302dc9ae 91 else if (speed < 0) {
littlexc 1:3da7302dc9ae 92 sign = -1;
littlexc 1:3da7302dc9ae 93 } else if (speed == 0) {
littlexc 1:3da7302dc9ae 94 sign = 0;
littlexc 1:3da7302dc9ae 95 }
littlexc 2:2dc873322032 96 return temp;
Gaetan_Andrieu 6:c411a45697f6 97 }*/
littlexc 0:edc152f119b7 98 // (additions)
littlexc 0:edc152f119b7 99 void Motor::coast(void) {
littlexc 0:edc152f119b7 100 _fwd = 0;
littlexc 0:edc152f119b7 101 _rev = 0;
littlexc 0:edc152f119b7 102 _pwm = 0;
littlexc 1:3da7302dc9ae 103 sign = 0;
littlexc 0:edc152f119b7 104 }
littlexc 0:edc152f119b7 105
littlexc 2:2dc873322032 106 float Motor::stop(float duty) {
littlexc 0:edc152f119b7 107 if (Brakeable == 1) {
littlexc 0:edc152f119b7 108 _fwd = 1;
littlexc 0:edc152f119b7 109 _rev = 1;
littlexc 2:2dc873322032 110 _pwm = duty;
littlexc 1:3da7302dc9ae 111 sign = 0;
littlexc 2:2dc873322032 112 return duty;
littlexc 1:3da7302dc9ae 113 } else
littlexc 5:3110b9209d3c 114 Motor::coast();
littlexc 3:8822f4955035 115 return -1;
littlexc 0:edc152f119b7 116 }
littlexc 1:3da7302dc9ae 117
littlexc 4:5fb1296c0d60 118 float Motor::state(void) {
littlexc 4:5fb1296c0d60 119 if ((_fwd == _rev) && (_pwm > 0)) {
littlexc 4:5fb1296c0d60 120 return -2;//braking
littlexc 4:5fb1296c0d60 121 } else if (_pwm == 0) {
littlexc 4:5fb1296c0d60 122 return 2;//coasting
littlexc 4:5fb1296c0d60 123 } else if ((_fwd == 0) && (_rev == 1)) {
littlexc 4:5fb1296c0d60 124 return -(_pwm);//reversing
littlexc 4:5fb1296c0d60 125 } else if ((_fwd == 1) && (_rev == 0)) {
littlexc 4:5fb1296c0d60 126 return _pwm;//fowards
littlexc 4:5fb1296c0d60 127 } else
littlexc 4:5fb1296c0d60 128 return -3;//error
littlexc 4:5fb1296c0d60 129 }
littlexc 4:5fb1296c0d60 130
Gaetan_Andrieu 6:c411a45697f6 131 float Motor::direction(void) {
Gaetan_Andrieu 6:c411a45697f6 132 if ((_fwd == _rev) && (_pwm > 0)) {
Gaetan_Andrieu 6:c411a45697f6 133 return -2;//braking
Gaetan_Andrieu 6:c411a45697f6 134 } else if (_pwm == 0) {
Gaetan_Andrieu 6:c411a45697f6 135 return 2;//coasting
Gaetan_Andrieu 6:c411a45697f6 136 } else if ((_fwd == 0) && (_rev == 1)) {
Gaetan_Andrieu 6:c411a45697f6 137 return -1;//reversing
Gaetan_Andrieu 6:c411a45697f6 138 } else if ((_fwd == 1) && (_rev == 0)) {
Gaetan_Andrieu 6:c411a45697f6 139 return 1;//fowards
Gaetan_Andrieu 6:c411a45697f6 140 } else
Gaetan_Andrieu 6:c411a45697f6 141 return -3;//error
Gaetan_Andrieu 6:c411a45697f6 142 }
Gaetan_Andrieu 6:c411a45697f6 143
littlexc 0:edc152f119b7 144 /*
littlexc 0:edc152f119b7 145 test code, this demonstrates working motor drivers.
littlexc 1:3da7302dc9ae 146
littlexc 1:3da7302dc9ae 147 Motor A(p22, p6, p5, 1); // pwm, fwd, rev, can break
littlexc 0:edc152f119b7 148 Motor B(p21, p7, p8, 1); // pwm, fwd, rev, can break
littlexc 0:edc152f119b7 149 int main() {
littlexc 0:edc152f119b7 150 for (float s=-1.0; s < 1.0 ; s += 0.01) {
littlexc 1:3da7302dc9ae 151 A.speed(s);
littlexc 1:3da7302dc9ae 152 B.speed(s);
littlexc 0:edc152f119b7 153 wait(0.02);
littlexc 0:edc152f119b7 154 }
littlexc 0:edc152f119b7 155 A.stop();
littlexc 0:edc152f119b7 156 B.stop();
littlexc 0:edc152f119b7 157 wait(1);
littlexc 0:edc152f119b7 158 A.coast();
littlexc 0:edc152f119b7 159 B.coast();
littlexc 0:edc152f119b7 160 }
littlexc 0:edc152f119b7 161 */