本文整理汇总了Python中motor.Motor.set_value方法的典型用法代码示例。如果您正苦于以下问题:Python Motor.set_value方法的具体用法?Python Motor.set_value怎么用?Python Motor.set_value使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类motor.Motor
的用法示例。
在下文中一共展示了Motor.set_value方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: ControlThread
# 需要导入模块: from motor import Motor [as 别名]
# 或者: from motor.Motor import set_value [as 别名]
class ControlThread(Thread):
def __init__(self, config):
Thread.__init__(self)
self.angle = 0
self.K = 0.98
self.logDataSetBuffer = []
self.Kp = config.config.as_float('Kp')
self.Ki = config.config.as_float('Ki')
self.Kd = config.config.as_float('Kd')
self.set_point = config.config.as_float('set_point')
self.disable_motors = config.config.as_bool('disable_motors')
self.integral_error = 0
self.last_error = 0
self.motor_left = Motor("L", port_motor_left_pwm, port_motor_left_backward, port_motor_left_forward)
self.motor_right = Motor("R", port_motor_right_pwm, port_motor_right_backward, port_motor_right_forward)
self.accelerometer = MPU6050()
self.last_time = time()
self.logger = logging.getLogger(__name__)
self.setDaemon(True)
self.latest_sensor = 0
self.logger.info('Initialized ControlThread with the following settings')
self.logger.info('disable_motors={}'.format(self.disable_motors))
self.logger.info('set_point={:1.2f}'.format(self.set_point))
self.logger.info('Kp={:1.1f}'.format(self.Kp))
self.logger.info('Ki={:1.1f}'.format(self.Ki))
self.logger.info('Kd={:1.1f}'.format(self.Kd))
def run(self):
while True:
self.perform_one_step()
def perform_one_step(self):
# create an object for logging the current sensors' data and current control data
currentLogDataSet = LogDataSet()
# read sensors
try:
# this gives IOError sometimes
axes = self.accelerometer.getAxes(True)
except (IOError):
# error, do nothing, try again
return
self.latest_sensor = axes
z = axes['z']
x = axes['y']
accelerometerAngle = atan2(x, -z)
# read gyroscope
gyroscopeRate = -axes['gx'] / 180 * math.pi
# log the values from the sensors
currentLogDataSet.setSensorsValues(axes, gyroscopeRate)
# calculate dt based on the current time and the previous measurement time
now = time()
dt = now - self.last_time
self.dt = dt
self.last_time = now
# complementary filter
self.angle = self.K * (self.angle + gyroscopeRate * dt) + (1 - self.K) * accelerometerAngle
# PID
error = self.angle - self.set_point
self.integral_error += error * dt
differential_error = (error - self.last_error) / dt
self.last_error = error
up = self.Kp * error
ui = self.Ki * self.integral_error
ud = self.Kd * differential_error
u = up + ui + ud
self.u = u
# log the calculated control values
currentLogDataSet.setControlValues(accelerometerAngle, self.angle, error, self.integral_error, differential_error, u, dt)
# append the current logDataSet object to the logging-buffer
self.logDataSetBuffer.append(currentLogDataSet)
# limit the size of the buffer
if len(self.logDataSetBuffer) > 500:
del self.logDataSetBuffer[0]
self.logger.debug(
'x={:5.2f} z={:5.2f} gy={:7.2f} accelAngle={:5.2f} gyrAngle={:6.3f} angle={:6.3f} e={:6.3f} ie={:6.3f} de={:5.2f} up={:5.2f} ui={:5.2f} ud={:5.2f} u={:5.2f} dt={:3.0f}'
.format(
x, z, gyroscopeRate, accelerometerAngle, gyroscopeRate * dt, self.angle, error, self.integral_error, differential_error, up, ui, ud, u, dt * 1000))
# control the motors
if not self.disable_motors:
self.motor_left.set_value(u, dt)
self.motor_right.set_value(u, dt)
#.........这里部分代码省略.........