Driver for two motors library

Committer:
cristian_junca
Date:
Wed Jul 29 04:40:37 2020 +0000
Revision:
1:e417951c16d7
Parent:
0:eafb70ae80f7
Library to driver two dc motors;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
cristian_junca 0:eafb70ae80f7 1 #include "mbed.h"
cristian_junca 0:eafb70ae80f7 2 #include "Driver.h"
cristian_junca 0:eafb70ae80f7 3
cristian_junca 1:e417951c16d7 4 Driver::Driver(PinName M1A, PinName M1B, PinName M2A, PinName M2B):
cristian_junca 0:eafb70ae80f7 5 M1A(M1A), M1B(M1B), M2A(M2A), M2B(M2B){
cristian_junca 0:eafb70ae80f7 6 setPeriod_ms(1000);
cristian_junca 0:eafb70ae80f7 7 }
cristian_junca 0:eafb70ae80f7 8
cristian_junca 1:e417951c16d7 9 void Driver::setPeriod_ms(int32_t period){
cristian_junca 0:eafb70ae80f7 10 _period = period;
cristian_junca 0:eafb70ae80f7 11 M1A.period_ms(_period);
cristian_junca 0:eafb70ae80f7 12 M1B.period_ms(_period);
cristian_junca 0:eafb70ae80f7 13 M2A.period_ms(_period);
cristian_junca 0:eafb70ae80f7 14 M2B.period_ms(_period);
cristian_junca 0:eafb70ae80f7 15 }
cristian_junca 0:eafb70ae80f7 16
cristian_junca 1:e417951c16d7 17 void Driver::neutral(){
cristian_junca 0:eafb70ae80f7 18 M1A.write(0);
cristian_junca 0:eafb70ae80f7 19 M1B.write(0);
cristian_junca 0:eafb70ae80f7 20 M2A.write(0);
cristian_junca 0:eafb70ae80f7 21 M2B.write(0);
cristian_junca 0:eafb70ae80f7 22 }
cristian_junca 0:eafb70ae80f7 23
cristian_junca 1:e417951c16d7 24 void Driver::forward(float dutyCycle1, float dutyCycle2){
cristian_junca 0:eafb70ae80f7 25 _dutyCycle1 = dutyCycle1;
cristian_junca 0:eafb70ae80f7 26 _dutyCycle2 = dutyCycle2;
cristian_junca 0:eafb70ae80f7 27 if(_dutyCycle1 > 1.0f)
cristian_junca 0:eafb70ae80f7 28 _dutyCycle1 = 1.0f;
cristian_junca 0:eafb70ae80f7 29 if(_dutyCycle2 > 1.0f)
cristian_junca 0:eafb70ae80f7 30 _dutyCycle2 = 1.0f;
cristian_junca 0:eafb70ae80f7 31 if(_dutyCycle1 < 0)
cristian_junca 0:eafb70ae80f7 32 _dutyCycle1 = 0.0f;
cristian_junca 0:eafb70ae80f7 33 if(_dutyCycle2 < 0)
cristian_junca 0:eafb70ae80f7 34 _dutyCycle2 = 0.0f;
cristian_junca 0:eafb70ae80f7 35
cristian_junca 0:eafb70ae80f7 36 M1A.write(_dutyCycle1);
cristian_junca 0:eafb70ae80f7 37 M1B.write(0);
cristian_junca 0:eafb70ae80f7 38 M2A.write(_dutyCycle2);
cristian_junca 0:eafb70ae80f7 39 M2B.write(0);
cristian_junca 0:eafb70ae80f7 40 }
cristian_junca 0:eafb70ae80f7 41
cristian_junca 1:e417951c16d7 42 void Driver::backward(float dutyCycle1, float dutyCycle2){
cristian_junca 0:eafb70ae80f7 43 _dutyCycle1 = dutyCycle1;
cristian_junca 0:eafb70ae80f7 44 _dutyCycle2 = dutyCycle2;
cristian_junca 0:eafb70ae80f7 45 if(_dutyCycle1 > 1.0f)
cristian_junca 0:eafb70ae80f7 46 _dutyCycle1 = 1.0f;
cristian_junca 0:eafb70ae80f7 47 if(_dutyCycle2 > 1.0f)
cristian_junca 0:eafb70ae80f7 48 _dutyCycle2 = 1.0f;
cristian_junca 0:eafb70ae80f7 49 if(_dutyCycle1 < 0)
cristian_junca 0:eafb70ae80f7 50 _dutyCycle1 = 0.0f;
cristian_junca 0:eafb70ae80f7 51 if(_dutyCycle2 < 0)
cristian_junca 0:eafb70ae80f7 52 _dutyCycle2 = 0.0f;
cristian_junca 0:eafb70ae80f7 53
cristian_junca 0:eafb70ae80f7 54 M1A.write(0);
cristian_junca 0:eafb70ae80f7 55 M1B.write(_dutyCycle1);
cristian_junca 0:eafb70ae80f7 56 M2A.write(0);
cristian_junca 0:eafb70ae80f7 57 M2B.write(_dutyCycle2);
cristian_junca 0:eafb70ae80f7 58 }
cristian_junca 0:eafb70ae80f7 59
cristian_junca 1:e417951c16d7 60 void Driver::right(float dutyCycle1, float dutyCycle2){
cristian_junca 0:eafb70ae80f7 61 _dutyCycle1 = dutyCycle1;
cristian_junca 0:eafb70ae80f7 62 _dutyCycle2 = dutyCycle2;
cristian_junca 0:eafb70ae80f7 63 if(_dutyCycle1 > 1.0f)
cristian_junca 0:eafb70ae80f7 64 _dutyCycle1 = 1.0f;
cristian_junca 0:eafb70ae80f7 65 if(_dutyCycle2 > 1.0f)
cristian_junca 0:eafb70ae80f7 66 _dutyCycle2 = 1.0f;
cristian_junca 0:eafb70ae80f7 67 if(_dutyCycle1 < 0)
cristian_junca 0:eafb70ae80f7 68 _dutyCycle1 = 0.0f;
cristian_junca 0:eafb70ae80f7 69 if(_dutyCycle2 < 0)
cristian_junca 0:eafb70ae80f7 70 _dutyCycle2 = 0.0f;
cristian_junca 0:eafb70ae80f7 71
cristian_junca 0:eafb70ae80f7 72 M1A.write(0);
cristian_junca 0:eafb70ae80f7 73 M1B.write(_dutyCycle1);
cristian_junca 0:eafb70ae80f7 74 M2A.write(_dutyCycle2);
cristian_junca 0:eafb70ae80f7 75 M2B.write(0);
cristian_junca 0:eafb70ae80f7 76 }
cristian_junca 0:eafb70ae80f7 77
cristian_junca 1:e417951c16d7 78 void Driver::left(float dutyCycle1, float dutyCycle2){
cristian_junca 0:eafb70ae80f7 79 _dutyCycle1 = dutyCycle1;
cristian_junca 0:eafb70ae80f7 80 _dutyCycle2 = dutyCycle2;
cristian_junca 0:eafb70ae80f7 81 if(_dutyCycle1 > 1.0f)
cristian_junca 0:eafb70ae80f7 82 _dutyCycle1 = 1.0f;
cristian_junca 0:eafb70ae80f7 83 if(_dutyCycle2 > 1.0f)
cristian_junca 0:eafb70ae80f7 84 _dutyCycle2 = 1.0f;
cristian_junca 0:eafb70ae80f7 85 if(_dutyCycle1 < 0)
cristian_junca 0:eafb70ae80f7 86 _dutyCycle1 = 0.0f;
cristian_junca 0:eafb70ae80f7 87 if(_dutyCycle2 < 0)
cristian_junca 0:eafb70ae80f7 88 _dutyCycle2 = 0.0f;
cristian_junca 0:eafb70ae80f7 89
cristian_junca 0:eafb70ae80f7 90 M1A.write(_dutyCycle1);
cristian_junca 0:eafb70ae80f7 91 M1B.write(0);
cristian_junca 0:eafb70ae80f7 92 M2A.write(0);
cristian_junca 0:eafb70ae80f7 93 M2B.write(_dutyCycle2);
cristian_junca 0:eafb70ae80f7 94 }