From 60be9d87367b48fca513f505b556752f45ecba4b Mon Sep 17 00:00:00 2001 From: Alexander Richter Date: Sun, 23 Jul 2023 17:06:16 +0200 Subject: [PATCH] maybe this works... --- LinuxCNC_ArduinoConnector.ino | 57 +++++++++++++++++------------ arduino.py | 67 +++++++++++------------------------ 2 files changed, 56 insertions(+), 68 deletions(-) diff --git a/LinuxCNC_ArduinoConnector.ino b/LinuxCNC_ArduinoConnector.ino index cbf73aa..af3ceaa 100644 --- a/LinuxCNC_ArduinoConnector.ino +++ b/LinuxCNC_ArduinoConnector.ino @@ -128,12 +128,14 @@ Note that Analog Pin numbering is different to the Print on the PCB. #endif -//#define ROTENC +#define QUADENC //Support for Rotatary Encoders with Quadrature Output. Define Pins for A and B Signals for your encoders. Visit https://www.pjrc.com/teensy/td_libs_Encoder.html for further explanation. -#ifdef ROTENC +#ifdef QUADENC #include - const int RotEncs = 2; //how many Rotary Encoders do you want? + const int QuadEncs = 2; //how many Rotary Encoders do you want? + #define QUADENCS 2 + // Encoders have 2 signals, which must be connected to 2 pins. There are three options. //Best Performance: Both signals connect to interrupt pins. @@ -159,10 +161,11 @@ Encoder Encoder1(31,33); //A,B Pin //Encoder Encoder2(A,B); //Encoder Encoder3(A,B); //Encoder Encoder4(A,B); - const int RotEncSig[] = {1,1}; //define wich kind of Signal you want to generate. + const int QuadEncSig[] = {1,1}; //define wich kind of Signal you want to generate. //1= send up or down signal (typical use for selecting modes in hal) //2= send position signal (typical use for MPG wheel) - const int RotEncMp[] = {1,4}; //some Rotary encoders send multiple Electronical Impulses per mechanical pulse. How many Electrical impulses are send for each mechanical Latch? + const int QuadEncMp[] = {1,4}; //some Rotary encoders send multiple Electronical Impulses per mechanical pulse. How many Electrical impulses are send for each mechanical Latch? + #endif //#define JOYSTICK //Support of an Rotating Knob that was build in my Machine. It encodes 32 Positions with 5 Pins in Binary. This will generate 32 Pins in LinuxCNC Hal. @@ -319,9 +322,9 @@ const int debounceDelay = 50; #ifdef KEYPAD byte KeyState = 0; #endif -#ifdef ROTENC - long EncCount[RotEncs]; - long OldEncCount[RotEncs]; +#ifdef QUADENC + long EncCount[QuadEncs]; + long OldEncCount[QuadEncs]; #endif #ifdef JOYSTICK @@ -455,7 +458,7 @@ void loop() { readKeypad(); //read Keyboard & send data #endif -#ifdef ROTENC +#ifdef QUADENC readEncoders(); //read Encoders & send data #endif #ifdef JOYSTICK @@ -505,30 +508,40 @@ void readJoySticks() { void readEncoders(){ - if(RotEncs>=1){ - EncCount[0] = Encoder0.read()/RotEncMp[0]; + if(QuadEncs>=1){ + #if QUADENCS >= 1 + EncCount[0] = Encoder0.read()/QuadEncMp[0]; + #endif } - if(RotEncs>=2){ - EncCount[1] = Encoder1.read()/RotEncMp[1]; + if(QuadEncs>=2){ + #if QUADENCS >= 2 + EncCount[1] = Encoder1.read()/QuadEncMp[1]; + #endif } - if(RotEncs>=3){ - //EncCount[2] = Encoder2.read()/RotEncMp[2]; + if(QuadEncs>=3){ + #if QUADENCS >= 3 + EncCount[2] = Encoder2.read()/QuadEncMp[2]; + #endif } - if(RotEncs>=4){ - //EncCount[3] = Encoder3.read()/RotEncMp[3]; + if(QuadEncs>=4){ + #if QUADENCS >= 4 + EncCount[3] = Encoder3.read()/QuadEncMp[3]; + #endif } - if(RotEncs>=5){ - //EncCount[4] = Encoder4.read()/RotEncMp[4]; + if(QuadEncs>=5){ + #if QUADENCS >= 5 + EncCount[4] = Encoder4.read()/QuadEncMp[4]; + #endif } - for(int i=0; i 0: - for pins in range(JoySticks*2): - if (io == JoyStickPins[pins]): - c["Counter.{}".format(io)] = value - if (Debug):print("Counter.{}:{}".format(io,value)) - if QuadEncs > 0: - if QuadEncSig[io]== 1: - if value == 0: - c["CounterDown.{}".format(io)] = 1 - time.sleep(0.1) - c["CounterDown.{}".format(io)] = 0 - if value == 1: - c["CounterUp.{}".format(io)] = 1 - time.sleep(0.1) - c["CounterUp.{}".format(io)] = 0 - if QuadEncSig[io]== 2: - c["Counter.{}".format(io)] = value + firstcom = 1 - - # Check if the button is not already in the dictionary, and set its last update time to 0 + if JoySticks > 0: + for pins in range(JoySticks * 2): + if io == JoyStickPins[pins]: + c["Counter.{}".format(io)] = value + if Debug: + print("Counter.{}:{}".format(io, value)) + + if QuadEncs > 0: if io not in counter_last_update: counter_last_update[io] = 0 + if time.time() - counter_last_update[io] >= min_update_interval: + global counter_last_update + if QuadEncSig[io] == 1: + if value == 0: + c["CounterDown.{}".format(io)] = 1 + counter_last_update[io] = time.time() # Set the last update time + elif value == 1: + c["CounterUp.{}".format(io)] = 1 + counter_last_update[io] = time.time() # Set the last update time + elif QuadEncSig[io] == 2: + c["Counter.{}".format(io)] = value - if JoySticks > 0: - for pins in range(JoySticks*2): - if (io == JoyStickPins[pins]): - c["Counter.{}".format(io)] = value - if Debug: - print("Counter.{}:{}".format(io, value)) - - if QuadEncs > 0: - if QuadEncSig[io] == 1: - # Check if enough time has passed since the last update - if millis() - counter_last_update[io] >= counter_hold_interval: - counter_last_update[io] = millis() # Update the last update time for the button - if value == 0: - c["CounterDown.{}".format(io)] = 1 - counter_last_update[io] = millis() # Update the last update time for the button - if value == 1: - c["CounterUp.{}".format(io)] = 1 - counter_last_update[io] = millis() # Update the last update time for the button - elif QuadEncSig[io] == 2: - c["Counter.{}".format(io)] = value - - elif cmd == "R": - firstcom = 1 - handle_button_press(io, value) - elif cmd == 'E': arduino.write(b"E0:0\n") if (Debug):print("Sending E0:0 to establish contact")