FRA221_2015 / ArduMotoShield

Fork of ArduMotoShield by Didier Donsez

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ArduMotoShield.cpp Source File

ArduMotoShield.cpp

00001 /* 
00002 Simple library for the SparkFun Ardumoto Shield https://www.sparkfun.com/products/9815
00003 with the ST Nucleo F401RE
00004  
00005 The SparkFun Ardumoto shield can control two DC motors (up to 2 amps per motor). It is based on the L298 H-bridge. 
00006 
00007 Developped by : Didier Donsez & Jérome Maisonnasse
00008  
00009  License: CC-SA 3.0, feel free to use this code however you'd like.
00010  Please improve upon it! Let me know how you've made it better.
00011  
00012  This is really simple example code to get you some basic functionality with the Ardumoto Shield.
00013  
00014  */
00015 
00016 #include "ArduMotoShield.h"
00017 #include "mbed.h"
00018 
00019 // depend of the Vin and the max voltage of the motors
00020 float MAXPWM = 0.5f;
00021                 
00022 #define LOW 0
00023 #define HIGH 1
00024 
00025 //DigitalOut inApin(D12, LOW); //direction control for motor outputs 1 and 2 is on digital pin 12
00026 //DigitalOut inBpin(D13, LOW); //direction control for motor outputs 3 and 4 is on digital pin 13
00027 BusOut inApin(D11,D12);
00028 BusOut inBpin(D13,D14);
00029 PwmOut pwmApin(D3); //PWM control for motor outputs 1 and 2 is on digital pin 3
00030 PwmOut pwmBpin(D5); //PWM control for motor outputs 3 and 4 is on digital pin 11
00031 
00032 bool isSetup=false;
00033 
00034 void ArduMotoShield::setVoltages(float vin, float vmaxmotor)
00035 {  
00036 
00037     if(vin<vmaxmotor)
00038         MAXPWM=1.0f;
00039     else
00040         MAXPWM=vmaxmotor/vin;
00041 }
00042 
00043 
00044 void ArduMotoShield::setup()
00045 {  
00046     pwmApin.period_ms(10);
00047     pwmApin.pulsewidth_ms(1);
00048     pwmApin.write(0.0f);
00049 
00050     pwmBpin.period_ms(10);
00051     pwmBpin.pulsewidth_ms(1);  
00052     pwmBpin.write(0.0f);
00053     
00054     isSetup=true;        
00055 }
00056 
00057 // Basic ArduMotoShield operations
00058 
00059 void ArduMotoShield::forward(float A_PWM,float B_PWM) //full speed forward
00060 { 
00061         if(!isSetup) setup();
00062 
00063         //inApin.write(LOW);
00064         //inBpin.write(LOW);
00065   
00066         inApin=1;
00067         inBpin=1;
00068         pwmApin.write(A_PWM);//(MAXPWM);
00069         pwmBpin.write(B_PWM);//(MAXPWM);
00070         //printf("Forward : %.2f\n",AnaPWM);
00071 }
00072 
00073 void ArduMotoShield::backward(float A_PWM,float B_PWM) //full speed backward
00074 {
00075         if(!isSetup) setup();
00076 
00077         //inApin.write(HIGH);
00078         //inBpin.write(HIGH);
00079 
00080         inApin=2;
00081         inBpin=2;
00082         pwmApin.write(A_PWM);//(MAXPWM);
00083         pwmBpin.write(B_PWM);//(MAXPWM);
00084         //printf("Backward\n");
00085 }
00086 
00087 void ArduMotoShield::right(float A_PWM,float B_PWM)
00088 {
00089         if(!isSetup) setup();
00090 
00091         //inApin.write(HIGH);
00092         //inBpin.write(LOW);
00093   
00094         inApin=1;
00095         inBpin=2;
00096         pwmApin.write(A_PWM);//(MAXPWM);
00097         pwmBpin.write(B_PWM);//(MAXPWM);
00098         //printf("Right\n");
00099 }
00100 
00101 void ArduMotoShield::left(float A_PWM,float B_PWM)
00102 {
00103         if(!isSetup) setup();
00104 
00105         //inApin.write(LOW);
00106         //inBpin.write(HIGH);
00107    
00108         inApin=2;
00109         inBpin=1;
00110         pwmApin.write(A_PWM);//(MAXPWM);
00111         pwmBpin.write(B_PWM);//(MAXPWM);
00112         //printf("Left\n");
00113 }
00114 
00115 void ArduMotoShield::stop()
00116 {
00117         if(!isSetup) setup();
00118 
00119         //inApin.write(LOW);
00120         //inBpin.write(LOW);
00121   
00122         inApin=0;
00123         inBpin=0;
00124         pwmApin.write(0.0f);
00125         pwmBpin.write(0.0f);
00126         //printf("Stop\n");
00127 }