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@20:8063c82bbb35, 2016-11-16 (annotated)
- Committer:
- dlweakley
- Date:
- Wed Nov 16 08:28:46 2016 +0000
- Revision:
- 20:8063c82bbb35
- Parent:
- 19:0e9bf6d61d0d
- Child:
- 21:eac29cf3f061
added velocities/ refined pot class
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> |
Snay22 | 18:5a1c351094d2 | 5 | |
dlweakley | 4:41bbbaecd322 | 6 | |
Snay22 | 5:74fcd196ff96 | 7 | Serial pc(USBTX, USBRX); |
Snay22 | 5:74fcd196ff96 | 8 | //Use X4 encoding. |
Snay22 | 5:74fcd196ff96 | 9 | //QEI wheel(p29, p30, NC, 624, QEI::X4_ENCODING); |
Snay22 | 5:74fcd196ff96 | 10 | //Use X2 encoding by default. |
dlweakley | 8:2abfdbf5a3b8 | 11 | QEI encoder (PTC16, PTC17, PTB9, 512); |
dlweakley | 11:ed9539245ea0 | 12 | Motor motor(PTC4, PTD0, PTC3, PTC12, PTB23, 2000); |
dlweakley | 20:8063c82bbb35 | 13 | Pot pend(PTC10, &encoder,0); |
dlweakley | 20:8063c82bbb35 | 14 | Ticker update; |
dlweakley | 20:8063c82bbb35 | 15 | |
dlweakley | 8:2abfdbf5a3b8 | 16 | void test_distance(){ |
dlweakley | 8:2abfdbf5a3b8 | 17 | int wait = 1000; |
dlweakley | 8:2abfdbf5a3b8 | 18 | int steps = 100; |
dlweakley | 8:2abfdbf5a3b8 | 19 | while (wait > 500){ |
dlweakley | 8:2abfdbf5a3b8 | 20 | while (steps > 10){ |
dlweakley | 8:2abfdbf5a3b8 | 21 | printf("steps: %i wait: %i \r\n", steps, wait); |
dlweakley | 8:2abfdbf5a3b8 | 22 | for( int i = 0; i < 3; i++){ |
dlweakley | 8:2abfdbf5a3b8 | 23 | encoder.reset(); |
dlweakley | 8:2abfdbf5a3b8 | 24 | motor.step_clockwise(steps, wait); |
dlweakley | 8:2abfdbf5a3b8 | 25 | int pulses_cw = encoder.getPulses(); |
dlweakley | 8:2abfdbf5a3b8 | 26 | wait_ms(200); |
dlweakley | 8:2abfdbf5a3b8 | 27 | int coast_pulses_cw = encoder.getPulses(); |
dlweakley | 8:2abfdbf5a3b8 | 28 | |
dlweakley | 8:2abfdbf5a3b8 | 29 | encoder.reset(); |
dlweakley | 8:2abfdbf5a3b8 | 30 | motor.step_anticlockwise(steps, wait); |
dlweakley | 8:2abfdbf5a3b8 | 31 | int pulses_ccw = encoder.getPulses(); |
dlweakley | 8:2abfdbf5a3b8 | 32 | wait_ms(200); |
dlweakley | 8:2abfdbf5a3b8 | 33 | int coast_pulses_ccw = encoder.getPulses(); |
dlweakley | 8:2abfdbf5a3b8 | 34 | printf("trial: %i \tclockwise pluses: \t%i \tanticlockwise pluses: \t%i \r\n", i + 1, pulses_cw, pulses_ccw); |
dlweakley | 8:2abfdbf5a3b8 | 35 | printf("\t\tpulses after coast: \t%i \tpulses after coast: \t%i\r\n", coast_pulses_cw, coast_pulses_ccw); |
dlweakley | 8:2abfdbf5a3b8 | 36 | } |
dlweakley | 8:2abfdbf5a3b8 | 37 | steps -=10; |
dlweakley | 8:2abfdbf5a3b8 | 38 | printf("\r\n"); |
dlweakley | 8:2abfdbf5a3b8 | 39 | } |
dlweakley | 8:2abfdbf5a3b8 | 40 | |
dlweakley | 8:2abfdbf5a3b8 | 41 | wait-=100; |
dlweakley | 8:2abfdbf5a3b8 | 42 | steps=100; |
dlweakley | 8:2abfdbf5a3b8 | 43 | } |
dlweakley | 8:2abfdbf5a3b8 | 44 | } |
dlweakley | 6:8d2171811f14 | 45 | |
enderceylan | 13:ab5a6ab6c492 | 46 | void test_speed() { |
enderceylan | 13:ab5a6ab6c492 | 47 | time_t start; |
enderceylan | 13:ab5a6ab6c492 | 48 | double elapsed_time = 0; |
enderceylan | 13:ab5a6ab6c492 | 49 | int steps = 10; |
enderceylan | 13:ab5a6ab6c492 | 50 | int wait = 1000; |
enderceylan | 13:ab5a6ab6c492 | 51 | printf("steps: %i\r\n", steps); |
enderceylan | 13:ab5a6ab6c492 | 52 | for( int i = 0; i < 3; i++){ |
enderceylan | 13:ab5a6ab6c492 | 53 | encoder.reset(); |
enderceylan | 13:ab5a6ab6c492 | 54 | start = time(0); |
enderceylan | 13:ab5a6ab6c492 | 55 | motor.step_clockwise(steps, wait); |
enderceylan | 13:ab5a6ab6c492 | 56 | elapsed_time = difftime( time(0), start); |
enderceylan | 13:ab5a6ab6c492 | 57 | printf("Round %i clockwise, Total duration: \t%f seconds\r\n", i, elapsed_time); |
enderceylan | 13:ab5a6ab6c492 | 58 | wait_ms(200); |
enderceylan | 13:ab5a6ab6c492 | 59 | |
enderceylan | 13:ab5a6ab6c492 | 60 | encoder.reset(); |
enderceylan | 13:ab5a6ab6c492 | 61 | start = time(0); |
enderceylan | 13:ab5a6ab6c492 | 62 | motor.step_anticlockwise(steps, wait); |
enderceylan | 13:ab5a6ab6c492 | 63 | elapsed_time = difftime( time(0), start); |
enderceylan | 13:ab5a6ab6c492 | 64 | printf("Round %i counter-clockwise, Total duration: \t%f seconds\r\n", i, elapsed_time); |
enderceylan | 13:ab5a6ab6c492 | 65 | wait_ms(200); |
enderceylan | 13:ab5a6ab6c492 | 66 | } |
enderceylan | 13:ab5a6ab6c492 | 67 | } |
enderceylan | 13:ab5a6ab6c492 | 68 | |
dlweakley | 8:2abfdbf5a3b8 | 69 | |
dlweakley | 8:2abfdbf5a3b8 | 70 | void test_pendulum(){ |
Snay22 | 5:74fcd196ff96 | 71 | while(1){ |
dlweakley | 8:2abfdbf5a3b8 | 72 | printf("angle: %f \r\n", pend.get_angle()); |
Snay22 | 0:44fc57e03a56 | 73 | } |
dlweakley | 8:2abfdbf5a3b8 | 74 | } |
dlweakley | 11:ed9539245ea0 | 75 | void calibrate_pendulum(){ |
dlweakley | 11:ed9539245ea0 | 76 | encoder.reset(); |
dlweakley | 11:ed9539245ea0 | 77 | while(1){ |
dlweakley | 20:8063c82bbb35 | 78 | printf("voltage %%: %f pulse: %i", pend.angle_as_voltage(), encoder.getPulses()); |
dlweakley | 11:ed9539245ea0 | 79 | } |
dlweakley | 11:ed9539245ea0 | 80 | } |
Snay22 | 19:0e9bf6d61d0d | 81 | void move_pulses(int pulses, int wait){ // find number of pulses from the encoder going from one end to the other. |
dlweakley | 11:ed9539245ea0 | 82 | int start = encoder.getPulses(); |
dlweakley | 20:8063c82bbb35 | 83 | if(pulses >= 0){ |
dlweakley | 11:ed9539245ea0 | 84 | while(encoder.getPulses() < start + pulses){ |
dlweakley | 20:8063c82bbb35 | 85 | printf("here"); |
dlweakley | 20:8063c82bbb35 | 86 | // motor.clockwise(wait); |
dlweakley | 11:ed9539245ea0 | 87 | } |
dlweakley | 11:ed9539245ea0 | 88 | }else{ |
dlweakley | 20:8063c82bbb35 | 89 | while(encoder.getPulses() > start + pulses){ |
dlweakley | 20:8063c82bbb35 | 90 | // motor.anticlockwise(wait); |
dlweakley | 20:8063c82bbb35 | 91 | } |
dlweakley | 20:8063c82bbb35 | 92 | } |
Snay22 | 18:5a1c351094d2 | 93 | } |
Snay22 | 18:5a1c351094d2 | 94 | |
dlweakley | 20:8063c82bbb35 | 95 | void test_pulsetodist(){ // have user input x amount of pulses to move motor a certain distance |
dlweakley | 20:8063c82bbb35 | 96 | |
Snay22 | 18:5a1c351094d2 | 97 | while(1) |
Snay22 | 18:5a1c351094d2 | 98 | { |
Snay22 | 18:5a1c351094d2 | 99 | char str[4]; |
Snay22 | 18:5a1c351094d2 | 100 | int steps = 0; |
Snay22 | 18:5a1c351094d2 | 101 | int wait = 1000; |
Snay22 | 18:5a1c351094d2 | 102 | time_t start = time(0); |
Snay22 | 18:5a1c351094d2 | 103 | double elapsed; |
Snay22 | 18:5a1c351094d2 | 104 | printf("Enter the amount of steps: "); |
Snay22 | 18:5a1c351094d2 | 105 | fgets(str,4,stdin); |
Snay22 | 18:5a1c351094d2 | 106 | printf("\r\nSteps: %s", str); |
Snay22 | 18:5a1c351094d2 | 107 | steps = atoi(str); |
Snay22 | 18:5a1c351094d2 | 108 | |
Snay22 | 18:5a1c351094d2 | 109 | if(steps > 0) |
Snay22 | 18:5a1c351094d2 | 110 | motor.step_clockwise(steps, wait); |
Snay22 | 18:5a1c351094d2 | 111 | else |
Snay22 | 18:5a1c351094d2 | 112 | motor.step_anticlockwise(steps, wait); |
Snay22 | 18:5a1c351094d2 | 113 | elapsed = difftime( time(0), start); |
Snay22 | 18:5a1c351094d2 | 114 | printf("\r\nTook %i steps in time: %f \r\n",steps, elapsed); |
Snay22 | 18:5a1c351094d2 | 115 | } |
Snay22 | 18:5a1c351094d2 | 116 | } |
dlweakley | 20:8063c82bbb35 | 117 | void update_handler(){ |
dlweakley | 20:8063c82bbb35 | 118 | pend.update(); |
dlweakley | 20:8063c82bbb35 | 119 | } |
dlweakley | 8:2abfdbf5a3b8 | 120 | int main() { |
dlweakley | 20:8063c82bbb35 | 121 | update.attach(&update_handler, pend.UPDATE_TIME); |
dlweakley | 20:8063c82bbb35 | 122 | wait(1); |
dlweakley | 20:8063c82bbb35 | 123 | pend.set_zeros(); |
dlweakley | 20:8063c82bbb35 | 124 | while(1)pend.print_test(); |
dlweakley | 8:2abfdbf5a3b8 | 125 | |
dlweakley | 8:2abfdbf5a3b8 | 126 | } |