WPILibC++ 2024.3.2
Servo.h
Go to the documentation of this file.
1// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
4
5#pragma once
6
7#include <units/angle.h>
8
9#include "frc/PWM.h"
10
11namespace frc {
12
13/**
14 * Standard hobby style servo.
15 *
16 * The range parameters default to the appropriate values for the Hitec HS-322HD
17 * servo provided in the FIRST Kit of Parts in 2008.
18 */
19class Servo : public PWM {
20 public:
21 /**
22 * Constructor.
23 *
24 * By default, 2.4 ms is used as the max PWM value and 0.6 ms is used as the
25 * min PWM value.
26 *
27 * @param channel The PWM channel to which the servo is attached. 0-9 are
28 * on-board, 10-19 are on the MXP port
29 */
30 explicit Servo(int channel);
31
32 Servo(Servo&&) = default;
33 Servo& operator=(Servo&&) = default;
34
35 /**
36 * Set the servo position.
37 *
38 * Servo values range from 0.0 to 1.0 corresponding to the range of full left
39 * to full right.
40 *
41 * @param value Position from 0.0 to 1.0.
42 */
43 void Set(double value);
44
45 /**
46 * Set the servo to offline.
47 *
48 * Set the servo raw value to 0 (undriven)
49 */
50 void SetOffline();
51
52 /**
53 * Get the servo position.
54 *
55 * Servo values range from 0.0 to 1.0 corresponding to the range of full left
56 * to full right. This returns the commanded position, not the position that
57 * the servo is actually at, as the servo does not report its own position.
58 *
59 * @return Position from 0.0 to 1.0.
60 */
61 double Get() const;
62
63 /**
64 * Set the servo angle.
65 *
66 * The angles are based on the HS-322HD Servo, and have a range of 0 to 180
67 * degrees.
68 *
69 * Servo angles that are out of the supported range of the servo simply
70 * "saturate" in that direction. In other words, if the servo has a range of
71 * (X degrees to Y degrees) than angles of less than X result in an angle of
72 * X being set and angles of more than Y degrees result in an angle of Y being
73 * set.
74 *
75 * @param angle The angle in degrees to set the servo.
76 */
77 void SetAngle(double angle);
78
79 /**
80 * Get the servo angle.
81 *
82 * This returns the commanded angle, not the angle that the servo is actually
83 * at, as the servo does not report its own angle.
84 *
85 * @return The angle in degrees to which the servo is set.
86 */
87 double GetAngle() const;
88
89 /**
90 * Get the maximum angle of the servo.
91 *
92 * @return The maximum angle of the servo in degrees.
93 */
94 double GetMaxAngle() const;
95
96 /**
97 * Get the minimum angle of the servo.
98 *
99 * @return The minimum angle of the servo in degrees.
100 */
101 double GetMinAngle() const;
102
103 void InitSendable(wpi::SendableBuilder& builder) override;
104
105 private:
106 double GetServoAngleRange() const;
107
108 static constexpr double kMaxServoAngle = 180.;
109 static constexpr double kMinServoAngle = 0.0;
110
111 static constexpr units::millisecond_t kDefaultMaxServoPWM = 2.4_ms;
112 static constexpr units::millisecond_t kDefaultMinServoPWM = 0.6_ms;
113};
114
115} // namespace frc
Class implements the PWM generation in the FPGA.
Definition: PWM.h:26
Standard hobby style servo.
Definition: Servo.h:19
Servo(int channel)
Constructor.
void InitSendable(wpi::SendableBuilder &builder) override
Initializes this Sendable object.
double Get() const
Get the servo position.
double GetMaxAngle() const
Get the maximum angle of the servo.
Servo & operator=(Servo &&)=default
void SetOffline()
Set the servo to offline.
double GetMinAngle() const
Get the minimum angle of the servo.
double GetAngle() const
Get the servo angle.
void Set(double value)
Set the servo position.
void SetAngle(double angle)
Set the servo angle.
Servo(Servo &&)=default
Helper class for building Sendable dashboard representations.
Definition: SendableBuilder.h:21
Definition: AprilTagPoseEstimator.h:15