-
Notifications
You must be signed in to change notification settings - Fork 22
Closed
Description
Having a simple application for learning PID control on an InventorHub's motor. Once I execute it, it prints out about 200 times and then stops. The program keeps on running on the hub, but the output is not printed into the terminal anymore.
About a 1 attempt out of 10 works well and the connection is not interrupted. Most of the time, however, it does not work.
Environment
- InventorHub
- macOS 12
pybricksdev v1.0.0a23- latest FW (pybricks-micropython,
1cba7db3485c336902ed4ab98f80f65cfea50ff0commit)
Replication steps
- Execute the code below by running
pybricksdev run ble derivative_control.py. The code increments a counter every time the while loop is at its end. - Actual result: About 200 lines are printed only, while the code execution goes on and the motor is turning.
- Expected result: The connection with the Hub shall not be terminated.
The code
from math import trunc
from pybricks.parameters import Port
from pybricks.pupdevices import Motor
motor = Motor(Port.A)
# the ranges observed on a real Inventor motor
duty_cycle_range = range(0, 100)
speed_range = range(0, 1080)
dc_coef = duty_cycle_range.stop / speed_range.stop
def angle_to_dc(angle_speed_deg: int) -> int:
# bound the speed to the allowed range
speed = min(speed_range.stop, max(angle_speed_deg, speed_range.start))
# recalculate the speed to the duty cycle (linear to speed)
duty = trunc((speed - speed_range.start) * dc_coef + duty_cycle_range.start)
# bound the duty cycle to the allowed range
return min(duty_cycle_range.stop, max(duty, duty_cycle_range.start))
# proportional coefficient
kp = 0.5
# the desired target angle speed
set_point = 360
counter = 0
print("Counter, Speed, Duty cycle")
while True:
speed = motor.speed()
# the difference between set point and the actual motor speed
error = set_point - speed
# the proportional function
fp = trunc(error * kp)
command = set_point + fp
duty_cycle = angle_to_dc(command)
motor.dc(duty_cycle)
print(counter, ",", speed, ",", duty_cycle)
counter += 1Metadata
Metadata
Assignees
Labels
No labels