Robot tryout
Dependencies: mbed QEI biquadFilter MODSERIAL FastPWM ttmath Math
Motor_tryout.cpp@12:2382468d36a4, 2019-10-05 (annotated)
- Committer:
- Feike
- Date:
- Sat Oct 05 12:27:51 2019 +0000
- Revision:
- 12:2382468d36a4
- Parent:
- 9:61bdf58f856e
- Child:
- 13:18dd7a15603f
Eerdere uitleesmethode de van motor verwijderd. ; Nieuwe uitleesmethode toegevoegd, inclusief counter.; Uitlezen gebeurt in een ticker die in regel 49 wordt aangeroepen.; Tot slot voorwaarde (regel 52) toegevoegd die betrekking heeft op counter
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Feike | 4:bd21569250c7 | 1 | #include "mbed.h" |
Feike | 8:017b813c72bb | 2 | #include "MODSERIAL.h" |
Feike | 12:2382468d36a4 | 3 | #include "QEI.h" |
Feike | 4:bd21569250c7 | 4 | |
Feike | 12:2382468d36a4 | 5 | // ALLE INPUT EN OUTPUT SIGNALEN |
Feike | 8:017b813c72bb | 6 | DigitalOut motor1_pwm(PTC2); // aansturen motor 1, via poort PTC2 |
Feike | 12:2382468d36a4 | 7 | DigitalOut motor1_dir(PTC3); // richting motor 1 |
Feike | 12:2382468d36a4 | 8 | DigitalOut motor2_pwm(PTA2); // aansturen motor 2, via poort PTA2 |
Feike | 12:2382468d36a4 | 9 | DigitalOut motor2_dir(PTB23); // richting motor 2 |
Feike | 8:017b813c72bb | 10 | DigitalIn motor1_1(PTD1); // data vanuit motor 1 |
Feike | 8:017b813c72bb | 11 | DigitalIn motor1_2(PTD3); // data vanuit motor 1 |
Feike | 8:017b813c72bb | 12 | AnalogIn input(PTB3); // input van ECG |
Feike | 8:017b813c72bb | 13 | MODSERIAL pc(USBTX, USBRX); |
Feike | 12:2382468d36a4 | 14 | QEI Encoder(D12,D13,NC,64,QEI::X4_ENCODING); |
Feike | 7:e119b12e5e7f | 15 | |
Feike | 12:2382468d36a4 | 16 | // DE TICKERS, deze ticker |
Feike | 12:2382468d36a4 | 17 | Ticker pulse; |
Feike | 12:2382468d36a4 | 18 | int counts; |
Feike | 12:2382468d36a4 | 19 | void pulseget() |
Feike | 12:2382468d36a4 | 20 | { |
Feike | 12:2382468d36a4 | 21 | counts = Encoder.getPulses(); |
Feike | 12:2382468d36a4 | 22 | } |
Feike | 12:2382468d36a4 | 23 | |
Feike | 12:2382468d36a4 | 24 | // DE MAIN FUNCTIE |
Feike | 4:bd21569250c7 | 25 | int main(void) |
Feike | 8:017b813c72bb | 26 | { |
Feike | 8:017b813c72bb | 27 | pc.baud(115200); |
Feike | 12:2382468d36a4 | 28 | char cc = pc.getc(); |
Feike | 8:017b813c72bb | 29 | pc.printf("In de main, check\r\n"); |
Feike | 12:2382468d36a4 | 30 | pc.putc(cc); |
Feike | 12:2382468d36a4 | 31 | if (cc == 'm' or cc == 'm') |
Feike | 8:017b813c72bb | 32 | { |
Feike | 8:017b813c72bb | 33 | pc.printf("In de if, check\r\n"); |
Feike | 8:017b813c72bb | 34 | pc.baud(115200); |
Feike | 8:017b813c72bb | 35 | int frequency_pwm = 10000; //10 kHz PWM |
Feike | 8:017b813c72bb | 36 | PwmOut motor1_pwm(PTC2); |
Feike | 8:017b813c72bb | 37 | PwmOut motor2_pwm(PTA2); |
Feike | 8:017b813c72bb | 38 | motor1_pwm.period(1.0/(double)frequency_pwm); // T=1/f |
Feike | 8:017b813c72bb | 39 | motor2_pwm.period(1.0/(double)frequency_pwm); // T=1/f |
Feike | 8:017b813c72bb | 40 | while(true) |
Feike | 8:017b813c72bb | 41 | { |
Feike | 8:017b813c72bb | 42 | pc.printf("In de while, check\r\n"); |
Feike | 12:2382468d36a4 | 43 | |
Feike | 12:2382468d36a4 | 44 | // MOTOR 1 START |
Feike | 12:2382468d36a4 | 45 | int limit = 20000; |
Feike | 12:2382468d36a4 | 46 | for(int pct = 55 ; pct <= 70 ; pct ++) |
Feike | 8:017b813c72bb | 47 | { |
Feike | 8:017b813c72bb | 48 | motor1_pwm.write(pct/100.0); // write Duty Cycle |
Feike | 12:2382468d36a4 | 49 | pulse.attach(pulseget,1.0/100); |
Feike | 12:2382468d36a4 | 50 | pc.printf("Motor 1 speed %i and the count is %i\r\n", pct, counts); |
Feike | 8:017b813c72bb | 51 | wait(1.0f); |
Feike | 12:2382468d36a4 | 52 | if(counts > limit) |
Feike | 12:2382468d36a4 | 53 | { |
Feike | 12:2382468d36a4 | 54 | break; |
Feike | 12:2382468d36a4 | 55 | } |
Feike | 8:017b813c72bb | 56 | } |
Feike | 8:017b813c72bb | 57 | motor1_pwm.write(0.0f); // motor 1 off again |
Feike | 8:017b813c72bb | 58 | pc.printf("Motor 1 off\r\n"); |
Feike | 12:2382468d36a4 | 59 | |
Feike | 12:2382468d36a4 | 60 | // MOTOR TWEE START |
Feike | 8:017b813c72bb | 61 | for(int pct = 0 ; pct <= 100 ; pct += 10) |
Feike | 8:017b813c72bb | 62 | { |
Feike | 8:017b813c72bb | 63 | pc.printf("Motor 2\r\n"); |
Feike | 8:017b813c72bb | 64 | motor2_pwm.write(pct/100.0); // write Duty Cycle |
Feike | 8:017b813c72bb | 65 | wait(1.0f); |
Feike | 8:017b813c72bb | 66 | } |
Feike | 8:017b813c72bb | 67 | motor2_pwm.write(0.0f); // motor 2 off again |
Feike | 8:017b813c72bb | 68 | pc.printf("Motor 2 off\r\n"); |
Feike | 8:017b813c72bb | 69 | } |
Feike | 8:017b813c72bb | 70 | } |
Feike | 12:2382468d36a4 | 71 | } |