From a4c3d718f5e74ffa1e68aa88103db3844a863095 Mon Sep 17 00:00:00 2001 From: Mjolnir Date: Thu, 20 Aug 2015 16:48:02 +0200 Subject: [PATCH] keyboard --- ArduinoKeyboard/ArduinoKeyboard.ino | 87 +++++++++++++++++++++ ArduinoKeyboard/motorcontrol.ino | 117 ++++++++++++++++++++++++++++ Library/LivingDesktopLibrary.pro | 6 +- Library/LivingKeyboard.cpp | 50 ++++++++++++ Library/LivingKeyboard.h | 26 +++++++ Library/XYPlotter.cpp | 10 ++- Library/XYPlotter.h | 1 + 7 files changed, 293 insertions(+), 4 deletions(-) create mode 100644 ArduinoKeyboard/ArduinoKeyboard.ino create mode 100644 ArduinoKeyboard/motorcontrol.ino create mode 100644 Library/LivingKeyboard.cpp create mode 100644 Library/LivingKeyboard.h diff --git a/ArduinoKeyboard/ArduinoKeyboard.ino b/ArduinoKeyboard/ArduinoKeyboard.ino new file mode 100644 index 0000000..3c6fc3d --- /dev/null +++ b/ArduinoKeyboard/ArduinoKeyboard.ino @@ -0,0 +1,87 @@ +#include + +//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 index 0000000..9ea6a8b --- /dev/null +++ b/ArduinoKeyboard/motorcontrol.ino @@ -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); +} + diff --git a/Library/LivingDesktopLibrary.pro b/Library/LivingDesktopLibrary.pro index af9b885..07fb8e6 100644 --- a/Library/LivingDesktopLibrary.pro +++ b/Library/LivingDesktopLibrary.pro @@ -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 index 0000000..06be01d --- /dev/null +++ b/Library/LivingKeyboard.cpp @@ -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 index 0000000..681034b --- /dev/null +++ b/Library/LivingKeyboard.h @@ -0,0 +1,26 @@ +#ifndef LIVINGKEYBOARD_H +#define LIVINGKEYBOARD_H + +#include "livingdesktoplibrary_global.h" +#include +#include + +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 diff --git a/Library/XYPlotter.cpp b/Library/XYPlotter.cpp index 2853692..eecbd77 100644 --- a/Library/XYPlotter.cpp +++ b/Library/XYPlotter.cpp @@ -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; diff --git a/Library/XYPlotter.h b/Library/XYPlotter.h index 6031550..a3064c0 100644 --- a/Library/XYPlotter.h +++ b/Library/XYPlotter.h @@ -9,6 +9,7 @@ class LIVINGDESKTOPLIBRARYSHARED_EXPORT XYPlotter { public: XYPlotter(QString port); + ~XYPlotter(); void moveTo(unsigned int x, unsigned int y, bool toolup); -- 2.30.2