--- /dev/null
+#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);
+}
+
+
+
--- /dev/null
+//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);
+}
+
--- /dev/null
+#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()));
+ }
+}