Files
MotionCore/MotionCore/Motor.h
T
2025-12-08 00:59:09 +01:00

66 lines
2.6 KiB
C++
Executable File

#pragma once
#include "OSerialBus.h"
#include <optional>
class Motor
{
public:
Motor(
const uint8_t index,
OSerialBus& serialBus,
const double initialPosition,
const double mechanicalMinPosition,
const double mechanicalMaxPosition,
const double initialSpeed = 0.5
) noexcept;
Motor(const Motor& other) = delete;
Motor(Motor&& other) noexcept;
// Will move the motor to the desired position with ramp-up and controlled speed
bool moveTo(const double theta, const std::optional<double> speed) noexcept;
// Will move the motor instantly to the desired position (this is jerky and may damage the hardware long-term)
bool moveToImmediate(const double theta) noexcept;
// Will move the motor by a specified angle with ramp-up and controlled speed
bool moveBy(const double theta, const std::optional<double> speed) noexcept;
// Will move the motor by a specified angle instantly (this is jerky and may damage the hardware long-term)
bool moveByImmediate(const double theta) noexcept;
bool stopMoving() noexcept;
void pauseMoving() noexcept;
void resumeMoving() noexcept;
// Will move the motor to its current target instantly (this is jerky and may damage the hardware long-term)
bool assumeTargetPositionImmediately() noexcept;
void update(const double frametime);
std::size_t getIndex() const { return index; };
double getCurrentPosition() const { return currentPosition; };
double getTargetPosition() const { return targetPosition; };
double getSpeed() const { return targetSpeed; };
void setSpeed(const double speedTarget) noexcept;
double getMechanicalMinPosition() const { return mechanicalMinPosition; };
double getMechanicalMaxPosition() const { return mechanicalMaxPosition; };
double getPaused() const { return paused; };
bool isPositionInMechanicalRange(const double pos) const noexcept;
private:
// Update-handler for moving
void handleMovement(const double frametime);
// Sends a move absolute command to the serial busses MCU
bool sendMACommand(const double targetPos) noexcept;
// Will calculate the current speed for rampup based on currentPosition, moveStartPosition, and targetPosition
double calculateCurrentSpeed() const noexcept;
const uint8_t index;
OSerialBus& serialBus;
double currentPosition; // we are here
double moveStartPosition; // last move instruction started here
double targetPosition; // movement should end here
double targetSpeed; // Motor should ideally move at this speed
double speedLow; // End- and startpoint of ramp-up phase
const double mechanicalMinPosition;
const double mechanicalMaxPosition;
bool disabled = false;
bool paused = false;
};