|
17 | 17 | DUTY_STEPS = 100 # How many duty cycle steps to sample the speed of |
18 | 18 | SETTLE_TIME = 0.1 # How long to wait after changing motor duty cycle |
19 | 19 | CAPTURE_TIME = 0.2 # How long to capture the motor's speed at each step |
20 | | -SLEEP_STEP = 0.02 # The time between taking counts during counting_sleep() |
21 | 20 |
|
22 | 21 | # Create a new InventorHATMini and get a motor and encoder from it |
23 | 22 | board = InventorHATMini(motor_gear_ratio=GEAR_RATIO, init_leds=False) |
|
34 | 33 | enc.direction(DIRECTION) |
35 | 34 |
|
36 | 35 |
|
37 | | -# The encoder needs to be read semi-regularly to avoid the chip's internal counter |
38 | | -# from wrapping and giving us wrong speed measurements, so use this instead of time.sleep() |
39 | | -def counting_sleep(duration): |
40 | | - start = time.monotonic() |
41 | | - while time.monotonic() - start < duration: |
42 | | - enc.count() # We don't do anything with the returned count |
43 | | - time.sleep(SLEEP_STEP) |
44 | | - |
45 | | - |
46 | 36 | # Function that performs a single profiling step |
47 | 37 | def profile_at_duty(duty): |
48 | 38 | # Set the motor to a new duty cycle and wait for it to settle |
49 | 39 | if DIRECTION == 1: |
50 | 40 | m.duty(0.0 - duty) |
51 | 41 | else: |
52 | 42 | m.duty(duty) |
53 | | - counting_sleep(SETTLE_TIME) |
| 43 | + time.sleep(SETTLE_TIME) |
54 | 44 |
|
55 | 45 | # Perform a dummy capture to clear the encoder |
56 | | - enc.capture(SETTLE_TIME) |
| 46 | + enc.capture() |
57 | 47 |
|
58 | 48 | # Wait for the capture time to pass |
59 | | - counting_sleep(CAPTURE_TIME) |
| 49 | + time.sleep(CAPTURE_TIME) |
60 | 50 |
|
61 | 51 | # Perform a capture and read the measured speed |
62 | | - capture = enc.capture(CAPTURE_TIME) |
| 52 | + capture = enc.capture() |
63 | 53 | measured_speed = capture.revolutions_per_second |
64 | 54 |
|
65 | 55 | # These are some alternate speed measurements from the encoder |
|
0 commit comments