make sine to determine simulink contants motor
Dependencies: HIDScope MODSERIAL QEI mbed
Fork of frdm_motorConstantesBepalen_bewerkt by
main.cpp@3:941386c5102e, 2016-11-02 (annotated)
- Committer:
- FloorC
- Date:
- Wed Nov 02 09:12:53 2016 +0000
- Revision:
- 3:941386c5102e
- Parent:
- 2:78a4dccff486
motor constantes bepalen, sinus hogere frequentie, werkt??
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Marieke | 0:8ea04b138f72 | 1 | #include "mbed.h" |
Marieke | 0:8ea04b138f72 | 2 | #include "QEI.h" |
FloorC | 3:941386c5102e | 3 | //#include "HIDScope.h" |
FloorC | 3:941386c5102e | 4 | #include "MODSERIAL.h" |
Marieke | 0:8ea04b138f72 | 5 | |
Marieke | 0:8ea04b138f72 | 6 | // Definiëren van alle poorten |
Marieke | 0:8ea04b138f72 | 7 | |
Marieke | 0:8ea04b138f72 | 8 | DigitalIn encoder1A (D13); //Channel A van Encoder 1 |
Marieke | 0:8ea04b138f72 | 9 | DigitalIn encoder1B (D12); //Channel B van Encoder 1 |
Marieke | 0:8ea04b138f72 | 10 | DigitalIn encoder2A (D11); //Channel A van Encoder 2, kan niet op D15 |
Marieke | 0:8ea04b138f72 | 11 | DigitalIn encoder2B (D10); //Channel B van Encoder 2, kan niet op D14 |
Marieke | 0:8ea04b138f72 | 12 | DigitalOut motor1 (D7); |
Marieke | 0:8ea04b138f72 | 13 | PwmOut pwm_motor1 (D6); |
Marieke | 0:8ea04b138f72 | 14 | DigitalOut motor2 (D4); |
Marieke | 0:8ea04b138f72 | 15 | PwmOut pwm_motor2 (D5); |
FloorC | 3:941386c5102e | 16 | MODSERIAL pc(USBTX, USBRX); |
FloorC | 3:941386c5102e | 17 | //HIDScope scope( 4 ); |
Marieke | 2:78a4dccff486 | 18 | Ticker sample_timer, snelheid_ticker; |
Marieke | 0:8ea04b138f72 | 19 | |
Marieke | 0:8ea04b138f72 | 20 | double t; |
Marieke | 2:78a4dccff486 | 21 | double pi=3.14159; |
Marieke | 2:78a4dccff486 | 22 | double Aout; |
Marieke | 2:78a4dccff486 | 23 | double PWMAout; |
Marieke | 2:78a4dccff486 | 24 | //float v_1; |
Marieke | 2:78a4dccff486 | 25 | int counts1_old; |
FloorC | 3:941386c5102e | 26 | double counts1; |
FloorC | 3:941386c5102e | 27 | double counts2; |
GerhardBerman | 1:b7a1138131bb | 28 | const int cw=0; |
GerhardBerman | 1:b7a1138131bb | 29 | const int ccw=1; |
GerhardBerman | 1:b7a1138131bb | 30 | |
FloorC | 3:941386c5102e | 31 | |
FloorC | 3:941386c5102e | 32 | |
Marieke | 2:78a4dccff486 | 33 | //v_1 = (float) ((counts1 - counts1_old)/0.02); |
Marieke | 2:78a4dccff486 | 34 | //counts1_old=counts1; |
FloorC | 3:941386c5102e | 35 | |
FloorC | 3:941386c5102e | 36 | |
Marieke | 0:8ea04b138f72 | 37 | |
Marieke | 0:8ea04b138f72 | 38 | |
Marieke | 2:78a4dccff486 | 39 | /*void snelheid() |
Marieke | 2:78a4dccff486 | 40 | { |
Marieke | 2:78a4dccff486 | 41 | //counts1 = Encoder1.getPulses(); |
Marieke | 2:78a4dccff486 | 42 | v_1 = (float) ((counts1 - counts1_old)/0.01); |
Marieke | 2:78a4dccff486 | 43 | counts1_old=counts1; |
Marieke | 2:78a4dccff486 | 44 | //counts2 = Encoder2.getPulses(); |
Marieke | 2:78a4dccff486 | 45 | |
Marieke | 2:78a4dccff486 | 46 | scope.set(2, v_1); |
Marieke | 2:78a4dccff486 | 47 | scope.send(); |
Marieke | 2:78a4dccff486 | 48 | }*/ |
Marieke | 2:78a4dccff486 | 49 | |
Marieke | 2:78a4dccff486 | 50 | |
Marieke | 0:8ea04b138f72 | 51 | int main() { |
FloorC | 3:941386c5102e | 52 | |
FloorC | 3:941386c5102e | 53 | pc.baud(115200); |
Marieke | 2:78a4dccff486 | 54 | const double frequency_pwm = 0.5; |
Marieke | 0:8ea04b138f72 | 55 | pwm_motor1.period(1.0/frequency_pwm); |
GerhardBerman | 1:b7a1138131bb | 56 | pwm_motor2.period(1.0/frequency_pwm); |
Marieke | 0:8ea04b138f72 | 57 | QEI Encoder1(D12, D13, NC, 32); //maakt de encoder aan, NC niet aanwezig, 32 bits |
Marieke | 0:8ea04b138f72 | 58 | QEI Encoder2(D10, D11, NC, 32); //maakt de encoder aan, NC niet aanwezig, 32 bits |
FloorC | 3:941386c5102e | 59 | |
Marieke | 2:78a4dccff486 | 60 | //snelheid_ticker.attach(&snelheid, 0.01f); |
Marieke | 2:78a4dccff486 | 61 | |
Marieke | 0:8ea04b138f72 | 62 | |
Marieke | 0:8ea04b138f72 | 63 | while(true){ |
FloorC | 3:941386c5102e | 64 | for (t=0; t<2000; t=t+0.002) { |
FloorC | 3:941386c5102e | 65 | Aout= (double) 1*sin(2*pi*2*t); |
Marieke | 2:78a4dccff486 | 66 | PWMAout=fabs((Aout+1)/2); |
GerhardBerman | 1:b7a1138131bb | 67 | if (Aout >=0) //clockwise rotation |
Marieke | 2:78a4dccff486 | 68 | {motor1.write(cw); //? |
GerhardBerman | 1:b7a1138131bb | 69 | } |
Marieke | 2:78a4dccff486 | 70 | else //counterclockwise rotation |
Marieke | 2:78a4dccff486 | 71 | {motor1.write(ccw); |
Marieke | 0:8ea04b138f72 | 72 | } |
Marieke | 2:78a4dccff486 | 73 | pwm_motor1.write(PWMAout); |
GerhardBerman | 1:b7a1138131bb | 74 | |
Marieke | 2:78a4dccff486 | 75 | if (Aout >=0) //clockwise rotation |
Marieke | 2:78a4dccff486 | 76 | {motor2.write(ccw); |
GerhardBerman | 1:b7a1138131bb | 77 | } |
Marieke | 2:78a4dccff486 | 78 | else //counterclockwise rotation |
Marieke | 2:78a4dccff486 | 79 | {motor2.write(cw); |
GerhardBerman | 1:b7a1138131bb | 80 | } |
Marieke | 2:78a4dccff486 | 81 | pwm_motor2.write(PWMAout); |
GerhardBerman | 1:b7a1138131bb | 82 | |
Marieke | 2:78a4dccff486 | 83 | |
Marieke | 2:78a4dccff486 | 84 | |
Marieke | 2:78a4dccff486 | 85 | //while(true){ |
Marieke | 2:78a4dccff486 | 86 | |
Marieke | 2:78a4dccff486 | 87 | /*motor1 = cw; //clockwise |
Marieke | 2:78a4dccff486 | 88 | pwm_motor1.write(0.5); |
Marieke | 2:78a4dccff486 | 89 | motor2 = ccw; |
Marieke | 2:78a4dccff486 | 90 | pwm_motor2.write(0.5);*/ |
Marieke | 2:78a4dccff486 | 91 | counts1 = Encoder1.getPulses(); |
Marieke | 2:78a4dccff486 | 92 | counts2 = Encoder2.getPulses(); |
FloorC | 3:941386c5102e | 93 | pc.printf("%f \r\n",counts1); |
FloorC | 3:941386c5102e | 94 | |
Marieke | 2:78a4dccff486 | 95 | } |
FloorC | 3:941386c5102e | 96 | //scope.set(0, counts1); |
FloorC | 3:941386c5102e | 97 | //scope.set(1, counts2); |
FloorC | 3:941386c5102e | 98 | //scope.set(2, Aout); |
FloorC | 3:941386c5102e | 99 | //scope.set(3, PWMAout); |
FloorC | 3:941386c5102e | 100 | |
FloorC | 3:941386c5102e | 101 | //scope.send(); |
Marieke | 0:8ea04b138f72 | 102 | } |
Marieke | 0:8ea04b138f72 | 103 | } |
Marieke | 0:8ea04b138f72 | 104 | |
Marieke | 2:78a4dccff486 | 105 | |
Marieke | 2:78a4dccff486 | 106 |