Signa-bot code for project BioRobotics, at University of Twente.
Dependencies: mbed QEI MODSERIAL FastPWM ttmath Math
Motor_tryout.cpp@13:18dd7a15603f, 2019-10-07 (annotated)
- Committer:
- Feike
- Date:
- Mon Oct 07 12:40:19 2019 +0000
- Revision:
- 13:18dd7a15603f
- Parent:
- 12:2382468d36a4
- Child:
- 14:b813ddfbcd71
Een motor, meest up to date
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 | 13:18dd7a15603f | 16 | // Global variables |
Feike | 13:18dd7a15603f | 17 | int quit; |
Feike | 13:18dd7a15603f | 18 | int counts = 0; |
Feike | 13:18dd7a15603f | 19 | int limit_pos = 8400; |
Feike | 13:18dd7a15603f | 20 | |
Feike | 13:18dd7a15603f | 21 | |
Feike | 12:2382468d36a4 | 22 | // DE TICKERS, deze ticker |
Feike | 12:2382468d36a4 | 23 | Ticker pulse; |
Feike | 12:2382468d36a4 | 24 | void pulseget() |
Feike | 13:18dd7a15603f | 25 | { |
Feike | 12:2382468d36a4 | 26 | counts = Encoder.getPulses(); |
Feike | 13:18dd7a15603f | 27 | if(counts > limit_pos) |
Feike | 13:18dd7a15603f | 28 | { |
Feike | 13:18dd7a15603f | 29 | quit = 1; |
Feike | 13:18dd7a15603f | 30 | } |
Feike | 12:2382468d36a4 | 31 | } |
Feike | 12:2382468d36a4 | 32 | |
Feike | 13:18dd7a15603f | 33 | Ticker signal; |
Feike | 13:18dd7a15603f | 34 | void get_signal() |
Feike | 13:18dd7a15603f | 35 | { |
Feike | 13:18dd7a15603f | 36 | int signal = 0; |
Feike | 13:18dd7a15603f | 37 | } |
Feike | 13:18dd7a15603f | 38 | |
Feike | 13:18dd7a15603f | 39 | |
Feike | 12:2382468d36a4 | 40 | // DE MAIN FUNCTIE |
Feike | 4:bd21569250c7 | 41 | int main(void) |
Feike | 13:18dd7a15603f | 42 | { |
Feike | 8:017b813c72bb | 43 | pc.baud(115200); |
Feike | 13:18dd7a15603f | 44 | pc.printf("In main\r\n"); |
Feike | 13:18dd7a15603f | 45 | |
Feike | 13:18dd7a15603f | 46 | while(true) |
Feike | 13:18dd7a15603f | 47 | { |
Feike | 13:18dd7a15603f | 48 | quit = 0; |
Feike | 13:18dd7a15603f | 49 | counts = 0; |
Feike | 13:18dd7a15603f | 50 | char cc = pc.getc(); |
Feike | 13:18dd7a15603f | 51 | while(cc == 'g') |
Feike | 8:017b813c72bb | 52 | { |
Feike | 13:18dd7a15603f | 53 | int frequency_pwm = 10000; //10 kHz PWM |
Feike | 13:18dd7a15603f | 54 | PwmOut motor1_pwm(PTC2); |
Feike | 13:18dd7a15603f | 55 | PwmOut motor2_pwm(PTA2); |
Feike | 13:18dd7a15603f | 56 | motor1_pwm.period(1.0/(double)frequency_pwm); // T=1/f |
Feike | 13:18dd7a15603f | 57 | motor1_dir.write(0); // positief |
Feike | 13:18dd7a15603f | 58 | motor1_pwm.write(0.56); // write Duty Cycle |
Feike | 13:18dd7a15603f | 59 | pulse.attach(pulseget,1.0/10000); |
Feike | 13:18dd7a15603f | 60 | if(quit == 1) |
Feike | 13:18dd7a15603f | 61 | { |
Feike | 13:18dd7a15603f | 62 | pulse.detach(); |
Feike | 13:18dd7a15603f | 63 | motor1_pwm.write(0); |
Feike | 13:18dd7a15603f | 64 | pc.printf("The motor is off with counts is %i\n\r",counts); |
Feike | 13:18dd7a15603f | 65 | break; |
Feike | 8:017b813c72bb | 66 | } |
Feike | 13:18dd7a15603f | 67 | wait(1.0/1000); |
Feike | 8:017b813c72bb | 68 | } |
Feike | 13:18dd7a15603f | 69 | cc = pc.getc(); |
Feike | 13:18dd7a15603f | 70 | counts = 0; |
Feike | 13:18dd7a15603f | 71 | pc.printf("The motor is in final state and counts is %i\n\r",counts); |
Feike | 8:017b813c72bb | 72 | } |
Feike | 12:2382468d36a4 | 73 | } |