本文整理汇总了Python中pid.PID.setSampleTime方法的典型用法代码示例。如果您正苦于以下问题:Python PID.setSampleTime方法的具体用法?Python PID.setSampleTime怎么用?Python PID.setSampleTime使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pid.PID
的用法示例。
在下文中一共展示了PID.setSampleTime方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from pid import PID [as 别名]
# 或者: from pid.PID import setSampleTime [as 别名]
class RobocapeController:
def __init__(self):
#initialize node
rospy.init_node('RoboCapeController')
rospy.loginfo(rospy.get_caller_id() + 'Initializing RoboCapeController node')
#initialize serial communication (with remote control)
self.dev = serial.Serial('/dev/ttyO2', 115200)
#initialize global variables
self.Roll = 0
self.RollRate = 0
self.ThrottleREMOTE = 0
self.SteeringREMOTE = 0
self.SteeringCONTROLdeg = 0
self.SteeringCONTROLpulse = 0
self.Kp = -0.654276982979989
self.Ki = -2.28191654162741
self.Kd = -0.0211479278661121
#initialize PID controller
self.pid = PID(self.Kp, self.Ki, self.Kd)
self.pid.SetPoint=0.0
self.pid.setSampleTime(1/30)
#initialize actuators
#Get all parameters from config (rosparam)
name = 'engine'
engine_output_pin = int(rospy.get_param('actuators/' + name + '/output_pin', 1))
engine_board_pin = int(rospy.get_param('actuators/' + name + '/board_pin', 60))
engine_period_us = int(1e6 / float(rospy.get_param('actuators/' + name + '/frequency', 60)))
name = 'steering'
steering_output_pin = int(rospy.get_param('actuators/' + name + '/output_pin', 1))
steering_board_pin = int(rospy.get_param('actuators/' + name + '/board_pin', 62))
steering_period_us = int(1e6 / float(rospy.get_param('actuators/' + name + '/frequency', 60)))
#Initialize PWM
self.dev1 = mraa.Pwm(engine_board_pin)
self.dev1.period_us(engine_period_us)
self.dev1.enable(True)
self.dev1.pulsewidth_us(1500)
self.dev2 = mraa.Pwm(steering_board_pin)
self.dev2.period_us(steering_period_us)
self.dev2.enable(True)
self.dev2.pulsewidth_us(1500)
def subscribe(self):
#subscribe to IMU messages
rospy.Subscriber('imu_readings', Imu, self.callbackIMU)
#subscribe to REMOTE messages
#rospy.Subscriber('remote_readings', Temperature, self.callbackREMOTE)
def callbackIMU(self, msg):
#save IMU messages into global variables
self.Roll = msg.orientation.x
self.RollRate = msg.angular_velocity.x
rospy.loginfo([self.Roll, self.RollRate])
def callbackREMOTE(self, msg):
#save REMOTE messages into global variables
self.ThrottleREMOTE = msg.temperature
self.SteeringREMOTE = msg.variance
rospy.loginfo([self.ThrottleREMOTE, self.SteeringREMOTE])
def publish(self):
pub = rospy.Publisher('controller_readings', Temperature, queue_size=1)
rate = rospy.Rate(40)
msg_temp = Temperature()
msg_temp.header.frame_id = "robocape"
while not rospy.is_shutdown():
now = rospy.get_rostime()
#read remote commands
while self.dev.inWaiting() <= 11:
pass
UART_READ = self.dev.readline()
self.dev.flushInput()
self.SteeringREMOTE = int(UART_READ[0:4])
self.ThrottleREMOTE = int(UART_READ[4:8])
rospy.loginfo([self.ThrottleREMOTE, self.SteeringREMOTE])
#controller
self.pid.update(self.Roll)
self.SteeringCONTROLdeg = self.pid.output
self.SteeringCONTROLpulse = ((self.SteeringCONTROLdeg + 66)*7.35294118)+1000
#steering saturation
if self.SteeringCONTROLpulse > 1800:
msg_temp.variance = 1800
elif self.SteeringCONTROLpulse < 1200:
msg_temp.variance = 1200
else:
msg_temp.variance = self.SteeringCONTROLpulse
#.........这里部分代码省略.........