Inverted Pendulum / Mbed 2 deprecated IP-Interface

Dependencies:   mbed QEI

Committer:
Snay22
Date:
Wed Nov 16 02:10:43 2016 +0000
Revision:
18:5a1c351094d2
Parent:
15:a0b5c4306246
Child:
19:0e9bf6d61d0d
Pulse testing;

Who changed what in which revision?

UserRevisionLine numberNew 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 11:ed9539245ea0 13 Pot pend(PTC10,0);
dlweakley 8:2abfdbf5a3b8 14 void test_distance(){
dlweakley 8:2abfdbf5a3b8 15 int wait = 1000;
dlweakley 8:2abfdbf5a3b8 16 int steps = 100;
dlweakley 8:2abfdbf5a3b8 17 while (wait > 500){
dlweakley 8:2abfdbf5a3b8 18 while (steps > 10){
dlweakley 8:2abfdbf5a3b8 19 printf("steps: %i wait: %i \r\n", steps, wait);
dlweakley 8:2abfdbf5a3b8 20 for( int i = 0; i < 3; i++){
dlweakley 8:2abfdbf5a3b8 21 encoder.reset();
dlweakley 8:2abfdbf5a3b8 22 motor.step_clockwise(steps, wait);
dlweakley 8:2abfdbf5a3b8 23 int pulses_cw = encoder.getPulses();
dlweakley 8:2abfdbf5a3b8 24 wait_ms(200);
dlweakley 8:2abfdbf5a3b8 25 int coast_pulses_cw = encoder.getPulses();
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){
Snay22 18:5a1c351094d2 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 }
Snay22 18:5a1c351094d2 90 void disttopulse(int distance) //find amount of pulses to distance
Snay22 18:5a1c351094d2 91 {
Snay22 18:5a1c351094d2 92
Snay22 18:5a1c351094d2 93 }
Snay22 18:5a1c351094d2 94 void set_speed(int speed) // wait time correspondence to speed
Snay22 18:5a1c351094d2 95 {
Snay22 18:5a1c351094d2 96
Snay22 18:5a1c351094d2 97 }
Snay22 18:5a1c351094d2 98
Snay22 18:5a1c351094d2 99 void test_pulsetodist() // have user input x amount of pulses to move motor a certain distance
Snay22 18:5a1c351094d2 100 {
Snay22 18:5a1c351094d2 101 while(1)
Snay22 18:5a1c351094d2 102 {
Snay22 18:5a1c351094d2 103 char str[4];
Snay22 18:5a1c351094d2 104 int steps = 0;
Snay22 18:5a1c351094d2 105 int wait = 1000;
Snay22 18:5a1c351094d2 106 time_t start = time(0);
Snay22 18:5a1c351094d2 107 double elapsed;
Snay22 18:5a1c351094d2 108 printf("Enter the amount of steps: ");
Snay22 18:5a1c351094d2 109 fgets(str,4,stdin);
Snay22 18:5a1c351094d2 110 printf("\r\nSteps: %s", str);
Snay22 18:5a1c351094d2 111 steps = atoi(str);
Snay22 18:5a1c351094d2 112
Snay22 18:5a1c351094d2 113 if(steps > 0)
Snay22 18:5a1c351094d2 114 motor.step_clockwise(steps, wait);
Snay22 18:5a1c351094d2 115 else
Snay22 18:5a1c351094d2 116 motor.step_anticlockwise(steps, wait);
Snay22 18:5a1c351094d2 117 elapsed = difftime( time(0), start);
Snay22 18:5a1c351094d2 118 printf("\r\nTook %i steps in time: %f \r\n",steps, elapsed);
Snay22 18:5a1c351094d2 119 }
Snay22 18:5a1c351094d2 120 }
dlweakley 8:2abfdbf5a3b8 121 int main() {
Snay22 18:5a1c351094d2 122 while(1)
Snay22 18:5a1c351094d2 123 printf("Pulses: %i\r\n", encoder.getPulses());
dlweakley 8:2abfdbf5a3b8 124 //motor.step_clockwise(100,1500);
dlweakley 8:2abfdbf5a3b8 125 //motor.step_clockwise(100,1000);
dlweakley 8:2abfdbf5a3b8 126 //motor.step_clockwise(100,700);
dlweakley 8:2abfdbf5a3b8 127
dlweakley 8:2abfdbf5a3b8 128
Snay22 5:74fcd196ff96 129
dlweakley 8:2abfdbf5a3b8 130 }