Determine motor constants
Dependencies: HIDScope QEI mbed
Fork of frdm_motorConstantesBepalen by
main.cpp@0:8ea04b138f72, 2016-10-28 (annotated)
- Committer:
- Marieke
- Date:
- Fri Oct 28 11:04:15 2016 +0000
- Revision:
- 0:8ea04b138f72
- Child:
- 1:b7a1138131bb
Motor_sinus_test
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); |
Marieke | 0:8ea04b138f72 | 15 | HIDScope scope( 2 ); |
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; |
Marieke | 0:8ea04b138f72 | 21 | int counts1; |
Marieke | 0:8ea04b138f72 | 22 | int counts2; |
Marieke | 0:8ea04b138f72 | 23 | |
Marieke | 0:8ea04b138f72 | 24 | void sample() |
Marieke | 0:8ea04b138f72 | 25 | { |
Marieke | 0:8ea04b138f72 | 26 | scope.set(0, counts1); |
Marieke | 0:8ea04b138f72 | 27 | scope.set(1, counts2); |
Marieke | 0:8ea04b138f72 | 28 | |
Marieke | 0:8ea04b138f72 | 29 | scope.send(); |
Marieke | 0:8ea04b138f72 | 30 | } |
Marieke | 0:8ea04b138f72 | 31 | |
Marieke | 0:8ea04b138f72 | 32 | |
Marieke | 0:8ea04b138f72 | 33 | int main() { |
Marieke | 0:8ea04b138f72 | 34 | const double frequency_pwm = 100.0; |
Marieke | 0:8ea04b138f72 | 35 | pwm_motor1.period(1.0/frequency_pwm); |
Marieke | 0:8ea04b138f72 | 36 | QEI Encoder1(D12, D13, NC, 32); //maakt de encoder aan, NC niet aanwezig, 32 bits |
Marieke | 0:8ea04b138f72 | 37 | QEI Encoder2(D10, D11, NC, 32); //maakt de encoder aan, NC niet aanwezig, 32 bits |
Marieke | 0:8ea04b138f72 | 38 | sample_timer.attach(&sample, 0.0004); |
Marieke | 0:8ea04b138f72 | 39 | |
Marieke | 0:8ea04b138f72 | 40 | while(true){ |
Marieke | 0:8ea04b138f72 | 41 | for (t=0; t<10; t=t+0.005) { |
Marieke | 0:8ea04b138f72 | 42 | Aout=1*sin(2*pi*t); |
Marieke | 0:8ea04b138f72 | 43 | } |
Marieke | 0:8ea04b138f72 | 44 | motor1= Aout; |
Marieke | 0:8ea04b138f72 | 45 | pwm_motor1.write(1.0); |
Marieke | 0:8ea04b138f72 | 46 | counts1 = Encoder1.getPulses(); |
Marieke | 0:8ea04b138f72 | 47 | counts2 = Encoder2.getPulses(); |
Marieke | 0:8ea04b138f72 | 48 | } |
Marieke | 0:8ea04b138f72 | 49 | } |
Marieke | 0:8ea04b138f72 | 50 | |
Marieke | 0:8ea04b138f72 | 51 |