keyboard
authorMjolnir <thomas.pietrzak@inria.fr>
Thu, 20 Aug 2015 14:48:02 +0000 (16:48 +0200)
committerMjolnir <thomas.pietrzak@inria.fr>
Thu, 20 Aug 2015 14:48:02 +0000 (16:48 +0200)
ArduinoKeyboard/ArduinoKeyboard.ino [new file with mode: 0644]
ArduinoKeyboard/motorcontrol.ino [new file with mode: 0644]
Library/LivingDesktopLibrary.pro
Library/LivingKeyboard.cpp [new file with mode: 0644]
Library/LivingKeyboard.h [new file with mode: 0644]
Library/XYPlotter.cpp
Library/XYPlotter.h

diff --git a/ArduinoKeyboard/ArduinoKeyboard.ino b/ArduinoKeyboard/ArduinoKeyboard.ino
new file mode 100644 (file)
index 0000000..3c6fc3d
--- /dev/null
@@ -0,0 +1,87 @@
+#include <Stepper.h>
+
+//Motors
+const int leftmotor1   =  5; 
+const int leftmotor2   =  6;
+const int rightmotor1  = 10;
+const int rightmotor2  = 11;
+const int motorsenable = 12;
+
+//Stepper motor
+const int stepper1 = 3; 
+const int stepper2 = 2;
+const int stepper3 = 7;
+const int stepper4 = 8;
+
+//buffer stuff
+char command = 0;
+int value = 0;
+#define POSITIVE 0
+#define NEGATIVE 1
+int sign = POSITIVE;
+
+
+void eval(int command, int value)
+{
+  switch(command)
+  {
+    case 'I':
+      Serial.println("Living Keyboard");
+      break;
+    case 'T':
+      Serial.print("Translate ");
+      Serial.println(value);
+      translate(value);
+      break;
+    case 'R':
+      Serial.print("Rotate ");
+      Serial.println(value);
+      rotate(value);
+      break;
+  }
+}
+
+
+void setup()
+{
+  //Motors
+  init_motors();
+
+  //Communication
+  Serial.begin(115200);
+}
+
+void loop()
+{
+
+  char c;
+  //read in characters if we got them.
+  if (Serial.available() > 0)
+  {
+    c = Serial.read();
+    
+    if (c == '\n' || c == '\r')
+    {
+      if (command)
+      {
+        if (sign == NEGATIVE)
+          value = -value;
+        eval(command, value);
+        command = 0;
+        value = 0;
+        sign = POSITIVE;
+      }
+    }
+    else if (!command)
+      command = c;
+    else if (c == '-')
+      sign = NEGATIVE;
+    else if (c >= '0' && c <= '9')
+      value = value * 10 + (c - '0');
+  }
+  else
+    delayMicroseconds(100);
+}
+
+
+
diff --git a/ArduinoKeyboard/motorcontrol.ino b/ArduinoKeyboard/motorcontrol.ino
new file mode 100644 (file)
index 0000000..9ea6a8b
--- /dev/null
@@ -0,0 +1,117 @@
+//DC motors
+const int motorspeed         =  33; // mm/s
+const int motorrotationspeed =  60; // deg/s
+
+//Stepper motor
+const int steps = 400;
+Stepper stepper = Stepper(steps, stepper1, stepper2, stepper3, stepper4);
+
+void init_motors()
+{
+    //Motors
+  pinMode(leftmotor1, OUTPUT);
+  pinMode(leftmotor2, OUTPUT);
+  pinMode(rightmotor1, OUTPUT);
+  pinMode(rightmotor2, OUTPUT);
+  pinMode(motorsenable,OUTPUT);
+
+  //Stepper
+  pinMode(stepper1, OUTPUT);
+  pinMode(stepper2, OUTPUT);
+  pinMode(stepper3, OUTPUT);
+  pinMode(stepper4, OUTPUT);
+  stepper.setSpeed(20);
+}
+
+/*
+  angle 
+    +: clockwise
+    -: anticlockwise
+*/
+void rotate(int angle)
+{
+  digitalWrite(motorsenable, HIGH);
+  //clockwise
+  if (angle > 0)
+  {
+    //left motor forward
+    digitalWrite(leftmotor1, HIGH);
+    digitalWrite(leftmotor2, LOW);
+    //right motor backward
+    digitalWrite(rightmotor1, LOW);
+    digitalWrite(rightmotor2, HIGH);
+  
+    unsigned int duration = (angle * 1000.) / motorrotationspeed;
+    Serial.println(duration);
+    delay(duration);
+  }
+  else
+  {
+    //left motor forward
+    digitalWrite(leftmotor1, LOW);
+    digitalWrite(leftmotor2, HIGH);
+    //right motor backward
+    digitalWrite(rightmotor1, HIGH);
+    digitalWrite(rightmotor2, LOW);
+    
+    unsigned int duration = (-angle * 1000.) / motorrotationspeed;
+    Serial.println(duration);
+    delay(duration);
+  }
+  
+  //left motor stop
+  digitalWrite(leftmotor1, LOW);
+  digitalWrite(leftmotor2, LOW);
+  //right motor stop
+  digitalWrite(rightmotor1, LOW);
+  digitalWrite(rightmotor2, LOW);
+  
+  digitalWrite(motorsenable, LOW);
+  
+  stepper.step(-angle * steps / 360);
+}
+
+/*
+  distance 
+    +: forward
+    -: backward
+*/
+void translate(int distance)
+{
+  digitalWrite(motorsenable, HIGH);
+  //forward
+  if (distance > 0)
+  {
+    //left motor forward
+    digitalWrite(leftmotor1, HIGH);
+    digitalWrite(leftmotor2, LOW);
+    //right motor forward
+    digitalWrite(rightmotor1, HIGH);
+    digitalWrite(rightmotor2, LOW);
+  }
+  else
+  {
+    //left motor backward
+    digitalWrite(leftmotor1, HIGH);
+    digitalWrite(leftmotor2, LOW);
+    //right motor backward
+    digitalWrite(rightmotor1, HIGH);
+    digitalWrite(rightmotor2, LOW);
+    
+    distance = -distance;
+  }
+  
+  unsigned int duration = (distance * 1000.) / motorspeed;
+  Serial.println(duration);
+  delay(duration);
+  
+  //left motor stop
+  digitalWrite(leftmotor1, LOW);
+  digitalWrite(leftmotor2, LOW);
+  //right motor stop
+  digitalWrite(rightmotor1, LOW);
+  digitalWrite(rightmotor2, LOW);
+  
+  digitalWrite(motorsenable, LOW);
+}
+
index af9b885ae2bd6be7f37c69959980c9badcce4caa..07fb8e63d997ad97cc1a5818aaa95552f52b2f3a 100644 (file)
@@ -13,12 +13,14 @@ DEFINES += LIVINGDESKTOPLIBRARY_LIBRARY
 
 SOURCES +=\
         ErrorMessage.cpp \
