Arduino Serial Control of TinyG

Home Forums TinyG TinyG Projects Arduino Serial Control of TinyG

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #11283
    Chris S
    Member

    I was goofing around with inter-Arduino serial networks the other day and got to wondering… can I use an Arduino to control the TinyG over it’s serial connection if I bypass the USB to serial converter?

    Yes, yes I can. It’s been running for 17.5 hours straight. Fritzing seems to be dead so you’ll have to look at a crappy MSPaint diagram:

    https://photos.app.goo.gl/fzdh5e6bCBQPkHNBA

    All the commands I TX are received fine but the bytes I read back from the TinyG are a bit screwy. The wires I’m using are un-shielded so that could be a problem; plus I’m going to have someone remove the USB to serial converter IC and see if that cleans it up.

    Here’s my crappy Arduino code, it just moves an X/Y gantry in a 2D ‘array’ pattern of 6×5. The initialization (void setup()) needs some work, that’s why it’s commented out right now.

    #include <SoftwareSerial.h>
    SoftwareSerial tinyGSerial(12, 13);                                   // Define the software serial pins; RX, TX
    static const int numGCodeCommands = 30;
    static const String GCodes[30] = {                                    // This is obviously all the GCode commands; should probably store them on an SD card for easy update
      "G01X0Y0Z0F4000", "G01X20F6000", "G01X40F6000", "G01X60F6000", "G01X80F6000", "G01X100F6000",
      "G01X0Y30Z0F4000", "G01X20F6000", "G01X40F6000", "G01X60F6000", "G01X80F6000", "G01X100F6000",
      "G01X0Y60Z0F4000", "G01X20F6000", "G01X40F6000", "G01X60F6000", "G01X80F6000", "G01X100F6000",
      "G01X0Y90Z0F4000", "G01X20F6000", "G01X40F6000", "G01X60F6000", "G01X80F6000", "G01X100F6000",
      "G01X0Y120Z0F4000", "G01X20F6000", "G01X40F6000", "G01X60F6000", "G01X80F6000", "G01X100F6000",
    };
    static const int initializationTimeout = 10000;                       // Ten second timeout on TinyG initiailization
    static const int commandAndResponseDelay = 100;                       // How long to wait (ms) for a response after sending a command
    static const int cellIndexingDelay = 3000;
    int commandIndex = 0;                                                 // Variable to hold the value of the current command array index.
    static const bool debugEnabled = false;
    
    void setup() {
      Serial.begin(115200);
      tinyGSerial.begin(115200);
      /* None of this initialization code _actually_ works yet but the goal is to: 
       1. Reset TinyG
       2. Send provisioning commands
       3. Home/Zero
       4. Request Status Report and verify no errors
        delay(5000);
        Serial.println("");                                            // Send the reset command to initialize the TinyG
        delay(10000);
        Serial.println("Homing the machine, please wait.");
        TinyGSerial.println("G28.2X0Y0Z0");                             // Home and zero the machine
        Serial.flush();
        float tinyGInitializationTimer = millis();                      // Start a timer
        while (!Serial.available())                                     // Wait for some bytes from the TinyG
        {
          delay(200);
          if ((millis() - tinyGInitializationTimer) > initializationTimeout)
          {
            Serial.println("Initialization timout occurred");
            while(1);
          }
        }
      */
    }
    
    void loop() {
      int i = 0;
      for (i = 0; i < numGCodeCommands; i++)                              // Use a for loop/array size to send all GCode commands
      {
        delay(cellIndexingDelay);                                         // Wait _some_ time for the machine to move
        sendGCodeCommand(i);
        readResponse();                                                   // Get the return fromt he TinyG
      }
      delay(cellIndexingDelay);                                           // Wait at the current location for _some_ time
    }
    
    void sendGCodeCommand(int commandIndex)
    {
      if (debugEnabled)
      {
        Serial.print("sending command # "); Serial.println(commandIndex);
      }
      tinyGSerial.println(GCodes[commandIndex]);        // Send a GCode command from the command array
      tinyGSerial.println("M02");                       // Send the cycle end command
      // tinyGSerial.flush();
      tinyGSerial.println("$qr");                       // Request a queue report; should probably do something useful with this
      tinyGSerial.flush();
      if (debugEnabled)
      {
        Serial.print("Command sent: "); Serial.println(GCodes[commandIndex]);
      }
    }
    
    void readResponse()
    {
      while (!tinyGSerial.available())            // Wait for a response from the TinyG
      {
        if (debugEnabled)
        {
          Serial.println("waiting for response from the TinyG");
        }
        delay (10);
      }
      String myTinyGString = (tinyGSerial.readStringUntil('/n'));
      if (debugEnabled)
      {
        Serial.print("TG: "); Serial.println(myTinyGString);
      }
    }

    I’m not sure where I’m going with this, just thought it would be neat to try out.

    #11284
    cmcgrath5035
    Moderator

    If you check out the schematics, you will see that the Tx/Rx leads connected to the microcontroller on tinyG are simple wire-or’ed with the output of the FTDI chip on board, so yes, you can communicate between a host and tinyG if you want with plain old RS232 protocol, but not necessarily Phy layer.. You can also use a native PC comm serial port, if you still have one. Should you want to try that, you need to make sure that the serial port is a low voltage implementation, 3.3v or maybe 5V logic.

    I am not capable of commenting on your Arduino code, but would probably look there first

    BTW, schematics -> https://github.com/synthetos/TinyG/tree/master/hardware/v8schematics/v8h

    #11285
    Chris S
    Member

    Thanks, I’ll check it out!

    #11293
    Zootalaws
    Member

    You can go further and make it wireless using ESP32 or Bluetooth.

    Works great.

    #12128
    gerta
    Participant

    I’m trying to achieve similar control of a TinyG from an Arduino Uno. Trying the Arduino code above did not work for me. I tried this more minimal version to diagnose the issue:

    #include <SoftwareSerial.h>
    SoftwareSerial tinyGSerial(12, 13);                                   // Define the software serial pins; RX, TX
    int counter = 0;
    
    void setup() {
      Serial.begin(115200);
      tinyGSerial.begin(115200);
      delay(1000);
      byte ctrlX = 0x18;
      tinyGSerial.write(ctrlX); // Send reset
    }
    
    void loop() {
      delay(100);
      int n = tinyGSerial.available();
      counter++;
      if (counter==30) {
        Serial.println(n);
        counter=0;
      }
    }

    This code can transmit the reset command, but nothing gets received; the number of bytes in the Rx buffer (tinyGSerial.available()) is 0, even if I manually reset the tinyG, which I know sends a status update e.g. if connected over USB on CoolTerm.

    I know the Arduino SoftwareSerial library is a little dicey, so I tried the same thing with the AltSoftSerial library. The behavior is similar, except that the size of the Rx buffer increments by exactly one every time the TinyG sends a message (e.g. one when I initiate a manual reset, and one more when the reset is complete). There should be a bunch of bytes sent by the TinyG, so I don’t know where I’m going wrong. I’m also posting to an Arduino forum in case this is just an Arduino programming issue, but I wanted to check if I’m maybe missing something about the TinyG. Is the behavior of the TinyG Tx contact somehow different from what I’m understanding?

    #12129
    gerta
    Participant

    A quick note to follow up in case anyone else tries this. I got things partly working using the AltSoftSerial library, but the replies from the TinyG were somewhat garbled, much as shown in the initial post of this thread. Fiddling with the program and checking around online, I discovered the standard TinyG baud rate of 115200 is just way too high for SoftwareSerial and a little too high to be dependable in the case of AltSoftSerial. Serial Tx from Arduino to TinyG does work at 115200, or at least enough to send an initial command to dial back the speed. Setting all communication to 9600 baud gives expected behavior.

    Functional, minimal code below works either through SoftwareSerial (as-is) or AltSoftSerial (comment out first 2 lines, uncomment subsequent 2 lines):

    #include <SoftwareSerial.h>
    SoftwareSerial tinyGSerial(8,9);
    //#include <AltSoftSerial.h>
    //AltSoftSerial tinyGSerial;
    
    byte ctrlX = 0x18; // TinyG reset command
    char myChar;
    
    void setup() {
      Serial.begin(9600);
      tinyGSerial.begin(115200);
      delay(1000);
    
      tinyGSerial.println("$baud=1");
      tinyGSerial.end();
      tinyGSerial.flush();
      delay(1000);
      tinyGSerial.begin(9600);
      delay(1000);
    }
    
    void loop() {
      if (Serial.available()) {
        myChar=Serial.read();
        tinyGSerial.print(myChar);
      }
      while (tinyGSerial.available()) {
        myChar=tinyGSerial.read();
        Serial.print(myChar);
      }
    }
Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.