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