Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of ArduMotoShield by
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 00023 BusOut inApin(D11,D12); //direction control for motor outputs 1 and 2 is on digital pin 12 00024 BusOut inBpin(D13,D14); //direction control for motor outputs 3 and 4 is on digital pin 13 00025 PwmOut pwmApin(D3); //PWM control for motor outputs 1 and 2 is on digital pin 3 00026 PwmOut pwmBpin(D11); //PWM control for motor outputs 3 and 4 is on digital pin 11 00027 00028 bool isSetup=false; 00029 00030 void ArduMotoShield::setVoltages(float vin, float vmaxmotor) 00031 { 00032 00033 if(vin<vmaxmotor) 00034 MAXPWM=1.0f; 00035 else 00036 MAXPWM=vmaxmotor/vin; 00037 } 00038 00039 00040 void ArduMotoShield::setup() 00041 { 00042 pwmApin.period_ms(10); 00043 pwmApin.pulsewidth_ms(1); 00044 pwmApin.write(0.0f); 00045 00046 pwmBpin.period_ms(10); 00047 pwmBpin.pulsewidth_ms(1); 00048 pwmBpin.write(0.0f); 00049 00050 isSetup=true; 00051 } 00052 00053 // Basic ArduMotoShield operations 00054 00055 void ArduMotoShield::forward() //full speed forward 00056 { 00057 if(!isSetup) setup(); 00058 00059 inApin=1; 00060 inBpin=1; 00061 pwmApin.write(MAXPWM); 00062 pwmBpin.write(MAXPWM); 00063 printf("Forward\n"); 00064 } 00065 00066 void ArduMotoShield::backward() //full speed backward 00067 { 00068 if(!isSetup) setup(); 00069 00070 inApin=2; 00071 inBpin=2; 00072 pwmApin.write(MAXPWM); 00073 pwmBpin.write(MAXPWM); 00074 printf("Backward\n"); 00075 } 00076 00077 void ArduMotoShield::stop() 00078 { 00079 if(!isSetup) setup(); 00080 00081 inApin=0; 00082 inBpin=0; 00083 pwmApin.write(0.0f); 00084 pwmBpin.write(0.0f); 00085 printf("Stop\n"); 00086 } 00087 00088 void ArduMotoShield::right() 00089 { 00090 if(!isSetup) setup(); 00091 00092 inApin=2; 00093 inBpin=1; 00094 pwmApin.write(MAXPWM); 00095 pwmBpin.write(MAXPWM); 00096 printf("Right\n"); 00097 } 00098 00099 void ArduMotoShield::left() 00100 { 00101 if(!isSetup) setup(); 00102 00103 inApin=1; 00104 inBpin=2; 00105 pwmApin.write(MAXPWM); 00106 pwmBpin.write(MAXPWM); 00107 printf("Left\n"); 00108 }
Generated on Wed Jul 27 2022 06:57:04 by
1.7.2
