Connection Surveilance working

added flashing LED if connection is lost.
- stops flashing once stuff is received again
- repaired analog and digital Pin reading & Sending
- changed Header
This commit is contained in:
Alexander Richter 2022-11-25 02:43:59 +01:00
parent f4aabdc99c
commit 036cd64a13
3 changed files with 242 additions and 162 deletions

View File

@ -1,24 +1,53 @@
/* /*
This Software is used as IO Expansion for LinuxCNC. Here i am using an Mega 2560. LinuxCNC_ArduinoConnector
It can use as many digital & analog Inputs, Outputs and PWM Outputs as your Arduino can handle. By Alexander Richter, info@theartoftinkering.com 2022
I also generate "virtual Pins" by using latching Potentiometers, which are connected to one analog Pin, but are read in Hal as individual Pins.
This Software is used as IO Expansion for LinuxCNC. Here i am using a Mega 2560.
The Send Protocol is <Signal><Pin Number>:<Pin State> It is NOT intended for timing and security relevant IO's. Don't use it for Emergency Stops or Endstop switches!
Inputs are encoded with Letter 'I' You can create as many digital & analog Inputs, Outputs and PWM Outputs as your Arduino can handle.
Keep alive Signal is send with Letter 'E' You can also generate "virtual Pins" by using latching Potentiometers, which are connected to one analog Pin, but are read in Hal as individual Pins.
Outputs are encoded with Letter 'O'
PWM Outputs are encoded with Letter 'P' Currently the Software provides:
Analog Inputs are encoded with Letter 'A' - analog Inputs
Latching Potentiometers are encoded with Letter 'L' - latching Potentiometers
Absolute Encoder input is encoded with Letter 'K' - 1 absolute encoder input
- digital Inputs
- digital Outputs
The Send and receive Protocol is <Signal><PinNumber>:<Pin State>
To begin Transmitting Ready is send out and expects to receive E: to establish connection. Afterwards Data is exchanged.
Data is only send everythime it changes once.
Inputs = 'I' -write only -Pin State: 0,1
Outputs = 'O' -read only -Pin State: 0,1
PWM Outputs = 'P' -read only -Pin State: 0-255
Analog Inputs = 'A' -write only -Pin State: 0-1024
Latching Potentiometers = 'L' -write only -Pin State: 0-max Position
Absolute Encoder input = 'K' -write only -Pin State: 0-32
Command 'E0:0' is used for connectivity checks and is send every 5 seconds as keep alive signal
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/ */
//###IO's### //###IO's###
#define INPUTS #define DINPUTS
#ifdef INPUTS #ifdef DINPUTS
const int Inputs = 16; //number of inputs using internal Pullup resistor. (short to ground to trigger) const int Inputs = 16; //number of inputs using internal Pullup resistor. (short to ground to trigger)
int InPinmap[] = {32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48}; int InPinmap[] = {32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48};
#endif #endif
@ -32,23 +61,25 @@ Absolute Encoder input is encoded with Letter 'K'
#define PWMOUTPUTS #define PWMOUTPUTS
#ifdef PWMOUTPUTS #ifdef PWMOUTPUTS
const int PwmOutput = 2; //number of outputs const int PwmOutputs = 2; //number of outputs
int PwmOutPinmap[] = {12,11}; int PwmOutPinmap[] = {13,11};
#endif #endif
#define AINPUTS #define AINPUTS
#ifdef AINPUTS #ifdef AINPUTS
const int AInputs = 1; const int AInputs = 1;
int AInPinmap[] = {A3}; //Potentiometer for SpindleSpeed override int AInPinmap[] = {A3}; //Potentiometer for SpindleSpeed override
int smooth = 200; //number of samples to denoise ADC, try lower numbers on your setup
#endif #endif
#define LPOTIS #define LPOTIS
#ifdef LPOTIS #ifdef LPOTIS
const int LPotis = 2; const int LPotis = 2;
int LPotiPins[LPotis][2] = { int LPotiPins[LPotis][2] = {
{A1,8}, //Latching Knob Spindle Overdrive on A1, has 9 Positions {96,8}, //Latching Knob Spindle Overdrive on A1, has 9 Positions
{A2,3} //Latching Knob Feed Resolution on A2, has 4 Positions {95,3} //Latching Knob Feed Resolution on A2, has 4 Positions
}; };
int margin = 20; //giving it some margin so Numbers dont jitter, make this number smaller if your knob has more than 50 Positions
#endif #endif
#define ABSENCODER #define ABSENCODER
@ -70,7 +101,7 @@ const int timeout = 10000; // timeout after 10 sec not receiving Stuff
#define DEBUG #define DEBUG
//Variables for Saving States //Variables for Saving States
#ifdef INPUTS #ifdef DINPUTS
int InState[Inputs]; int InState[Inputs];
int oldInState[Inputs]; int oldInState[Inputs];
#endif #endif
@ -78,10 +109,15 @@ const int timeout = 10000; // timeout after 10 sec not receiving Stuff
int OutState[Outputs]; int OutState[Outputs];
int oldOutState[Outputs]; int oldOutState[Outputs];
#endif #endif
#ifdef PWMOUTPUTS
int OutPWMState[PwmOutputs];
int oldOutPWMState[PwmOutputs];
#endif
#ifdef AINPUTS #ifdef AINPUTS
int oldAinput[AInputs]; int oldAinput[AInputs];
#endif #endif
#ifdef LPOTIS #ifdef LPOTIS
int Lpoti[LPotis];
int oldLpoti[LPotis]; int oldLpoti[LPotis];
#endif #endif
#ifdef ABSENCODER #ifdef ABSENCODER
@ -91,7 +127,7 @@ const int timeout = 10000; // timeout after 10 sec not receiving Stuff
//### global Variables setup### //### global Variables setup###
//Diese variablen nicht von außen anfassen //Please don't touch them
unsigned long oldmillis = 0; unsigned long oldmillis = 0;
unsigned long newcom = 0; unsigned long newcom = 0;
unsigned long lastcom = 0; unsigned long lastcom = 0;
@ -109,8 +145,39 @@ uint16_t io = 0;
uint16_t value = 0; uint16_t value = 0;
void setup() { void setup() {
#ifdef DINPUTS
//setting Inputs with internal Pullup Resistors
for(int i= 0; i<Inputs;i++){
pinMode(InPinmap[i], INPUT_PULLUP);
oldInState[i] = -1;
}
#endif
#ifdef AINPUTS
for(int i= 0; i<AInputs;i++){
pinMode(AInPinmap[i], INPUT);
oldAinput[i] = -1;
}
#endif
#ifdef OUTPUTS
for(int o= 0; o<Outputs;o++){
pinMode(OutPinmap[o], OUTPUT);
oldOutState[o] = 0;
}
#endif
#ifdef PWMOUTPUTS
for(int o= 0; o<PwmOutputs;o++){
pinMode(PwmOutPinmap[o], OUTPUT);
oldOutPWMState[o] = 0;
}
#endif
#ifdef STATUSLED
pinMode(StatLedPin, OUTPUT);
#endif
#ifdef ABSENCODER #ifdef ABSENCODER
pinMode(AbsEncPins[0], INPUT_PULLUP); pinMode(AbsEncPins[0], INPUT_PULLUP);
@ -120,74 +187,60 @@ void setup() {
pinMode(AbsEncPins[4], INPUT_PULLUP); pinMode(AbsEncPins[4], INPUT_PULLUP);
#endif #endif
#ifdef INPUTS
//setting Inputs with internal Pullup Resistors
for(int i= 0; i<Inputs;i++){
pinMode(InPinmap[i], INPUT_PULLUP);
oldInState[i] = -1;
}
#endif
#ifdef OUTPUTS
for(int o= 0; o<Outputs;o++){
pinMode(OutPinmap[o], OUTPUT);
oldOutState[o] = 0;
}
#endif
#ifdef STATUSLED
pinMode(StatLedPin, OUTPUT);
#endif
//Setup Serial //Setup Serial
Serial.begin(115200); Serial.begin(115200);
while (!Serial){ while (!Serial){}
#ifdef STATUSLED while (lastcom == 0){
StatLedErr(); readCommands();
#endif
}
if (Serial){
delay(1000);
flushSerial(); flushSerial();
Serial.println("Ready"); Serial.println("R:");
#ifdef STATUSLED
StatLedErr(1000,1000);
#endif
} }
} }
void loop() { void loop() {
while (!Serial){
#ifdef STATUSLED
StatLedErr();
#endif
}
readCommands(); readCommands(); //receive and execute Commands
#ifdef INPUTS comalive(); //if nothing is received for 10 sec. blink warning LED
// readInputs();
#ifdef DINPUTS
readInputs(); //read Inputs & send
#endif
#ifdef AINPUTS
readAInputs();
#endif
#ifdef LPOTIS
// readLPoti(); //read LPotis & send
#endif
#ifdef ABSENCODER
readAbsKnob(); //read ABS Encoder & send
#endif #endif
} }
void comalive(){ void comalive(){
if(millis() - lastcom > timeout){ if(millis() - lastcom > timeout){
StatLedErr(); StatLedErr(1000,10);
}
else{
digitalWrite(StatLedPin, HIGH);
} }
} }
void StatLedErr(){ void StatLedErr(int offtime, int ontime){
unsigned long newMillis = millis(); unsigned long newMillis = millis();
if (newMillis - oldmillis >= StatLedErrDel[0]){ if (newMillis - oldmillis >= offtime){
digitalWrite(StatLedPin, HIGH); digitalWrite(StatLedPin, HIGH);
} }
if (newMillis - oldmillis >= StatLedErrDel[0]+StatLedErrDel[1]){{ if (newMillis - oldmillis >= offtime+ontime){{
digitalWrite(StatLedPin, LOW); digitalWrite(StatLedPin, LOW);
oldmillis = newMillis; oldmillis = newMillis;
} }
@ -207,57 +260,55 @@ void flushSerial(){
Serial.read(); Serial.read();
} }
} }
/*
void readData(){
int pin = 0;
int state = 0;
byte terminated = false;
if (Serial.available() > 0) { void writeOutputs(int Pin, int Stat){
for(int x = 0; x<Outputs;x++){
char inChar = Serial.read(); if(OutPinmap[x]==Pin){
Serial.println(inChar); digitalWrite(OutPinmap[x], Stat);
if (inChar == 'o'){
Serial.println("O erkannt");
while (!terminated && comalive()){
inChar = Serial.read();
if (inChar == ':'){
} }
} }
}
if (inChar == 'p'){ void writePwmOutputs(int Pin, int Stat){
Serial.println("p erkannt"); for(int x = 0; x<PwmOutputs;x++){
sig = 'p'; if(PwmOutPinmap[x]==Pin){
analogWrite(PwmOutPinmap[x], Stat);
} }
} }
} }
}
*/
void writeOutputs(){
for(int x = 0; x<Outputs;x++){ int readLPoti(){
digitalWrite(OutPinmap[x], OutState[x]); for(int i= 0;i<LPotis; i++){
int State = analogRead(LPotiPins[i][0])+margin;
Lpoti[i] = 1024/LPotiPins[i][1];
State = State/LPotiPins[i][1];
if(oldLpoti[i]!= State){
oldLpoti[i] = State;
sendData('L', LPotiPins[i][0],oldLpoti[i]);
}
} }
} }
int readLPoti(int Pin,int Pos, int Stat){
int var = analogRead(Pin)+20; //giving it some margin so Numbers dont jitter
Pos = 1024/Pos;
var = var/Pos;
return (Stat);
}
int readAIn(int Pin){
int readAInputs(){
unsigned long var = 0; unsigned long var = 0;
for(int i= 0;i<500; i++){ for(int i= 0;i<AInputs; i++){
var = var+ analogRead(Pin); int State = analogRead(AInPinmap[i]);
} for(int i= 0;i<smooth; i++){// take couple samples to denoise signal
var = var / 500; var = var+ analogRead(AInPinmap[i]);
return (var); }
var = var / smooth;
if(oldAinput[i]!= var){
oldAinput[i] = var;
sendData('A',AInPinmap[i],oldAinput[i]);
}
}
} }
void readInputs(){ void readInputs(){
@ -267,45 +318,54 @@ void readInputs(){
InState[i] = State; InState[i] = State;
sendData('I',InPinmap[i],InState[i]); sendData('I',InPinmap[i],InState[i]);
} }
} }
} }
/*
int readAbsKnob(){ int readAbsKnob(){
int var = 0; int var = 0;
if(digitalRead(DI0)==1){ if(digitalRead(AbsEncPins[0])==1){
var += 1; var += 1;
} }
if(digitalRead(DI1)==1){ if(digitalRead(AbsEncPins[1])==1){
var += 2; var += 2;
} }
if(digitalRead(DI2)==1){ if(digitalRead(AbsEncPins[2])==1){
var += 4; var += 4;
} }
if(digitalRead(DI3)==1){ if(digitalRead(AbsEncPins[3])==1){
var += 8; var += 8;
} }
if(digitalRead(DI4)==1){ if(digitalRead(AbsEncPins[4])==1){
var += 16; var += 16;
} }
if(var != oldvar){ if(var != oldAbsEncState){
Serial.print("AK:"); Serial.print("K:");
Serial.println(var); Serial.println(var);
} }
oldvar = var; oldAbsEncState = var;
return (var); return (var);
} }
*/
void commandReceived(char cmd, uint16_t io, uint16_t value){ void commandReceived(char cmd, uint16_t io, uint16_t value){
switch(state){ if(cmd == 'O'){
case writeOutputs(io,value);
} Serial.print(cmd); }
Serial.print(io); if(cmd == 'P'){
Serial.print(":"); writePwmOutputs(io,value);
Serial.println(value); }
if(cmd == 'E'){
lastcom=millis();
}
#ifdef DEBUG
Serial.print("I Received= ");
Serial.print(cmd);
Serial.print(io);
Serial.print(":");
Serial.println(value);
#endif
} }
int isCmdChar(char cmd){ int isCmdChar(char cmd){
@ -334,22 +394,30 @@ void readCommands(){
state = STATE_VALUE; state = STATE_VALUE;
bufferIndex = 0; bufferIndex = 0;
}else{ }
else{
#ifdef DEBUG
Serial.print("Ungültiges zeichen: "); Serial.print("Ungültiges zeichen: ");
Serial.println(current); Serial.println(current);
#endif
} }
break; break;
case STATE_VALUE: case STATE_VALUE:
if(isDigit(current)){ if(isDigit(current)){
inputbuffer[bufferIndex++] = current; inputbuffer[bufferIndex++] = current;
}else if(current == '\n'){ }
else if(current == '\n'){
inputbuffer[bufferIndex] = 0; inputbuffer[bufferIndex] = 0;
value = atoi(inputbuffer); value = atoi(inputbuffer);
commandReceived(cmd, io, value); commandReceived(cmd, io, value);
state = STATE_CMD; state = STATE_CMD;
}else{ }
Serial.print("Ungültiges zeichen: "); else{
Serial.println(current); #ifdef DEBUG
Serial.print("Ungültiges zeichen: ");
Serial.println(current);
#endif
} }
break; break;
} }

