55# the WPILib BSD license file in the root directory of this project. 
66# 
77
8+ """ 
9+ This example shows how to use a duty cycle encoder for devices such as 
10+ an arm or elevator. 
11+ """ 
12+ 
813import  wpilib 
14+ import  wpimath 
15+ 
16+ FULL_RANGE  =  1.3 
17+ EXPECTED_ZERO  =  0.0 
918
1019
1120class  MyRobot (wpilib .TimedRobot ):
1221    def  robotInit (self ):
13-         """Robot initialization function """ 
22+         """Called once at the beginning of the robot program. """ 
1423
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 )
1631
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 )
1844
1945    def  robotPeriodic (self ):
2046        # Connected can be checked, and uses the frequency of the encoder 
@@ -26,10 +52,22 @@ def robotPeriodic(self):
2652        # Output of encoder 
2753        output  =  self .dutyCycleEncoder .get ()
2854
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+         )
3169
3270        wpilib .SmartDashboard .putBoolean ("Connected" , connected )
3371        wpilib .SmartDashboard .putNumber ("Frequency" , frequency )
3472        wpilib .SmartDashboard .putNumber ("Output" , output )
35-         wpilib .SmartDashboard .putNumber ("Distance " , distance )
73+         wpilib .SmartDashboard .putNumber ("ShiftedOutput " , shiftedOutput )
0 commit comments