Hier werden die Unterschiede zwischen zwei Versionen gezeigt.
Nächste Überarbeitung | Vorhergehende Überarbeitung | ||
mechatronik:sose24:remote-plotter:code [2024/07/26 00:45] Paul-Hoeft angelegt |
mechatronik:sose24:remote-plotter:code [2024/07/31 10:29] (aktuell) TPfoch [Backend] |
||
---|---|---|---|
Zeile 1: | Zeile 1: | ||
- | <file C++ controls_mtrobo.cpp> | + | ======Code====== |
+ | Falls Github spinnt :) | ||
+ | =====Backend===== | ||
+ | <file C++ main.cpp> | ||
+ | #include "DrawRobot.hpp" | ||
+ | #define cm | ||
+ | |||
+ | DrawRobot<Arm> robot; | ||
+ | |||
+ | void setup() | ||
+ | { | ||
+ | Serial.begin(115200); | ||
+ | |||
+ | Motor root(AccelStepper(8,4,6,5,7), 4076); | ||
+ | Motor ext(AccelStepper(8,8,9,10,11), 4076); | ||
+ | |||
+ | robot = { | ||
+ | .axies = Arm(11 cm, 11.5 cm, root, ext), | ||
+ | .gantry = Gantry(6, 7) | ||
+ | }; | ||
+ | |||
+ | // robot.axies.moveTo({22.5,0}); | ||
+ | robot.axies.moveTo({11,11.5}); | ||
+ | |||
+ | } | ||
+ | void loop() | ||
+ | { | ||
+ | int cmd = Serial.parseInt(); | ||
+ | switch(cmd){ | ||
+ | case 0:{ | ||
+ | int x = Serial.parseInt(); | ||
+ | int y = Serial.parseInt(); | ||
+ | robot.axies.moveTo({x,y}); | ||
+ | break; | ||
+ | } | ||
+ | case 1: { | ||
+ | int tool = Serial.parseInt(); | ||
+ | robot.gantry.setTool(tool); // ist tool | ||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | } | ||
</file> | </file> | ||
+ | ====Axies==== | ||
+ | <file C++ Axies.hpp> | ||
+ | #pragma once | ||
+ | #include "AccelStepper.h" | ||
+ | #include "MultiStepper.h" | ||
+ | |||
+ | #ifndef PI | ||
+ | #define PI 3.14159265358979 | ||
+ | #endif | ||
+ | |||
+ | struct Point {double x,y;}; | ||
+ | struct Angles {double root, ext;}; | ||
+ | |||
+ | class Axies | ||
+ | { | ||
+ | public: | ||
+ | virtual void moveTo(Point p)=0; | ||
+ | }; | ||
+ | |||
+ | struct Motor | ||
+ | { | ||
+ | AccelStepper stepper; | ||
+ | int stepsPerRev=-1; | ||
+ | Motor(AccelStepper stepper, int stepsPerRev, int speed=4000): | ||
+ | stepper(stepper), stepsPerRev(stepsPerRev) | ||
+ | { | ||
+ | this->stepper.setAcceleration(1000); | ||
+ | this->stepper.setMaxSpeed(speed); | ||
+ | this->stepper.setSpeed(speed); | ||
+ | } | ||
+ | |||
+ | long getSteps(double deg) | ||
+ | { | ||
+ | return deg/360.0*stepsPerRev; | ||
+ | } | ||
+ | }; | ||
+ | |||
+ | class Arm | ||
+ | { | ||
+ | double rootLen, extLen; | ||
+ | Motor root, ext; | ||
+ | MultiStepper ms; | ||
+ | public: | ||
+ | Arm(double rootLen, double extLen, Motor root, Motor ext); | ||
+ | void moveTo(Point p); | ||
+ | private: | ||
+ | //calculates the angles of the two sections so the end of the second is at point p | ||
+ | Angles pointToAngles(Point p); | ||
+ | }; | ||
+ | </file> | ||
+ | <file C++ Axies.cpp> | ||
+ | #include "Axies.hpp" | ||
+ | |||
+ | Arm::Arm(double rootLen, double extLen, Motor root, Motor ext) : | ||
+ | rootLen(rootLen), extLen(extLen), ms(), root(root), ext(ext) | ||
+ | { | ||
+ | ms.addStepper(root.stepper); | ||
+ | ms.addStepper(ext.stepper); | ||
+ | } | ||
+ | |||
+ | |||
+ | void Arm::moveTo(Point p) | ||
+ | { | ||
+ | Angles angle = pointToAngles(p); // 0-360 | ||
+ | Serial.println("Root: " + String(angle.root) + "°->" + String(root.getSteps(angle.root))); | ||
+ | Serial.println("Ext: " + String(angle.ext) + "°->" + String(ext.getSteps(angle.root))); | ||
+ | long pos[2] = {root.getSteps(angle.root), ext.getSteps(angle.ext)}; | ||
+ | ms.moveTo(pos); | ||
+ | ms.runSpeedToPosition(); | ||
+ | } | ||
+ | |||
+ | Angles Arm::pointToAngles(Point p) | ||
+ | { | ||
+ | double ph2= acos( | ||
+ | (p.x*p.x+p.y*p.y-rootLen*rootLen-extLen*extLen)/ | ||
+ | (2*rootLen*extLen)); | ||
+ | |||
+ | double phi1 = atan2(p.y,p.x) | ||
+ | -atan2(extLen*sin(ph2), | ||
+ | (rootLen+extLen*cos(ph2))); | ||
+ | |||
+ | return {phi1/PI*180, ph2/PI*180}; | ||
+ | } | ||
+ | </file> | ||
+ | ====Gantry==== | ||
+ | <file C++ Gantry.hpp> | ||
+ | #pragma once | ||
+ | #include <Servo.h> | ||
+ | |||
+ | class Gantry | ||
+ | { | ||
+ | Servo use, // servo for lowering tool | ||
+ | select;// Servo to switch between tools | ||
+ | // ^ currently unused | ||
+ | |||
+ | public: | ||
+ | Gantry(int usePin, int selectPin) | ||
+ | { | ||
+ | use.attach(usePin); | ||
+ | select.attach(selectPin); | ||
+ | } | ||
+ | enum Tool { | ||
+ | NONE, | ||
+ | PEN | ||
+ | }; | ||
+ | void setTool(Tool tool); | ||
+ | |||
+ | }; | ||
+ | </file> | ||
+ | <file C++ Gantry.cpp> | ||
+ | #include "Gantry.hpp" | ||
+ | |||
+ | void Gantry::setTool(Tool tool) | ||
+ | { | ||
+ | // maybe second servo for tool switch | ||
+ | |||
+ | // dummy implementation | ||
+ | use.write(180*(tool==PEN)); | ||
+ | } | ||
+ | </file> | ||
+ | |||
+ | |||
+ | =====Frontend===== | ||
+ | Dieses Programm ist mithilfe des Tkinter-Moduls geschrieben. Hiermt hat man eine Zeichenoberfläche, welche die aktuelle Zeichnung speichern kann als auch die Koordinaten der Zeichnung während Laufzeit ausgibt und an den Arduino schicken kann.(?) | ||
<file python draw_mtrobo.py> | <file python draw_mtrobo.py> | ||
from tkinter import * | from tkinter import * |