|
@@ -18,12 +18,11 @@ from sensor_msgs.msg import Imu
|
|
|
class BalanceMode:
|
|
|
def __init__(self):
|
|
|
self.angularVal = 57.2957
|
|
|
- self.motorPulse = 0
|
|
|
|
|
|
self.pitchData = 0
|
|
|
self.angularPitch = 0
|
|
|
|
|
|
- self.middle_val = rospy.get_param("~middle_val", 0.0733)
|
|
|
+ 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)
|
|
|
|
|
@@ -31,16 +30,21 @@ class BalanceMode:
|
|
|
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
|
|
|
+ self.angularPitch = msg.angular_velocity.y*self.angularVal
|
|
|
|
|
|
def keepBalance(self):
|
|
|
- pitch_err = self.pitchData-self.middle_val
|
|
|
- self.motorPulse = self.balance_kp * pitch_err \
|
|
|
- + self.balance_kd * self.angularPitch
|
|
|
+ pitch_err = self.pitchData - self.middle_val
|
|
|
+ motorPWM = int(self.balance_kp*pitch_err + self.balance_kd*self.angularPitch)
|
|
|
+
|
|
|
+ #将计算得到的pwm值进行限幅,最大pwm值为255
|
|
|
+ if motorPWM > 255:
|
|
|
+ motorPWM = 255
|
|
|
+ elif motorPWM < -255:
|
|
|
+ motorPWM = -255
|
|
|
|
|
|
rospy.loginfo("pitch=" + str(self.pitchData) + \
|
|
|
- " angular=" + str(self.angularPitch) + \
|
|
|
- " motorPulse=" + str(self.motorPulse))
|
|
|
+ " angular=" + str(self.angularPitch) + " PWM=" + str(motorPWM))
|
|
|
+ return motorPWM
|