Publishing for Michael McKinley to calibrate.

Fork of Encoders by Bradley Perry

Committer:
perr1940
Date:
Mon Nov 24 03:33:29 2014 +0000
Revision:
0:1d2644a20b71
Child:
1:2e39be3d2088
Encoders stuff!!! going to Italy;

Who changed what in which revision?

UserRevisionLine numberNew 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 0:1d2644a20b71 62
perr1940 0:1d2644a20b71 63 // Mask and bit shift to look at encoder flag value
perr1940 0:1d2644a20b71 64 enc_flag=(raw & 0x4000)>>14;
perr1940 0:1d2644a20b71 65
perr1940 0:1d2644a20b71 66 raw&=0x3FFF;
perr1940 0:1d2644a20b71 67
perr1940 0:1d2644a20b71 68 return raw;
perr1940 0:1d2644a20b71 69 }
perr1940 0:1d2644a20b71 70
perr1940 0:1d2644a20b71 71 short int Encoder::readRaw(){
perr1940 0:1d2644a20b71 72 return raw;
perr1940 0:1d2644a20b71 73 }
perr1940 0:1d2644a20b71 74 /***********************************************************
perr1940 0:1d2644a20b71 75 Translate Function - Translates Ticks to Position
perr1940 0:1d2644a20b71 76 ***********************************************************/
perr1940 0:1d2644a20b71 77 // Translates the left encoder value from encoder ticks into degrees from the pilot's zero.
perr1940 0:1d2644a20b71 78 // @param ticks The number of encoder ticks from the encoder zero to the current position.
perr1940 0:1d2644a20b71 79 // @returns A float which is the degrees away from the pilot's zero position.
perr1940 0:1d2644a20b71 80
perr1940 0:1d2644a20b71 81
perr1940 0:1d2644a20b71 82 float Encoder::angle()
perr1940 0:1d2644a20b71 83 {
perr1940 0:1d2644a20b71 84 // converts the result (encoder tick position) to degrees
perr1940 0:1d2644a20b71 85 float pos = -sign*read()*enc2deg+zero_ang;
perr1940 0:1d2644a20b71 86 return pos;
perr1940 0:1d2644a20b71 87 }
perr1940 0:1d2644a20b71 88
perr1940 0:1d2644a20b71 89 /***********************************************************
perr1940 0:1d2644a20b71 90 Get parity from parityFlag
perr1940 0:1d2644a20b71 91 ***********************************************************/
perr1940 0:1d2644a20b71 92 //Error Flag. The encoder has a built in hardware error flag. This is used for a safety check.
perr1940 0:1d2644a20b71 93
perr1940 0:1d2644a20b71 94 bool Encoder::parityFlag()
perr1940 0:1d2644a20b71 95 {
perr1940 0:1d2644a20b71 96 return parity;
perr1940 0:1d2644a20b71 97 }
perr1940 0:1d2644a20b71 98
perr1940 0:1d2644a20b71 99 /***********************************************************
perr1940 0:1d2644a20b71 100 Get enc_flag from encFlag
perr1940 0:1d2644a20b71 101 ***********************************************************/
perr1940 0:1d2644a20b71 102 bool Encoder::encFlag()
perr1940 0:1d2644a20b71 103 {
perr1940 0:1d2644a20b71 104 return enc_flag;
perr1940 0:1d2644a20b71 105 }
perr1940 0:1d2644a20b71 106
perr1940 0:1d2644a20b71 107 void Encoder::flip()
perr1940 0:1d2644a20b71 108 {
perr1940 0:1d2644a20b71 109 sign=-sign;
perr1940 0:1d2644a20b71 110 }