L298 Shild Drive Motor 4A

Fork of ArduMotoShield by Didier Donsez

ArduMotoShield.cpp

Committer:
NorNick
Date:
2015-12-04
Revision:
1:b4604cdd9597
Parent:
0:72e45c332025

File content as of revision 1:b4604cdd9597:

/* 
Simple library for the SparkFun Ardumoto Shield https://www.sparkfun.com/products/9815
with the ST Nucleo F401RE
 
The SparkFun Ardumoto shield can control two DC motors (up to 2 amps per motor). It is based on the L298 H-bridge. 

Developped by : Didier Donsez & Jérome Maisonnasse
 
 License: CC-SA 3.0, feel free to use this code however you'd like.
 Please improve upon it! Let me know how you've made it better.
 
 This is really simple example code to get you some basic functionality with the Ardumoto Shield.
 
 */

#include "ArduMotoShield.h"
#include "mbed.h"

// depend of the Vin and the max voltage of the motors
float MAXPWM = 0.5f;
                

BusOut inApin(D11,D12); //direction control for motor outputs 1 and 2 is on digital pin 12
BusOut inBpin(D13,D14); //direction control for motor outputs 3 and 4 is on digital pin 13
PwmOut pwmApin(D3); //PWM control for motor outputs 1 and 2 is on digital pin 3
PwmOut pwmBpin(D11); //PWM control for motor outputs 3 and 4 is on digital pin 11

bool isSetup=false;

void ArduMotoShield::setVoltages(float vin, float vmaxmotor)
{  

    if(vin<vmaxmotor)
        MAXPWM=1.0f;
    else
        MAXPWM=vmaxmotor/vin;
}


void ArduMotoShield::setup()
{  
    pwmApin.period_ms(10);
    pwmApin.pulsewidth_ms(1);
    pwmApin.write(0.0f);

    pwmBpin.period_ms(10);
    pwmBpin.pulsewidth_ms(1);  
    pwmBpin.write(0.0f);
    
    isSetup=true;        
}

// Basic ArduMotoShield operations

void ArduMotoShield::forward() //full speed forward
{ 
        if(!isSetup) setup();

        inApin=1;
        inBpin=1;
        pwmApin.write(MAXPWM);
        pwmBpin.write(MAXPWM);
        printf("Forward\n");
}

void ArduMotoShield::backward() //full speed backward
{
        if(!isSetup) setup();

        inApin=2;
        inBpin=2;
        pwmApin.write(MAXPWM);
        pwmBpin.write(MAXPWM);
        printf("Backward\n");
}

void ArduMotoShield::stop()
{
        if(!isSetup) setup();

        inApin=0;
        inBpin=0;
        pwmApin.write(0.0f);
        pwmBpin.write(0.0f);
        printf("Stop\n");
}

void ArduMotoShield::right()
{
        if(!isSetup) setup();

        inApin=2;
        inBpin=1;
        pwmApin.write(MAXPWM);
        pwmBpin.write(MAXPWM);
        printf("Right\n");
}

void ArduMotoShield::left()
{
        if(!isSetup) setup();

        inApin=1;
        inBpin=2;
        pwmApin.write(MAXPWM);
        pwmBpin.write(MAXPWM);
        printf("Left\n");
}