|
@@ -20,18 +20,22 @@ from geometry_msgs.msg import Quaternion, Twist
|
|
|
from nav_msgs.msg import Odometry
|
|
|
from tf.broadcaster import TransformBroadcaster
|
|
|
from std_msgs.msg import Int32
|
|
|
+from ros_arduino_python.balance_mode import BalanceMode
|
|
|
|
|
|
|
|
|
""" Class to receive Twist commands and publish wheel Odometry data """
|
|
|
class BaseController:
|
|
|
- def __init__(self, is_use_serial, arduino, base_frame):
|
|
|
+ def __init__(self, is_use_serial, arduino, base_frame, cmd_topic, balance_mode):
|
|
|
self.use_serial = is_use_serial
|
|
|
self.arduino = arduino
|
|
|
self.base_frame = base_frame
|
|
|
+ self.isBalance = balance_mode
|
|
|
+ self.cmd_topic = cmd_topic
|
|
|
+ self.PID_rate = 100
|
|
|
+
|
|
|
self.odom_name = rospy.get_param("~odom_name", "odom")
|
|
|
- self.cmd_topic = rospy.get_param("~cmd_topic", "cmd_vel")
|
|
|
self.debugPID = rospy.get_param('/dynamic_tutorials/debugPID', False)
|
|
|
-
|
|
|
+
|
|
|
pid_params = dict()
|
|
|
pid_params['wheel_diameter'] = rospy.get_param("~wheel_diameter", 0.067)
|
|
|
pid_params['wheel_track'] = rospy.get_param("~wheel_track", 0.126)
|
|
@@ -51,8 +55,6 @@ class BaseController:
|
|
|
self.linear_scale_correction = rospy.get_param("~linear_scale_correction", 1.0)
|
|
|
self.angular_scale_correction = rospy.get_param("~angular_scale_correction", 1.0)
|
|
|
|
|
|
-
|
|
|
-
|
|
|
|
|
|
self.setup_pid(pid_params)
|
|
|
|
|
@@ -66,7 +68,7 @@ class BaseController:
|
|
|
now = rospy.Time.now()
|
|
|
self.old_time = now
|
|
|
|
|
|
-
|
|
|
+
|
|
|
self.enc_A = 0
|
|
|
self.enc_B = 0
|
|
|
|
|
@@ -74,17 +76,21 @@ class BaseController:
|
|
|
self.y = 0
|
|
|
self.th = 0
|
|
|
|
|
|
- self.v_A = 0
|
|
|
+ self.v_A = 0
|
|
|
self.v_B = 0
|
|
|
|
|
|
- self.v_des_AWheel = 0
|
|
|
+ self.v_des_AWheel = 0
|
|
|
self.v_des_BWheel = 0
|
|
|
|
|
|
self.last_cmd_time = now
|
|
|
|
|
|
-
|
|
|
+
|
|
|
rospy.Subscriber(self.cmd_topic, Twist, self.cmdVelCallback)
|
|
|
|
|
|
+
|
|
|
+ if self.isBalance:
|
|
|
+ self.balanceControl = BalanceMode()
|
|
|
+
|
|
|
|
|
|
if self.use_serial:
|
|
|
self.arduino.reset_encoders()
|
|
@@ -104,7 +110,7 @@ class BaseController:
|
|
|
self.AVelPub = rospy.Publisher('Avel', Int32, queue_size=10)
|
|
|
self.BVelPub = rospy.Publisher('Bvel', Int32, queue_size=10)
|
|
|
|
|
|
- rospy.loginfo("Started base controller for a base of " + str(self.wheel_track) + "m with " + str(self.encoder_resolution) + " ticks per rev")
|
|
|
+ rospy.loginfo("Started base controller of " + str(self.wheel_track) + "m with " + str(self.encoder_resolution) + " ticks per rev")
|
|
|
|
|
|
def setup_pid(self, pid_params):
|
|
|
|
|
@@ -118,7 +124,7 @@ class BaseController:
|
|
|
os._exit(1)
|
|
|
|
|
|
self.encoder_resolution = pid_params['encoder_resolution']
|
|
|
- self.gear_reduction = pid_params['gear_reduction']
|
|
|
+ self.gear_reduction = pid_params['gear_reduction']
|
|
|
self.wheel_diameter = pid_params['wheel_diameter']
|
|
|
self.wheel_track = pid_params['wheel_track']
|
|
|
self.wheel_track = self.wheel_track/self.angular_scale_correction
|
|
@@ -140,11 +146,21 @@ class BaseController:
|
|
|
self.arduino.i2c_update_pid(self.AWheel_Kp, self.AWheel_Kd, self.AWheel_Ki, self.AWheel_Ko,
|
|
|
self.BWheel_Kp, self.BWheel_Kd, self.BWheel_Ki, self.BWheel_Ko,)
|
|
|
|
|
|
-
|
|
|
+
|
|
|
def poll(self):
|
|
|
|
|
|
self.send_debug_pid()
|
|
|
|
|
|
+
|
|
|
+ if self.isBalance:
|
|
|
+ self.balanceControl.keepBalance()
|
|
|
+ data = self.balanceControl.motorPulse
|
|
|
+
|
|
|
+ if self.use_serial:
|
|
|
+ self.arduino.drive(-data, data)
|
|
|
+ else:
|
|
|
+ self.arduino.i2c_drive(-data, data)
|
|
|
+
|
|
|
|
|
|
try:
|
|
|
if self.use_serial:
|
|
@@ -261,18 +277,19 @@ class BaseController:
|
|
|
self.AVelPub.publish(self.v_A)
|
|
|
self.BVelPub.publish(self.v_B)
|
|
|
|
|
|
+
|
|
|
def cmdVelCallback(self, req):
|
|
|
self.last_cmd_time = rospy.Time.now()
|
|
|
|
|
|
- x = req.linear.x
|
|
|
- th = req.angular.z
|
|
|
+ x = req.linear.x
|
|
|
+ th = req.angular.z
|
|
|
|
|
|
-
|
|
|
+
|
|
|
vA = -(x + 0.5*self.wheel_track*th)
|
|
|
vB = (x - 0.5*self.wheel_track*th)
|
|
|
|
|
|
- self.v_des_AWheel = int(vA * self.ticks_per_meter / self.arduino.PID_RATE)
|
|
|
- self.v_des_BWheel = int(vB * self.ticks_per_meter / self.arduino.PID_RATE)
|
|
|
+ self.v_des_AWheel = int(vA * self.ticks_per_meter / self.PID_rate)
|
|
|
+ self.v_des_BWheel = int(vB * self.ticks_per_meter / self.PID_rate)
|
|
|
|
|
|
|
|
|
|