Encoder program for K64F microcontroller with 64 bit confic

Dependencies:   MODSERIAL QEI mbed

Fork of Encoder_program by Robert van der Wal

Committer:
Wallie117
Date:
Wed Oct 07 11:49:14 2015 +0000
Revision:
3:dfed7f210afa
Parent:
2:3e6f179dfe7d
Authors change

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Wallie117 2:3e6f179dfe7d 1 //****************************************************************** Encoder Script ******************************************************************
Wallie117 2:3e6f179dfe7d 2 // In this script there will be looked at how we can calculate the degrees that a pulomo DC motor with a gearbox 1:131 kan make.
Wallie117 2:3e6f179dfe7d 3 // The usage of this script will be explained in Steps by
Wallie117 3:dfed7f210afa 4 // Author: Robert v/d Wal, Lisa Verhoeven, Thijs van der Wal, Ewoud Velu, Emily Zoetbrood
Wallie117 2:3e6f179dfe7d 5
Wallie117 2:3e6f179dfe7d 6 // ******************************************* Library DECLARATION **************************************
Wallie117 2:3e6f179dfe7d 7 // We need to add the standard "mbed.h" library for standard commands.
Wallie117 2:3e6f179dfe7d 8 // The second library is used to read the encoder on the DC motor. If you look at the library you see a command with QEI wheel. This needs information
Wallie117 2:3e6f179dfe7d 9 // of how many Inputs are measuring the Encoder(2 in this script other is NC). We want to read it with a known reading speed and that is 64 bit,
Wallie117 2:3e6f179dfe7d 10 // the last thing is if the encoder works with X1, X2 or X4 encoding.
Wallie117 0:9b63614c20f3 11 #include "mbed.h"
Wallie117 0:9b63614c20f3 12 #include "QEI.h"
Wallie117 0:9b63614c20f3 13 #include "MODSERIAL.h"
Wallie117 2:3e6f179dfe7d 14
Wallie117 2:3e6f179dfe7d 15 // ******************************************* FUNCTION DECLA *******************************************
Wallie117 0:9b63614c20f3 16 Serial pc(USBTX, USBRX);
Wallie117 0:9b63614c20f3 17 DigitalOut motor2direction(D4); //D4 en D5 zijn motor 2 (op het motorshield)
Wallie117 0:9b63614c20f3 18 PwmOut motor2speed(D5);
Wallie117 2:3e6f179dfe7d 19 QEI wheel (D11, D12, NC, 64,QEI::X4_ENCODING); //
Wallie117 2:3e6f179dfe7d 20
Wallie117 2:3e6f179dfe7d 21 //***************************** Variables DECLA *****************
Wallie117 2:3e6f179dfe7d 22
Wallie117 2:3e6f179dfe7d 23 bool flag_1 = false;
Wallie117 2:3e6f179dfe7d 24
Wallie117 2:3e6f179dfe7d 25 // ************************** TICKER DECLA ****************
Wallie117 1:b9fb9ec8a9e6 26 Ticker flipper;
Wallie117 2:3e6f179dfe7d 27
Wallie117 2:3e6f179dfe7d 28 //*****************************************************************************************************************************************************************
Wallie117 2:3e6f179dfe7d 29
Wallie117 2:3e6f179dfe7d 30
Wallie117 2:3e6f179dfe7d 31
Wallie117 2:3e6f179dfe7d 32 //******** SCRIPT **************
Wallie117 2:3e6f179dfe7d 33 void flip()
Wallie117 2:3e6f179dfe7d 34 {
Wallie117 2:3e6f179dfe7d 35 flag_1 = true;
Wallie117 1:b9fb9ec8a9e6 36 }
Wallie117 2:3e6f179dfe7d 37
Wallie117 2:3e6f179dfe7d 38
Wallie117 2:3e6f179dfe7d 39
Wallie117 2:3e6f179dfe7d 40 int main()
Wallie117 2:3e6f179dfe7d 41 {
Wallie117 2:3e6f179dfe7d 42
Wallie117 2:3e6f179dfe7d 43 flipper.attach(&flip, 0.01); // 100 HZ is the samplefrequency to read the encoder
Wallie117 2:3e6f179dfe7d 44 while(1) {
Wallie117 2:3e6f179dfe7d 45 motor2direction.write(0);
Wallie117 0:9b63614c20f3 46 motor2speed.write(0.2);
Wallie117 0:9b63614c20f3 47 wait(0.1);
Wallie117 2:3e6f179dfe7d 48 if(flag_1 == true)
Wallie117 2:3e6f179dfe7d 49 {
Wallie117 2:3e6f179dfe7d 50 pc.printf("Pulses is: %i\n", (wheel.getPulses()/((64*131)/360))); // The reading of the Encoder
Wallie117 2:3e6f179dfe7d 51 flag_1 = false;
Wallie117 2:3e6f179dfe7d 52 } // set if loop off
Wallie117 0:9b63614c20f3 53 }
Wallie117 2:3e6f179dfe7d 54
Wallie117 0:9b63614c20f3 55 }
Wallie117 2:3e6f179dfe7d 56
Wallie117 2:3e6f179dfe7d 57 // *****Explanation script******
Wallie117 2:3e6f179dfe7d 58 // You don't need to look at the Ticker or the motor settings but only on the command "wheel.getPulses()".
Wallie117 2:3e6f179dfe7d 59 // With this command you can read how many pulses are being read. To read it you can set a Ticker to constant read the value.
Wallie117 2:3e6f179dfe7d 60 // Warning!!! The encoder input gets in a formula to calculate the degrees the encoder is making in the "pc.printf" loop,
Wallie117 2:3e6f179dfe7d 61 // but if you want to use this formula in signal processing you get a problem. C++ cannot work with a Input and formula processing at
Wallie117 2:3e6f179dfe7d 62 // the same time so get first Input and then set this known value in a formula.