Drive Motor Group B4-B5

Fork of ArduMotoShield by Didier Donsez

Committer:
donsez
Date:
Sat Nov 08 15:44:22 2014 +0000
Revision:
0:72e45c332025
Child:
1:1c712818d82a
Create simple library and program for the SparkFun Ardumoto Shield https://www.sparkfun.com/products/9815;

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
donsez 0:72e45c332025 25 DigitalOut inApin(D12, LOW); //direction control for motor outputs 1 and 2 is on digital pin 12
donsez 0:72e45c332025 26 DigitalOut inBpin(D13, LOW); //direction control for motor outputs 3 and 4 is on digital pin 13
donsez 0:72e45c332025 27 PwmOut pwmApin(D3); //PWM control for motor outputs 1 and 2 is on digital pin 3
donsez 0:72e45c332025 28 PwmOut pwmBpin(D11); //PWM control for motor outputs 3 and 4 is on digital pin 11
donsez 0:72e45c332025 29
donsez 0:72e45c332025 30 bool isSetup=false;
donsez 0:72e45c332025 31
donsez 0:72e45c332025 32 void ArduMotoShield::setVoltages(float vin, float vmaxmotor)
donsez 0:72e45c332025 33 {
donsez 0:72e45c332025 34
donsez 0:72e45c332025 35 if(vin<vmaxmotor)
donsez 0:72e45c332025 36 MAXPWM=1.0f;
donsez 0:72e45c332025 37 else
donsez 0:72e45c332025 38 MAXPWM=vmaxmotor/vin;
donsez 0:72e45c332025 39 }
donsez 0:72e45c332025 40
donsez 0:72e45c332025 41
donsez 0:72e45c332025 42 void ArduMotoShield::setup()
donsez 0:72e45c332025 43 {
donsez 0:72e45c332025 44 pwmApin.period_ms(10);
donsez 0:72e45c332025 45 pwmApin.pulsewidth_ms(1);
donsez 0:72e45c332025 46 pwmApin.write(0.0f);
donsez 0:72e45c332025 47
donsez 0:72e45c332025 48 pwmBpin.period_ms(10);
donsez 0:72e45c332025 49 pwmBpin.pulsewidth_ms(1);
donsez 0:72e45c332025 50 pwmBpin.write(0.0f);
donsez 0:72e45c332025 51
donsez 0:72e45c332025 52 isSetup=true;
donsez 0:72e45c332025 53 }
donsez 0:72e45c332025 54
donsez 0:72e45c332025 55 // Basic ArduMotoShield operations
donsez 0:72e45c332025 56
donsez 0:72e45c332025 57 void ArduMotoShield::forward() //full speed forward
donsez 0:72e45c332025 58 {
donsez 0:72e45c332025 59 if(!isSetup) setup();
donsez 0:72e45c332025 60
donsez 0:72e45c332025 61 inApin.write(LOW);
donsez 0:72e45c332025 62 inBpin.write(LOW);
donsez 0:72e45c332025 63 pwmApin.write(MAXPWM);
donsez 0:72e45c332025 64 pwmBpin.write(MAXPWM);
donsez 0:72e45c332025 65 printf("Forward\n");
donsez 0:72e45c332025 66 }
donsez 0:72e45c332025 67
donsez 0:72e45c332025 68 void ArduMotoShield::backward() //full speed backward
donsez 0:72e45c332025 69 {
donsez 0:72e45c332025 70 if(!isSetup) setup();
donsez 0:72e45c332025 71
donsez 0:72e45c332025 72 inApin.write(HIGH);
donsez 0:72e45c332025 73 inBpin.write(HIGH);
donsez 0:72e45c332025 74 pwmApin.write(MAXPWM);
donsez 0:72e45c332025 75 pwmBpin.write(MAXPWM);
donsez 0:72e45c332025 76 printf("Backward\n");
donsez 0:72e45c332025 77 }
donsez 0:72e45c332025 78
donsez 0:72e45c332025 79 void ArduMotoShield::stop()
donsez 0:72e45c332025 80 {
donsez 0:72e45c332025 81 if(!isSetup) setup();
donsez 0:72e45c332025 82
donsez 0:72e45c332025 83 inApin.write(LOW);
donsez 0:72e45c332025 84 inBpin.write(LOW);
donsez 0:72e45c332025 85 pwmApin.write(0.0f);
donsez 0:72e45c332025 86 pwmBpin.write(0.0f);
donsez 0:72e45c332025 87 printf("Stop\n");
donsez 0:72e45c332025 88 }
donsez 0:72e45c332025 89
donsez 0:72e45c332025 90 void ArduMotoShield::right()
donsez 0:72e45c332025 91 {
donsez 0:72e45c332025 92 if(!isSetup) setup();
donsez 0:72e45c332025 93
donsez 0:72e45c332025 94 inApin.write(HIGH);
donsez 0:72e45c332025 95 inBpin.write(LOW);
donsez 0:72e45c332025 96 pwmApin.write(MAXPWM);
donsez 0:72e45c332025 97 pwmBpin.write(MAXPWM);
donsez 0:72e45c332025 98 printf("Right\n");
donsez 0:72e45c332025 99 }
donsez 0:72e45c332025 100
donsez 0:72e45c332025 101 void ArduMotoShield::left()
donsez 0:72e45c332025 102 {
donsez 0:72e45c332025 103 if(!isSetup) setup();
donsez 0:72e45c332025 104
donsez 0:72e45c332025 105 inApin.write(LOW);
donsez 0:72e45c332025 106 inBpin.write(HIGH);
donsez 0:72e45c332025 107 pwmApin.write(MAXPWM);
donsez 0:72e45c332025 108 pwmBpin.write(MAXPWM);
donsez 0:72e45c332025 109 printf("Left\n");
donsez 0:72e45c332025 110 }