Drive Motor Group B4-B5

Fork of ArduMotoShield by Didier Donsez

Committer:
NorNick
Date:
Wed Dec 09 04:08:22 2015 +0000
Revision:
1:1c712818d82a
Parent:
0:72e45c332025
For Finish

Who changed what in which revision?

UserRevisionLine numberNew contents of line
donsez 0:72e45c332025 1 /*
donsez 0:72e45c332025 2 Simple library for the SparkFun Ardumoto Shield https://www.sparkfun.com/products/9815
donsez 0:72e45c332025 3 with the ST Nucleo F401RE
donsez 0:72e45c332025 4
donsez 0:72e45c332025 5 The SparkFun Ardumoto shield can control two DC motors (up to 2 amps per motor). It is based on the L298 H-bridge.
donsez 0:72e45c332025 6
donsez 0:72e45c332025 7 Developped by : Didier Donsez & Jérome Maisonnasse
donsez 0:72e45c332025 8
donsez 0:72e45c332025 9 License: CC-SA 3.0, feel free to use this code however you'd like.
donsez 0:72e45c332025 10 Please improve upon it! Let me know how you've made it better.
donsez 0:72e45c332025 11
donsez 0:72e45c332025 12 This is really simple example code to get you some basic functionality with the Ardumoto Shield.
donsez 0:72e45c332025 13
donsez 0:72e45c332025 14 */
donsez 0:72e45c332025 15
donsez 0:72e45c332025 16 #include "ArduMotoShield.h"
donsez 0:72e45c332025 17 #include "mbed.h"
donsez 0:72e45c332025 18
donsez 0:72e45c332025 19 // depend of the Vin and the max voltage of the motors
donsez 0:72e45c332025 20 float MAXPWM = 0.5f;
donsez 0:72e45c332025 21
donsez 0:72e45c332025 22 #define LOW 0
donsez 0:72e45c332025 23 #define HIGH 1
donsez 0:72e45c332025 24
NorNick 1:1c712818d82a 25 //DigitalOut inApin(D12, LOW); //direction control for motor outputs 1 and 2 is on digital pin 12
NorNick 1:1c712818d82a 26 //DigitalOut inBpin(D13, LOW); //direction control for motor outputs 3 and 4 is on digital pin 13
NorNick 1:1c712818d82a 27 BusOut inApin(D11,D12);
NorNick 1:1c712818d82a 28 BusOut inBpin(D13,D14);
donsez 0:72e45c332025 29 PwmOut pwmApin(D3); //PWM control for motor outputs 1 and 2 is on digital pin 3
NorNick 1:1c712818d82a 30 PwmOut pwmBpin(D5); //PWM control for motor outputs 3 and 4 is on digital pin 11
donsez 0:72e45c332025 31
donsez 0:72e45c332025 32 bool isSetup=false;
donsez 0:72e45c332025 33
donsez 0:72e45c332025 34 void ArduMotoShield::setVoltages(float vin, float vmaxmotor)
donsez 0:72e45c332025 35 {
donsez 0:72e45c332025 36
donsez 0:72e45c332025 37 if(vin<vmaxmotor)
donsez 0:72e45c332025 38 MAXPWM=1.0f;
donsez 0:72e45c332025 39 else
donsez 0:72e45c332025 40 MAXPWM=vmaxmotor/vin;
donsez 0:72e45c332025 41 }
donsez 0:72e45c332025 42
donsez 0:72e45c332025 43
donsez 0:72e45c332025 44 void ArduMotoShield::setup()
donsez 0:72e45c332025 45 {
donsez 0:72e45c332025 46 pwmApin.period_ms(10);
donsez 0:72e45c332025 47 pwmApin.pulsewidth_ms(1);
donsez 0:72e45c332025 48 pwmApin.write(0.0f);
donsez 0:72e45c332025 49
donsez 0:72e45c332025 50 pwmBpin.period_ms(10);
donsez 0:72e45c332025 51 pwmBpin.pulsewidth_ms(1);
donsez 0:72e45c332025 52 pwmBpin.write(0.0f);
donsez 0:72e45c332025 53
donsez 0:72e45c332025 54 isSetup=true;
donsez 0:72e45c332025 55 }
donsez 0:72e45c332025 56
donsez 0:72e45c332025 57 // Basic ArduMotoShield operations
donsez 0:72e45c332025 58
NorNick 1:1c712818d82a 59 void ArduMotoShield::forward(float A_PWM,float B_PWM) //full speed forward
donsez 0:72e45c332025 60 {
donsez 0:72e45c332025 61 if(!isSetup) setup();
donsez 0:72e45c332025 62
NorNick 1:1c712818d82a 63 //inApin.write(LOW);
NorNick 1:1c712818d82a 64 //inBpin.write(LOW);
NorNick 1:1c712818d82a 65
NorNick 1:1c712818d82a 66 inApin=1;
NorNick 1:1c712818d82a 67 inBpin=1;
NorNick 1:1c712818d82a 68 pwmApin.write(A_PWM);//(MAXPWM);
NorNick 1:1c712818d82a 69 pwmBpin.write(B_PWM);//(MAXPWM);
NorNick 1:1c712818d82a 70 //printf("Forward : %.2f\n",AnaPWM);
donsez 0:72e45c332025 71 }
donsez 0:72e45c332025 72
NorNick 1:1c712818d82a 73 void ArduMotoShield::backward(float A_PWM,float B_PWM) //full speed backward
donsez 0:72e45c332025 74 {
donsez 0:72e45c332025 75 if(!isSetup) setup();
donsez 0:72e45c332025 76
NorNick 1:1c712818d82a 77 //inApin.write(HIGH);
NorNick 1:1c712818d82a 78 //inBpin.write(HIGH);
NorNick 1:1c712818d82a 79
NorNick 1:1c712818d82a 80 inApin=2;
NorNick 1:1c712818d82a 81 inBpin=2;
NorNick 1:1c712818d82a 82 pwmApin.write(A_PWM);//(MAXPWM);
NorNick 1:1c712818d82a 83 pwmBpin.write(B_PWM);//(MAXPWM);
NorNick 1:1c712818d82a 84 //printf("Backward\n");
NorNick 1:1c712818d82a 85 }
NorNick 1:1c712818d82a 86
NorNick 1:1c712818d82a 87 void ArduMotoShield::right(float A_PWM,float B_PWM)
NorNick 1:1c712818d82a 88 {
NorNick 1:1c712818d82a 89 if(!isSetup) setup();
NorNick 1:1c712818d82a 90
NorNick 1:1c712818d82a 91 //inApin.write(HIGH);
NorNick 1:1c712818d82a 92 //inBpin.write(LOW);
NorNick 1:1c712818d82a 93
NorNick 1:1c712818d82a 94 inApin=1;
NorNick 1:1c712818d82a 95 inBpin=2;
NorNick 1:1c712818d82a 96 pwmApin.write(A_PWM);//(MAXPWM);
NorNick 1:1c712818d82a 97 pwmBpin.write(B_PWM);//(MAXPWM);
NorNick 1:1c712818d82a 98 //printf("Right\n");
NorNick 1:1c712818d82a 99 }
NorNick 1:1c712818d82a 100
NorNick 1:1c712818d82a 101 void ArduMotoShield::left(float A_PWM,float B_PWM)
NorNick 1:1c712818d82a 102 {
NorNick 1:1c712818d82a 103 if(!isSetup) setup();
NorNick 1:1c712818d82a 104
NorNick 1:1c712818d82a 105 //inApin.write(LOW);
NorNick 1:1c712818d82a 106 //inBpin.write(HIGH);
NorNick 1:1c712818d82a 107
NorNick 1:1c712818d82a 108 inApin=2;
NorNick 1:1c712818d82a 109 inBpin=1;
NorNick 1:1c712818d82a 110 pwmApin.write(A_PWM);//(MAXPWM);
NorNick 1:1c712818d82a 111 pwmBpin.write(B_PWM);//(MAXPWM);
NorNick 1:1c712818d82a 112 //printf("Left\n");
donsez 0:72e45c332025 113 }
donsez 0:72e45c332025 114
donsez 0:72e45c332025 115 void ArduMotoShield::stop()
donsez 0:72e45c332025 116 {
donsez 0:72e45c332025 117 if(!isSetup) setup();
donsez 0:72e45c332025 118
NorNick 1:1c712818d82a 119 //inApin.write(LOW);
NorNick 1:1c712818d82a 120 //inBpin.write(LOW);
NorNick 1:1c712818d82a 121
NorNick 1:1c712818d82a 122 inApin=0;
NorNick 1:1c712818d82a 123 inBpin=0;
donsez 0:72e45c332025 124 pwmApin.write(0.0f);
donsez 0:72e45c332025 125 pwmBpin.write(0.0f);
NorNick 1:1c712818d82a 126 //printf("Stop\n");
NorNick 1:1c712818d82a 127 }