WPILibC++ 2023.4.3
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 "frc/PWM.h"
8
9namespace frc {
10
11/**
12 * Standard hobby style servo.
13 *
14 * The range parameters default to the appropriate values for the Hitec HS-322HD
15 * servo provided in the FIRST Kit of Parts in 2008.
16 */
17class Servo : public PWM {
18 public:
19 /**
20 * @param channel The PWM channel to which the servo is attached. 0-9 are
21 * on-board, 10-19 are on the MXP port
22 */
23 explicit Servo(int channel);
24
25 Servo(Servo&&) = default;
26 Servo& operator=(Servo&&) = default;
27
28 /**
29 * Set the servo position.
30 *
31 * Servo values range from 0.0 to 1.0 corresponding to the range of full left
32 * to full right.
33 *
34 * @param value Position from 0.0 to 1.0.
35 */
36 void Set(double value);
37
38 /**
39 * Set the servo to offline.
40 *
41 * Set the servo raw value to 0 (undriven)
42 */
43 void SetOffline();
44
45 /**
46 * Get the servo position.
47 *
48 * Servo values range from 0.0 to 1.0 corresponding to the range of full left
49 * to full right. This returns the commanded position, not the position that
50 * the servo is actually at, as the servo does not report its own position.
51 *
52 * @return Position from 0.0 to 1.0.
53 */
54 double Get() const;
55
56 /**
57 * Set the servo angle.
58 *
59 * The angles are based on the HS-322HD Servo, and have a range of 0 to 180
60 * degrees.
61 *
62 * Servo angles that are out of the supported range of the servo simply
63 * "saturate" in that direction. In other words, if the servo has a range of
64 * (X degrees to Y degrees) than angles of less than X result in an angle of
65 * X being set and angles of more than Y degrees result in an angle of Y being
66 * set.
67 *
68 * @param angle The angle in degrees to set the servo.
69 */
70 void SetAngle(double angle);
71
72 /**
73 * Get the servo angle.
74 *
75 * This returns the commanded angle, not the angle that the servo is actually
76 * at, as the servo does not report its own angle.
77 *
78 * @return The angle in degrees to which the servo is set.
79 */
80 double GetAngle() const;
81
82 /**
83 * Get the maximum angle of the servo.
84 *
85 * @return The maximum angle of the servo in degrees.
86 */
87 double GetMaxAngle() const;
88
89 /**
90 * Get the minimum angle of the servo.
91 *
92 * @return The minimum angle of the servo in degrees.
93 */
94 double GetMinAngle() const;
95
96 void InitSendable(wpi::SendableBuilder& builder) override;
97
98 private:
99 double GetServoAngleRange() const;
100
101 static constexpr double kMaxServoAngle = 180.0;
102 static constexpr double kMinServoAngle = 0.0;
103
104 static constexpr double kDefaultMaxServoPWM = 2.4;
105 static constexpr double kDefaultMinServoPWM = 0.6;
106};
107
108} // namespace frc
Class implements the PWM generation in the FPGA.
Definition: PWM.h:34
Standard hobby style servo.
Definition: Servo.h:17
Servo(int channel)
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
Definition: core.h:1240
Definition: SendableBuilder.h:18
Definition: AprilTagFieldLayout.h:22