Hello.
Testing four buttons triggering interrupts to flag variables results in uncontrollable behavior.
For example: Pressing multiple times button1 to signal PIN22 (interrupt) SOMETIMES triggers ANOTHER PIN too.
Same with other buttons. Pressing them should only trigger correspondending interrupts, but SOMETIMES it triggers other interrupts too.
Looking at the software it should be not possible, but is does do it in this way.
Maybe possible, that signal from button1(PIN22) is flooding backwards to PIN23?
Using: Arduino DUE, oDrive3.6
[EDIT: Changed picture]
[EDIT2: Changed code. Added "nointerrupts()" and "interrupts()"]
// // #include <SoftwareSerial.h>
#include <ODriveArduino.h>
// Printing with stream operator
template<class T> inline Print& operator <<(Print &obj, T arg) { obj.print(arg); return obj; }
template<> inline Print& operator <<(Print &obj, float arg) { obj.print(arg, 4); return obj; }
// ODrive object
ODriveArduino odrive(Serial1);
int buttonPin22 = 22, buttonPin23 = 23, buttonPin24 = 24, buttonPin25 = 25; // Interrupt 2
volatile int vel_m0 = 0, vel_m1 = 0;
volatile boolean runnow0 = false, stop0 = false, runnow1 = false, stop1 = false;
void buttonInterrupt22() {
runnow0 = true;
stop0 = false;
}
void buttonInterrupt23() {
stop0 = true;
runnow0 = false;
}
void buttonInterrupt24() {
runnow1 = true;
stop1 = false;
}
void buttonInterrupt25() {
stop1 = true;
runnow1 = false;
}
void setup() {
pinMode(buttonPin22, INPUT);
pinMode(buttonPin23, INPUT);
pinMode(buttonPin24, INPUT);
pinMode(buttonPin25, INPUT);
attachInterrupt(digitalPinToInterrupt(buttonPin22), buttonInterrupt22, RISING);
attachInterrupt(digitalPinToInterrupt(buttonPin23), buttonInterrupt23, RISING);
attachInterrupt(digitalPinToInterrupt(buttonPin24), buttonInterrupt24, RISING);
attachInterrupt(digitalPinToInterrupt(buttonPin25), buttonInterrupt25, RISING);
// ODrive uses 115200 baud
Serial.begin(115200);
while (!Serial) ; // wait for Arduino Serial Monitor to open
Serial1.begin(115200);
while (!Serial1) ; // wait for Arduino Serial Monitor to open
Serial1 << "w axis0.controller.config.vel_limit 50.0\n";
Serial1 << "w axis0.motor.config.current_lim 10.0\n";
Serial1 << "w axis1.controller.config.vel_limit 500.0\n";
Serial1 << "w axis1.motor.config.current_lim 10.0\n";
runnow0 = false;
stop0 = false;
runnow1 = false;
stop1 = false;
Serial.println("Ready!");
}
void loop() {
if (runnow0 == true) {
noInterrupts();
Serial1 << "w axis0" << ".requested_state " << "8" << '\n'; // AXIS_STATE_CLOSED_LOOP_CONTROL
vel_m0 = 20;
Serial1 << "v " << "0" << " " << vel_m0 << "\n";
printVar();
runnow0 = false;
interrupts();
}
if (stop0 == true){
noInterrupts();
Serial1 << "w axis0" << ".requested_state " << "1" << '\n'; // AXIS_STATE_IDLE
printVar();
stop0 = false;
interrupts();
}
if (runnow1 == true) {
noInterrupts();
Serial1 << "w axis1" << ".requested_state " << "8" << '\n'; // AXIS_STATE_CLOSED_LOOP_CONTROL
vel_m1 = 50;
Serial1 << "v " << "1" << " " << vel_m1 << "\n";
printVar();
runnow1 = false;
interrupts();
}
if (stop1 == true){
noInterrupts();
Serial1 << "w axis1" << ".requested_state " << "1" << '\n'; // AXIS_STATE_IDLE
printVar();
stop1 = false;
interrupts();
}
delay(100);
}
void printVar() {
Serial.print(runnow0);
Serial.print(" ");
Serial.print(stop0);
Serial.print(" ");
Serial.print(runnow1);
Serial.print(" ");
Serial.println(stop1);
}