blob: 8acb0d7be030df00462f9775cb2cc1ad5c2c9daa [file] [log] [blame]
jerrymf1579332013-02-07 01:56:28 +00001/*----------------------------------------------------------------------------*/
2/* Copyright (c) FIRST 2008. All Rights Reserved. */
3/* Open Source Software - may be modified and shared by FRC teams. The code */
4/* must be accompanied by the FIRST BSD license file in $(WIND_BASE)/WPILib. */
5/*----------------------------------------------------------------------------*/
6
7#include "Servo.h"
8
9#include "NetworkCommunication/UsageReporting.h"
10#include "LiveWindow/LiveWindow.h"
11
12const float Servo::kMaxServoAngle;
13const float Servo::kMinServoAngle;
14
15/**
16 * Common initialization code called by all constructors.
17 *
18 * InitServo() assigns defaults for the period multiplier for the servo PWM control signal, as
19 * well as the minimum and maximum PWM values supported by the servo.
20 */
21void Servo::InitServo()
22{
23 // TODO: compute the appropriate values based on digital loop timing
24 SetBounds(245, 0, 0, 0, 11);
25 SetPeriodMultiplier(kPeriodMultiplier_4X);
26
27
28 LiveWindow::GetInstance()->AddActuator("Servo", GetModuleNumber(), GetChannel(), this);
29 nUsageReporting::report(nUsageReporting::kResourceType_Servo, GetChannel(), GetModuleNumber() - 1);
30}
31
32/**
33 * Constructor that assumes the default digital module.
34 *
35 * @param channel The PWM channel on the digital module to which the servo is attached.
36 */
37Servo::Servo(UINT32 channel) : SafePWM(channel)
38{
39 InitServo();
40}
41
42/**
43 * Constructor that specifies the digital module.
44 *
45 * @param moduleNumber The digital module (1 or 2).
46 * @param channel The PWM channel on the digital module to which the servo is attached (1..10).
47 */
48Servo::Servo(UINT8 moduleNumber, UINT32 channel) : SafePWM(moduleNumber, channel)
49{
50 InitServo();
51}
52
53Servo::~Servo()
54{
55}
56
57/**
58 * Set the servo position.
59 *
60 * Servo values range from 0.0 to 1.0 corresponding to the range of full left to full right.
61 *
62 * @param value Position from 0.0 to 1.0.
63 */
64void Servo::Set(float value)
65{
66 SetPosition(value);
67}
68
69/**
70 * Set the servo to offline.
71 *
72 * Set the servo raw value to 0 (undriven)
73 */
74void Servo::SetOffline() {
75 SetRaw(0);
76}
77
78/**
79 * Get the servo position.
80 *
81 * Servo values range from 0.0 to 1.0 corresponding to the range of full left to full right.
82 *
83 * @return Position from 0.0 to 1.0.
84 */
85float Servo::Get()
86{
87 return GetPosition();
88}
89
90/**
91 * Set the servo angle.
92 *
93 * Assume that the servo angle is linear with respect to the PWM value (big assumption, need to test).
94 *
95 * Servo angles that are out of the supported range of the servo simply "saturate" in that direction
96 * In other words, if the servo has a range of (X degrees to Y degrees) than angles of less than X
97 * result in an angle of X being set and angles of more than Y degrees result in an angle of Y being set.
98 *
99 * @param degrees The angle in degrees to set the servo.
100 */
101void Servo::SetAngle(float degrees)
102{
103 if (degrees < kMinServoAngle)
104 {
105 degrees = kMinServoAngle;
106 }
107 else if (degrees > kMaxServoAngle)
108 {
109 degrees = kMaxServoAngle;
110 }
111
112 SetPosition(((float) (degrees - kMinServoAngle)) / GetServoAngleRange());
113}
114
115/**
116 * Get the servo angle.
117 *
118 * Assume that the servo angle is linear with respect to the PWM value (big assumption, need to test).
119 * @return The angle in degrees to which the servo is set.
120 */
121float Servo::GetAngle()
122{
123 return (float)GetPosition() * GetServoAngleRange() + kMinServoAngle;
124}
125
126void Servo::ValueChanged(ITable* source, const std::string& key, EntryValue value, bool isNew) {
127 Set(value.f);
128}
129
130void Servo::UpdateTable() {
131 if (m_table != NULL) {
132 m_table->PutNumber("Value", Get());
133 }
134}
135
136void Servo::StartLiveWindowMode() {
137 m_table->AddTableListener("Value", this, true);
138}
139
140void Servo::StopLiveWindowMode() {
141 m_table->RemoveTableListener(this);
142}
143
144std::string Servo::GetSmartDashboardType() {
145 return "Servo";
146}
147
148void Servo::InitTable(ITable *subTable) {
149 m_table = subTable;
150 UpdateTable();
151}
152
153ITable * Servo::GetTable() {
154 return m_table;
155}
156
157