Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Timing FW interface loop frequency #813

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import math
import os
import statistics
import threading
import time

Expand Down Expand Up @@ -57,7 +58,10 @@ def reconnect_serial_port(self):
rospy.logerr_throttle(10, "Unable to connect to any serial port /dev/ttyACM0-10")

def firmware_update_loop(self):
num_samples = 100
frequency_holder = [0] * num_samples
while not rospy.is_shutdown():
start_time = time.time()
try:
self.reconnect_serial_port()
# data format
Expand Down Expand Up @@ -88,10 +92,10 @@ def firmware_update_loop(self):
val = data[i * 2 + 2] | data[i * 2 + 3] << 8

motor_name = self.motor_id_to_name_dict[i]
motor_type = self.motor_mapping[motor_name]['type']
motor_angle_zero = self.motor_mapping[motor_name]['angle_zero']
max_angle_bytes = self.motor_types[motor_type]['max_angle_bytes']
max_angle_radians = self.motor_types[motor_type]['max_angle_degrees'] / 180 * math.pi
motor_type = self.motor_mapping[motor_name]["type"]
motor_angle_zero = self.motor_mapping[motor_name]["angle_zero"]
max_angle_bytes = self.motor_types[motor_type]["max_angle_bytes"]
max_angle_radians = self.motor_types[motor_type]["max_angle_degrees"] / 180 * math.pi

motor_angle_zero_radian = motor_angle_zero / 180 * math.pi

Expand Down Expand Up @@ -125,7 +129,6 @@ def firmware_update_loop(self):
imu.linear_acceleration.y = -(ay) * G / ACC_RANGE
imu.linear_acceleration.z = -(az) * G / ACC_RANGE


vx = int.from_bytes(imu_data[6:8], byteorder="big", signed=True) / IMU_GY_RANGE
vy = int.from_bytes(imu_data[8:10], byteorder="big", signed=True) / IMU_GY_RANGE
vz = int.from_bytes(imu_data[10:12], byteorder="big", signed=True) / IMU_GY_RANGE
Expand All @@ -146,6 +149,11 @@ def firmware_update_loop(self):
rospy.logerr_throttle(10, f"Lost connection to serial port {type(ex)} {ex}, retrying...")
pass

frequency_holder.append(time.time() - start_time)
frequency_holder = frequency_holder[-num_samples:]
average = statistics.mean(frequency_holder)
rospy.loginfo_throttle(1, f"FW interface average loop period = {average}sec, frequency = {1/average}Hz")

def joint_command_callback(self, joint_state: JointState):
try:

Expand All @@ -162,7 +170,6 @@ def joint_command_callback(self, joint_state: JointState):
if time_diff_real < time_diff_message_in:
rospy.sleep(time_diff_message_in - time_diff_real)


t1 = time.time()

self.reconnect_serial_port()
Expand Down Expand Up @@ -199,15 +206,15 @@ def joint_command_callback(self, joint_state: JointState):
bytes_to_write[2 + id * 2 + 1] = angle_final_bytes_2
pass


t2 = time.time()

self.serial.write(bytes_to_write)
self.last_motor_publish_time_real = rospy.Time.now()
self.last_motor_publish_time = joint_state.header.stamp
t3 = time.time()
rospy.loginfo(f"Time Lag : {(rospy.Time.now() - joint_state.header.stamp).to_sec()} Bytes Written: {bytes_to_write} Time Take {t2-t1} {t3-t1}")

rospy.loginfo(
f"Time Lag : {(rospy.Time.now() - joint_state.header.stamp).to_sec()} Bytes Written: {bytes_to_write} Time Take {t2-t1} {t3-t1}"
)

except Exception as ex:
rospy.logerr_throttle(10, f"Lost connection to serial port {ex}, retrying...")
Expand Down