Publishing for Michael McKinley to calibrate.
Fork of Encoders by
Encoder.cpp@1:2e39be3d2088, 2015-03-07 (annotated)
- Committer:
- perr1940
- Date:
- Sat Mar 07 01:56:09 2015 +0000
- Revision:
- 1:2e39be3d2088
- Parent:
- 0:1d2644a20b71
Publishing for Michael McKinley to calibrate.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
perr1940 | 0:1d2644a20b71 | 1 | #include "mbed.h" |
perr1940 | 0:1d2644a20b71 | 2 | #include "Encoder.h" |
perr1940 | 0:1d2644a20b71 | 3 | |
perr1940 | 0:1d2644a20b71 | 4 | /*********************************************************** |
perr1940 | 0:1d2644a20b71 | 5 | Constructor |
perr1940 | 0:1d2644a20b71 | 6 | ***********************************************************/ |
perr1940 | 0:1d2644a20b71 | 7 | Encoder::Encoder(PinName mosi, PinName miso, PinName sck, PinName ncs): _cs(ncs), _spi(mosi,miso,sck), enc2deg(360./(pow(2.,14.)-1)), zero_ang(1),sign(1) |
perr1940 | 0:1d2644a20b71 | 8 | { |
perr1940 | 0:1d2644a20b71 | 9 | // When CSPin is high the device is unselected |
perr1940 | 0:1d2644a20b71 | 10 | _cs = 1; |
perr1940 | 0:1d2644a20b71 | 11 | |
perr1940 | 0:1d2644a20b71 | 12 | //format SPI for 16 bit data, low steady state clock, second edge capture with 10MHz clock |
perr1940 | 0:1d2644a20b71 | 13 | _spi.format(16,1); |
perr1940 | 0:1d2644a20b71 | 14 | _spi.frequency(10000000); |
perr1940 | 0:1d2644a20b71 | 15 | } |
perr1940 | 0:1d2644a20b71 | 16 | |
perr1940 | 0:1d2644a20b71 | 17 | /*********************************************************** |
perr1940 | 0:1d2644a20b71 | 18 | Parity Check Function |
perr1940 | 0:1d2644a20b71 | 19 | ***********************************************************/ |
perr1940 | 0:1d2644a20b71 | 20 | //Parity check. Will be 0 if parity fails. This is used for a safety check. |
perr1940 | 0:1d2644a20b71 | 21 | |
perr1940 | 0:1d2644a20b71 | 22 | bool Encoder::parity_calc(int x) |
perr1940 | 0:1d2644a20b71 | 23 | { |
perr1940 | 0:1d2644a20b71 | 24 | x ^= x >> 8; |
perr1940 | 0:1d2644a20b71 | 25 | x ^= x >> 4; |
perr1940 | 0:1d2644a20b71 | 26 | x ^= x >> 2; |
perr1940 | 0:1d2644a20b71 | 27 | x ^= x >> 1; |
perr1940 | 0:1d2644a20b71 | 28 | x &= 1; |
perr1940 | 0:1d2644a20b71 | 29 | return x; |
perr1940 | 0:1d2644a20b71 | 30 | } |
perr1940 | 0:1d2644a20b71 | 31 | |
perr1940 | 0:1d2644a20b71 | 32 | /*********************************************************** |
perr1940 | 0:1d2644a20b71 | 33 | Initialize Read_Encoder |
perr1940 | 0:1d2644a20b71 | 34 | ***********************************************************/ |
perr1940 | 0:1d2644a20b71 | 35 | //Taken from Init.cpp, used to be zero_ang_L, changed to zero_ang to accompany change to local variable |
perr1940 | 0:1d2644a20b71 | 36 | //approximate point where the pilot should be standing straight |
perr1940 | 0:1d2644a20b71 | 37 | |
perr1940 | 0:1d2644a20b71 | 38 | void Encoder::init(float blah) |
perr1940 | 0:1d2644a20b71 | 39 | { |
perr1940 | 0:1d2644a20b71 | 40 | zero_ang=blah; |
perr1940 | 0:1d2644a20b71 | 41 | } |
perr1940 | 0:1d2644a20b71 | 42 | |
perr1940 | 0:1d2644a20b71 | 43 | /*********************************************************** |
perr1940 | 0:1d2644a20b71 | 44 | Read_Encoder Program |
perr1940 | 0:1d2644a20b71 | 45 | ***********************************************************/ |
perr1940 | 0:1d2644a20b71 | 46 | //Reads the encoder, does parity check, and stores the error flag. |
perr1940 | 0:1d2644a20b71 | 47 | //@returns An unsigned integer which is the number of ticks away from the zero position. |
perr1940 | 0:1d2644a20b71 | 48 | |
perr1940 | 0:1d2644a20b71 | 49 | int Encoder::read() |
perr1940 | 0:1d2644a20b71 | 50 | { |
perr1940 | 0:1d2644a20b71 | 51 | // Select the device by seting chip select low |
perr1940 | 0:1d2644a20b71 | 52 | _cs = 0; |
perr1940 | 0:1d2644a20b71 | 53 | |
perr1940 | 0:1d2644a20b71 | 54 | //send a dummy byte to the MOSI to get a reading through the MISO |
perr1940 | 0:1d2644a20b71 | 55 | raw=_spi.write(0xFFFF); |
perr1940 | 0:1d2644a20b71 | 56 | |
perr1940 | 0:1d2644a20b71 | 57 | // Set the select back to low to allow the encoder to chill |
perr1940 | 0:1d2644a20b71 | 58 | _cs=1; |
perr1940 | 0:1d2644a20b71 | 59 | |
perr1940 | 0:1d2644a20b71 | 60 | // XNOR parity check |
perr1940 | 0:1d2644a20b71 | 61 | parity=~(parity_calc(raw) ^ (raw>>15)); |
perr1940 | 1:2e39be3d2088 | 62 | if(!parity) { |
perr1940 | 1:2e39be3d2088 | 63 | // Select the device by seting chip select low |
perr1940 | 1:2e39be3d2088 | 64 | _cs = 0; |
perr1940 | 1:2e39be3d2088 | 65 | |
perr1940 | 1:2e39be3d2088 | 66 | //send a dummy byte to the MOSI to get a reading through the MISO |
perr1940 | 1:2e39be3d2088 | 67 | raw=_spi.write(0xFFFF); |
perr1940 | 1:2e39be3d2088 | 68 | |
perr1940 | 1:2e39be3d2088 | 69 | // Set the select back to low to allow the encoder to chill |
perr1940 | 1:2e39be3d2088 | 70 | _cs=1; |
perr1940 | 1:2e39be3d2088 | 71 | |
perr1940 | 1:2e39be3d2088 | 72 | // XNOR parity check |
perr1940 | 1:2e39be3d2088 | 73 | parity=~(parity_calc(raw) ^ (raw>>15)); |
perr1940 | 1:2e39be3d2088 | 74 | } |
perr1940 | 0:1d2644a20b71 | 75 | |
perr1940 | 0:1d2644a20b71 | 76 | // Mask and bit shift to look at encoder flag value |
perr1940 | 0:1d2644a20b71 | 77 | enc_flag=(raw & 0x4000)>>14; |
perr1940 | 0:1d2644a20b71 | 78 | |
perr1940 | 0:1d2644a20b71 | 79 | raw&=0x3FFF; |
perr1940 | 0:1d2644a20b71 | 80 | |
perr1940 | 0:1d2644a20b71 | 81 | return raw; |
perr1940 | 0:1d2644a20b71 | 82 | } |
perr1940 | 0:1d2644a20b71 | 83 | |
perr1940 | 1:2e39be3d2088 | 84 | short int Encoder::readRaw() |
perr1940 | 1:2e39be3d2088 | 85 | { |
perr1940 | 0:1d2644a20b71 | 86 | return raw; |
perr1940 | 1:2e39be3d2088 | 87 | } |
perr1940 | 0:1d2644a20b71 | 88 | /*********************************************************** |
perr1940 | 0:1d2644a20b71 | 89 | Translate Function - Translates Ticks to Position |
perr1940 | 0:1d2644a20b71 | 90 | ***********************************************************/ |
perr1940 | 0:1d2644a20b71 | 91 | // Translates the left encoder value from encoder ticks into degrees from the pilot's zero. |
perr1940 | 0:1d2644a20b71 | 92 | // @param ticks The number of encoder ticks from the encoder zero to the current position. |
perr1940 | 0:1d2644a20b71 | 93 | // @returns A float which is the degrees away from the pilot's zero position. |
perr1940 | 0:1d2644a20b71 | 94 | |
perr1940 | 0:1d2644a20b71 | 95 | |
perr1940 | 0:1d2644a20b71 | 96 | float Encoder::angle() |
perr1940 | 0:1d2644a20b71 | 97 | { |
perr1940 | 0:1d2644a20b71 | 98 | // converts the result (encoder tick position) to degrees |
perr1940 | 0:1d2644a20b71 | 99 | float pos = -sign*read()*enc2deg+zero_ang; |
perr1940 | 0:1d2644a20b71 | 100 | return pos; |
perr1940 | 0:1d2644a20b71 | 101 | } |
perr1940 | 0:1d2644a20b71 | 102 | |
perr1940 | 0:1d2644a20b71 | 103 | /*********************************************************** |
perr1940 | 0:1d2644a20b71 | 104 | Get parity from parityFlag |
perr1940 | 0:1d2644a20b71 | 105 | ***********************************************************/ |
perr1940 | 0:1d2644a20b71 | 106 | //Error Flag. The encoder has a built in hardware error flag. This is used for a safety check. |
perr1940 | 0:1d2644a20b71 | 107 | |
perr1940 | 0:1d2644a20b71 | 108 | bool Encoder::parityFlag() |
perr1940 | 0:1d2644a20b71 | 109 | { |
perr1940 | 0:1d2644a20b71 | 110 | return parity; |
perr1940 | 0:1d2644a20b71 | 111 | } |
perr1940 | 0:1d2644a20b71 | 112 | |
perr1940 | 0:1d2644a20b71 | 113 | /*********************************************************** |
perr1940 | 0:1d2644a20b71 | 114 | Get enc_flag from encFlag |
perr1940 | 0:1d2644a20b71 | 115 | ***********************************************************/ |
perr1940 | 0:1d2644a20b71 | 116 | bool Encoder::encFlag() |
perr1940 | 0:1d2644a20b71 | 117 | { |
perr1940 | 0:1d2644a20b71 | 118 | return enc_flag; |
perr1940 | 0:1d2644a20b71 | 119 | } |
perr1940 | 0:1d2644a20b71 | 120 | |
perr1940 | 0:1d2644a20b71 | 121 | void Encoder::flip() |
perr1940 | 0:1d2644a20b71 | 122 | { |
perr1940 | 0:1d2644a20b71 | 123 | sign=-sign; |
perr1940 | 1:2e39be3d2088 | 124 | } |