View File

@ -15,8 +15,7 @@ This protocol is slow compared to other solutions, but easily adaptable and expa
- 1 absolute encoder input - 1 absolute encoder input
- digital Inputs - digital Inputs
- digital Outputs - digital Outputs
Right now i am also working on
- virtual Pins (multiplexed LED's or WS2812)
# Installation # Installation

View File

@ -1,37 +1,53 @@
#!/usr/bin/python3.9 #!/usr/bin/python3.9
import serial, time, hal import serial, time, hal
# LinuxCNC_ArduinoConnector # LinuxCNC_ArduinoConnector
# This Software is used to use an Arduino as IO Expansion. # By Alexander Richter, info@theartoftinkering.com 2022
# Note, these IO's are not run in the servo-thread. Therefor the IO's shouldn't be used for timing critical applications.
# Currently the Software provides: # This Software is used as IO Expansion for LinuxCNC. Here i am using a Mega 2560.
#
# - analog Inputs # It is NOT intended for timing and security relevant IO's. Don't use it for Emergency Stops or Endstop switches!
# - latching Potentiometers
# - 1 absolute encoder input # You can create as many digital & analog Inputs, Outputs and PWM Outputs as your Arduino can handle.
# - digital Inputs # You can also generate "virtual Pins" by using latching Potentiometers, which are connected to one analog Pin, but are read in Hal as individual Pins.
# - digital Outputs
# Right now i am also working on # Currently the Software provides:
# - virtual Pins (multiplexed LED's or WS2812) # - analog Inputs
# # - latching Potentiometers
# By Alexander Richter, info@theartoftinkering.com # - 1 absolute encoder input
# inspired by Jeff Epler, jepler@unpythonic.net # - digital Inputs
# # - digital Outputs
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by # The Send and receive Protocol is <Signal><PinNumber>:<Pin State>
# the Free Software Foundation; either version 2 of the License, or # To begin Transmitting Ready is send out and expects to receive E: to establish connection. Afterwards Data is exchanged.
# (at your option) any later version. # Data is only send everythime it changes once.
#
# This program is distributed in the hope that it will be useful, # Inputs = 'I' -write only -Pin State: 0,1
# but WITHOUT ANY WARRANTY; without even the implied warranty of # Outputs = 'O' -read only -Pin State: 0,1
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # PWM Outputs = 'P' -read only -Pin State: 0-255
# GNU General Public License for more details. # Analog Inputs = 'A' -write only -Pin State: 0-1024
# # Latching Potentiometers = 'L' -write only -Pin State: 0-max Position
# You should have received a copy of the GNU General Public License # Absolute Encoder input = 'K' -write only -Pin State: 0-32
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
# Command 'E0:0' is used for connectivity checks and is send every 5 seconds as keep alive signal
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
c = hal.component("arduino") #name that we will cal pins from in hal c = hal.component("arduino") #name that we will cal pins from in hal
connection = '/dev/ttyACM0'
# Set how many Inputs you have programmed in Arduino and which pins are Inputs # Set how many Inputs you have programmed in Arduino and which pins are Inputs
Inputs = 17 Inputs = 17
@ -60,6 +76,7 @@ AbsKnobPos = 30
######## End of Config! ######## ######## End of Config! ########
######## SetUp of HalPins ######## ######## SetUp of HalPins ########
# setup Input halpins # setup Input halpins
@ -118,7 +135,7 @@ def extract_nbr(input_str):
######## Detect Serial connection and blink Status LED if connection lost -todo ######## ######## Detect Serial connection and blink Status LED if connection lost -todo ########
#try: #try:
arduino = serial.Serial('/dev/ttyACM0', 9600, timeout=1, xonxoff=False, rtscts=False, dsrdtr=True) arduino = serial.Serial(connection, 9600, timeout=1, xonxoff=False, rtscts=False, dsrdtr=True)
while True: while True:
@ -139,18 +156,14 @@ while True:
if data[0] == "I": if data[0] == "I":
c.dIn = data[1] c.dIn = data[1]
elif data[0] == "aI": elif data[0] == "A":
c.aIn = data[1] c.aIn = data[1]
elif data[0] == "lP": elif data[0] == "L":
for port in range(LPotiLatches[latches]): for port in range(LPotiLatches[latches]):
if ("LPoti-%02d %" [port]) == data[1]: if ("LPoti-%02d %" [port]) == data[1]:
#s
else:
c.("LPoti-%02d %" [port]) = 0
c.LPotiKnob = data[1] c.LPotiKnob = data[1]
elif data[0] == "AK":
elif data[0] == "K":
c.AbsKnob = data[1] c.AbsKnob = data[1]
else: pass else: pass