hej

Dependents:   RoboticHackathon

hack_motor.cpp

Committer:
iLyngklip
Date:
2014-04-05
Revision:
0:66a1feb3ba85

File content as of revision 0:66a1feb3ba85:

#include "mbed.h"
#include "hack_motor.h"
#include "PwmOut.h"

Wheel::Wheel() : M1A(PTC8), M1B(PTC10), M2A(PTC9), M2B(PTC11)
    {
        init();    
    }
    
    
void Wheel::init() //Initialize the driver pwm to 150Hz
    {
        M1A.period(0.0066);
        M2A.period(0.0066);
        speed = 0.0;
    }    
    
void Wheel::FW()
    {
        fw = 0.5+(0.5*speed); //forward lies in the upper 50% of the duty cycle
        M1A.write(fw); //Set the duty cycle to the wanted percent, from speed variable
        M2A.write(fw); // -//-
        M1B = 0;
        M2B = 0;
        wait_ms(1);
    }
    
void Wheel::BW()
    {
        bw = 0.5-(0.5*speed); //Backward lies within the lower 50% of the duty cycle
        M1A.write(bw); //Set the duty cycle to the wanted percent, from speed variable
        M2A.write(bw); // -//-
        M1B = 1; 
        M2B = 1;
    }
    
void Wheel::right()
    {
        M1A.write(0.75); //Left side forward 50%
        M2A.write(0.25); //Right side backwards 50%
        M1B = 0;
        M2B = 1;
    }
    
void Wheel::left()
    {
        M1A.write(0.25); //Right side forward 50%
        M2A.write(0.75); //Left side backwards 50%
        M1B = 1;
        M2B = 0;
    }
    
void Wheel::stop()
    {
        M1A.write(0.0); //Pin A's set low
        M2A.write(0.0);
        M1B = 0;
        M2B = 0;  //Pin B's set high
    }