com port discovery
authorMjolnir <thomas.pietrzak@inria.fr>
Wed, 2 Sep 2015 13:48:51 +0000 (15:48 +0200)
committerMjolnir <thomas.pietrzak@inria.fr>
Wed, 2 Sep 2015 13:48:51 +0000 (15:48 +0200)
13 files changed:
ArduinoScreen/ArduinoScreen.ino
GCodeParser/GCodeParser.ino
GCodeParser/process_string.ino
Library/LivingDevice.cpp
Library/LivingDevice.h
Library/LivingKeyboard.cpp
Library/LivingKeyboard.h
Library/LivingScreen.cpp
Library/LivingScreen.h
Library/SerialLinux.cpp
Library/XYPlotter.cpp
Library/XYPlotter.h
Test/mainwindow.cpp

index d3772f05687126d79a99ffe3fe43702d605696d1..e85c873c263972e5b47eb3f447c52fbf751489b2 100644 (file)
@@ -24,8 +24,8 @@ void eval(int command, int value)
   switch(command)
   {
     case 'I':
-      Serial.print(F("LivingScreen\0\n"));
-      Serial.flush();
+      Serial.println(F("LivingScreen"));
+      //Serial.flush();
       break;
     case 'T':
       Serial.print("Translate ");
@@ -47,7 +47,8 @@ void eval(int command, int value)
       break;
     default:
       Serial.print("Unknown command ");
-      Serial.println(command);
+      Serial.write(command);
+      Serial.write('\n');
   }
 }
 
@@ -61,7 +62,7 @@ void setup()
   init_motors();
   
   //Calibration
-  calibrate();
+  //calibrate();
 }
 
 void loop()
index 77c801c08dc78d87a2610208c9aa04ea46789cd5..7eaad3255307f3b92615d473ebc10bce2546acfc 100644 (file)
@@ -95,11 +95,39 @@ void setup()
   }
   //other initialization.
   init_process_string();
-  init_steppers();
-  process_string("G90",3);//Absolute Position
+  //init_steppers();
+  //process_string("G90",3);//Absolute Position
   //Serial.println("start");
 }
 
+void loop()
+{
+
+  char c;
+  //read in characters if we got them.
+  if (Serial.available() > 0)
+  {
+    c = Serial.read();
+    
+    if (c == '\n' || c == '\r')
+    {
+      //process our command!
+      process_string(commands, serial_count);
+      //clear command.
+      init_process_string();
+    }
+    else if (serial_count < COMMAND_SIZE)
+    {
+      commands[serial_count] = c;
+      serial_count++;
+    }
+  }
+  else
+    delayMicroseconds(100);
+}
+
+
+/*
 void loop()
 {
 
@@ -112,8 +140,13 @@ void loop()
     //newlines are ends of commands.
     if (c != '\n')
     {
-      if(c=='I')
-        Serial.print(F("XYPlotter\0\n"));
+      if (c == 'I')
+        Serial.println(F("XYPlotter"));
+      else if (c == 'C')
+      {
+        init_steppers();
+        process_string("G90",3);//Absolute Position
+      }
       else
       {
         if (c == '(')
@@ -160,5 +193,5 @@ void loop()
   //                }
 
 
-}
+}*/
 
index 0240f02b5acedc66ae6cc11605905bfccb3a86a8..9033296934ee6152bd891fe43a453026d0598a1b 100644 (file)
@@ -65,7 +65,16 @@ void process_string(char instruction[], int size)
        
         
        //did we get a gcode?
