motor aansturing moet lineair zijn is het niet

Dependencies:   MODSERIAL Encoder mbed HIDScope

main.cpp

Committer:
Zeekat
Date:
2015-09-28
Revision:
4:e171c9fa5447
Parent:
3:c9f9db6581bc
Child:
5:46dae55f0ab0

File content as of revision 4:e171c9fa5447:

#include "mbed.h"
#include "MODSERIAL.h"
#include "QEI.h"


Serial pc(USBTX,USBRX);

Ticker          mod;

//MOTOR
// motor 1 en 2 zijn omgedraait
// pinnen motor 1 -> pinnen motor 2

//motor 1 gegevens
PwmOut motor1_aan(D6);      // PWM signaal motor 2 (uit sheets)
DigitalOut motor1_rich(D7); // digitaal signaal voor richting 
// einde motor 1

//motor 2 gegevens
PwmOut motor2_aan(D5);      // PWM signaal motor 1 (uit sheets)
DigitalOut motor2_rich(D4); // digitaal signaal voor richting 
// einde motor 1
//EINDE MOTOR

// ENcoDER
QEI motor1_enc(D12,D11,NC,64);
QEI motor2_enc(D10,D9,NC,64);


//POTMETERS
AnalogIn potleft(A1);
AnalogIn potright(A0);

DigitalIn button(PTA4)
int button_on = 0

void move_mot1(float left)
{
    if(left < 0.4)
    {
        float calc1 = left - 1;
        float calc2 = abs(calc1);
        float leftin = (calc2-0.6)*2.5 ;
        motor1_aan.write(leftin);
        motor1_rich.write(0);
    }
    else if(left > 0.4 && left < 0.6)
    {
        motor1_aan.write(0);
    }
    else if(left > 0.6)
    {
        float leftin = (left-0.6)*2.5;
        motor1_aan.write(leftin);
        motor1_rich.write(1);
    }
}

void move_mot2(float right)
{
    if(right < 0.4)
    { 
        float calc3 = right - 1;
        float calc4 = abs(calc3);
        float rightin = (calc4-0.6)*2.5 ;
        motor2_aan.write(rightin);
        motor2_rich.write(0);
    }
    else if(right > 0.4 && right < 0.6)
    {
        motor2_aan.write(0);
    }
    else if(right > 0.6)
    {
        float rightin = (right-0.6)*2.5;
        motor2_aan.write(rightin);
        motor2_rich.write(1);
    }
}  


void send()
{
    int countsl = motor1_enc.getPulses();
    int countsr = motor2_enc.getPulses();
    pc.printf("motor 1 counts %d motor 2 counts %d \n", countsl, countsr);
}


int main()
{
    pc.baud(115200);
    mod.attach(&send,1);
    while(true)
    {
        float left = potleft.read();
        float right = potright.read();
        move_mot1(left);
        move_mot2(right);
        if(button.read() == button_on)
        {
            motor1_enc.reset;
            motor2_enc.reset;
        }
    
    }
}