Chen Wei Ting
/
LSM9DS1_project_3
read encoder
main.cpp@1:2f2a74337b77, 2018-07-31 (annotated)
- Committer:
- JJting
- Date:
- Tue Jul 31 13:24:56 2018 +0000
- Revision:
- 1:2f2a74337b77
- Parent:
- 0:f2657b94ea70
- Child:
- 2:33062fc279ca
20180731ver2;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
JJting | 0:f2657b94ea70 | 1 | #include "mbed.h" |
JJting | 1:2f2a74337b77 | 2 | #include "encoder.h" |
JJting | 0:f2657b94ea70 | 3 | |
JJting | 0:f2657b94ea70 | 4 | Serial uart(USBTX, USBRX); |
JJting | 0:f2657b94ea70 | 5 | //Serial uart(D10,D2); // TX : D10 RX : D2 // blueteeth |
JJting | 0:f2657b94ea70 | 6 | DigitalOut LED(A4); // check if the code is running |
JJting | 0:f2657b94ea70 | 7 | |
JJting | 0:f2657b94ea70 | 8 | // timer |
JJting | 0:f2657b94ea70 | 9 | Ticker timer1; |
JJting | 0:f2657b94ea70 | 10 | float ITR_time1 = 10000.0; // unit:ms |
JJting | 0:f2657b94ea70 | 11 | |
JJting | 0:f2657b94ea70 | 12 | |
JJting | 0:f2657b94ea70 | 13 | // encoder |
JJting | 1:2f2a74337b77 | 14 | //DigitalOut encoder_cs(D9); |
JJting | 1:2f2a74337b77 | 15 | //SPI spi_encoder(D11, D12, D13); // mosi, miso, sclk |
JJting | 1:2f2a74337b77 | 16 | //unsigned short encoder_value = 0; |
JJting | 1:2f2a74337b77 | 17 | //unsigned short angle = 0; |
JJting | 1:2f2a74337b77 | 18 | //unsigned short angle_old; |
JJting | 1:2f2a74337b77 | 19 | //unsigned short angle_init; |
JJting | 1:2f2a74337b77 | 20 | //int angle_dif; |
JJting | 1:2f2a74337b77 | 21 | //int a_dif; |
JJting | 1:2f2a74337b77 | 22 | //int Angle; |
JJting | 1:2f2a74337b77 | 23 | //unsigned short k = 0; |
JJting | 0:f2657b94ea70 | 24 | |
JJting | 0:f2657b94ea70 | 25 | // uart_tx |
JJting | 0:f2657b94ea70 | 26 | union splitter { |
JJting | 0:f2657b94ea70 | 27 | short j; |
JJting | 0:f2657b94ea70 | 28 | char C[2]; |
JJting | 0:f2657b94ea70 | 29 | // C[0] is lowbyte of j, C[1] is highbyte of j |
JJting | 0:f2657b94ea70 | 30 | }; |
JJting | 0:f2657b94ea70 | 31 | char T[16] = {255,255,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; |
JJting | 0:f2657b94ea70 | 32 | int i = 0; |
JJting | 0:f2657b94ea70 | 33 | |
JJting | 0:f2657b94ea70 | 34 | // function |
JJting | 1:2f2a74337b77 | 35 | //void init_encoder(); |
JJting | 1:2f2a74337b77 | 36 | //void init_SPI_encoder(); |
JJting | 0:f2657b94ea70 | 37 | void init_UART(); |
JJting | 0:f2657b94ea70 | 38 | void init_TIMER(); |
JJting | 0:f2657b94ea70 | 39 | void timer1_ITR(); |
JJting | 1:2f2a74337b77 | 40 | //void angle_measure(); |
JJting | 1:2f2a74337b77 | 41 | //int angle_count(unsigned short,unsigned short); |
JJting | 0:f2657b94ea70 | 42 | void uart_tx(); |
JJting | 0:f2657b94ea70 | 43 | |
JJting | 0:f2657b94ea70 | 44 | |
JJting | 0:f2657b94ea70 | 45 | int main() |
JJting | 0:f2657b94ea70 | 46 | { |
JJting | 0:f2657b94ea70 | 47 | LED = 1; |
JJting | 0:f2657b94ea70 | 48 | wait_ms(500); |
JJting | 0:f2657b94ea70 | 49 | // initial sensor |
JJting | 0:f2657b94ea70 | 50 | init_encoder(); |
JJting | 1:2f2a74337b77 | 51 | init_SPI_encoder(); |
JJting | 0:f2657b94ea70 | 52 | // initial uart |
JJting | 0:f2657b94ea70 | 53 | init_UART(); |
JJting | 0:f2657b94ea70 | 54 | |
JJting | 0:f2657b94ea70 | 55 | wait_ms(500); |
JJting | 0:f2657b94ea70 | 56 | LED = 0; |
JJting | 0:f2657b94ea70 | 57 | |
JJting | 0:f2657b94ea70 | 58 | init_TIMER(); |
JJting | 0:f2657b94ea70 | 59 | |
JJting | 0:f2657b94ea70 | 60 | while(1) { |
JJting | 0:f2657b94ea70 | 61 | } |
JJting | 0:f2657b94ea70 | 62 | |
JJting | 0:f2657b94ea70 | 63 | } |
JJting | 1:2f2a74337b77 | 64 | /* |
JJting | 0:f2657b94ea70 | 65 | void init_encoder() |
JJting | 0:f2657b94ea70 | 66 | { |
JJting | 0:f2657b94ea70 | 67 | // encoder_cs = 1; // disable encoder |
JJting | 0:f2657b94ea70 | 68 | |
JJting | 0:f2657b94ea70 | 69 | encoder_cs = 0; // Select the device by seting chip select low |
JJting | 0:f2657b94ea70 | 70 | encoder_value = spi_encoder.write(0x00); |
JJting | 0:f2657b94ea70 | 71 | angle_init = encoder_value >> 3; |
JJting | 0:f2657b94ea70 | 72 | encoder_cs = 1; // Deselect the device |
JJting | 0:f2657b94ea70 | 73 | |
JJting | 0:f2657b94ea70 | 74 | } |
JJting | 1:2f2a74337b77 | 75 | */ |
JJting | 1:2f2a74337b77 | 76 | /* |
JJting | 1:2f2a74337b77 | 77 | void init_SPI_encoder() |
JJting | 0:f2657b94ea70 | 78 | { |
JJting | 0:f2657b94ea70 | 79 | spi_encoder.format(16,3); |
JJting | 0:f2657b94ea70 | 80 | spi_encoder.frequency(1000000); // 1MHz clock rate |
JJting | 0:f2657b94ea70 | 81 | } |
JJting | 1:2f2a74337b77 | 82 | */ |
JJting | 0:f2657b94ea70 | 83 | void init_UART() |
JJting | 0:f2657b94ea70 | 84 | { |
JJting | 0:f2657b94ea70 | 85 | uart.baud(115200); |
JJting | 0:f2657b94ea70 | 86 | } |
JJting | 0:f2657b94ea70 | 87 | |
JJting | 0:f2657b94ea70 | 88 | void init_TIMER() |
JJting | 0:f2657b94ea70 | 89 | { |
JJting | 0:f2657b94ea70 | 90 | timer1.attach_us(&timer1_ITR, ITR_time1); |
JJting | 0:f2657b94ea70 | 91 | } |
JJting | 0:f2657b94ea70 | 92 | |
JJting | 0:f2657b94ea70 | 93 | void timer1_ITR() |
JJting | 0:f2657b94ea70 | 94 | { |
JJting | 0:f2657b94ea70 | 95 | angle_measure(); |
JJting | 0:f2657b94ea70 | 96 | // angle_count(); |
JJting | 0:f2657b94ea70 | 97 | uart_tx(); |
JJting | 0:f2657b94ea70 | 98 | } |
JJting | 1:2f2a74337b77 | 99 | /* |
JJting | 0:f2657b94ea70 | 100 | void angle_measure() |
JJting | 0:f2657b94ea70 | 101 | { |
JJting | 0:f2657b94ea70 | 102 | encoder_cs = 0; // Select the device by seting chip select low |
JJting | 0:f2657b94ea70 | 103 | encoder_value = spi_encoder.write(0x00); |
JJting | 0:f2657b94ea70 | 104 | angle = encoder_value >> 3; |
JJting | 0:f2657b94ea70 | 105 | encoder_cs = 1; // Deselect the device |
JJting | 0:f2657b94ea70 | 106 | |
JJting | 0:f2657b94ea70 | 107 | if (k == 0) |
JJting | 0:f2657b94ea70 | 108 | { |
JJting | 0:f2657b94ea70 | 109 | angle_dif = angle_count(angle, angle_init); |
JJting | 0:f2657b94ea70 | 110 | Angle = angle_init + angle_dif; |
JJting | 0:f2657b94ea70 | 111 | angle_old = angle; |
JJting | 0:f2657b94ea70 | 112 | k++; |
JJting | 0:f2657b94ea70 | 113 | } |
JJting | 0:f2657b94ea70 | 114 | else |
JJting | 0:f2657b94ea70 | 115 | { |
JJting | 0:f2657b94ea70 | 116 | angle_dif = angle_count(angle, angle_old); |
JJting | 0:f2657b94ea70 | 117 | Angle = Angle + angle_dif; |
JJting | 0:f2657b94ea70 | 118 | angle_old = angle; |
JJting | 0:f2657b94ea70 | 119 | } |
JJting | 0:f2657b94ea70 | 120 | } |
JJting | 1:2f2a74337b77 | 121 | */ |
JJting | 1:2f2a74337b77 | 122 | /* |
JJting | 0:f2657b94ea70 | 123 | int angle_count(unsigned short now,unsigned short old) |
JJting | 0:f2657b94ea70 | 124 | { |
JJting | 0:f2657b94ea70 | 125 | a_dif = now - old; |
JJting | 0:f2657b94ea70 | 126 | if (a_dif > 4096/2) |
JJting | 0:f2657b94ea70 | 127 | a_dif = -(4096 - a_dif); |
JJting | 0:f2657b94ea70 | 128 | else if (a_dif < -4096/2) |
JJting | 0:f2657b94ea70 | 129 | a_dif = 4096 + a_dif; |
JJting | 0:f2657b94ea70 | 130 | else |
JJting | 0:f2657b94ea70 | 131 | a_dif = a_dif; |
JJting | 0:f2657b94ea70 | 132 | |
JJting | 0:f2657b94ea70 | 133 | return a_dif; |
JJting | 0:f2657b94ea70 | 134 | } |
JJting | 1:2f2a74337b77 | 135 | */ |
JJting | 0:f2657b94ea70 | 136 | void uart_tx() |
JJting | 0:f2657b94ea70 | 137 | { |
JJting | 0:f2657b94ea70 | 138 | splitter s1; |
JJting | 0:f2657b94ea70 | 139 | splitter s2; |
JJting | 0:f2657b94ea70 | 140 | splitter s3; |
JJting | 0:f2657b94ea70 | 141 | splitter s4; |
JJting | 0:f2657b94ea70 | 142 | splitter s5; |
JJting | 0:f2657b94ea70 | 143 | splitter s6; |
JJting | 0:f2657b94ea70 | 144 | splitter s7; |
JJting | 0:f2657b94ea70 | 145 | |
JJting | 0:f2657b94ea70 | 146 | s1.j = angle; |
JJting | 0:f2657b94ea70 | 147 | s2.j = Angle; |
JJting | 0:f2657b94ea70 | 148 | s3.j = 2; |
JJting | 0:f2657b94ea70 | 149 | s4.j = 3; |
JJting | 0:f2657b94ea70 | 150 | s5.j = 1; |
JJting | 0:f2657b94ea70 | 151 | s6.j = 2; |
JJting | 0:f2657b94ea70 | 152 | s7.j = 3; |
JJting | 0:f2657b94ea70 | 153 | |
JJting | 0:f2657b94ea70 | 154 | T[2] = s1.C[0]; |
JJting | 0:f2657b94ea70 | 155 | T[3] = s1.C[1]; |
JJting | 0:f2657b94ea70 | 156 | T[4] = s2.C[0]; |
JJting | 0:f2657b94ea70 | 157 | T[5] = s2.C[1]; |
JJting | 0:f2657b94ea70 | 158 | T[6] = s3.C[0]; |
JJting | 0:f2657b94ea70 | 159 | T[7] = s3.C[1]; |
JJting | 0:f2657b94ea70 | 160 | T[8] = s4.C[0]; |
JJting | 0:f2657b94ea70 | 161 | T[9] = s4.C[1]; |
JJting | 0:f2657b94ea70 | 162 | T[10] = s5.C[0]; |
JJting | 0:f2657b94ea70 | 163 | T[11] = s5.C[1]; |
JJting | 0:f2657b94ea70 | 164 | T[12] = s6.C[0]; |
JJting | 0:f2657b94ea70 | 165 | T[13] = s6.C[1]; |
JJting | 0:f2657b94ea70 | 166 | T[14] = s7.C[0]; |
JJting | 0:f2657b94ea70 | 167 | T[15] = s7.C[1]; |
JJting | 0:f2657b94ea70 | 168 | |
JJting | 0:f2657b94ea70 | 169 | while(1) { |
JJting | 0:f2657b94ea70 | 170 | if (uart.writable() == 1) { |
JJting | 0:f2657b94ea70 | 171 | uart.putc(T[i]); |
JJting | 0:f2657b94ea70 | 172 | i++; |
JJting | 0:f2657b94ea70 | 173 | } |
JJting | 0:f2657b94ea70 | 174 | if (i >= (sizeof(T)-1)) { |
JJting | 0:f2657b94ea70 | 175 | i = 0; |
JJting | 0:f2657b94ea70 | 176 | break; |
JJting | 0:f2657b94ea70 | 177 | } |
JJting | 0:f2657b94ea70 | 178 | } |
JJting | 0:f2657b94ea70 | 179 | } |