5
5
# the WPILib BSD license file in the root directory of this project.
6
6
#
7
7
8
+ """
9
+ This example shows how to use a duty cycle encoder for devices such as
10
+ an arm or elevator.
11
+ """
12
+
8
13
import wpilib
14
+ import wpimath
15
+
16
+ FULL_RANGE = 1.3
17
+ EXPECTED_ZERO = 0.0
9
18
10
19
11
20
class MyRobot (wpilib .TimedRobot ):
12
21
def robotInit (self ):
13
- """Robot initialization function """
22
+ """Called once at the beginning of the robot program. """
14
23
15
- self .dutyCycleEncoder = wpilib .DutyCycleEncoder (0 )
24
+ # 2nd parameter is the range of values. This sensor will output between
25
+ # 0 and the passed in value.
26
+ # 3rd parameter is the the physical value where you want "0" to be. How
27
+ # to measure this is fairly easy. Set the value to 0, place the mechanism
28
+ # where you want "0" to be, and observe the value on the dashboard, That
29
+ # is the value to enter for the 3rd parameter.
30
+ self .dutyCycleEncoder = wpilib .DutyCycleEncoder (0 , FULL_RANGE , EXPECTED_ZERO )
16
31
17
- self .dutyCycleEncoder .setDistancePerRotation (0.5 )
32
+ # If you know the frequency of your sensor, uncomment the following
33
+ # method, and set the method to the frequency of your sensor.
34
+ # This will result in more stable readings from the sensor.
35
+ # Do note that occasionally the datasheet cannot be trusted
36
+ # and you should measure this value. You can do so with either
37
+ # an oscilloscope, or by observing the "Frequency" output
38
+ # on the dashboard while running this sample. If you find
39
+ # the value jumping between the 2 values, enter halfway between
40
+ # those values. This number doesn't have to be perfect,
41
+ # just having a fairly close value will make the output readings
42
+ # much more stable.
43
+ self .dutyCycleEncoder .setAssumedFrequency (967.8 )
18
44
19
45
def robotPeriodic (self ):
20
46
# Connected can be checked, and uses the frequency of the encoder
@@ -26,10 +52,22 @@ def robotPeriodic(self):
26
52
# Output of encoder
27
53
output = self .dutyCycleEncoder .get ()
28
54
29
- # Output scaled by DistancePerPulse
30
- distance = self .dutyCycleEncoder .getDistance ()
55
+ # By default, the output will wrap around to the full range value
56
+ # when the sensor goes below 0. However, for moving mechanisms this
57
+ # is not usually ideal, as if 0 is set to a hard stop, its still
58
+ # possible for the sensor to move slightly past. If this happens
59
+ # The sensor will assume its now at the furthest away position,
60
+ # which control algorithms might not handle correctly. Therefore
61
+ # it can be a good idea to slightly shift the output so the sensor
62
+ # can go a bit negative before wrapping. Usually 10% or so is fine.
63
+ # This does not change where "0" is, so no calibration numbers need
64
+ # to be changed.
65
+ percentOfRange = FULL_RANGE * 0.1
66
+ shiftedOutput = wpimath .inputModulus (
67
+ output , 0 - percentOfRange , FULL_RANGE - percentOfRange
68
+ )
31
69
32
70
wpilib .SmartDashboard .putBoolean ("Connected" , connected )
33
71
wpilib .SmartDashboard .putNumber ("Frequency" , frequency )
34
72
wpilib .SmartDashboard .putNumber ("Output" , output )
35
- wpilib .SmartDashboard .putNumber ("Distance " , distance )
73
+ wpilib .SmartDashboard .putNumber ("ShiftedOutput " , shiftedOutput )
0 commit comments