Determine motor constants
Dependencies: HIDScope QEI mbed
Fork of frdm_motorConstantesBepalen by
main.cpp@2:78a4dccff486, 2016-11-01 (annotated)
- Committer:
- Marieke
- Date:
- Tue Nov 01 16:24:38 2016 +0000
- Revision:
- 2:78a4dccff486
- Parent:
- 1:b7a1138131bb
Script to determine motor constants
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" |
Marieke | 0:8ea04b138f72 | 3 | #include "HIDScope.h" |
Marieke | 0:8ea04b138f72 | 4 | |
Marieke | 0:8ea04b138f72 | 5 | // Definiëren van alle poorten |
Marieke | 0:8ea04b138f72 | 6 | |
Marieke | 0:8ea04b138f72 | 7 | DigitalIn encoder1A (D13); //Channel A van Encoder 1 |
Marieke | 0:8ea04b138f72 | 8 | DigitalIn encoder1B (D12); //Channel B van Encoder 1 |
Marieke | 0:8ea04b138f72 | 9 | DigitalIn encoder2A (D11); //Channel A van Encoder 2, kan niet op D15 |
Marieke | 0:8ea04b138f72 | 10 | DigitalIn encoder2B (D10); //Channel B van Encoder 2, kan niet op D14 |
Marieke | 0:8ea04b138f72 | 11 | DigitalOut motor1 (D7); |
Marieke | 0:8ea04b138f72 | 12 | PwmOut pwm_motor1 (D6); |
Marieke | 0:8ea04b138f72 | 13 | DigitalOut motor2 (D4); |
Marieke | 0:8ea04b138f72 | 14 | PwmOut pwm_motor2 (D5); |
GerhardBerman | 1:b7a1138131bb | 15 | HIDScope scope( 4 ); |
Marieke | 2:78a4dccff486 | 16 | Ticker sample_timer, snelheid_ticker; |
Marieke | 0:8ea04b138f72 | 17 | |
Marieke | 0:8ea04b138f72 | 18 | double t; |
Marieke | 2:78a4dccff486 | 19 | double pi=3.14159; |
Marieke | 2:78a4dccff486 | 20 | double Aout; |
Marieke | 2:78a4dccff486 | 21 | double PWMAout; |
Marieke | 2:78a4dccff486 | 22 | //float v_1; |
Marieke | 2:78a4dccff486 | 23 | int counts1_old; |
Marieke | 0:8ea04b138f72 | 24 | int counts1; |
Marieke | 0:8ea04b138f72 | 25 | int counts2; |
GerhardBerman | 1:b7a1138131bb | 26 | const int cw=0; |
GerhardBerman | 1:b7a1138131bb | 27 | const int ccw=1; |
GerhardBerman | 1:b7a1138131bb | 28 | |
Marieke | 0:8ea04b138f72 | 29 | void sample() |
Marieke | 0:8ea04b138f72 | 30 | { |
Marieke | 2:78a4dccff486 | 31 | //v_1 = (float) ((counts1 - counts1_old)/0.02); |
Marieke | 2:78a4dccff486 | 32 | //counts1_old=counts1; |
Marieke | 0:8ea04b138f72 | 33 | scope.set(0, counts1); |
Marieke | 0:8ea04b138f72 | 34 | scope.set(1, counts2); |
GerhardBerman | 1:b7a1138131bb | 35 | scope.set(2, Aout); |
GerhardBerman | 1:b7a1138131bb | 36 | scope.set(3, PWMAout); |
Marieke | 2:78a4dccff486 | 37 | //scope.set(3, PWMAout); |
Marieke | 0:8ea04b138f72 | 38 | scope.send(); |
Marieke | 0:8ea04b138f72 | 39 | } |
Marieke | 0:8ea04b138f72 | 40 | |
Marieke | 0:8ea04b138f72 | 41 | |
Marieke | 2:78a4dccff486 | 42 | /*void snelheid() |
Marieke | 2:78a4dccff486 | 43 | { |
Marieke | 2:78a4dccff486 | 44 | //counts1 = Encoder1.getPulses(); |
Marieke | 2:78a4dccff486 | 45 | v_1 = (float) ((counts1 - counts1_old)/0.01); |
Marieke | 2:78a4dccff486 | 46 | counts1_old=counts1; |
Marieke | 2:78a4dccff486 | 47 | //counts2 = Encoder2.getPulses(); |
Marieke | 2:78a4dccff486 | 48 | |
Marieke | 2:78a4dccff486 | 49 | scope.set(2, v_1); |
Marieke | 2:78a4dccff486 | 50 | scope.send(); |
Marieke | 2:78a4dccff486 | 51 | }*/ |
Marieke | 2:78a4dccff486 | 52 | |
Marieke | 2:78a4dccff486 | 53 | |
Marieke | 0:8ea04b138f72 | 54 | int main() { |
Marieke | 2:78a4dccff486 | 55 | const double frequency_pwm = 0.5; |
Marieke | 0:8ea04b138f72 | 56 | pwm_motor1.period(1.0/frequency_pwm); |
GerhardBerman | 1:b7a1138131bb | 57 | pwm_motor2.period(1.0/frequency_pwm); |
Marieke | 0:8ea04b138f72 | 58 | QEI Encoder1(D12, D13, NC, 32); //maakt de encoder aan, NC niet aanwezig, 32 bits |
Marieke | 0:8ea04b138f72 | 59 | QEI Encoder2(D10, D11, NC, 32); //maakt de encoder aan, NC niet aanwezig, 32 bits |
GerhardBerman | 1:b7a1138131bb | 60 | sample_timer.attach(&sample, 0.001); |
Marieke | 2:78a4dccff486 | 61 | //snelheid_ticker.attach(&snelheid, 0.01f); |
Marieke | 2:78a4dccff486 | 62 | |
Marieke | 0:8ea04b138f72 | 63 | |
Marieke | 0:8ea04b138f72 | 64 | while(true){ |
Marieke | 2:78a4dccff486 | 65 | for (t=0; t<2000; t=t+0.1) { |
Marieke | 2:78a4dccff486 | 66 | Aout= (double) 1*sin(0.001*pi*t); |
Marieke | 2:78a4dccff486 | 67 | PWMAout=fabs((Aout+1)/2); |
GerhardBerman | 1:b7a1138131bb | 68 | if (Aout >=0) //clockwise rotation |
Marieke | 2:78a4dccff486 | 69 | {motor1.write(cw); //? |
GerhardBerman | 1:b7a1138131bb | 70 | } |
Marieke | 2:78a4dccff486 | 71 | else //counterclockwise rotation |
Marieke | 2:78a4dccff486 | 72 | {motor1.write(ccw); |
Marieke | 0:8ea04b138f72 | 73 | } |
Marieke | 2:78a4dccff486 | 74 | pwm_motor1.write(PWMAout); |
GerhardBerman | 1:b7a1138131bb | 75 | |
Marieke | 2:78a4dccff486 | 76 | if (Aout >=0) //clockwise rotation |
Marieke | 2:78a4dccff486 | 77 | {motor2.write(ccw); |
GerhardBerman | 1:b7a1138131bb | 78 | } |
Marieke | 2:78a4dccff486 | 79 | else //counterclockwise rotation |
Marieke | 2:78a4dccff486 | 80 | {motor2.write(cw); |
GerhardBerman | 1:b7a1138131bb | 81 | } |
Marieke | 2:78a4dccff486 | 82 | pwm_motor2.write(PWMAout); |
GerhardBerman | 1:b7a1138131bb | 83 | |
Marieke | 2:78a4dccff486 | 84 | |
Marieke | 2:78a4dccff486 | 85 | |
Marieke | 2:78a4dccff486 | 86 | //while(true){ |
Marieke | 2:78a4dccff486 | 87 | |
Marieke | 2:78a4dccff486 | 88 | /*motor1 = cw; //clockwise |
Marieke | 2:78a4dccff486 | 89 | pwm_motor1.write(0.5); |
Marieke | 2:78a4dccff486 | 90 | motor2 = ccw; |
Marieke | 2:78a4dccff486 | 91 | pwm_motor2.write(0.5);*/ |
Marieke | 2:78a4dccff486 | 92 | counts1 = Encoder1.getPulses(); |
Marieke | 2:78a4dccff486 | 93 | counts2 = Encoder2.getPulses(); |
Marieke | 2:78a4dccff486 | 94 | } |
Marieke | 0:8ea04b138f72 | 95 | } |
Marieke | 0:8ea04b138f72 | 96 | } |
Marieke | 0:8ea04b138f72 | 97 | |
Marieke | 2:78a4dccff486 | 98 | |
Marieke | 2:78a4dccff486 | 99 |