Daniel Camuti
/
ES309_Project3
Code written for the encoder
Encoder.cpp@0:57a56e69e3cc, 2016-03-22 (annotated)
- Committer:
- DCamuti
- Date:
- Tue Mar 22 15:30:43 2016 +0000
- Revision:
- 0:57a56e69e3cc
N/A
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
DCamuti | 0:57a56e69e3cc | 1 | #include "mbed.h" |
DCamuti | 0:57a56e69e3cc | 2 | #include "Motor.h" |
DCamuti | 0:57a56e69e3cc | 3 | #include "QEI.h" |
DCamuti | 0:57a56e69e3cc | 4 | |
DCamuti | 0:57a56e69e3cc | 5 | // Definitions |
DCamuti | 0:57a56e69e3cc | 6 | #define PI (3.1415926) |
DCamuti | 0:57a56e69e3cc | 7 | #define LOG_FREQ (100) // (Hz) - data logging frequency |
DCamuti | 0:57a56e69e3cc | 8 | #define NUM_DATA_PTS (500) // (#) - number of data points at above LOG_FREQ; |
DCamuti | 0:57a56e69e3cc | 9 | |
DCamuti | 0:57a56e69e3cc | 10 | // Classes and objects |
DCamuti | 0:57a56e69e3cc | 11 | QEI enc(p17,p16,NC,1600); // Need to know PPR count of selected encoder |
DCamuti | 0:57a56e69e3cc | 12 | Serial pc(USBTX, USBRX); |
DCamuti | 0:57a56e69e3cc | 13 | Motor m(p22,p21,p23); // pin 18 will not be used |
DCamuti | 0:57a56e69e3cc | 14 | Timer t; |
DCamuti | 0:57a56e69e3cc | 15 | Ticker ticker_timer; |
DCamuti | 0:57a56e69e3cc | 16 | DigitalOut mbed_LED(LED1); |
DCamuti | 0:57a56e69e3cc | 17 | |
DCamuti | 0:57a56e69e3cc | 18 | // Create local file system under the name "mBed_drive" |
DCamuti | 0:57a56e69e3cc | 19 | LocalFileSystem mBed_drive("local"); |
DCamuti | 0:57a56e69e3cc | 20 | |
DCamuti | 0:57a56e69e3cc | 21 | // Global declarations |
DCamuti | 0:57a56e69e3cc | 22 | int num_data_pts_rec, i; |
DCamuti | 0:57a56e69e3cc | 23 | char c; |
DCamuti | 0:57a56e69e3cc | 24 | float theta; |
DCamuti | 0:57a56e69e3cc | 25 | float lv_time[NUM_DATA_PTS]; |
DCamuti | 0:57a56e69e3cc | 26 | float lv_theta[NUM_DATA_PTS]; |
DCamuti | 0:57a56e69e3cc | 27 | |
DCamuti | 0:57a56e69e3cc | 28 | |
DCamuti | 0:57a56e69e3cc | 29 | // timer_function |
DCamuti | 0:57a56e69e3cc | 30 | void timer_function() |
DCamuti | 0:57a56e69e3cc | 31 | { |
DCamuti | 0:57a56e69e3cc | 32 | |
DCamuti | 0:57a56e69e3cc | 33 | //generate random data |
DCamuti | 0:57a56e69e3cc | 34 | //float min = 0.1 / 3.3; //Define 0.1 V as ~03% from 3.3 V |
DCamuti | 0:57a56e69e3cc | 35 | //float max = 3.2 / 3.3; //Define 3.2 V as ~97% from 3.3 V |
DCamuti | 0:57a56e69e3cc | 36 | |
DCamuti | 0:57a56e69e3cc | 37 | //measure signal |
DCamuti | 0:57a56e69e3cc | 38 | //signal = min + ((float)rand()/RAND_MAX)*(max - min)*3.3; // Generate random voltage output from 0.1 to 3.2 |
DCamuti | 0:57a56e69e3cc | 39 | //printf("Voltage= %1.2f \n", signal); // Debug |
DCamuti | 0:57a56e69e3cc | 40 | |
DCamuti | 0:57a56e69e3cc | 41 | // Measure theta |
DCamuti | 0:57a56e69e3cc | 42 | theta = (long)enc.getPulses()/(1600.0*2.0)*2.0*PI; // The QEI uses 2X interpolation by default. |
DCamuti | 0:57a56e69e3cc | 43 | if(num_data_pts_rec<NUM_DATA_PTS) { |
DCamuti | 0:57a56e69e3cc | 44 | lv_time[num_data_pts_rec] = t.read(); |
DCamuti | 0:57a56e69e3cc | 45 | lv_theta[num_data_pts_rec] = theta; |
DCamuti | 0:57a56e69e3cc | 46 | num_data_pts_rec++; |
DCamuti | 0:57a56e69e3cc | 47 | } |
DCamuti | 0:57a56e69e3cc | 48 | } |
DCamuti | 0:57a56e69e3cc | 49 | |
DCamuti | 0:57a56e69e3cc | 50 | // main function |
DCamuti | 0:57a56e69e3cc | 51 | int main() |
DCamuti | 0:57a56e69e3cc | 52 | { |
DCamuti | 0:57a56e69e3cc | 53 | // Serial port initializations |
DCamuti | 0:57a56e69e3cc | 54 | pc.baud(9600); |
DCamuti | 0:57a56e69e3cc | 55 | |
DCamuti | 0:57a56e69e3cc | 56 | // Initialize encoder |
DCamuti | 0:57a56e69e3cc | 57 | enc.reset(); |
DCamuti | 0:57a56e69e3cc | 58 | |
DCamuti | 0:57a56e69e3cc | 59 | // Number of data points recorded |
DCamuti | 0:57a56e69e3cc | 60 | num_data_pts_rec = 0; |
DCamuti | 0:57a56e69e3cc | 61 | |
DCamuti | 0:57a56e69e3cc | 62 | // Clear Terra Term screen |
DCamuti | 0:57a56e69e3cc | 63 | for(i=1; i<80; i++) |
DCamuti | 0:57a56e69e3cc | 64 | pc.printf("\n\r"); |
DCamuti | 0:57a56e69e3cc | 65 | |
DCamuti | 0:57a56e69e3cc | 66 | // Prompt user to start recording data. |
DCamuti | 0:57a56e69e3cc | 67 | pc.printf("Running datalog.cpp.\r\n"); |
DCamuti | 0:57a56e69e3cc | 68 | pc.printf("____________________________________________________________r\n"); |
DCamuti | 0:57a56e69e3cc | 69 | pc.printf("\r\nPress any key to start.\r\n"); |
DCamuti | 0:57a56e69e3cc | 70 | |
DCamuti | 0:57a56e69e3cc | 71 | // Wait until user presses a key |
DCamuti | 0:57a56e69e3cc | 72 | while(!pc.readable()); |
DCamuti | 0:57a56e69e3cc | 73 | c = pc.getc(); |
DCamuti | 0:57a56e69e3cc | 74 | c = '\0'; |
DCamuti | 0:57a56e69e3cc | 75 | |
DCamuti | 0:57a56e69e3cc | 76 | // Start timer and tickers |
DCamuti | 0:57a56e69e3cc | 77 | t.start(); |
DCamuti | 0:57a56e69e3cc | 78 | ticker_timer.attach(&timer_function,1.0/LOG_FREQ); |
DCamuti | 0:57a56e69e3cc | 79 | |
DCamuti | 0:57a56e69e3cc | 80 | // Measurement while loop |
DCamuti | 0:57a56e69e3cc | 81 | while(num_data_pts_rec < NUM_DATA_PTS) { |
DCamuti | 0:57a56e69e3cc | 82 | // Print data to screen |
DCamuti | 0:57a56e69e3cc | 83 | pc.printf("%.2f %.2f; \n\r",t.read(),theta); |
DCamuti | 0:57a56e69e3cc | 84 | } |
DCamuti | 0:57a56e69e3cc | 85 | |
DCamuti | 0:57a56e69e3cc | 86 | // Turn off ticker functions |
DCamuti | 0:57a56e69e3cc | 87 | ticker_timer.detach(); |
DCamuti | 0:57a56e69e3cc | 88 | |
DCamuti | 0:57a56e69e3cc | 89 | // File initialization |
DCamuti | 0:57a56e69e3cc | 90 | pc.printf("Opening mBed_output.m ... "); |
DCamuti | 0:57a56e69e3cc | 91 | FILE *fp = fopen("/local/mBedOut.m", "w"); |
DCamuti | 0:57a56e69e3cc | 92 | pc.printf("done.\n"); |
DCamuti | 0:57a56e69e3cc | 93 | |
DCamuti | 0:57a56e69e3cc | 94 | // Print measured data to local file. |
DCamuti | 0:57a56e69e3cc | 95 | pc.printf("Printing measured data to local file ... "); |
DCamuti | 0:57a56e69e3cc | 96 | fprintf(fp,"lv.time = [ \n"); |
DCamuti | 0:57a56e69e3cc | 97 | for(i=0; i<num_data_pts_rec; i++) { |
DCamuti | 0:57a56e69e3cc | 98 | //fprintf(fp,"lv.time(%d,1) = [ %.5f;\n",i+1,lv_time[i]); |
DCamuti | 0:57a56e69e3cc | 99 | fprintf(fp, "%.5f; \n",lv_time[i]); |
DCamuti | 0:57a56e69e3cc | 100 | } |
DCamuti | 0:57a56e69e3cc | 101 | fprintf(fp,"]; \n"); |
DCamuti | 0:57a56e69e3cc | 102 | fprintf(fp,"lv.theta = [ \n"); |
DCamuti | 0:57a56e69e3cc | 103 | for(i=0; i<num_data_pts_rec; i++) { |
DCamuti | 0:57a56e69e3cc | 104 | fprintf(fp,"%.5f;\n",lv_theta[i]); |
DCamuti | 0:57a56e69e3cc | 105 | } |
DCamuti | 0:57a56e69e3cc | 106 | fprintf(fp,"]; \n"); |
DCamuti | 0:57a56e69e3cc | 107 | fprintf(fp,"plot(lv.time,lv.theta); \n"); |
DCamuti | 0:57a56e69e3cc | 108 | fprintf(fp,"xlabel('time (s)'); \n"); |
DCamuti | 0:57a56e69e3cc | 109 | fprintf(fp,"ylabel('radians'); \n"); |
DCamuti | 0:57a56e69e3cc | 110 | |
DCamuti | 0:57a56e69e3cc | 111 | //fprintf(fp,"lv.units = '(volts)';"); |
DCamuti | 0:57a56e69e3cc | 112 | pc.printf("done.\n\r"); |
DCamuti | 0:57a56e69e3cc | 113 | |
DCamuti | 0:57a56e69e3cc | 114 | // Close file |
DCamuti | 0:57a56e69e3cc | 115 | fclose(fp); |
DCamuti | 0:57a56e69e3cc | 116 | |
DCamuti | 0:57a56e69e3cc | 117 | // Finished |
DCamuti | 0:57a56e69e3cc | 118 | pc.printf("____________________________________________________________\r\n\r\n"); |
DCamuti | 0:57a56e69e3cc | 119 | pc.printf("\n\rProgram exited normally. End of line.\n\r"); |
DCamuti | 0:57a56e69e3cc | 120 | |
DCamuti | 0:57a56e69e3cc | 121 | } |
DCamuti | 0:57a56e69e3cc | 122 |