mm4_encoder_testing

Dependencies:   QEI mbed-src-AV

main.cpp

Committer:
kelvincheng
Date:
2015-11-06
Revision:
0:c1b3cc00b091
Child:
1:1fed86c424fd

File content as of revision 0:c1b3cc00b091:

// Micromouse 2
// Further Test out the motors.
// Control with PWM.
// Test Serial Output.


#include "mbed.h"
#include "PinDefinitions.h"
Serial pc(PC_6, PC_7);        // These pins work for serial communications.
InterruptIn mybutton(USER_BUTTON);
DigitalOut led3(LED3);
DigitalOut motor_enabler(PB_6);
 
int Rdistance() { return RENC.getPulses(); }
int Ldistance() { return -LENC.getPulses(); }
 
 
void setLeftPwm(float speed) {
    if (speed == 0) {
        LMOTORA = 1.0;
        LMOTORB = 1.0;
    }
    
    if (speed > 0) {
        LMOTORA = speed;
        LMOTORB = 0;
    }
    else {
        LMOTORA = 0;
        LMOTORB = -speed;
    }
}
 
void setRightPwm(float speed) {
    if (speed == 0) {
        RMOTORA = 1.0;
        RMOTORB = 1.0;
    }
    
    if (speed > 0) {
        RMOTORA = speed;
        RMOTORB = 0;
    }
    else {
        RMOTORA = 0;
        RMOTORB = -speed;
    }
}
 
void pressed()
{
    led3 = !led3;
    pc.printf("Hellooooo\r\n");
}
 
int main() {
    mybutton.fall(&pressed);
    led3 = 0;
    motor_enabler = 1;
    pc.printf("\n\n\nHello world\n");
    
    /*
    while (Rdistance() < 5000) {   //while encoder pulses are less than 5000, turn led3 on and set LMOTORA to 0.50
        pc.printf("Pulse Count: %d\n", Rdistance());
        led3 = 1;
        setRightPwm(0.5);
    }
    RENC.reset();                       // reset encoder pulse count to zero
    while (Rdistance() < 5000) {   //while encoder pulses are less than 5000, turn led3 off and set LMOTORA to 0.25
        pc.printf("Pulse Count: %d\n", Rdistance());
        led3 = 0;
        setRightPwm(0.25);
    }
    RENC.reset();
    while (Rdistance() < 10000) {  //while encoder pulses are less than 10000, turn led3 on and set LMOTORA to 1.00
        pc.printf("Pulse Count: %d\n", Rdistance());
        led3 = 1;
        setRightPwm(1.0);
    }
    setRightPwm(0.0);
    */
    
    
    
    while (Ldistance() < 5000) {   //while encoder pulses are less than 5000, turn led3 on and set LMOTORA to 0.50
        pc.printf("Pulse Count: %d\n", Ldistance());
        led3 = 1;
        setLeftPwm(0.5);
    }
    LENC.reset();                       // reset encoder pulse count to zero
    while (Ldistance() < 5000) {   //while encoder pulses are less than 5000, turn led3 off and set LMOTORA to 0.25
        pc.printf("Pulse Count: %d\n", Ldistance());
        led3 = 0;
        setLeftPwm(0.25);
    }
    LENC.reset();
    while (Ldistance() < 10000) {  //while encoder pulses are less than 10000, turn led3 on and set LMOTORA to 1.00
        pc.printf("Pulse Count: %d\n", Ldistance());
        led3 = 1;
        setLeftPwm(1.0);
    }
    setLeftPwm(0.0);
    
    pc.printf("Done.\n");
}