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 6x5. 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.