Update rmp220_middleware.py

optimize python code to only publish the correct messages at a rate of 100 Hz. Should cause less confusion to the controller.
This commit is contained in:
bjoernellens1 2023-08-15 18:10:57 +02:00 committed by GitHub
parent 998045b794
commit 7cbea942da
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -76,26 +76,30 @@ class StateMachineNode(Node):
self.disable_chassis() self.disable_chassis()
def cmd_vel_callback(self, msg): def cmd_vel_callback(self, msg):
if self.state == State.ENABLED and (abs(msg.linear.x) >= 0.03 or abs(msg.angular.z) >= 0.03): # if self.state == State.ENABLED and (abs(msg.linear.x) >= 0.03 or abs(msg.angular.z) >= 0.03):
self.cmd_vel_pub.publish(msg) # self.cmd_vel_pub.publish(msg)
self.timeout = 20.0 # Reset timeout when receiving commands # self.timeout = 20.0 # Reset timeout when receiving commands
if msg.linear.x == 0.0 and msg.angular.z == 0.0: # if msg.linear.x == 0.0 and msg.angular.z == 0.0:
# No data received on cmd_vel_mux, publish zeros # # No data received on cmd_vel_mux, publish zeros
twist_msg = Twist() # twist_msg = Twist()
self.cmd_vel_pub.publish(twist_msg) # self.cmd_vel_pub.publish(twist_msg)
else: # else:
# Forward received data to cmd_vel_out # # Forward received data to cmd_vel_out
self.cmd_vel_pub.publish(msg) # self.cmd_vel_pub.publish(msg)
self.timeout = 10.0 # Reset timeout when receiving commands # self.timeout = 10.0 # Reset timeout when receiving commands
# Forward received data to cmd_vel_out # # Forward received data to cmd_vel_out
self.cmd_vel_pub.publish(msg) # self.cmd_vel_pub.publish(msg)
self.timeout = 10.0 # Reset timeout when receiving commands # self.timeout = 10.0 # Reset timeout when receiving commands
if abs(msg.linear.x) > 0.03 or abs(msg.angular.z) > 0.03: # if abs(msg.linear.x) > 0.03 or abs(msg.angular.z) > 0.03:
# self.latest_cmd_vel = msg
# else:
# self.latest_cmd_vel = Twist()
# This method shall only update the latest_cmd_vel attribute so it can be republished by the timer_callback with 100 HZ. Should have a look at performance though.
self.latest_cmd_vel = msg self.latest_cmd_vel = msg
else: # TODO: Add setting chassis state to enabled automatically upon receiving commands (with filter maybe)
self.latest_cmd_vel = Twist()
def timer_callback(self): def timer_callback(self):
#self.cmd_vel_pub.publish(self.twist) #self.cmd_vel_pub.publish(self.twist)