-       if (!has_command('$', instruction, size)&&(
+        if (has_command('I', instruction, size))
+        {
+          Serial.println(F("XYPlotter"));
+        }
+        else if (has_command('C', instruction, size))
+        {
+          init_steppers();
+          process_string("G90",3);//Absolute Position
+        }
+       else if (!has_command('$', instruction, size)&&(
                has_command('G', instruction, size) ||
                has_command('X', instruction, size) ||
                has_command('Y', instruction, size) ||
index c136796f94e8f5d2fb09d826af313df9b90d7066..5bccf371cfd7eef3ba41a06ce2b1e1b5d3cd57d1 100644 (file)
@@ -8,6 +8,7 @@
 //#define QIODEVICE_DEBUG
 
 char const *LivingDevice::serialports[] = {"/dev/ttyUSB0", "/dev/ttyUSB1", "/dev/ttyUSB2", "/dev/ttyUSB3", "/dev/ttyUSB4"};
+bool LivingDevice::usedports[] = {false, false, false, false, false};
 
 LivingDevice::LivingDevice(QString name, int timeout)
 : serialPort(NULL)
@@ -25,18 +26,33 @@ void LivingDevice::discover(QString name, int timeout)
     qDebug() << "Discovering" << name;
     for (int i = 0 ; i < 5 && !serialPort ; i++)
     {
-        try{
-                serialPort = new SerialLinux(serialports[i], 9600);
-
+        if (usedports[i])
+            continue;
+        try {
                 qDebug() << "Trying port" << serialports[i];
+                serialPort = new SerialLinux(serialports[i], 9600);
 
-                //ask fo his name
-                sendCommand('I');
-                QThread::msleep(500);
-                QString answer = readAnswer();
+                int tries = 0;
+                QString answer;
+                qDebug() << "Identify";
+                do
+                {
+                    qDebug() << "Try" << tries;
+                    //ask fo his name
+                    if (!sendCommand('I'))
+                    {
+                        qDebug() << "Cannot speak with port";
+                        closeSerialPort();
+                        continue;
+                    }
+                    answer = readAnswer(timeout);
+                } while (answer == "" && tries++ < 5);
 
                 if (answer == name)
-                    qDebug() << "Found LivingKeyboard on port" << serialports[i];
+                {
+                    qDebug() << "Found " << name << " on port" << serialports[i];
+                    usedports[i] = true;
+                }
                 else
                 {
                     qDebug() << "Not" << name;
@@ -65,25 +81,27 @@ void LivingDevice::closeSerialPort()
     serialPort = NULL;
 }
 
-void LivingDevice::sendCommand(char command)
+bool LivingDevice::sendCommand(char command)
 {
     if (serialPort)
     {
         char buffer[] = {command, '\n'};
-        serialPort->WriteData(buffer, 2);
+        return serialPort->WriteData(buffer, 2);
     }
+    return false;
 }
 
-void LivingDevice::sendCommand(char command, int value)
+bool LivingDevice::sendCommand(char command, int value)
 {
     if (serialPort)
     {
         QString buffer = command + QString::number(value) + "\n";
-        serialPort->WriteData(buffer.toStdString().data(), strlen(buffer.toStdString().data()));
+        return serialPort->WriteData(buffer.toStdString().data(), strlen(buffer.toStdString().data()));
     }
+    return false;
 }
 
-void LivingDevice::sendCommand(char *command, int *value, int nb)
+bool LivingDevice::sendCommand(char *command, int *value, int nb)
 {
     QString buffer;
     if (serialPort)
@@ -91,18 +109,32 @@ void LivingDevice::sendCommand(char *command, int *value, int nb)
         for (int i = 0 ; i < nb ; i++)
             buffer += command[i] + QString::number(value[i]);
         buffer += '\n';
-        serialPort->WriteData(buffer.toStdString().data(), strlen(buffer.toStdString().data()));
+        return serialPort->WriteData(buffer.toStdString().data(), strlen(buffer.toStdString().data()));
     }
+    return false;
 }
 
-QString LivingDevice::readAnswer()
+QString LivingDevice::readAnswer(int timeout)
 {
     char buffer[128];
 
     if (serialPort)
     {
-        int nb = serialPort->ReadData(buffer, 127);
+        int tries = 0;
+        int nb = 0;
+        do
+        {
+            QThread::msleep(100);
+            nb = serialPort->ReadData(buffer, 127);
+        } while (nb == 0 && tries++ < timeout / 100);
+        if (nb <= 0)
+        {
+            qDebug() << "No response";
+            return ""; //TODO: do something else
+        }
         buffer[nb] = '\0';
+        while(buffer[strlen(buffer)-1] == '\n' || buffer[strlen(buffer)-1] == '\r')
+            buffer[strlen(buffer)-1] = '\0';
         qDebug() << "Read" << buffer;
         return QString(buffer);
     }
index c7d0acee3cd842e514ef403e22b5af009e53ea50..a820cfebfb2d5a879f1de4a7d48605f8665be774 100644 (file)
@@ -11,20 +11,23 @@ class LIVINGDESKTOPLIBRARYSHARED_EXPORT LivingDevice
         LivingDevice(QString name, int timeout=5000);
         virtual ~LivingDevice();
 
-        void discover(QString name, int timeout=5000);
-
-        void closeSerialPort();
+        virtual void calibrate() = 0;
 
-        void sendCommand(char command);
-        void sendCommand(char command, int value);
-        void sendCommand(char *command, int *value, int nb);
-
-        QString readAnswer();
+        QString readAnswer(int timeout);
 
     protected:
         Serial *serialPort;
 
         static char const *serialports[];
+        static bool usedports[];
+
+        void discover(QString name, int timeout=5000);
+        void closeSerialPort();
+
+        bool sendCommand(char command);
+        bool sendCommand(char command, int value);
+        bool sendCommand(char *command, int *value, int nb);
+
 };
 
 #endif // LIVINGDEVICE_H
index cb733b3ceef138684829cc0a6d4a148483d36f81..eabbf668b35310cb5aeca770831655b538eb8c40 100644 (file)
@@ -3,7 +3,7 @@
 #include <QDebug>
 
 LivingKeyboard::LivingKeyboard()
-: LivingDevice("LivingKeyboard", 500)
+: LivingDevice("LivingKeyboard", 5000)
 {
 }
 
@@ -26,3 +26,7 @@ void LivingKeyboard::translation(LivingKeyboard::direction d, unsigned int dista
         dis *= -1;
     sendCommand('T', dis);
 }
+
+void LivingKeyboard::calibrate()
+{
+}
index 7d7ff8ba628e2bc40d48b2ce716e0d5a09567cc4..71137c5090267fd71650a3d6c81a586640fbbddf 100644 (file)
@@ -15,6 +15,8 @@ class LIVINGDESKTOPLIBRARYSHARED_EXPORT LivingKeyboard : public LivingDevice
 
         void rotation(tilt t, unsigned int angle);
         void translation(direction d, unsigned int distance);
+
+        void calibrate();
 };
 
 #endif // LIVINGKEYBOARD_H
index 1042bfa9e3707d21bfd5edb5a85dafa599a014a3..06fb71c97b03a25e46823e797c92793a8c387765 100644 (file)
@@ -3,7 +3,7 @@
 #include <QDebug>
 
 LivingScreen::LivingScreen()
-: LivingDevice("LivingScreen", 30000)
+: LivingDevice("LivingScreen", 5000)
 {
 }
 
@@ -26,3 +26,8 @@ void LivingScreen::translation(LivingScreen::direction d, unsigned int distance)
         dis *= -1;
     sendCommand('T', dis);
 }
+
+void LivingScreen::calibrate()
+{
+    sendCommand('C');
+}
index 55def11c4b2ef0d3d33e02788b9f1e367d735754..ac468f1f4f50ca14ffaab42b03aa81ffd8a791ef 100644 (file)
@@ -14,6 +14,8 @@ class LIVINGDESKTOPLIBRARYSHARED_EXPORT LivingScreen : public LivingDevice
 
         void rotation(direction t, unsigned int angle);
         void translation(direction d, unsigned int distance);
+
+        void calibrate();
 };
 
 #endif // LIVINGSCREEN_H
index 2ce9a18ea23bab4606df395a1dfff3baff741f4e..95410cd558228c941b992c504a0f852cc45a16d3 100644 (file)
@@ -1,5 +1,7 @@
 #include "SerialLinux.h"
 
+#include <ErrorMessage.h>
+
 #include <termios.h>
 #include <errno.h>
 #include <unistd.h>
@@ -17,14 +19,15 @@ SerialLinux::SerialLinux(const char *portName, int baudrate)
 {
     _arduinowaittime = 1;
     //Try to connect to the given port throuh CreateFile
-    _hSerial = open(portName, O_RDWR | O_NOCTTY);// | O_NDELAY);
+    //_hSerial = open(portName, O_RDWR | O_NOCTTY | O_NDELAY);
+    _hSerial = open(portName, O_RDWR | O_NDELAY);
 
     //Check if the connection was successfull
     if(_hSerial < 0)
     {
         char buffer[256];
         sprintf(buffer, "Open error on port %s %d %s\n", portName, errno, strerror(errno));
-        throw buffer;
+        throw ErrorMessage(buffer);
     }
 
     struct termios serialParams;
index f0c90e9884fe8903ef4da235962bb55a412f50bc..134067e56287476186e3b3cab5442fa67be81d0e 100644 (file)
@@ -5,7 +5,7 @@
 const int XYPlotter::toolDistance = 70;
 
 XYPlotter::XYPlotter()
-: LivingDevice("XYPlotter", 13000)
+: LivingDevice("XYPlotter", 5000)
 {
 }
 
@@ -19,3 +19,8 @@ void XYPlotter::moveTo(int x, int y, bool toolup=true)
     int values[] = {0, x, y, toolup * toolDistance};
     sendCommand(commands, values, 4);
 }
+
+void XYPlotter::calibrate()
+{
+    sendCommand('C');
+}
index af8f331d5f3230ce841360b4800e2638041ad6c5..a3903c08ffe93f742efa99ed507b69e8ebf0f257 100644 (file)
@@ -11,6 +11,8 @@ class LIVINGDESKTOPLIBRARYSHARED_EXPORT XYPlotter : public LivingDevice
 
         void moveTo(int x, int y, bool toolup);
 
+        void calibrate();
+
     private:
         unsigned int posX, posY;
         bool toolUp;
index b7d653c0336bcf3e345b5d27cd492162fdc35543..eda2defd0ac18c2fd193256dcc05c624637fac52 100644 (file)
@@ -2,6 +2,7 @@
 #include "ui_mainwindow.h"
 
 #include <ErrorMessage.h>
+#include <QThread>
 
 MainWindow::MainWindow(QWidget *parent)
 : QMainWindow(parent), ui(new Ui::MainWindow)
@@ -15,6 +16,7 @@ MainWindow::MainWindow(QWidget *parent)
         connect(ui->keyboardrotatebutton, SIGNAL(clicked()), this, SLOT(rotateKeyboard()));
         ui->keyboardmovebutton->setEnabled(true);
         ui->keyboardrotatebutton->setEnabled(true);
+        keyboard->calibrate();
     }
     catch (ErrorMessage e)
     {
@@ -30,10 +32,11 @@ MainWindow::MainWindow(QWidget *parent)
         connect(ui->screenrotatebutton, SIGNAL(clicked()), this, SLOT(rotateScreen()));
         ui->screenmovebutton->setEnabled(true);
         ui->screenrotatebutton->setEnabled(true);
+        screen->calibrate();
     }
     catch (ErrorMessage e)
     {
-        mouse = NULL;
+        screen = NULL;
         qDebug() << QString(e.what());
     }
     qDebug("Connect mouse");
@@ -43,6 +46,7 @@ MainWindow::MainWindow(QWidget *parent)
         mouse = new XYPlotter();
         connect(ui->mousebutton, SIGNAL(clicked()), this, SLOT(moveMouse()));
         ui->mousebutton->setEnabled(true);
+        mouse->calibrate();
     }
     catch (ErrorMessage e)
     {