Rotork Research Team / Mbed 2 deprecated TFM_Encoder

Dependencies:   mbed QEI

Committer:
simontruelove
Date:
Wed Oct 24 15:01:11 2018 +0000
Revision:
1:0191658b6ff4
Parent:
0:634dd505dace
Child:
2:3f95c82c26bb
Encoder added plus data recording for position

Who changed what in which revision?

UserRevisionLine numberNew contents of line
simontruelove 0:634dd505dace 1 #include "mbed.h"
simontruelove 1:0191658b6ff4 2 void ReportA(void); //These voids are written after the main. They must be listed here too (functional prototypes).
simontruelove 1:0191658b6ff4 3 void ReportB(void);
simontruelove 1:0191658b6ff4 4 void ReportX(void);
simontruelove 1:0191658b6ff4 5 void Startup(void);
simontruelove 1:0191658b6ff4 6 void StepCW(void);
simontruelove 1:0191658b6ff4 7 void Ph1(void);
simontruelove 1:0191658b6ff4 8 void Ph2(void);
simontruelove 1:0191658b6ff4 9 void Ph3(void);
simontruelove 1:0191658b6ff4 10 void Ph4(void);
simontruelove 1:0191658b6ff4 11
simontruelove 1:0191658b6ff4 12 Serial pc(USBTX, USBRX); // tx, rx - set up the Terraterm input from mbed
simontruelove 1:0191658b6ff4 13
simontruelove 1:0191658b6ff4 14 DigitalOut Phase1 (p21); //Pin and LED set up
simontruelove 1:0191658b6ff4 15 DigitalOut Phase2 (p22);
simontruelove 1:0191658b6ff4 16 DigitalOut Phase3 (p23);
simontruelove 1:0191658b6ff4 17 DigitalOut Phase4 (p24);
simontruelove 1:0191658b6ff4 18
simontruelove 1:0191658b6ff4 19 InterruptIn ChannelA (p5);
simontruelove 1:0191658b6ff4 20 InterruptIn ChannelB (p6);
simontruelove 1:0191658b6ff4 21 InterruptIn Index (p8);
simontruelove 1:0191658b6ff4 22
simontruelove 1:0191658b6ff4 23 DigitalOut led1(LED1);
simontruelove 1:0191658b6ff4 24 DigitalOut led2(LED2);
simontruelove 1:0191658b6ff4 25 DigitalOut led3(LED3);
simontruelove 1:0191658b6ff4 26 DigitalOut led4(LED4);
simontruelove 1:0191658b6ff4 27
simontruelove 1:0191658b6ff4 28 int CountA=0; //On start up the integer will be zero
simontruelove 1:0191658b6ff4 29 int CountB=0;
simontruelove 1:0191658b6ff4 30 int CountX=0;
simontruelove 1:0191658b6ff4 31
simontruelove 1:0191658b6ff4 32 float x=0.04, y=0.01; //x=time of square wave when 1 phase energised, y=time of square wave when 2 phases energised
simontruelove 1:0191658b6ff4 33
simontruelove 1:0191658b6ff4 34 int main(void)
simontruelove 1:0191658b6ff4 35 {
simontruelove 1:0191658b6ff4 36 ChannelA.rise(&ReportA); // Interrupt - When channel A goes from 0 to 1 it is reported
simontruelove 1:0191658b6ff4 37 ChannelB.rise(&ReportB); // Interrupt - When channel B goes from 0 to 1 it is reported
simontruelove 1:0191658b6ff4 38 Index.rise(&ReportX); // Interrupt - When Index goes from 0 to 1 it is reported
simontruelove 1:0191658b6ff4 39 pc.baud(230400); //Set fastest baud rate
simontruelove 1:0191658b6ff4 40 Startup();
simontruelove 1:0191658b6ff4 41
simontruelove 1:0191658b6ff4 42 while(CountX==3)
simontruelove 1:0191658b6ff4 43 {
simontruelove 1:0191658b6ff4 44 wait(0.5);
simontruelove 1:0191658b6ff4 45 pc.printf("A = %d\n\r",CountA);
simontruelove 1:0191658b6ff4 46 pc.printf("B = %d\n\r",CountB);
simontruelove 1:0191658b6ff4 47 wait(0.5);
simontruelove 1:0191658b6ff4 48 Ph1();
simontruelove 1:0191658b6ff4 49 wait(0.5);
simontruelove 1:0191658b6ff4 50 pc.printf("A = %d\n\r",CountA);
simontruelove 1:0191658b6ff4 51 pc.printf("B = %d\n\r",CountB);
simontruelove 1:0191658b6ff4 52 wait(0.5);
simontruelove 1:0191658b6ff4 53 Ph2();
simontruelove 1:0191658b6ff4 54 wait(0.5);
simontruelove 1:0191658b6ff4 55 pc.printf("A = %d\n\r",CountA);
simontruelove 1:0191658b6ff4 56 pc.printf("B = %d\n\r",CountB);
simontruelove 1:0191658b6ff4 57 wait(0.5);
simontruelove 1:0191658b6ff4 58 Ph3();
simontruelove 1:0191658b6ff4 59 wait(0.5);
simontruelove 1:0191658b6ff4 60 pc.printf("A = %d\n\r",CountA);
simontruelove 1:0191658b6ff4 61 pc.printf("B = %d\n\r",CountB);
simontruelove 1:0191658b6ff4 62 wait(0.5);
simontruelove 1:0191658b6ff4 63 Ph4();
simontruelove 1:0191658b6ff4 64 }
simontruelove 1:0191658b6ff4 65 }
simontruelove 1:0191658b6ff4 66
simontruelove 0:634dd505dace 67
simontruelove 1:0191658b6ff4 68 void Startup(void)
simontruelove 1:0191658b6ff4 69 {
simontruelove 1:0191658b6ff4 70 while(CountX==0)
simontruelove 1:0191658b6ff4 71 {
simontruelove 1:0191658b6ff4 72 StepCW();
simontruelove 1:0191658b6ff4 73 }
simontruelove 1:0191658b6ff4 74 }
simontruelove 1:0191658b6ff4 75
simontruelove 1:0191658b6ff4 76
simontruelove 1:0191658b6ff4 77
simontruelove 1:0191658b6ff4 78
simontruelove 1:0191658b6ff4 79 void ReportA(void)
simontruelove 1:0191658b6ff4 80 {
simontruelove 1:0191658b6ff4 81 CountA++;
simontruelove 1:0191658b6ff4 82 led1 = !led1; //Counts A
simontruelove 1:0191658b6ff4 83 //pc.printf("A = %d\n\r",CountA); //Prints cumulative counts to Terraterm
simontruelove 1:0191658b6ff4 84 }
simontruelove 1:0191658b6ff4 85
simontruelove 1:0191658b6ff4 86
simontruelove 1:0191658b6ff4 87 void ReportB(void)
simontruelove 1:0191658b6ff4 88 {
simontruelove 1:0191658b6ff4 89 CountB++;
simontruelove 1:0191658b6ff4 90 led2 = !led2; //Counts B
simontruelove 1:0191658b6ff4 91 //pc.printf("B = %d\n\r",CountB); //Prints cumulative counts to Terraterm
simontruelove 0:634dd505dace 92
simontruelove 1:0191658b6ff4 93 }
simontruelove 1:0191658b6ff4 94
simontruelove 1:0191658b6ff4 95
simontruelove 1:0191658b6ff4 96 void ReportX(void)
simontruelove 1:0191658b6ff4 97 {
simontruelove 1:0191658b6ff4 98 CountX++;
simontruelove 1:0191658b6ff4 99 pc.printf("X = %d\n\r",CountX); //Prints cumulative counts to Terraterm
simontruelove 1:0191658b6ff4 100 led4 = !led4;
simontruelove 1:0191658b6ff4 101 CountA = CountB = 0;
simontruelove 1:0191658b6ff4 102 }
simontruelove 1:0191658b6ff4 103
simontruelove 1:0191658b6ff4 104 void StepCW(void) //Square wave switching
simontruelove 1:0191658b6ff4 105 {
simontruelove 1:0191658b6ff4 106 Ph1();
simontruelove 1:0191658b6ff4 107
simontruelove 1:0191658b6ff4 108 Phase1 = Phase2 = 1;
simontruelove 1:0191658b6ff4 109 Phase3 = Phase4 = 0;
simontruelove 1:0191658b6ff4 110 wait(y);
simontruelove 1:0191658b6ff4 111
simontruelove 1:0191658b6ff4 112 Ph2();
simontruelove 1:0191658b6ff4 113
simontruelove 1:0191658b6ff4 114 Phase2 = Phase3 = 1;
simontruelove 1:0191658b6ff4 115 Phase1 = Phase4 = 0;
simontruelove 1:0191658b6ff4 116 wait(y);
simontruelove 1:0191658b6ff4 117
simontruelove 1:0191658b6ff4 118 Ph3();
simontruelove 1:0191658b6ff4 119
simontruelove 1:0191658b6ff4 120 Phase3 = Phase4 = 1;
simontruelove 1:0191658b6ff4 121 Phase1 = Phase2 = 0;
simontruelove 1:0191658b6ff4 122 wait(y);
simontruelove 1:0191658b6ff4 123
simontruelove 1:0191658b6ff4 124 Ph4();
simontruelove 1:0191658b6ff4 125
simontruelove 1:0191658b6ff4 126 Phase4 = Phase1 = 1;
simontruelove 1:0191658b6ff4 127 Phase2 = Phase3 = 0;
simontruelove 1:0191658b6ff4 128 wait(y);
simontruelove 1:0191658b6ff4 129 }
simontruelove 1:0191658b6ff4 130
simontruelove 1:0191658b6ff4 131 void Ph1(void)
simontruelove 1:0191658b6ff4 132 {
simontruelove 1:0191658b6ff4 133 Phase1 = 1;
simontruelove 0:634dd505dace 134 Phase2 = Phase3 = Phase4 = 0;
simontruelove 0:634dd505dace 135 wait(x);
simontruelove 1:0191658b6ff4 136 }
simontruelove 1:0191658b6ff4 137
simontruelove 1:0191658b6ff4 138 void Ph2(void)
simontruelove 1:0191658b6ff4 139 {
simontruelove 1:0191658b6ff4 140 Phase2 = 1;
simontruelove 1:0191658b6ff4 141 Phase1 = Phase3 = Phase4 = 0;
simontruelove 1:0191658b6ff4 142 wait(x);
simontruelove 1:0191658b6ff4 143 }
simontruelove 0:634dd505dace 144
simontruelove 1:0191658b6ff4 145 void Ph3(void)
simontruelove 1:0191658b6ff4 146 {
simontruelove 1:0191658b6ff4 147 Phase3 = 1;
simontruelove 1:0191658b6ff4 148 Phase1 = Phase2 = Phase4 = 0;
simontruelove 1:0191658b6ff4 149 wait(x);
simontruelove 1:0191658b6ff4 150 }
simontruelove 1:0191658b6ff4 151
simontruelove 1:0191658b6ff4 152 void Ph4(void)
simontruelove 1:0191658b6ff4 153 {
simontruelove 1:0191658b6ff4 154 Phase4 = 1;
simontruelove 1:0191658b6ff4 155 Phase1 = Phase2 = Phase3 = 0;
simontruelove 1:0191658b6ff4 156 wait(x);
simontruelove 1:0191658b6ff4 157 }