|
@@ -9,42 +9,73 @@
|
|
|
20211005:初始化代码.
|
|
|
"""
|
|
|
|
|
|
-import os
|
|
|
-import rospy
|
|
|
+import os,rospy
|
|
|
import roslib; roslib.load_manifest('ros_arduino_python')
|
|
|
from std_msgs.msg import Float32
|
|
|
from sensor_msgs.msg import Imu
|
|
|
|
|
|
+
|
|
|
class BalanceMode:
|
|
|
def __init__(self):
|
|
|
- self.angularVal = 57.2957
|
|
|
+ self.angularVal = 57.296
|
|
|
+ self.encoderFilterPer = 0.7
|
|
|
|
|
|
self.pitchData = 0
|
|
|
self.angularPitch = 0
|
|
|
+ self.encoder_integral = 0
|
|
|
+ self.encoder_lowout = 0
|
|
|
+ self.downPitchData = 0
|
|
|
|
|
|
- self.middle_val = rospy.get_param("~middle_val", 4.3)
|
|
|
- self.balance_kp = rospy.get_param("~balance_kp", 1)
|
|
|
- self.balance_kd = rospy.get_param("~balance_kd", 1)
|
|
|
+ self.middle_val = rospy.get_param("~middle_val", 4.3)
|
|
|
+ self.downPitchData = rospy.get_param("~check_down_pitch", 18)
|
|
|
+ self.pitch_tolerance = rospy.get_param("~pitch_tolerance", 0.2)
|
|
|
+ self.balance_kp = rospy.get_param("~balance_kp", 17)
|
|
|
+ self.balance_kd = rospy.get_param("~balance_kd", 1)
|
|
|
+ self.speed_kp = rospy.get_param("~speed_kp", 1)
|
|
|
+ self.speed_ki = rospy.get_param("~speed_ki", 1)
|
|
|
|
|
|
rospy.Subscriber("/pitch_data", Float32, self.pitchDataProc)
|
|
|
rospy.Subscriber("/imu_data", Imu, self.angularProc)
|
|
|
|
|
|
def pitchDataProc(self, msg):
|
|
|
- self.pitchData = msg.data*self.angularVal
|
|
|
+ self.pitchData = msg.data * self.angularVal
|
|
|
|
|
|
def angularProc(self, msg):
|
|
|
- self.angularPitch = msg.angular_velocity.y*self.angularVal
|
|
|
-
|
|
|
- def keepBalance(self):
|
|
|
- pitch_err = self.pitchData - self.middle_val
|
|
|
- motorPWM = int(self.balance_kp*pitch_err + self.balance_kd*self.angularPitch)
|
|
|
-
|
|
|
-
|
|
|
- if motorPWM > 255:
|
|
|
- motorPWM = 255
|
|
|
- elif motorPWM < -255:
|
|
|
- motorPWM = -255
|
|
|
-
|
|
|
- rospy.loginfo("pitch=" + str(self.pitchData) + \
|
|
|
- " angular=" + str(self.angularPitch) + " PWM=" + str(motorPWM))
|
|
|
+ self.angularPitch = msg.angular_velocity.y * self.angularVal
|
|
|
+
|
|
|
+
|
|
|
+ def keepBalance(self, pitch_err):
|
|
|
+ if abs(pitch_err) > self.pitch_tolerance:
|
|
|
+ motorPWM = int(self.balance_kp*pitch_err + self.balance_kd*self.angularPitch)
|
|
|
+ return motorPWM
|
|
|
+ else:
|
|
|
+ return 0
|
|
|
+
|
|
|
+
|
|
|
+ def keepVelocity(self, encoder_A, encoder_B, target_speed):
|
|
|
+ encoder_err = encoder_A + encoder_B
|
|
|
+
|
|
|
+
|
|
|
+ self.encoder_lowOut = (1-self.encoderFilterPer)*encoder_err + \
|
|
|
+ self.encoderFilterPer*self.encoder_lowout
|
|
|
+
|
|
|
+ self.encoder_integral += self.encoder_lowOut
|
|
|
+
|
|
|
+ self.encoder_integral -= target_speed
|
|
|
+
|
|
|
+ if self.encoder_integral > 1000:
|
|
|
+ self.encoder_integral = 1000
|
|
|
+ elif self.encoder_integral < -1000:
|
|
|
+ self.encoder_integral = -1000
|
|
|
+
|
|
|
+
|
|
|
+ motorPWM = self.speed_kp*self.encoder_lowOut + self.speed_ki*self.encoder_integral
|
|
|
+
|
|
|
return motorPWM
|
|
|
+
|
|
|
+
|
|
|
+ def checkIfDown(self, pitch_err, pwm_A, pwm_B):
|
|
|
+ if pitch_err < self.downPitchData and pitch_err > (-self.downPitchData):
|
|
|
+ return pwm_A, pwm_B
|
|
|
+ else:
|
|
|
+ return 0, 0
|