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