df

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Encoder.cpp Source File

Encoder.cpp

00001 #include "mbed.h"
00002 #include "Encoder.h"
00003 
00004 /***********************************************************
00005 Constructor
00006 ***********************************************************/
00007 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)
00008 {
00009     // When CSPin is high the device is unselected
00010     _cs = 1;
00011 
00012     //format SPI for 16 bit data, low steady state clock, second edge capture with 10MHz clock
00013     _spi.format(16,1);
00014     _spi.frequency(96000000);
00015 }
00016 
00017 /***********************************************************
00018 Parity Check Function
00019 ***********************************************************/
00020 //Parity check.  Will be 0 if parity fails.  This is used for a safety check.
00021 
00022 bool Encoder::parity_calc(int x)
00023 {
00024     x ^= x >> 8;
00025     x ^= x >> 4;
00026     x ^= x >> 2;
00027     x ^= x >> 1;
00028     x &= 1;
00029     return x;
00030 }
00031 
00032 /***********************************************************
00033 Initialize Read_Encoder
00034 ***********************************************************/
00035 //Taken from Init.cpp, used to be zero_ang_L, changed to zero_ang to accompany change to local variable
00036 //approximate point where the pilot should be standing straight
00037 
00038 void Encoder::init(float blah)
00039 {
00040     zero_ang=blah;
00041 }
00042 
00043 /***********************************************************
00044 Read_Encoder Program
00045 ***********************************************************/
00046 //Reads the encoder, does parity check, and stores the error flag.
00047 //@returns An unsigned integer which is the number of ticks away from the zero position.
00048 
00049 int Encoder::read()
00050 {
00051     // Select the device by seting chip select low
00052     _cs = 0;
00053 
00054     //send a dummy byte to the MOSI to get a reading through the MISO
00055     raw=_spi.write(0xFFFF);
00056 
00057     // Set the select back to low to allow the encoder to chill
00058     _cs=1;
00059 
00060     // XNOR parity check
00061     parity=~(parity_calc(raw) ^ (raw>>15));
00062 
00063     // Mask and bit shift to look at encoder flag value
00064     enc_flag=(raw & 0x4000)>>14;
00065 
00066     raw&=0x3FFF;
00067 
00068     return raw;
00069 }
00070 
00071 short int Encoder::readRaw(){
00072     return raw;
00073     }
00074 /***********************************************************
00075 Translate Function - Translates Ticks to Position
00076 ***********************************************************/
00077 // Translates the left encoder value from encoder ticks into degrees from the pilot's zero.
00078 // @param ticks The number of encoder ticks from the encoder zero to the current position.
00079 // @returns A float which is the degrees away from the pilot's zero position.
00080 
00081 
00082 float Encoder::angle()
00083 {
00084     // converts the result (encoder tick position) to degrees
00085     float pos = -sign*read()*enc2deg+zero_ang;
00086     return pos;
00087 }
00088 
00089 /***********************************************************
00090 Get parity from parityFlag
00091 ***********************************************************/
00092 //Error Flag.  The encoder has a built in hardware error flag.  This is used for a safety check.
00093 
00094 bool Encoder::parityFlag()
00095 {
00096     return parity;
00097 }
00098 
00099 /***********************************************************
00100 Get enc_flag from encFlag
00101 ***********************************************************/
00102 bool Encoder::encFlag()
00103 {
00104     return enc_flag;
00105 }
00106 
00107 void Encoder::flip()
00108 {
00109     sign=-sign;
00110     }