-        XYPlotter.cpp
+        XYPlotter.cpp \
+    LivingKeyboard.cpp
 
 HEADERS +=\
         livingdesktoplibrary_global.h\
         ErrorMessage.h \
-        XYPlotter.h
+        XYPlotter.h \
+    LivingKeyboard.h
 
 unix {
     target.path = /usr/lib
diff --git a/Library/LivingKeyboard.cpp b/Library/LivingKeyboard.cpp
new file mode 100644 (file)
index 0000000..06be01d
--- /dev/null
@@ -0,0 +1,50 @@
+#include "LivingKeyboard.h"
+
+LivingKeyboard::LivingKeyboard()
+: serialPort(new QSerialPort(port)), connected(false), posX(0), posY(0), toolUp(false)
+{
+    if(serialPort->open(QIODevice::ReadWrite))
+    {
+        serialPort->setBaudRate(QSerialPort::Baud115200,QSerialPort::AllDirections);
+        serialPort->setDataBits(QSerialPort::Data8);
+        serialPort->setParity(QSerialPort::NoParity);
+        serialPort->setStopBits(QSerialPort::TwoStop);
+        serialPort->setFlowControl(QSerialPort::NoFlowControl);
+        connected = true;
+    }
+    else
+    {
+        serialPort->close();
+        throw(ErrorMessage("Keyboard: serial port error " + QString::number(serialPort->error())));
+    }
+}
+
+LivingKeyboard::~LivingKeyboard()
+{
+    serialPort->close();
+    delete serialPort;
+}
+
+void LivingKeyboard::rotation(LivingKeyboard::tilt t, unsigned int angle)
+{
+    if (connected)
+    {
+        int a = angle;
+        if (t == LEFT)
+            a *= -1;
+        QString buffer = "R" + QString::number(a) + "\n";
+        serialPort->write(buffer.toStdString().data(), strlen(buffer.toStdString().data()));
+    }
+}
+
+void LivingKeyboard::translation(LivingKeyboard::direction d, unsigned int distance)
+{
+    if (connected)
+    {
+        int dis = distance;
+        if (d == BACKWARD)
+            dis *= -1;
+        QString buffer = "T" + QString::number(dis) + "\n";
+        serialPort->write(buffer.toStdString().data(), strlen(buffer.toStdString().data()));
+    }
+}
diff --git a/Library/LivingKeyboard.h b/Library/LivingKeyboard.h
new file mode 100644 (file)
index 0000000..681034b
--- /dev/null
@@ -0,0 +1,26 @@
+#ifndef LIVINGKEYBOARD_H
+#define LIVINGKEYBOARD_H
+
+#include "livingdesktoplibrary_global.h"
+#include <QSerialPort>
+#include <ErrorMessage.h>
+
+class LivingKeyboard
+{
+    public:
+        LivingKeyboard();
+
+        ~LivingKeyboard();
+
+        enum tilt{LEFT, RIGHT};
+        enum direction{FORWARD, BACKWARD};
+
+        void rotation(tilt t, unsigned int angle);
+        void translation(direction d, unsigned int distance);
+
+    private:
+        QSerialPort* serialPort;
+        bool connected;
+};
+
+#endif // LIVINGKEYBOARD_H
index 2853692e62c988982f377bb35f32032b3f939255..eecbd77cee56011a107ceebe81833e81c787dc5b 100644 (file)
@@ -23,14 +23,20 @@ XYPlotter::XYPlotter(QString port)
     }
 }
 
+XYPlotter::~XYPlotter()
+{
+    serialPort->close();
+    delete serialPort;
+}
+
 void XYPlotter::moveTo(unsigned int x, unsigned int y, bool toolup=true)
 {
     if (connected)
     {
         QString buffer = "G01X" + QString::number(x) +
                 "Y" + QString::number(y) +
-                "Z" + QString::number(toolup * toolDistance) +
-                "F50000\n";
+                "Z" + QString::number(toolup * toolDistance) + "\n";
+                //"F50000\n";
         serialPort->write(buffer.toStdString().data(), strlen(buffer.toStdString().data()));
         posX = x;
         posY = y;
index 603155089c881dbb802a6c8e1449384767613e7d..a3064c03ad44513c7450ee4f9d3b56c923f83c28 100644 (file)
@@ -9,6 +9,7 @@ class LIVINGDESKTOPLIBRARYSHARED_EXPORT XYPlotter
 {
     public:
         XYPlotter(QString port);
+        ~XYPlotter();
 
         void moveTo(unsigned int x, unsigned int y, bool toolup);