Renishaw / Mbed 2 deprecated QuadratureEncoder_X1

Dependencies:   SevenSegmentDisplay mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*******************************************************************************
00002 * Program to interface RenBed to a simple optical linear encoder               *
00003 * Copyright (c) 2016 Elijah Orr                                                *
00004 *                                                                              *
00005 * Permission is hereby granted, free of charge, to any person obtaining a copy *
00006 * of this software and associated documentation files (the "Software"), to deal*
00007 * in the Software without restriction, including without limitation the rights *
00008 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell    *
00009 * copies of the Software, and to permit persons to whom the Software is        *
00010 * furnished to do so, subject to the following conditions:                     *
00011 *                                                                              *
00012 * The above copyright notice and this permission notice shall be included in   *
00013 * all copies or substantial portions of the Software.                          *
00014 *                                                                              *
00015 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR   *
00016 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,     *
00017 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE  *
00018 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER       *
00019 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,*
00020 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN    *
00021 * THE SOFTWARE.                                                                *
00022 *                                                                              *
00023 * Encoder                                                                      *
00024 *                                                                              *
00025 * V1.0 08/09/2016 First issue of code                      Elijah Orr          *
00026 *******************************************************************************/
00027 
00028 #include "mbed.h"
00029 #include "SevenSegmentDisplay.h"
00030 
00031 /* define values for high and low analog signals, used to compare to ADC values. 0.56 indicates that the low level is set to 56% of 3.3V.
00032 * you will likely need to change these values for your particular encoder */
00033 #define SIGNAL_LOW 0.56
00034 #define SIGNAL_HIGH 0.60
00035 
00036 class Encoder{
00037 
00038 //public functions and variables can be accessed from anywhere
00039 public:
00040     
00041 /****************************************************************************************************************
00042 *   Encoder - class constructor, used to declare an instant of the Encoder class.                               *
00043 *                                                                                                               *
00044 *   Parameters: PinA - analog channel A, PinB - analog channel B, OutputA - digital output for channel A,       *
00045 *               OutputB - digital output for channel B, InputA - input for the feedback of digital channel A,   *
00046 *               InputB - input for the feedback of digital channel B                                            *
00047 *                                                                                                               *
00048 *   Returns: none                                                                                               *
00049 ****************************************************************************************************************/      
00050     Encoder(PinName pinA, PinName pinB, PinName OutputA, PinName OutputB, PinName InputA, PinName InputB) : 
00051         SignalA(pinA), SignalB(pinB), SquaredA(OutputA), SquaredB(OutputB), SquaredA_in(InputA), SquaredB_in(InputB){
00052         
00053         //attach ADC function to ticker, will be called every 75 microseconds (pretty much as often as possible)
00054         ProcessCycle.attach_us(this, &Encoder::ConvertSignals, 75);
00055         //attach rising_A function to rising edge interrupt on the feedback signal for digital channel A
00056         SquaredA_in.rise(this, &Encoder::rising_A);
00057         //attach falling_A function to falling edge interrupt on the feedback signal for digital channel B 
00058         SquaredA_in.fall(this, &Encoder::falling_A);
00059     }
00060     
00061 /*******************************************************************************
00062 *   get_count - returns the current encoder counter value
00063 *
00064 *   Parameters: none
00065 *
00066 *   Returns: int - current counter value
00067 *******************************************************************************/    
00068     int get_count(void){
00069         return count;
00070     }
00071 
00072 //private functions and variables are only accessable from within the encoder class        
00073 private:
00074     AnalogIn SignalA;           //analog input for signal A
00075     AnalogIn SignalB;           //analog input for signal B
00076     DigitalOut SquaredA;        //digital output for the digital translation of channel A
00077     DigitalOut SquaredB;        //digital output for the digital translation of channel B
00078     InterruptIn SquaredA_in;    //interrupt for the feedback of digital channel A signal 
00079     InterruptIn SquaredB_in;    //interrupt for the feedback of digital channel B signal
00080     Ticker ProcessCycle;        //ticker to attach signal converter function to
00081     
00082     int count;                  //variable to store counter value
00083     
00084 /*******************************************************************************
00085 *   ConvertSignals - converts the analog signals from the encoder into digital
00086 *                    quadrature signals.
00087 *
00088 *   Parameters: none
00089 *
00090 *   Returns: none
00091 *******************************************************************************/    
00092     void ConvertSignals(void){
00093         
00094         //take readings from the ADC
00095         float A_reading = SignalA;
00096         float B_reading = SignalB;
00097         
00098         //decision algorithm to decide when to set the digital signals high/low, 
00099         //SIGNAL_LOW and SIGNAL_HIGH are user defined high and low for the analog signals
00100         if(A_reading < SIGNAL_LOW){
00101             SquaredA = 0;
00102         }
00103         else if(A_reading > SIGNAL_HIGH){
00104             SquaredA = 1;
00105         }
00106         
00107         if(B_reading < SIGNAL_LOW){
00108             SquaredB = 0;
00109         }
00110         else if(B_reading > SIGNAL_HIGH){
00111             SquaredB = 1;
00112         }
00113     }
00114     
00115 /*******************************************************************************
00116 *   rising_A - interrupt function for rising edges on channel A, decides when 
00117 *              the count should be decremented.
00118 *
00119 *   Parameters: none
00120 *
00121 *   Returns: none
00122 *******************************************************************************/    
00123     
00124     void rising_A(void){
00125         if(SquaredB.read() == 0){
00126             count--;
00127         }
00128     }
00129 
00130 /*******************************************************************************
00131 *   falling_A - interrupt function for rising edges on channel A, decides when 
00132 *              the count should be incremented.
00133 *
00134 *   Parameters: none
00135 *
00136 *   Returns: none
00137 *******************************************************************************/
00138     
00139     void falling_A(void){
00140         if(SquaredB.read() == 0){
00141             count++;
00142         }
00143     }
00144         
00145 };     
00146 
00147 /* Create an instance of the class Encoder, which will be called lollipop (because of the lolly stick height control). 
00148 * Pins must be given in the order specified in the constructor, and analog inputs must be ADC pins */        
00149 Encoder lollipop(P0_15, P0_22, P0_17, P0_7, P1_14, P0_1);
00150 SevenSegmentDisplay display(INSTANT);                       //create instance of SevenSegmentDisplay to drive the 7 segs
00151 
00152 /*******************************************************************************
00153 *   main - this is the main program routine. The encoder class is working in the 
00154 *   background, as the constructor was called above.
00155 *
00156 *   Parameters: none
00157 *
00158 *   Returns: none
00159 *******************************************************************************/    
00160 int main(){
00161     //a while loop with the parameter 1 will always execute, and repeat forever 
00162     while(1){
00163         int counter = lollipop.get_count(); //get the current encoder count value
00164         
00165         // if the counter value is positive, seperate into individual digits and drive displays
00166         if(counter >= 0){
00167             display.FadeMode(INSTANT);
00168             int first_digit = counter/10;
00169             int second_digit = counter%10;
00170             display.DisplayDigits(first_digit, second_digit);
00171         }
00172         //if the counter value is negative, make the display flash while displaying value
00173         else if(counter < 0){
00174             display.FadeMode(FLASH);
00175             display.FlashRate(500);
00176             counter = counter*-1;
00177             int first_digit = counter/10;
00178             int second_digit = counter%10;
00179             display.DisplayDigits(first_digit, second_digit);
00180         }
00181         wait(0.2);
00182     }
00183 }
00184