Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp@13:ab5a6ab6c492, 2016-11-16 (annotated)
- Committer:
- enderceylan
- Date:
- Wed Nov 16 00:01:15 2016 +0000
- Revision:
- 13:ab5a6ab6c492
- Parent:
- 11:ed9539245ea0
- Child:
- 15:a0b5c4306246
- Child:
- 16:b5862ba97453
Added test_speed() to main.cpp
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Snay22 | 5:74fcd196ff96 | 1 | #include "QEI.h" |
dlweakley | 6:8d2171811f14 | 2 | #include "Motor.h" |
dlweakley | 8:2abfdbf5a3b8 | 3 | #include "pot.h" |
enderceylan | 13:ab5a6ab6c492 | 4 | #include <time.h> |
dlweakley | 4:41bbbaecd322 | 5 | |
Snay22 | 5:74fcd196ff96 | 6 | Serial pc(USBTX, USBRX); |
Snay22 | 5:74fcd196ff96 | 7 | //Use X4 encoding. |
Snay22 | 5:74fcd196ff96 | 8 | //QEI wheel(p29, p30, NC, 624, QEI::X4_ENCODING); |
Snay22 | 5:74fcd196ff96 | 9 | //Use X2 encoding by default. |
dlweakley | 8:2abfdbf5a3b8 | 10 | QEI encoder (PTC16, PTC17, PTB9, 512); |
dlweakley | 11:ed9539245ea0 | 11 | Motor motor(PTC4, PTD0, PTC3, PTC12, PTB23, 2000); |
dlweakley | 11:ed9539245ea0 | 12 | Pot pend(PTC10,0); |
dlweakley | 8:2abfdbf5a3b8 | 13 | void test_distance(){ |
dlweakley | 8:2abfdbf5a3b8 | 14 | int wait = 1000; |
dlweakley | 8:2abfdbf5a3b8 | 15 | int steps = 100; |
dlweakley | 8:2abfdbf5a3b8 | 16 | while (wait > 500){ |
dlweakley | 8:2abfdbf5a3b8 | 17 | while (steps > 10){ |
dlweakley | 8:2abfdbf5a3b8 | 18 | printf("steps: %i wait: %i \r\n", steps, wait); |
dlweakley | 8:2abfdbf5a3b8 | 19 | for( int i = 0; i < 3; i++){ |
dlweakley | 8:2abfdbf5a3b8 | 20 | encoder.reset(); |
dlweakley | 8:2abfdbf5a3b8 | 21 | motor.step_clockwise(steps, wait); |
dlweakley | 8:2abfdbf5a3b8 | 22 | int pulses_cw = encoder.getPulses(); |
dlweakley | 8:2abfdbf5a3b8 | 23 | wait_ms(200); |
dlweakley | 8:2abfdbf5a3b8 | 24 | int coast_pulses_cw = encoder.getPulses(); |
dlweakley | 8:2abfdbf5a3b8 | 25 | |
dlweakley | 8:2abfdbf5a3b8 | 26 | |
dlweakley | 8:2abfdbf5a3b8 | 27 | encoder.reset(); |
dlweakley | 8:2abfdbf5a3b8 | 28 | motor.step_anticlockwise(steps, wait); |
dlweakley | 8:2abfdbf5a3b8 | 29 | int pulses_ccw = encoder.getPulses(); |
dlweakley | 8:2abfdbf5a3b8 | 30 | wait_ms(200); |
dlweakley | 8:2abfdbf5a3b8 | 31 | int coast_pulses_ccw = encoder.getPulses(); |
dlweakley | 8:2abfdbf5a3b8 | 32 | printf("trial: %i \tclockwise pluses: \t%i \tanticlockwise pluses: \t%i \r\n", i + 1, pulses_cw, pulses_ccw); |
dlweakley | 8:2abfdbf5a3b8 | 33 | printf("\t\tpulses after coast: \t%i \tpulses after coast: \t%i\r\n", coast_pulses_cw, coast_pulses_ccw); |
dlweakley | 8:2abfdbf5a3b8 | 34 | } |
dlweakley | 8:2abfdbf5a3b8 | 35 | steps -=10; |
dlweakley | 8:2abfdbf5a3b8 | 36 | printf("\r\n"); |
dlweakley | 8:2abfdbf5a3b8 | 37 | } |
dlweakley | 8:2abfdbf5a3b8 | 38 | |
dlweakley | 8:2abfdbf5a3b8 | 39 | wait-=100; |
dlweakley | 8:2abfdbf5a3b8 | 40 | steps=100; |
dlweakley | 8:2abfdbf5a3b8 | 41 | } |
dlweakley | 8:2abfdbf5a3b8 | 42 | } |
dlweakley | 6:8d2171811f14 | 43 | |
enderceylan | 13:ab5a6ab6c492 | 44 | void test_speed() { |
enderceylan | 13:ab5a6ab6c492 | 45 | time_t start; |
enderceylan | 13:ab5a6ab6c492 | 46 | double elapsed_time = 0; |
enderceylan | 13:ab5a6ab6c492 | 47 | int steps = 10; |
enderceylan | 13:ab5a6ab6c492 | 48 | int wait = 1000; |
enderceylan | 13:ab5a6ab6c492 | 49 | printf("steps: %i\r\n", steps); |
enderceylan | 13:ab5a6ab6c492 | 50 | for( int i = 0; i < 3; i++){ |
enderceylan | 13:ab5a6ab6c492 | 51 | encoder.reset(); |
enderceylan | 13:ab5a6ab6c492 | 52 | start = time(0); |
enderceylan | 13:ab5a6ab6c492 | 53 | motor.step_clockwise(steps, wait); |
enderceylan | 13:ab5a6ab6c492 | 54 | elapsed_time = difftime( time(0), start); |
enderceylan | 13:ab5a6ab6c492 | 55 | printf("Round %i clockwise, Total duration: \t%f seconds\r\n", i, elapsed_time); |
enderceylan | 13:ab5a6ab6c492 | 56 | wait_ms(200); |
enderceylan | 13:ab5a6ab6c492 | 57 | |
enderceylan | 13:ab5a6ab6c492 | 58 | encoder.reset(); |
enderceylan | 13:ab5a6ab6c492 | 59 | start = time(0); |
enderceylan | 13:ab5a6ab6c492 | 60 | motor.step_anticlockwise(steps, wait); |
enderceylan | 13:ab5a6ab6c492 | 61 | elapsed_time = difftime( time(0), start); |
enderceylan | 13:ab5a6ab6c492 | 62 | printf("Round %i counter-clockwise, Total duration: \t%f seconds\r\n", i, elapsed_time); |
enderceylan | 13:ab5a6ab6c492 | 63 | wait_ms(200); |
enderceylan | 13:ab5a6ab6c492 | 64 | } |
enderceylan | 13:ab5a6ab6c492 | 65 | } |
enderceylan | 13:ab5a6ab6c492 | 66 | |
dlweakley | 8:2abfdbf5a3b8 | 67 | |
dlweakley | 8:2abfdbf5a3b8 | 68 | void test_pendulum(){ |
Snay22 | 5:74fcd196ff96 | 69 | while(1){ |
dlweakley | 8:2abfdbf5a3b8 | 70 | printf("angle: %f \r\n", pend.get_angle()); |
Snay22 | 0:44fc57e03a56 | 71 | } |
dlweakley | 8:2abfdbf5a3b8 | 72 | } |
dlweakley | 11:ed9539245ea0 | 73 | void calibrate_pendulum(){ |
dlweakley | 11:ed9539245ea0 | 74 | encoder.reset(); |
dlweakley | 11:ed9539245ea0 | 75 | while(1){ |
dlweakley | 11:ed9539245ea0 | 76 | printf("voltage %%: %f pulse: %i", pend.get_voltage(), encoder.getPulses()); |
dlweakley | 11:ed9539245ea0 | 77 | } |
dlweakley | 11:ed9539245ea0 | 78 | } |
dlweakley | 11:ed9539245ea0 | 79 | void move_pulses(int pulses){ // find number of pulses from the encoder going from one end to the other. |
dlweakley | 11:ed9539245ea0 | 80 | int start = encoder.getPulses(); |
dlweakley | 11:ed9539245ea0 | 81 | if(pulses >=0){ |
dlweakley | 11:ed9539245ea0 | 82 | while(encoder.getPulses() < start + pulses){ |
dlweakley | 11:ed9539245ea0 | 83 | motor.clockwise(); |
dlweakley | 11:ed9539245ea0 | 84 | } |
dlweakley | 11:ed9539245ea0 | 85 | }else{ |
dlweakley | 11:ed9539245ea0 | 86 | while(encoder.getPulses() > start + pulses) |
dlweakley | 11:ed9539245ea0 | 87 | motor.anticlockwise(); |
dlweakley | 11:ed9539245ea0 | 88 | } |
dlweakley | 11:ed9539245ea0 | 89 | } |
dlweakley | 8:2abfdbf5a3b8 | 90 | int main() { |
dlweakley | 8:2abfdbf5a3b8 | 91 | test_pendulum(); |
dlweakley | 8:2abfdbf5a3b8 | 92 | //motor.step_clockwise(100,1500); |
dlweakley | 8:2abfdbf5a3b8 | 93 | //motor.step_clockwise(100,1000); |
dlweakley | 8:2abfdbf5a3b8 | 94 | //motor.step_clockwise(100,700); |
dlweakley | 8:2abfdbf5a3b8 | 95 | |
dlweakley | 8:2abfdbf5a3b8 | 96 | |
Snay22 | 5:74fcd196ff96 | 97 | |
dlweakley | 8:2abfdbf5a3b8 | 98 | } |