Files
MinumelArmMCU/AdafruitServoMotor.hpp
T

43 lines
1.7 KiB
C++
Raw Normal View History

2025-12-14 15:02:55 +01:00
#pragma once
#include <Arduino.h>
#include "RangedMotor.hpp"
#include "AdafruitPWMBoard.hpp"
class AdafruitServoMotor : public RangedMotor
{
public:
AdafruitServoMotor(
AdafruitPWMBoard& driver,
const uint8_t motorIndex,
const float gearboxRatio,
const float scaleCalibration,
const float zeroOffset,
const float minSafeAngle,
const float maxSafeAngle,
const float specRangeDeg, // 180 for a 180-deg-servo, 270 for a 270-deg-servo, etc ...
2025-12-14 18:57:28 +01:00
const float initialPosition = 0,
2025-12-14 15:02:55 +01:00
const uint16_t minPulseLength = 125,
2025-12-14 18:57:28 +01:00
const uint16_t maxPulseLength = 625
2025-12-14 15:02:55 +01:00
) noexcept;
2025-12-14 19:28:41 +01:00
AdafruitServoMotor(const AdafruitServoMotor& other) = delete;
AdafruitServoMotor(AdafruitServoMotor&& other) noexcept = default;
2025-12-14 15:02:55 +01:00
// Returns true if the motion is accepted, false if it is outside of safe bounds.
2025-12-14 18:57:28 +01:00
// If out of safe bounds, maximum or minimun safe value will be assumen
2025-12-14 15:02:55 +01:00
bool moveTo(const float target) noexcept override;
float getPosition() const noexcept override { return hardwareAngleToVirtualAngle(currentHardwarePosition); };
// Will apply the current position immediately, no matter how small the move might be.
void commitMoveImmediately() noexcept;
protected:
uint16_t hardwareAngleToPulse(const float hardwareAngle) const noexcept;
AdafruitPWMBoard& driver;
const uint8_t motorIndex;
float currentHardwarePosition = 0;
float lastCommittedHardwarePosition = 0;
float specRangeDeg;
2025-12-14 18:57:28 +01:00
const uint16_t minPulseLength;
const uint16_t maxPulseLength;
2025-12-14 15:02:55 +01:00
};