L298 Shild Drive Motor 4A
Fork of ArduMotoShield by
ArduMotoShield.cpp@1:b4604cdd9597, 2015-12-04 (annotated)
- Committer:
- NorNick
- Date:
- Fri Dec 04 19:23:37 2015 +0000
- Revision:
- 1:b4604cdd9597
- Parent:
- 0:72e45c332025
Chang DigitalOut -> BusOut
Who changed what in which revision?
User | Revision | Line number | New 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 | |
NorNick | 1:b4604cdd9597 | 23 | BusOut inApin(D11,D12); //direction control for motor outputs 1 and 2 is on digital pin 12 |
NorNick | 1:b4604cdd9597 | 24 | BusOut inBpin(D13,D14); //direction control for motor outputs 3 and 4 is on digital pin 13 |
donsez | 0:72e45c332025 | 25 | PwmOut pwmApin(D3); //PWM control for motor outputs 1 and 2 is on digital pin 3 |
donsez | 0:72e45c332025 | 26 | PwmOut pwmBpin(D11); //PWM control for motor outputs 3 and 4 is on digital pin 11 |
donsez | 0:72e45c332025 | 27 | |
donsez | 0:72e45c332025 | 28 | bool isSetup=false; |
donsez | 0:72e45c332025 | 29 | |
donsez | 0:72e45c332025 | 30 | void ArduMotoShield::setVoltages(float vin, float vmaxmotor) |
donsez | 0:72e45c332025 | 31 | { |
donsez | 0:72e45c332025 | 32 | |
donsez | 0:72e45c332025 | 33 | if(vin<vmaxmotor) |
donsez | 0:72e45c332025 | 34 | MAXPWM=1.0f; |
donsez | 0:72e45c332025 | 35 | else |
donsez | 0:72e45c332025 | 36 | MAXPWM=vmaxmotor/vin; |
donsez | 0:72e45c332025 | 37 | } |
donsez | 0:72e45c332025 | 38 | |
donsez | 0:72e45c332025 | 39 | |
donsez | 0:72e45c332025 | 40 | void ArduMotoShield::setup() |
donsez | 0:72e45c332025 | 41 | { |
donsez | 0:72e45c332025 | 42 | pwmApin.period_ms(10); |
donsez | 0:72e45c332025 | 43 | pwmApin.pulsewidth_ms(1); |
donsez | 0:72e45c332025 | 44 | pwmApin.write(0.0f); |
donsez | 0:72e45c332025 | 45 | |
donsez | 0:72e45c332025 | 46 | pwmBpin.period_ms(10); |
donsez | 0:72e45c332025 | 47 | pwmBpin.pulsewidth_ms(1); |
donsez | 0:72e45c332025 | 48 | pwmBpin.write(0.0f); |
donsez | 0:72e45c332025 | 49 | |
donsez | 0:72e45c332025 | 50 | isSetup=true; |
donsez | 0:72e45c332025 | 51 | } |
donsez | 0:72e45c332025 | 52 | |
donsez | 0:72e45c332025 | 53 | // Basic ArduMotoShield operations |
donsez | 0:72e45c332025 | 54 | |
donsez | 0:72e45c332025 | 55 | void ArduMotoShield::forward() //full speed forward |
donsez | 0:72e45c332025 | 56 | { |
donsez | 0:72e45c332025 | 57 | if(!isSetup) setup(); |
donsez | 0:72e45c332025 | 58 | |
NorNick | 1:b4604cdd9597 | 59 | inApin=1; |
NorNick | 1:b4604cdd9597 | 60 | inBpin=1; |
donsez | 0:72e45c332025 | 61 | pwmApin.write(MAXPWM); |
donsez | 0:72e45c332025 | 62 | pwmBpin.write(MAXPWM); |
donsez | 0:72e45c332025 | 63 | printf("Forward\n"); |
donsez | 0:72e45c332025 | 64 | } |
donsez | 0:72e45c332025 | 65 | |
donsez | 0:72e45c332025 | 66 | void ArduMotoShield::backward() //full speed backward |
donsez | 0:72e45c332025 | 67 | { |
donsez | 0:72e45c332025 | 68 | if(!isSetup) setup(); |
donsez | 0:72e45c332025 | 69 | |
NorNick | 1:b4604cdd9597 | 70 | inApin=2; |
NorNick | 1:b4604cdd9597 | 71 | inBpin=2; |
donsez | 0:72e45c332025 | 72 | pwmApin.write(MAXPWM); |
donsez | 0:72e45c332025 | 73 | pwmBpin.write(MAXPWM); |
donsez | 0:72e45c332025 | 74 | printf("Backward\n"); |
donsez | 0:72e45c332025 | 75 | } |
donsez | 0:72e45c332025 | 76 | |
donsez | 0:72e45c332025 | 77 | void ArduMotoShield::stop() |
donsez | 0:72e45c332025 | 78 | { |
donsez | 0:72e45c332025 | 79 | if(!isSetup) setup(); |
donsez | 0:72e45c332025 | 80 | |
NorNick | 1:b4604cdd9597 | 81 | inApin=0; |
NorNick | 1:b4604cdd9597 | 82 | inBpin=0; |
donsez | 0:72e45c332025 | 83 | pwmApin.write(0.0f); |
donsez | 0:72e45c332025 | 84 | pwmBpin.write(0.0f); |
donsez | 0:72e45c332025 | 85 | printf("Stop\n"); |
donsez | 0:72e45c332025 | 86 | } |
donsez | 0:72e45c332025 | 87 | |
donsez | 0:72e45c332025 | 88 | void ArduMotoShield::right() |
donsez | 0:72e45c332025 | 89 | { |
donsez | 0:72e45c332025 | 90 | if(!isSetup) setup(); |
donsez | 0:72e45c332025 | 91 | |
NorNick | 1:b4604cdd9597 | 92 | inApin=2; |
NorNick | 1:b4604cdd9597 | 93 | inBpin=1; |
donsez | 0:72e45c332025 | 94 | pwmApin.write(MAXPWM); |
donsez | 0:72e45c332025 | 95 | pwmBpin.write(MAXPWM); |
donsez | 0:72e45c332025 | 96 | printf("Right\n"); |
donsez | 0:72e45c332025 | 97 | } |
donsez | 0:72e45c332025 | 98 | |
donsez | 0:72e45c332025 | 99 | void ArduMotoShield::left() |
donsez | 0:72e45c332025 | 100 | { |
donsez | 0:72e45c332025 | 101 | if(!isSetup) setup(); |
donsez | 0:72e45c332025 | 102 | |
NorNick | 1:b4604cdd9597 | 103 | inApin=1; |
NorNick | 1:b4604cdd9597 | 104 | inBpin=2; |
donsez | 0:72e45c332025 | 105 | pwmApin.write(MAXPWM); |
donsez | 0:72e45c332025 | 106 | pwmBpin.write(MAXPWM); |
donsez | 0:72e45c332025 | 107 | printf("Left\n"); |
donsez | 0:72e45c332025 | 108 | } |