Benutzer-Werkzeuge

Webseiten-Werkzeuge


mechatronik:sose24:remote-plotter:code

Unterschiede

Hier werden die Unterschiede zwischen zwei Versionen gezeigt.

Link zu dieser Vergleichsansicht

Beide Seiten der vorigen Revision Vorhergehende Überarbeitung
mechatronik:sose24:remote-plotter:code [2024/07/31 10:26]
TPfoch [Motor]
mechatronik:sose24:remote-plotter:code [2024/07/31 10:29] (aktuell)
TPfoch [Backend]
Zeile 44: Zeile 44:
 } }
 </​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===== =====Frontend=====
mechatronik/sose24/remote-plotter/code.1722414410.txt.gz · Zuletzt geändert: 2024/07/31 10:26 von TPfoch