const float pi=3.14159265358979; // file name udpLCNCMEGA.ino edit for using pin 10 or not. I modded my ethernet W5100 to use pin 53 instead of pin10 // I can't find in the arduino source if that matters. // look below for // file udpioMEGA.hal edit for your IP numbers // file udpioMEGA.py edit for your IP numbers // file define.h for Arduino edit your IP numbers //#include #include // needed for Arduino versions later than 0018 #include #include // UDP library from: bjoern@cs.stanford.edu 12/30/2008 #include #include #include #include #include "defines.h" #define UDP_TX_PACKET_MAX_SIZE 1506 // Enter a MAC address and IP address for your controller below. // The IP address will be dependent on your local network: byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; IPAddress ip(ARDUINOIP); IPAddress remoteIp(COMPUTERIP); unsigned int localPort = IPPORT; // local port to listen on unsigned int remotePort = IPPORT; // buffers for receiving and sending data char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet, char ReplyBuffer[20];// = "acknowledged"; // a string to send back uint16_t adcresult; // An EthernetUDP instance to let us send and receive packets over UDP EthernetUDP Udp; byte adc=0; byte firstbyte=0x0; byte miscpins=0; // output data for MISCPORT pins // offsets in buffers #define PACKETDATA 2 #define REPLYDATA 2 #define USEPIN10 0 // have to figure out which end is lsb // output Pins // Bits 0 1 2 3 4 5 6 7 #define STEPPORTDATA PACKETDATA + 1 #define STEPPORT PORTA // 22x 23y 24z 25a 26b 27c 28w 29u #define STEPPORTBITS 8 const byte stepportArrray[STEPPORTBITS] = {22,23,24,25,26,27,28,29}; #define DIRPORTDATA PACKETDATA + 2 #define DIRPORT PORTC // 37x 36y 35z 34a 33b 32c 31w 30u const byte dirportArrray[STEPPORTBITS] = {37,36,35,34,33,32,31,30};// inverted to PortA #define XTRPORTDATA PACKETDATA + 3 #define XTRPORT PORTL // 49 48 47 46 45 44 43 42 const byte xtrportArrray[STEPPORTBITS] = {49,48,47,46,45,44,43,42};// inverted to PortA // using Analog pins as digitals // digital pins 54 55 56 57 58 59 60 61 #define HOMESPORT PORTF // A0 A1 A2 A3 A4 A5 A6 A7 #define HOMESPORTDATA REPLYDATA + 0 const byte homesportArrray[STEPPORTBITS] = {54,55,56,57,58,59,60,61}; // digital pins 62 63 64 65 66 67 68 69 #define LIMITSPORT PORTK // A8 A9 A10 A11 A12 A13 A14 A15 #define LIMITSPORTDATA REPLYDATA + 1 const byte limitsportArrray[STEPPORTBITS] = {62,63,64,65,66,67,68,69}; //#Find a use for these pins, something that needs speed or timing. #define PWMDATA XTRPORTDATA + 1 #if USEPIN10 == 1 //#Find a use for these pins, something that needs speed or timing. #define PWMCOUNT 10 #define PWMDATA4 PWMDATA + 0 #define PWMDATA5 PWMDATA + 1 #define PWMDATA6 PWMDATA + 2 #define PWMDATA7 PWMDATA + 3 #define PWMDATA8 PWMDATA + 4 #define PWMDATA9 PWMDATA + 5 #define PWMDATA10 PWMDATA + 6 #define PWMDATA11 PWMDATA + 7 #define PWMDATA12 PWMDATA + 8 #define PWMDATA13 PWMDATA + 9 const byte pwmpinmap[PWMCOUNT] = {4,5,6,7,8,9,10,11,12,13}; #define ENDOFPWM PWMDATA+10 #endif #if USEPIN10 == 0 #define PWMCOUNT 9 const byte pwmpinmap[PWMCOUNT] = {4,5,6,7,8,9,11,12,13}; //#Find a use for these pins, something that needs speed or timing. #define PWMDATA4 PWMDATA+0 #define PWMDATA5 PWMDATA+1 #define PWMDATA6 PWMDATA+2 #define PWMDATA7 PWMDATA+3 #define PWMDATA8 PWMDATA+4 #define PWMDATA9 PWMDATA+5 //#define PWMDATA10 PACKETDATA +10 #define PWMDATA11 PWMDATA+6 #define PWMDATA12 PWMDATA+7 #define PWMDATA13 PWMDATA+8 #define ENDOFPWM PWMDATA + 9 #endif //#define SERIAL0 //#define SERIAL1 #define SERIAL2 #define SERIAL3 #define SERIALCOUNT 0 #define SERIALDATA ENDOFPWM #ifdef SERIAL0 #define SERIALCOUNT 1 #define SERIALTX0DATA SERIALDATA + 0 #define SERIALTX0 Serial0out #define SERIALRX0 Serial0in #define ENDSERIAL SERIALTX0DATA + 1 #endif #ifdef SERIAL1 #define SERIALCOUNT SERIALCOUNT + 1 #define SERIALTX1DATA ENDSERIAL #define SERIALTX1 Serial1out #define SERIALRX1 Serial1in #define ENDSERIAL SERIALTX1DATA + 1 #endif #ifdef SERIAL2 #define SERIALCOUNT SERIALCOUNT + 1 #define SERIALTX2DATA ENDSERIAL #define SERIALTX2 Serial2out #define SERIALRX2 Serial2in #define ENDSERIAL SERIALTX2DATA + 1 #endif #ifdef SERIAL3 #define SERIALCOUNT SERIALCOUNT + 1 #define SERIALTX3DATA ENDSERIAL #define SERIALTX3 Serial3out #define SERIALRX3 Serial3in #define ENDSERIAL SERIALTX3DATA + 1 #endif #define MISCPORTDATA ENDSERIAL #define MISCPORT miscpins // 38, 39, 40 ,41 #define ISRDATA REPLYDATA + 2 // bits of InterruptPins volatile byte InterruptPins = 0; // input pins via interrupt; #define INTMAPCOUNT 6 //# Int 4 5 3 2 1 0 const byte intmap[INTMAPCOUNT] = {2, 3, 18,19,20,21}; #define PULLUP INPUT_PULLUP #define LIMITSPULL PULLUP #define HOMESPULL PULLUP void setup() { // start the Ethernet and UDP: Ethernet.begin(mac,ip); Udp.begin(localPort); Serial.begin(9600); for(int i=0; i< PWMCOUNT; i++) { pinMode(pwmpinmap[i] ,OUTPUT); // PWM } SetPWMdividers (); //Output pins for step and Dir for(int i=0; i < STEPPORTBITS; i++) { pinMode(stepportArrray[i],OUTPUT); pinMode(dirportArrray[i],OUTPUT); pinMode(xtrportArrray[i],OUTPUT); // this may be need to be edited if you have sensors (induction, optical, or hall effect) or switches pinMode(homesportArrray[i],HOMESPULL); pinMode(limitsportArrray[i],LIMITSPULL); } // Input pins using ISR Interrupts attachInterrupt(0,Int0Reader,CHANGE); // pin 21 attachInterrupt(1,Int1Reader,CHANGE); // pin 20 attachInterrupt(2,Int2Reader,CHANGE); // pin 19 attachInterrupt(3,Int3Reader,CHANGE); // pin 18 attachInterrupt(4,Int4Reader,CHANGE); // pin 2 attachInterrupt(5,Int5Reader,CHANGE); // pin 3 } // ISR's for reading pins void Int0Reader (void) { if (PORTD & B00000001) { InterruptPins = InterruptPins & B11111110; } else { InterruptPins = InterruptPins | B00000001; } } void Int1Reader (void){ if (PORTD & B00000010) { InterruptPins = InterruptPins & B11111101; } else { InterruptPins = InterruptPins | B00000010; } } void Int2Reader (void){ if (PORTD & B00000100) { InterruptPins = InterruptPins & B11111011; } else { InterruptPins = InterruptPins | B00000100; } } void Int3Reader (void){ if (PORTD & B00000001) { InterruptPins = InterruptPins & B11110111; } else { InterruptPins = InterruptPins | B00001000; } } void Int4Reader (void){ if (PORTE & B00010000) { InterruptPins = InterruptPins & B11101111; } else { InterruptPins = InterruptPins | B00010000; } } void Int5Reader (void){ if (PORTE & B00100000) { InterruptPins = InterruptPins & B11011111; } else { InterruptPins = InterruptPins | B00100000; } } char *skip_spaces(const char *str) { while (isspace(*str)) ++str; return (char *)str; } char rxbyte; char workbuf[100]; char rxtokens[5]; char *newtoken; const char s[2] = ","; char *token; // decodeRx is gonna completely change... // going to make it binary not text to int. // less over head faster processing. and add a CRC32 for error checking... void decodeRx(char* rxbuffer, int buflen) { if(rxbuffer[0] == 'D') { STEPPORT = atoi(rxbuffer[STEPPORTDATA]); DIRPORT = atoi(rxbuffer[DIRPORTDATA]); XTRPORT = atoi(rxbuffer[XTRPORTDATA]); for(int i=0; i< PWMCOUNT; i++) { analogWrite(pwmpinmap[i],atoi(rxbuffer[PWMDATA + i])); } return; } } void encodeTx() { Udp.beginPacket(remoteIp, remotePort); byte v = 0; //;byte PORTA, PORTC,PORTL; // byte PortBuffer[65]; ReplyBuffer[0]=1; ReplyBuffer[1]=char(HOMESPORT); ReplyBuffer[2]=char(LIMITSPORT); if (InterruptPins>0) ReplyBuffer[3]=char(InterruptPins); else InterruptPins=0; Udp.write(ReplyBuffer); Udp.endPacket(); } void loop() { encodeTx(); // if there's data available, read a packet int packetSize = Udp.parsePacket(); if(packetSize) { Udp.read(packetBuffer,UDP_TX_PACKET_MAX_SIZE); decodeRx(packetBuffer,packetSize); } /* long newPosition = myEnc.read(); if (newPosition != oldPosition) { counters[0] = oldPosition; counters[1] = newPosition; oldPosition = newPosition; //Serial.println(newPosition); } */ delay(1); } void SetPWMdividers () { //For Arduino Mega1280, Mega2560, MegaADK, Spider or any other board using ATmega1280 or ATmega2560** //---------------------------------------------- Set PWM frequency for D4 & D13 ------------------------------ //TCCR0B = TCCR0B & B11111000 | B00000001; // set timer 0 divisor to 1 for PWM frequency of 62500.00 Hz //TCCR0B = TCCR0B & B11111000 | B00000010; // set timer 0 divisor to 8 for PWM frequency of 7812.50 Hz TCCR0B = TCCR0B & B11111000 | B00000011; // set timer 0 divisor to 64 for PWM frequency of 976.56 Hz (Default) //TCCR0B = TCCR0B & B11111000 | B00000100; // set timer 0 divisor to 256 for PWM frequency of 244.14 Hz //TCCR0B = TCCR0B & B11111000 | B00000101; // set timer 0 divisor to 1024 for PWM frequency of 61.04 Hz //---------------------------------------------- Set PWM frequency for D11 & D12 ----------------------------- //TCCR1B = TCCR1B & B11111000 | B00000001; // set timer 1 divisor to 1 for PWM frequency of 31372.55 Hz //TCCR1B = TCCR1B & B11111000 | B00000010; // set timer 1 divisor to 8 for PWM frequency of 3921.16 Hz TCCR1B = TCCR1B & B11111000 | B00000011; // set timer 1 divisor to 64 for PWM frequency of 490.20 Hz //TCCR1B = TCCR1B & B11111000 | B00000100; // set timer 1 divisor to 256 for PWM frequency of 122.55 Hz //TCCR1B = TCCR1B & B11111000 | B00000101; // set timer 1 divisor to 1024 for PWM frequency of 30.64 Hz //---------------------------------------------- Set PWM frequency for D9 & D10 ------------------------------ //TCCR2B = TCCR2B & B11111000 | B00000001; // set timer 2 divisor to 1 for PWM frequency of 31372.55 Hz //TCCR2B = TCCR2B & B11111000 | B00000010; // set timer 2 divisor to 8 for PWM frequency of 3921.16 Hz TCCR2B = TCCR2B & B11111000 | B00000011; // set timer 2 divisor to 32 for PWM frequency of 980.39 Hz //TCCR2B = TCCR2B & B11111000 | B00000100; // set timer 2 divisor to 64 for PWM frequency of 490.20 Hz //TCCR2B = TCCR2B & B11111000 | B00000101; // set timer 2 divisor to 128 for PWM frequency of 245.10 Hz //TCCR2B = TCCR2B & B11111000 | B00000110; // set timer 2 divisor to 256 for PWM frequency of 122.55 Hz //TCCR2B = TCCR2B & B11111000 | B00000111; // set timer 2 divisor to 1024 for PWM frequency of 30.64 Hz //---------------------------------------------- Set PWM frequency for D2, D3 & D5 --------------------------- //TCCR3B = TCCR3B & B11111000 | B00000001; // set timer 3 divisor to 1 for PWM frequency of 31372.55 Hz //TCCR3B = TCCR3B & B11111000 | B00000010; // set timer 3 divisor to 8 for PWM frequency of 3921.16 Hz TCCR3B = TCCR3B & B11111000 | B00000011; // set timer 3 divisor to 64 for PWM frequency of 490.20 Hz //TCCR3B = TCCR3B & B11111000 | B00000100; // set timer 3 divisor to 256 for PWM frequency of 122.55 Hz //TCCR3B = TCCR3B & B11111000 | B00000101; // set timer 3 divisor to 1024 for PWM frequency of 30.64 Hz //---------------------------------------------- Set PWM frequency for D6, D7 & D8 --------------------------- //TCCR4B = TCCR4B & B11111000 | B00000001; // set timer 4 divisor to 1 for PWM frequency of 31372.55 Hz //TCCR4B = TCCR4B & B11111000 | B00000010; // set timer 4 divisor to 8 for PWM frequency of 3921.16 Hz TCCR4B = TCCR4B & B11111000 | B00000011; // set timer 4 divisor to 64 for PWM frequency of 490.20 Hz //TCCR4B = TCCR4B & B11111000 | B00000100; // set timer 4 divisor to 256 for PWM frequency of 122.55 Hz //TCCR4B = TCCR4B & B11111000 | B00000101; // set timer 4 divisor to 1024 for PWM frequency of 30.64 Hz //---------------------------------------------- Set PWM frequency for D44, D45 & D46 ------------------------ //TCCR5B = TCCR5B & B11111000 | B00000001; // set timer 5 divisor to 1 for PWM frequency of 31372.55 Hz //TCCR5B = TCCR5B & B11111000 | B00000010; // set timer 5 divisor to 8 for PWM frequency of 3921.16 Hz TCCR5B = TCCR5B & B11111000 | B00000011; // set timer 5 divisor to 64 for PWM frequency of 490.20 Hz //TCCR5B = TCCR5B & B11111000 | B00000100; // set timer 5 divisor to 256 for PWM frequency of 122.55 Hz //TCCR5B = TCCR5B & B11111000 | B00000101; // set timer 5 divisor to 1024 for PWM frequency of 30.64 Hz } /* #udpioMEGA.hal # Include your customized HAL commands here # The commands in this file are run after the AXIS GUI (including PyVCP panel) starts loadusr -W ~/bin/udpioMEGA 192.168.16.10 8888 192.168.16.200 8888 1 sets spindle-at-speed true # Comment Outputs, The Inputs need the pullup net test-bit udpioMEGA.digital-out-04 motion.in-position setp udpioMEGA.digital-in-02-pullup 1# INT4 setp udpioMEGA.digital-in-03-pullup 1# INT5 #setp udpioMEGA.digital-in-04-pullup 1# PWM #setp udpioMEGA.digital-in-05-pullup 1# PWM #setp udpioMEGA.digital-in-06-pullup 1# PWM #setp udpioMEGA.digital-in-07-pullup 1# PWM #setp udpioMEGA.digital-in-08-pullup 1# PWM #setp udpioMEGA.digital-in-09-pullup 1# PWM #setp udpioMEGA.digital-in-10-pullup 1# PWM ethernet Shield select #setp udpioMEGA.digital-in-11-pullup 1# PWM ethernet Shield uno# PWM on Mega2560 #setp udpioMEGA.digital-in-12-pullup 1# PWM ethernet Shield uno# PWM on Mega2560 #setp udpioMEGA.digital-in-13-pullup 1# PWM ethernet Shield uno# PWM on Mega2560 #setp udpioMEGA.digital-in-14-pullup 1# serial 3 TX setp udpioMEGA.digital-in-15-pullup 1# serial 3 RX #setp udpioMEGA.digital-in-16-pullup 1# serial 2 TX setp udpioMEGA.digital-in-17-pullup 1# serial 2 RX setp udpioMEGA.digital-in-18-pullup 1# serial 1 TX INT3 setp udpioMEGA.digital-in-19-pullup 1# serial 1 RX INT2 setp udpioMEGA.digital-in-20-pullup 1# TWI SDA INT1 setp udpioMEGA.digital-in-21-pullup 1# TWI SCL INT0 #setp udpioMEGA.digital-in-22-pullup 1# X Step Port A bit 0 #setp udpioMEGA.digital-in-23-pullup 1# Y Step #setp udpioMEGA.digital-in-24-pullup 1# Z Step #setp udpioMEGA.digital-in-25-pullup 1# A Step #setp udpioMEGA.digital-in-26-pullup 1# B Step #setp udpioMEGA.digital-in-27-pullup 1# C Step #setp udpioMEGA.digital-in-28-pullup 1# W Step #setp udpioMEGA.digital-in-29-pullup 1# U Step Port A Bit 7 #setp udpioMEGA.digital-in-30-pullup 1# U Dir Port C Bit 7 #setp udpioMEGA.digital-in-31-pullup 1# W Dir #setp udpioMEGA.digital-in-32-pullup 1# C Dir #setp udpioMEGA.digital-in-33-pullup 1# B Dir #setp udpioMEGA.digital-in-34-pullup 1# A Dir #setp udpioMEGA.digital-in-35-pullup 1# Z Dir #setp udpioMEGA.digital-in-36-pullup 1# Y Dir #setp udpioMEGA.digital-in-37-pullup 1# X Dir Port C bit 0 #setp udpioMEGA.digital-in-38-pullup 1 #setp udpioMEGA.digital-in-39-pullup 1 #setp udpioMEGA.digital-in-40-pullup 1 #setp udpioMEGA.digital-in-41-pullup 1 #setp udpioMEGA.digital-in-42-pullup 1# PORTL #setp udpioMEGA.digital-in-43-pullup 1# PORTL #setp udpioMEGA.digital-in-44-pullup 1# PWM# PORTL #setp udpioMEGA.digital-in-45-pullup 1# PWM# PORTL #setp udpioMEGA.digital-in-46-pullup 1# PWM# PORTL #setp udpioMEGA.digital-in-47-pullup 1# PORTL #setp udpioMEGA.digital-in-48-pullup 1# PORTL #setp udpioMEGA.digital-in-49-pullup 1# PORTL setp udpioMEGA.digital-in-50-pullup 1# SPI MISO setp udpioMEGA.digital-in-51-pullup 1# SPI MOSI setp udpioMEGA.digital-in-52-pullup 1# SPI SCK setp udpioMEGA.digital-in-53-pullup 1# SPI SS setp udpioMEGA.digital-in-54-pullup 1# SPI SS setp udpioMEGA.digital-in-55-pullup 1# SPI SS setp udpioMEGA.digital-in-56-pullup 1# SPI SS setp udpioMEGA.digital-in-57-pullup 1# SPI SS setp udpioMEGA.digital-in-58-pullup 1# SPI SS setp udpioMEGA.digital-in-59-pullup 1# SPI SS setp udpioMEGA.digital-in-60-pullup 1# SPI SS setp udpioMEGA.digital-in-61-pullup 1# SPI SS setp udpioMEGA.digital-in-62-pullup 1# SPI SS setp udpioMEGA.digital-in-63-pullup 1# SPI SS setp udpioMEGA.digital-in-64-pullup 1# SPI SS setp udpioMEGA.digital-in-65-pullup 1# SPI SS setp udpioMEGA.digital-in-66-pullup 1# SPI SS setp udpioMEGA.digital-in-67-pullup 1# SPI SS setp udpioMEGA.digital-in-68-pullup 1# SPI SS setp udpioMEGA.digital-in-69-pullup 1# SPI SS #ethernet shield UNO use pins 10,11,12,13 # on Mega 2560 10,50,51,52,53 #setp udpioMEGA.analog-out-03-scale 10# PWM Interrupt 5 setp udpioMEGA.analog-out-04-scale 10# PWM Spindle setp udpioMEGA.analog-out-05-scale 10# PWM Spinde setp udpioMEGA.analog-out-06-scale 10# PWM setp udpioMEGA.analog-out-07-scale 10# PWM setp udpioMEGA.analog-out-08-scale 10# PWM setp udpioMEGA.analog-out-09-scale 10# PWM setp udpioMEGA.analog-out-10-scale 10# PWM Ethernet Select on Uno and Mega setp udpioMEGA.analog-out-11-scale 10# PWM setp udpioMEGA.analog-out-12-scale 10# PWM setp udpioMEGA.analog-out-13-scale 10# PWM #setp udpioMEGA.analog-out-44-scale 10# PWM# PORTL Better off using as digital #setp udpioMEGA.analog-out-45-scale 10# PWM# PORTL " #setp udpioMEGA.analog-out-46-scale 10# PWM# PORTL " setp udpioMEGA.digital-out-22 1# X Step setp udpioMEGA.digital-out-37 1# X Dir setp udpioMEGA.digital-out-23 1# Y Step setp udpioMEGA.digital-out-36 1# Y Dir setp udpioMEGA.digital-out-24 1# Z Step setp udpioMEGA.digital-out-35 1# Z Dir setp udpioMEGA.digital-out-25 1# A Step setp udpioMEGA.digital-out-34 1# A Dir setp udpioMEGA.digital-out-26 1# B Step setp udpioMEGA.digital-out-33 1# B Dir setp udpioMEGA.digital-out-27 1# C Step setp udpioMEGA.digital-out-32 1# C Dir setp udpioMEGA.digital-out-28 1# W Step setp udpioMEGA.digital-out-31 1# W Dir setp udpioMEGA.digital-out-29 1# U Step setp udpioMEGA.digital-out-30 1# U Dir setp udpioMEGA.digital-out-31 1# U Dir setp udpioMEGA.digital-out-32 1# U Dir setp udpioMEGA.digital-out-33 1# U Dir setp udpioMEGA.digital-out-34 1# U Dir setp udpioMEGA.digital-out-35 1# U Dir setp udpioMEGA.digital-out-36 1# U Dir setp udpioMEGA.digital-out-37 1# U Dir # PORTL like PORTA and PORTC all 8 bits are digital out pins. # This could be 4 more step ports or other combinations. setp udpioMEGA.digital-out-42 1# PORTL Bit 7# V Dir setp udpioMEGA.digital-out-43 1# PORTL Bit 6# 10th Dir setp udpioMEGA.digital-out-44 1# PORTL Bit 5 Brake? setp udpioMEGA.digital-out-45 1# PORTL Bit 4 Brake on another axis? setp udpioMEGA.digital-out-46 1# PORTL Bit 3# V Step setp udpioMEGA.digital-out-47 1# PORTL Bit 2# 10th Step setp udpioMEGA.digital-out-48 1# PORTL Bit 1 Enable? setp udpioMEGA.digital-out-49 1# PORTL Bit 0 Power relay for resetting drivers after alarm? net spindle-cmd => udpioMEGA.analog-out-09# PWM Spindle #net spindle-cmd => udpioMEGA.analog-out-10# PWM Spindle# XX==XX Add code for 16 bit resolution of spindle speed. */ /* #!/usr/bin/python #udpioMEGA.py # HAL userspace component to interface with Arduino board # Copyright (C) 2015 Marius Liebenberg # Mods to use Mega2560 Robi Akerley-mcKee # # This program and accompanied works are provided as is and the # author will not be liable for anything whatsoever related to # this software or not. # # This user space program will allow you to connect to an Arduino # UNO, or the likes of, that is fitted with a network shield. I am # other boards will work as well with a bit of adaptation to the embedded # code. # # The arduino configuration is as follows: # O Analog Outputs - 1 at pin 3 # B Analog Inputs - 6 at pins A0 to A5 # S Digtal pins: - 6 in total configured as In or OUT at startup # O Encoder - pins 8 and 9 are used for the MPG encoder. Make # L sure the pins are set as inputs. That means you # E cannot have six outputs if you use the encoder. # T # E # # Analog Inputs - 16 at pins A0-15 # Analog Outputs PWM - 9 at pins 4,5,6,7,8,9, 11,12,13 # Interrupt Inputs - 6 at pins 2,3,18,19,20,21 # Polled Pins - 2 at pins 17, 17 # BitBanged using # PORT? - 24 at PORTA pins 22-29, PORTC pins 30-37, PORTL pins 42-49 # SerialPorts - 4 (3 unique) Serial0 pins 0,1 is also USB, Serial1,2,3 is pins 14,15,16,17,18,19 # # If you want encoder, you'll have to add it back in, use the interrupt pins though # # # Note: The mpg-count-00 is the previous count and mpg-count-01 is the new count # # Usage: loadusr -W /yourdir/udps NIC_IP NIC_PORT REMOTE_IP REMOTE_PORT number_of_ouputs # loadusr -W ~/bin/udps 192.168.3.10 8888 192.168.3.200 8888 1 # # I have used this on a second network interface as well as a USB plugin ethernet # adapter hence the need to provide the local IP details. # # Use the Arduino code udpcomms.ino in conjunction with this program. # # # 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 #import serial import os import hal import sys import time import socket from gwibber.microblog.can import RECEIVE from _socket import SOL_SOCKET from IN import SO_BINDTODEVICE data = " " REMOTE, R_PORT = "192.168.3.199", 8888 if len(sys.argv) > 4: HOST = sys.argv[1] H_PORT = int(sys.argv[2]) REMOTE = sys.argv[3] R_PORT = int(sys.argv[4]) print ("HOST = ", HOST) print ("PORT = ", H_PORT) print ("REMOTE = ", REMOTE) print ("PORT = ", R_PORT) #// this changes cuz were hard coded now #//if len(sys.argv) > 4: #// nout = int(sys.argv[5]) #//else: #// nout = 6 #//print ("Number of digital outputs = ", nout) #//if nout > 6 or nout < 0: #// print "##########################################################################################" #// print "Usage: loadusr -W /yourdir/udpsMEGA NIC_IP NIC_PORT REMOTE_IP REMOTE_PORT " #// print "##########################################################################################" #// raise SystemExit, "Number of digital outputs must be from 0 to 6" #//pinmap = [4,5,6,7,8,9]# Encoder can be connected to 8 and 9 pwmrange = 10 pwmpinmap = [4,5,6,7,8,9,10,11,12,13]# DAC fixed #pwmrange = 9 #pwmpinmap = [4,5,6,7,8,9,11,12,13]# DAC fixed pinmap = [22,23,24,25,26,27,28,29] pinmap2 = [37,36,35,34,33,32,31,30] pinmap3 = [49,48,47,46,45,44,43,42] inpmap = [54,55,56,57,58,59,60,61] inpmap2 = [62,63,64,65,66,67,68,69] inpmap3 = [21,20,19,18,2,3,1,0] c = hal.component("udpioMEGA") #old code for the one pwm pin #c.newpin("analog-out-%02d" % pwmpinmap[0], hal.HAL_FLOAT, hal.HAL_IN) #c.newparam("analog-out-%02d-offset" % pwmpinmap[0], hal.HAL_FLOAT, hal.HAL_RW) #c.newparam("analog-out-%02d-scale" % pwmpinmap[0], hal.HAL_FLOAT, hal.HAL_RW) #c['analog-out-%02d-scale' % pwmpinmap[0]] = 1.0 #new code for the 9 or 10 pwm pins if pin 10 is used for ethernet for port in range(pwmrange): c.newpin("analog-out-%02d" % pwmpinmap[port], hal.HAL_FLOAT, hal.HAL_IN) c.newparam("analog-out-%02d-offset" % pwmpinmap[port], hal.HAL_FLOAT, hal.HAL_RW) c.newparam("analog-out-%02d-scale" % pwmpinmap[port], hal.HAL_FLOAT, hal.HAL_RW) c['analog-out-%02d-scale' % pwmpinmap[port]] = 1.0 # c.newpin("analog-in-%02d" % port, hal.HAL_FLOAT, hal.HAL_OUT) # c.newparam("analog-in-%02d-offset" % port, hal.HAL_FLOAT, hal.HAL_RW) # c.newparam("analog-in-%02d-gain" % port, hal.HAL_FLOAT, hal.HAL_RW) # # c['analog-in-%02d-gain' % port] = 1.0 for port in range(8): c.newpin("digital-out-%02d" % pinmap[port], hal.HAL_BIT, hal.HAL_OUT) c.newparam("digital-out-%02d-invert" % pinmap[port], hal.HAL_BIT, hal.HAL_RW) c.newpin("digital-out-%02d" % pinmap2[port], hal.HAL_BIT, hal.HAL_OUT) c.newparam("digital-out-%02d-invert" % pinmap2[port], hal.HAL_BIT, hal.HAL_RW) c.newpin("digital-out-%02d" % pinmap3[port], hal.HAL_BIT, hal.HAL_OUT) c.newparam("digital-out-%02d-invert" % pinmap3[port], hal.HAL_BIT, hal.HAL_RW) c.newpin("digital-in-%02d" % inpmap[port], hal.HAL_BIT, hal.HAL_OUT) c.newpin("digital-in-%02d-not" % inpmap[port], hal.HAL_BIT, hal.HAL_OUT) c.newparam("digital-in-%02d-pullup" % inpmap[port], hal.HAL_BIT, hal.HAL_RW) c.newpin("digital-in-%02d" % inpmap2[port], hal.HAL_BIT, hal.HAL_OUT) c.newpin("digital-in-%02d-not" % inpmap2[port], hal.HAL_BIT, hal.HAL_OUT) c.newparam("digital-in-%02d-pullup" % inpmap2[port], hal.HAL_BIT, hal.HAL_RW) c.newpin("network-error", hal.HAL_BIT, hal.HAL_IN) #for i in range(2): # c.newpin("mpg-count-%02d" % i, hal.HAL_FLOAT, hal.HAL_IN) c.ready() data = " ".join(sys.argv[3:]) firstbyte = 0 state = 0 txbuf = range(6) rxbyte = 0 analogout = range(10) analogin = range(8) dout = 0 invert = 0 din = 0 pullup = 0 v = 0 count = 0 soh = "#" service = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) service.bind((HOST, H_PORT)) service.settimeout(1) print "listening on port", R_PORT ############################################################### # RECEIVE # ############################################################### def doRx(): # reading from Arduino Mega2560 try: received = service.recv(150) except IOError: c['network-error'] = 1 else: c['network-error'] = 0 workbuf = (received) # change following to strip bits and put to proper pin # pins 30-37,42-49 are inverted bit wise # tokens = [int(x) for x in workbuf.split(',') if x.strip()] # for i in range(8): # gain = c['analog-in-%02d-gain' % i+22] # offset = c['analog-in-%02d-offset' % i=22] # value = ((int(workbuf[i+]) & (1 >> i))<> (7-i)))<<(7-i)) * 5.0 * gain + offset # c['analog-in-%02d' % i] = value # dig = int(tokens[6]) digh=workbuf[0] digl=workbuf[1] digi=workbuf[2] for i in range(8): msk = (1 << i) v = (digh & msk) if(v > 0): c['digital-in-%02d' % inpmap[i]] = 1 c['digital-in-%02d-not' % inpmap[i]] = 0 else: c['digital-in-%02d' % inpmap[i]] = 0 c['digital-in-%02d-not' % inpmap[i]] = 1 v = (digl & msk) if(v > 0): c['digital-in-%02d' % inpmap2[i]] = 1 c['digital-in-%02d-not' % inpmap2[i]] = 0 else: c['digital-in-%02d' % inpmap2[i]] = 0 c['digital-in-%02d-not' % inpmap2[i]] = 1 for i in range(6): msk = (1 << i) v = (digi & msk) if(v > 0): c['digital-in-%02d' % inpmap3[i]] = 1 c['digital-in-%02d-not' % inpmap3[i]] = 0 else: c['digital-in-%02d' % inpmap3[i]] = 0 c['digital-in-%02d-not' % inpmap3[i]] = 1 return ################################################################ # TRANSMIT # ################################################################ def doTx(): for state in range(10): scale = c['analog-out-%02d-scale' % pwmpinmap[state]] or 1. offset = c['analog-out-%02d-offset' % pwmpinmap[state]] data = int((c['analog-out-%02d' % pwmpinmap[state]] - offset) / scale) if data < 0: data = 0 if data > 255: data = 255 analogout[state] = data # 10 bytes of analog out data data = 0 sout = 0 dout = 0 xout = 0 pullup = 0 for state in range(8): if ( c['digital-out-%02d' % pinmap[state]]) == 1: if (not c['digital-out-%02d-invert' % pinmap[state]]): sout |= (1 << state) else: sout |= (0 << state) else: if (not c['digital-out-%02d-invert' % pinmap[state]]): sout |= (0 << state) else: sout |= (1 << state) if ( c['digital-out-%02d' % pinmap2[state]]) == 1: if (not c['digital-out-%02d-invert' % pinmap2[state]]): dout |= (1 << state) else: dout |= (0 << state) else: if (not c['digital-out-%02d-invert' % pinmap2[state]]): dout |= (0 << state) else: dout |= (1 << state) if ( c['digital-out-%02d' % pinmap3[state]]) == 1: if (not c['digital-out-%02d-invert' % pinmap3[state]]): xout |= (1 << state) else: xout |= (0 << state) else: if (not c['digital-out-%02d-invert' % pinmap3[state]]): xout |= (0 << state) else: xout |= (1 << state) # if ((c['digital-in-%02d-pullup' % pinmap[state]]) == 1): # pullup |= (1 << state) txbuf[0] = "D" txbuf[1] = (sout) txbuf[2] = (dout) txbuf[3] = (xout) txbuf[4] = analogout[state] # txbuf[5] = '~' # txbuf[0] = soh service.sendto(format(txbuf), (REMOTE, R_PORT)) # time.sleep(0.02) ################################################################ # MAIN LOOP # ################################################################ if __name__ == "__main__": try: while 1: doTx() doRx() except (KeyboardInterrupt,): raise SystemExit, 0 */ /* // defines.h #ifndef ETHERMEGADEFINES #define ETHERMEGADEFINES #define COMPUTERIP 192, 168, 3, 31 #define ARDUINOIP 192, 168, 3, 199 #define IPPORT 8888 // Mega 2560 ports // Pin Port Bit // 22 PORTA 0 // STEPPORT // 23 PORTA 1 // 24 PORTA 2 // 25 PORTA 3 // 26 PORTA 4 // 27 PORTA 5 // 28 PORTA 6 // 29 PORTA 7 // 53 PORTB 0 // 52 PORTB 1 // 51 PORTB 2 // 50 PORTB 3 // 10 PORTB 4 pwm // Ethernet Shield // 11 PORTB 5 pwm // Ethernet Shield // 12 PORTB 6 pwm // Ethernet Shield // 13 PORTB 7 pwm // Ethernet Shield // 37 PORTC 0 // DIRPORT // 36 PORTC 1 // 35 PORTC 2 // 34 PORTC 3 // 33 PORTC 4 // 32 PORTC 5 // 31 PORTC 6 // 30 PORTC 7 // 21 PORTD 0 // INT 0 // 20 PORTD 1 // INT 1 // 19 PORTD 2 // INT 2 // 18 PORTD 3 // INT 3 // 38 PORTD 7 // 0 PORTE 0 // 1 PORTE 1 // 5 PORTE 3 // 2 PORTE 4 // INT 4 // 3 PORTE 5 // INT 5 // 54 PORTF 0 // Analog Pin 0 // Digital Pin 54 // HOMES // 55 PORTF 1 // Analog Pin 1 // Digital Pin 55 // 56 PORTF 2 // Analog Pin 2 // Digital Pin 56 // 57 PORTF 3 // Analog Pin 3 // Digital Pin 57 // 58 PORTF 4 // Analog Pin 4 // Digital Pin 58 // 59 PORTF 5 // Analog Pin 5 // Digital Pin 59 // 60 PORTF 6 // Analog Pin 6 // Digital Pin 60 // 61 PORTF 7 // Analog Pin 7 // Digital Pin 61 // 62 PORTK 0 // Analog Pin 8 // Digital Pin 62 // LIMITS // 63 PORTK 1 // Analog Pin 9 // Digital Pin 63 // 64 PORTK 2 // Analog Pin 10 // Digital Pin 64 // 65 PORTK 3 // Analog Pin 11 // Digital Pin 65 // 66 PORTK 4 // Analog Pin 12 // Digital Pin 66 // 67 PORTK 5 // Analog Pin 13 // Digital Pin 67 // 68 PORTK 6 // Analog Pin 14 // Digital Pin 68 // 69 PORTK 7 // Analog Pin 15 // Digital Pin 69 // 41 PORTG 0 // 40 PORTG 1 // 39 PORTG 2 // 4 PORTG 5 // 17 PORTH 0 // 16 PORTH 1 // 6 PORTH 3 // 7 PORTH 4 // 8 PORTH 5 // 9 PORTH 6 // 15 PORTJ 0 // 14 PORTJ 1 // 49 PORTL 0 // XTRPORT // 48 PORTL 1 // 47 PORTL 2 // 46 PORTL 3 pwm // 45 PORTL 4 pwm // 44 PORTL 5 pwm // 43 PORTL 6 // 42 PORTL 7 /////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////// // Pin Port Bit // 22 PORTA 0 // Step X // 23 PORTA 1 // Step Y // 24 PORTA 2 // Step Z // 25 PORTA 3 // Step A // 26 PORTA 4 // Step B // 27 PORTA 5 // Step C // 28 PORTA 6 // Step W // 29 PORTA 7 // Step U // 53 PORTB 0 // SS // 52 PORTB 1 // SCK // Ethernet Shield Mega // 51 PORTB 2 // MOSI // Ethernet Shield Mega // 50 PORTB 3 // MISO // Ethernet Shield Mega // 10 PORTB 4 // ethernet shield // 11 PORTB 5 // ethernet shield // 12 PORTB 6 // ethernet shield // 13 PORTB 7 // ethernet shield // 37 PORTC 0 // Dir X // 36 PORTC 1 // Dir Y // 35 PORTC 2 // Dir Z // 34 PORTC 3 // Dir A // 33 PORTC 4 // Dir B // 32 PORTC 5 // Dir C // 31 PORTC 6 // Dir W // 30 PORTC 7 // Dir U // 21 PORTD 0 // SCL //int // 20 PORTD 1 // SCA //int // 19 PORTD 2 // RX1 //int // 18 PORTD 3 // TX1 //int // 38 PORTD 7 // // 0 PORTE 0 // RX0 // 1 PORTE 1 // TX0 // 5 PORTE 3 // pwm // 2 PORTE 4 // pwm //int // 3 PORTE 5 // pwm //int // 41 PORTG 0 // // 40 PORTG 1 // // 39 PORTG 2 // // 4 PORTG 5 // pwm // 17 PORTH 0 // RX2 // 16 PORTH 1 // TX2 // 6 PORTH 3 // pwm // 7 PORTH 4 // pwm // 8 PORTH 5 // pwm // 9 PORTH 6 // pwm // 15 PORTJ 0 // RX3 // 14 PORTJ 1 // TX3 // 49 PORTL 0 // // 48 PORTL 1 // // 47 PORTL 2 // // 46 PORTL 3 // pwm // 45 PORTL 4 // pwm // 44 PORTL 5 // pwm // 43 PORTL 6 // // 42 PORTL 7 // #endif */