Determine motor constants

Dependencies:   HIDScope QEI mbed

Fork of frdm_motorConstantesBepalen by Gerhard Berman

main.cpp

Committer:
Marieke
Date:
2016-11-01
Revision:
2:78a4dccff486
Parent:
1:b7a1138131bb

File content as of revision 2:78a4dccff486:

#include "mbed.h"
#include "QEI.h"
#include "HIDScope.h"

// Definiëren van alle poorten

DigitalIn encoder1A (D13); //Channel A van Encoder 1
DigitalIn encoder1B (D12); //Channel B van Encoder 1
DigitalIn encoder2A (D11); //Channel A van Encoder 2, kan niet op D15
DigitalIn encoder2B (D10); //Channel B van Encoder 2, kan niet op D14
DigitalOut motor1 (D7);
PwmOut pwm_motor1 (D6);
DigitalOut motor2 (D4);
PwmOut pwm_motor2 (D5);
HIDScope    scope( 4 );
Ticker      sample_timer, snelheid_ticker;

double t;
double pi=3.14159;
double Aout;
double PWMAout;
//float v_1;
int counts1_old;
int counts1;
int counts2;
const int cw=0;
const int ccw=1;

void sample()
{
    //v_1 = (float) ((counts1 - counts1_old)/0.02);
    //counts1_old=counts1;
    scope.set(0, counts1);
    scope.set(1, counts2);
    scope.set(2, Aout);
    scope.set(3, PWMAout);
    //scope.set(3, PWMAout);
    scope.send();
}


/*void snelheid()
{
        //counts1 = Encoder1.getPulses();
        v_1 = (float) ((counts1 - counts1_old)/0.01);
        counts1_old=counts1;
        //counts2 = Encoder2.getPulses();
        
        scope.set(2, v_1);
        scope.send();
}*/


int main() {
    const double frequency_pwm = 0.5;
    pwm_motor1.period(1.0/frequency_pwm);
    pwm_motor2.period(1.0/frequency_pwm);
    QEI Encoder1(D12, D13, NC, 32); //maakt de encoder aan, NC niet aanwezig, 32 bits
    QEI Encoder2(D10, D11, NC, 32); //maakt de encoder aan, NC niet aanwezig, 32 bits
    sample_timer.attach(&sample, 0.001);
    //snelheid_ticker.attach(&snelheid, 0.01f);
    
 
    while(true){
      for (t=0; t<2000; t=t+0.1) {
                Aout= (double) 1*sin(0.001*pi*t);
                PWMAout=fabs((Aout+1)/2);
            if (Aout >=0) //clockwise rotation
                {motor1.write(cw);        //?
            }
            else    //counterclockwise rotation 
            {motor1.write(ccw);         
            }
            pwm_motor1.write(PWMAout);
    
            if (Aout >=0) //clockwise rotation
            {motor2.write(ccw);        
            }
            else    //counterclockwise rotation 
            {motor2.write(cw);         
            }
            pwm_motor2.write(PWMAout);
    
        
        
        //while(true){
      
            /*motor1 = cw;  //clockwise
            pwm_motor1.write(0.5);
            motor2 = ccw;        
            pwm_motor2.write(0.5);*/
            counts1 = Encoder1.getPulses();
            counts2 = Encoder2.getPulses();
            }
    }
 }