.. _program_listing_file_src_tap_motor_servo.cpp: Program Listing for File servo.cpp ================================== |exhale_lsh| :ref:`Return to documentation for file ` (``src/tap/motor/servo.cpp``) .. |exhale_lsh| unicode:: U+021B0 .. UPWARDS ARROW WITH TIP LEFTWARDS .. code-block:: cpp /* * Copyright (c) 2020-2021 Advanced Robotics at the University of Washington * * This file is part of Taproot. * * Taproot is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * Taproot is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Taproot. If not, see . */ #include "servo.hpp" #include "tap/algorithms/math_user_utils.hpp" #include "tap/architecture/clock.hpp" #include "tap/drivers.hpp" #include "tap/errors/create_errors.hpp" namespace tap { namespace motor { Servo::Servo( Drivers *drivers, tap::gpio::Pwm::Pin pwmPin, float maximumPwm, float minimumPwm, float pwmRampSpeed) : drivers(drivers), pwmOutputRamp(0.0f), maxPwm(tap::algorithms::limitVal(maximumPwm, 0.0f, 1.0f)), minPwm(tap::algorithms::limitVal(minimumPwm, 0.0f, 1.0f)), pwmRampSpeed(pwmRampSpeed), prevTime(0), servoPin(pwmPin) { if (maxPwm < minPwm) { minPwm = 0.0f; maxPwm = 1.0f; RAISE_ERROR(drivers, "min servo PWM > max servo PWM"); } } void Servo::setTargetPwm(float pwm) { pwmOutputRamp.setTarget(tap::algorithms::limitVal(pwm, minPwm, maxPwm)); prevTime = tap::arch::clock::getTimeMilliseconds(); } void Servo::updateSendPwmRamp() { uint32_t currTime = tap::arch::clock::getTimeMilliseconds(); pwmOutputRamp.update(pwmRampSpeed * (currTime - prevTime)); prevTime = currTime; currentPwm = pwmOutputRamp.getValue(); drivers->pwm.write(pwmOutputRamp.getValue(), servoPin); } float Servo::getPWM() const { return currentPwm; } float Servo::getMinPWM() const { return minPwm; } float Servo::getMaxPWM() const { return maxPwm; } bool Servo::isRampTargetMet() const { return pwmOutputRamp.isTargetReached(); } } // namespace motor } // namespace tap