From 050c1156f2211af993619d163c0dfc9e63a0b80f Mon Sep 17 00:00:00 2001 From: Stedd Date: Mon, 23 Dec 2019 14:13:37 +0100 Subject: [PATCH] Moved interrupt to separate function --- Main/Main.ino | 117 +-------------------------------- Main/interrupt.ino | 114 ++++++++++++++++++++++++++++++++ Main/{plotter.ino => plot.ino} | 0 3 files changed, 116 insertions(+), 115 deletions(-) create mode 100644 Main/interrupt.ino rename Main/{plotter.ino => plot.ino} (100%) diff --git a/Main/Main.ino b/Main/Main.ino index 5cc159b..da2ae1a 100644 --- a/Main/Main.ino +++ b/Main/Main.ino @@ -46,110 +46,6 @@ mtx_type vel_Matrix [2][1]; mtx_type inv_Kin [2][2]; -//Interrupt routines -void IRAM_ATTR m1_A_changed() { - M1_A_state = digitalRead(M1_ENC_A); - M1_B_state = digitalRead(M1_ENC_B); - - //Rising - if (M1_A_state == HIGH) { - if (M1_B_state == HIGH) { - m1Raw = m1Raw - 1; - } - else if (M1_B_state == LOW) { - m1Raw = m1Raw + 1; - } - } - - //Falling - else if (M1_A_state == LOW) { - if (M1_B_state == HIGH) { - m1Raw = m1Raw + 1; - } - else if (M1_B_state == LOW) { - m1Raw = m1Raw - 1; - } - } -} - - -void IRAM_ATTR m1_B_changed() { - M1_A_state = digitalRead(M1_ENC_A); - M1_B_state = digitalRead(M1_ENC_B); - - //Rising - if (M1_B_state == HIGH) { - if (M1_A_state == HIGH) { - m1Raw = m1Raw + 1; - } - else if (M1_A_state == LOW) { - m1Raw = m1Raw - 1; - } - } - - //Falling - else if (M1_B_state == LOW) { - if (M1_A_state == HIGH) { - m1Raw = m1Raw - 1; - } - else if (M1_A_state == LOW) { - m1Raw = m1Raw + 1; - } - } -} - -void IRAM_ATTR m2_A_changed() { - M2_A_state = digitalRead(M2_ENC_A); - M2_B_state = digitalRead(M2_ENC_B); - - //Rising - if (M2_A_state == HIGH) { - if (M2_B_state == HIGH) { - m2Raw = m2Raw + 1; - } - else if (M2_B_state == LOW) { - m2Raw = m2Raw - 1; - } - } - - //Falling - else if (M2_A_state == LOW) { - if (M2_B_state == HIGH) { - m2Raw = m2Raw - 1; - } - else if (M2_B_state == LOW) { - m2Raw = m2Raw + 1; - } - } -} - - -void IRAM_ATTR m2_B_changed() { - M2_A_state = digitalRead(M2_ENC_A); - M2_B_state = digitalRead(M2_ENC_B); - - //Rising - if (M2_B_state == HIGH) { - if (M2_A_state == HIGH) { - m2Raw = m2Raw - 1; - } - else if (M2_A_state == LOW) { - m2Raw = m2Raw + 1; - } - } - - //Falling - else if (M2_B_state == LOW) { - if (M2_A_state == HIGH) { - m2Raw = m2Raw + 1; - } - else if (M2_A_state == LOW) { - m2Raw = m2Raw - 1; - } - } -} - - void setup() { //Initialize serial Serial.begin(57600); @@ -166,14 +62,8 @@ void setup() { delay(10); //Initialize encoder interrupts - pinMode(M1_ENC_A, INPUT_PULLUP); - pinMode(M1_ENC_B, INPUT_PULLUP); - pinMode(M2_ENC_A, INPUT_PULLUP); - pinMode(M2_ENC_B, INPUT_PULLUP); - attachInterrupt(digitalPinToInterrupt(M1_ENC_A), m1_A_changed, CHANGE); - attachInterrupt(digitalPinToInterrupt(M1_ENC_B), m1_B_changed, CHANGE); - attachInterrupt(digitalPinToInterrupt(M2_ENC_A), m2_A_changed, CHANGE); - attachInterrupt(digitalPinToInterrupt(M2_ENC_B), m2_B_changed, CHANGE); + initInterrupt(); + //Initialize encoders m1Raw = 0; @@ -182,9 +72,6 @@ void setup() { m2RawLast = 100; // Initialize PWM channels - // channels 0-15, resolution 1-16 bits, freq limits depend on resolution - // ledcSetup(uint8_t channel, uint32_t freq, uint8_t resolution_bits); - ledcAttachPin(M1_A, 1); ledcAttachPin(M1_B, 2); ledcAttachPin(M2_A, 3); diff --git a/Main/interrupt.ino b/Main/interrupt.ino new file mode 100644 index 0000000..29ca3a7 --- /dev/null +++ b/Main/interrupt.ino @@ -0,0 +1,114 @@ +//Interrupt routines +void IRAM_ATTR m1_A_changed() { + M1_A_state = digitalRead(M1_ENC_A); + M1_B_state = digitalRead(M1_ENC_B); + + //Rising + if (M1_A_state == HIGH) { + if (M1_B_state == HIGH) { + m1Raw = m1Raw - 1; + } + else if (M1_B_state == LOW) { + m1Raw = m1Raw + 1; + } + } + + //Falling + else if (M1_A_state == LOW) { + if (M1_B_state == HIGH) { + m1Raw = m1Raw + 1; + } + else if (M1_B_state == LOW) { + m1Raw = m1Raw - 1; + } + } +} + + +void IRAM_ATTR m1_B_changed() { + M1_A_state = digitalRead(M1_ENC_A); + M1_B_state = digitalRead(M1_ENC_B); + + //Rising + if (M1_B_state == HIGH) { + if (M1_A_state == HIGH) { + m1Raw = m1Raw + 1; + } + else if (M1_A_state == LOW) { + m1Raw = m1Raw - 1; + } + } + + //Falling + else if (M1_B_state == LOW) { + if (M1_A_state == HIGH) { + m1Raw = m1Raw - 1; + } + else if (M1_A_state == LOW) { + m1Raw = m1Raw + 1; + } + } +} + +void IRAM_ATTR m2_A_changed() { + M2_A_state = digitalRead(M2_ENC_A); + M2_B_state = digitalRead(M2_ENC_B); + + //Rising + if (M2_A_state == HIGH) { + if (M2_B_state == HIGH) { + m2Raw = m2Raw + 1; + } + else if (M2_B_state == LOW) { + m2Raw = m2Raw - 1; + } + } + + //Falling + else if (M2_A_state == LOW) { + if (M2_B_state == HIGH) { + m2Raw = m2Raw - 1; + } + else if (M2_B_state == LOW) { + m2Raw = m2Raw + 1; + } + } +} + + +void IRAM_ATTR m2_B_changed() { + M2_A_state = digitalRead(M2_ENC_A); + M2_B_state = digitalRead(M2_ENC_B); + + //Rising + if (M2_B_state == HIGH) { + if (M2_A_state == HIGH) { + m2Raw = m2Raw - 1; + } + else if (M2_A_state == LOW) { + m2Raw = m2Raw + 1; + } + } + + //Falling + else if (M2_B_state == LOW) { + if (M2_A_state == HIGH) { + m2Raw = m2Raw + 1; + } + else if (M2_A_state == LOW) { + m2Raw = m2Raw - 1; + } + } +} + + +void initInterrupt(){ + pinMode(M1_ENC_A, INPUT_PULLUP); + pinMode(M1_ENC_B, INPUT_PULLUP); + pinMode(M2_ENC_A, INPUT_PULLUP); + pinMode(M2_ENC_B, INPUT_PULLUP); + attachInterrupt(digitalPinToInterrupt(M1_ENC_A), m1_A_changed, CHANGE); + attachInterrupt(digitalPinToInterrupt(M1_ENC_B), m1_B_changed, CHANGE); + attachInterrupt(digitalPinToInterrupt(M2_ENC_A), m2_A_changed, CHANGE); + attachInterrupt(digitalPinToInterrupt(M2_ENC_B), m2_B_changed, CHANGE); +} diff --git a/Main/plotter.ino b/Main/plot.ino similarity index 100% rename from Main/plotter.ino rename to Main/plot.ino