2 motoren, pins correct

Dependencies:   MODSERIAL mbed QEI

Fork of PWM_motor by Felix Dransfeld

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "MODSERIAL.h"
00003 #include "QEI.h"
00004 
00005 AnalogIn pot1(A1);
00006 AnalogIn pot2(A2);
00007 PwmOut pwmpin1(D5);
00008 PwmOut pwmpin2(D6);
00009 PwmOut pwmpin3(D12);
00010 DigitalOut directionpin1(D4);
00011 DigitalOut directionpin2(D7);
00012 DigitalOut directionpin3(D13);
00013 QEI encoder1 (D11, D10, NC, 8400, QEI::X4_ENCODING);
00014 QEI encoder2 (D9, D8, NC, 8400, QEI::X4_ENCODING);
00015 QEI encoder3 (D3, D2, NC, 8400, QEI::X4_ENCODING);
00016 MODSERIAL pc(USBTX, USBRX);
00017 
00018 // ALS DE MOTOR GEK GEDRAG VERTOONT. ZORG ER VOOR DAT PINS D4-7 NIET WORDEN GEBRUIKT. DEZE ZIJN AL IN GEBURIK!!!!
00019 
00020 
00021 // INITIALIZING ENCODER
00022 int steps_per_rev = 8400;
00023 
00024 float pulsesToDegrees(float pulses)
00025     {
00026         return((pulses/steps_per_rev)*360);
00027         }
00028 
00029 int main()
00030 {
00031         pc.baud(9600);
00032         pc.printf("hello world\n");
00033    
00034 // INITIZALIZING ROTATION OF MOTOR  
00035     pwmpin1.period_us(60); //60 microsecondsPWM period, 16.7 kHz
00036     pwmpin2.period_us(60);
00037     pwmpin3.period_us(60);
00038       
00039     
00040     while(true){
00041            
00042 float pot_control1 = 2*pot1.read()-1;
00043 float pot_control2 = 2*pot2.read()-1;
00044         
00045         
00046 // CHANGE DIRECTION AND SPEED
00047         float u = pot_control1; //determineusefulvalue, -0.3f is justanexample
00048         directionpin1= u > 0.0f; //eithertrueor false
00049         pwmpin1= fabs(u); //pwmduty cycle canonlybepositive, floatingpoint absolute value
00050         
00051         float k = pot_control2; //determineusefulvalue, -0.3f is justanexample
00052         directionpin2= k > 0.0f; //eithertrueor false
00053         pwmpin2= fabs(k);
00054         
00055         float q = k; //determineusefulvalue, -0.3f is justanexample
00056         directionpin3= q > 0.0f; //eithertrueor false
00057         pwmpin3= fabs(q);
00058         
00059         
00060 // ENCODER READOUT
00061         
00062          
00063         float alpha = pulsesToDegrees(encoder1.getPulses());
00064         pc.printf("alpha: %f       ", alpha); 
00065         
00066         
00067         float beta = pulsesToDegrees(encoder2.getPulses());
00068         pc.printf("beta: %f        ", beta); 
00069         
00070         float gamma = pulsesToDegrees(encoder3.getPulses());
00071         pc.printf("gamma: %f\n\r", gamma);
00072             
00073         wait(0.5f);
00074         
00075     }
00076 }