Edited, sinus not yet good
Dependencies: HIDScope QEI mbed
Fork of frdm_motorConstantesBepalen by
main.cpp@1:b7a1138131bb, 2016-10-28 (annotated)
- Committer:
- GerhardBerman
- Date:
- Fri Oct 28 11:32:42 2016 +0000
- Revision:
- 1:b7a1138131bb
- Parent:
- 0:8ea04b138f72
Edited, sinus signal is not yet good
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 | 0:8ea04b138f72 | 16 | Ticker sample_timer; |
Marieke | 0:8ea04b138f72 | 17 | |
Marieke | 0:8ea04b138f72 | 18 | double t; |
Marieke | 0:8ea04b138f72 | 19 | float pi=3.14159; |
Marieke | 0:8ea04b138f72 | 20 | float Aout; |
GerhardBerman | 1:b7a1138131bb | 21 | float PWMAout; |
Marieke | 0:8ea04b138f72 | 22 | int counts1; |
Marieke | 0:8ea04b138f72 | 23 | int counts2; |
GerhardBerman | 1:b7a1138131bb | 24 | const int cw=0; |
GerhardBerman | 1:b7a1138131bb | 25 | const int ccw=1; |
GerhardBerman | 1:b7a1138131bb | 26 | |
Marieke | 0:8ea04b138f72 | 27 | void sample() |
Marieke | 0:8ea04b138f72 | 28 | { |
Marieke | 0:8ea04b138f72 | 29 | scope.set(0, counts1); |
Marieke | 0:8ea04b138f72 | 30 | scope.set(1, counts2); |
GerhardBerman | 1:b7a1138131bb | 31 | scope.set(2, Aout); |
GerhardBerman | 1:b7a1138131bb | 32 | scope.set(3, PWMAout); |
Marieke | 0:8ea04b138f72 | 33 | scope.send(); |
Marieke | 0:8ea04b138f72 | 34 | } |
Marieke | 0:8ea04b138f72 | 35 | |
Marieke | 0:8ea04b138f72 | 36 | |
Marieke | 0:8ea04b138f72 | 37 | int main() { |
Marieke | 0:8ea04b138f72 | 38 | const double frequency_pwm = 100.0; |
Marieke | 0:8ea04b138f72 | 39 | pwm_motor1.period(1.0/frequency_pwm); |
GerhardBerman | 1:b7a1138131bb | 40 | pwm_motor2.period(1.0/frequency_pwm); |
Marieke | 0:8ea04b138f72 | 41 | QEI Encoder1(D12, D13, NC, 32); //maakt de encoder aan, NC niet aanwezig, 32 bits |
Marieke | 0:8ea04b138f72 | 42 | QEI Encoder2(D10, D11, NC, 32); //maakt de encoder aan, NC niet aanwezig, 32 bits |
GerhardBerman | 1:b7a1138131bb | 43 | sample_timer.attach(&sample, 0.001); |
Marieke | 0:8ea04b138f72 | 44 | |
Marieke | 0:8ea04b138f72 | 45 | while(true){ |
GerhardBerman | 1:b7a1138131bb | 46 | for (t=0; t<10; t=t+0.05) { |
Marieke | 0:8ea04b138f72 | 47 | Aout=1*sin(2*pi*t); |
GerhardBerman | 1:b7a1138131bb | 48 | PWMAout=(Aout+1)/2; |
GerhardBerman | 1:b7a1138131bb | 49 | if (Aout >=0) //clockwise rotation |
GerhardBerman | 1:b7a1138131bb | 50 | {motor1=cw; |
GerhardBerman | 1:b7a1138131bb | 51 | } |
GerhardBerman | 1:b7a1138131bb | 52 | else //counterclockwise rotation |
GerhardBerman | 1:b7a1138131bb | 53 | {motor1=ccw; |
Marieke | 0:8ea04b138f72 | 54 | } |
GerhardBerman | 1:b7a1138131bb | 55 | pwm_motor1.write(PWMAout); |
GerhardBerman | 1:b7a1138131bb | 56 | |
GerhardBerman | 1:b7a1138131bb | 57 | if (Aout >=0) //clockwise rotation |
GerhardBerman | 1:b7a1138131bb | 58 | {motor2=cw; |
GerhardBerman | 1:b7a1138131bb | 59 | } |
GerhardBerman | 1:b7a1138131bb | 60 | else //counterclockwise rotation |
GerhardBerman | 1:b7a1138131bb | 61 | {motor2=ccw; |
GerhardBerman | 1:b7a1138131bb | 62 | } |
GerhardBerman | 1:b7a1138131bb | 63 | pwm_motor2.write(PWMAout); |
GerhardBerman | 1:b7a1138131bb | 64 | |
Marieke | 0:8ea04b138f72 | 65 | counts1 = Encoder1.getPulses(); |
GerhardBerman | 1:b7a1138131bb | 66 | counts2 = Encoder2.getPulses(); |
GerhardBerman | 1:b7a1138131bb | 67 | } |
GerhardBerman | 1:b7a1138131bb | 68 | |
GerhardBerman | 1:b7a1138131bb | 69 | |
Marieke | 0:8ea04b138f72 | 70 | } |
Marieke | 0:8ea04b138f72 | 71 | } |
Marieke | 0:8ea04b138f72 | 72 | |
Marieke | 0:8ea04b138f72 | 73 |