-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from little-red-rover/10-switch-to-tcp
Switch from UDP to TCP
- Loading branch information
Showing
9 changed files
with
243 additions
and
134 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
little_red_rover/little_red_rover/drive_base_peripheral.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import rospy | ||
|
||
from sensor_msgs.msg import JointState | ||
from geometry_msgs.msg import Twist | ||
|
||
import little_red_rover.pb.messages_pb2 as messages | ||
from little_red_rover.rover_connection import RoverConnection | ||
|
||
|
||
class DriveBasePeripheral: | ||
def __init__(self, connection: RoverConnection): | ||
self.joint_state_publisher = rospy.Publisher( | ||
"joint_states", JointState, queue_size=10 | ||
) | ||
|
||
self.joint_state_msg = JointState() | ||
self.joint_state_msg.header.frame_id = "robot_body" | ||
self.subscription = rospy.Subscriber("cmd_vel", Twist, self.cmd_vel_callback) | ||
|
||
self.connection = connection | ||
|
||
def handle_packet(self, packet: messages.NetworkPacket): | ||
if not packet.HasField("joint_states"): | ||
return | ||
|
||
self.joint_state_msg.header.stamp.set( | ||
packet.joint_states.time.sec, packet.joint_states.time.nanosec | ||
) | ||
self.joint_state_msg.name = list(packet.joint_states.name) | ||
self.joint_state_msg.effort = packet.joint_states.effort | ||
self.joint_state_msg.position = packet.joint_states.position | ||
self.joint_state_msg.velocity = packet.joint_states.velocity | ||
self.joint_state_publisher.publish(self.joint_state_msg) | ||
|
||
def cmd_vel_callback(self, msg: Twist): | ||
packet = messages.NetworkPacket() | ||
packet.cmd_vel.v = msg.linear.x | ||
packet.cmd_vel.w = msg.angular.z | ||
|
||
self.connection.send(packet.SerializeToString()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import rospy | ||
from sensor_msgs.msg import Imu | ||
import little_red_rover.pb.messages_pb2 as messages | ||
|
||
|
||
class ImuPeripheral: | ||
def __init__(self): | ||
self.imu_publisher = rospy.Publisher("imu/data_raw", Imu, queue_size=10) | ||
self.imu_msg = Imu() | ||
self.imu_msg.header.frame_id = "robot_body" | ||
|
||
def handle_packet(self, packet: messages.NetworkPacket): | ||
if not packet.HasField("imu"): | ||
return | ||
self.imu_msg.header.stamp.set(packet.imu.time.sec, packet.imu.time.nanosec) | ||
|
||
# disable orientation | ||
self.imu_msg.orientation_covariance = [-1.0] + [0.0] * 8 | ||
|
||
# accel | ||
self.imu_msg.linear_acceleration.x = packet.imu.accel_x | ||
self.imu_msg.linear_acceleration.y = packet.imu.accel_y | ||
self.imu_msg.linear_acceleration.z = packet.imu.accel_z | ||
self.imu_msg.linear_acceleration_covariance = [0.0] * 9 # TODO | ||
|
||
# gyro | ||
self.imu_msg.angular_velocity.x = packet.imu.gyro_x | ||
self.imu_msg.angular_velocity.y = packet.imu.gyro_y | ||
self.imu_msg.angular_velocity.z = packet.imu.gyro_z | ||
self.imu_msg.angular_velocity_covariance = [0.0] * 9 # TODO | ||
|
||
self.imu_publisher.publish(self.imu_msg) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import rospy | ||
from sensor_msgs.msg import LaserScan | ||
import little_red_rover.pb.messages_pb2 as messages | ||
|
||
from math import inf, pi | ||
|
||
|
||
class LidarPeripheral: | ||
def __init__(self): | ||
self.ranges = [0.0] * 720 | ||
self.intensities = [0.0] * 720 | ||
|
||
self.scan_publisher = rospy.Publisher("scan", LaserScan, queue_size=10) | ||
|
||
self.laser_msg = LaserScan() | ||
self.laser_msg.header.frame_id = "lidar" | ||
self.laser_msg.range_min = 0.1 | ||
self.laser_msg.range_max = 8.0 | ||
self.laser_msg.angle_min = 0.0 | ||
self.laser_msg.angle_max = 2.0 * pi | ||
self.laser_msg.angle_increment = (2 * pi) / (len(self.ranges)) | ||
|
||
def handle_packet(self, packet: messages.NetworkPacket): | ||
if len(packet.laser) == 0: | ||
return | ||
for scan in packet.laser: | ||
self.handle_laser_scan(self.laser_msg, scan) | ||
pass | ||
|
||
def handle_laser_scan(self, msg: LaserScan, packet: messages.LaserScan): | ||
break_in_packet = False | ||
for i in range(len(packet.ranges)): | ||
angle = packet.angle_min + (packet.angle_max - packet.angle_min) * ( | ||
i / (len(packet.ranges) - 1) | ||
) | ||
index = int(((angle % (2.0 * pi)) / (2.0 * pi)) * 720.0) | ||
|
||
if angle > pi * 2.0 and not break_in_packet: | ||
msg.time_increment = packet.time_increment | ||
msg.scan_time = packet.scan_time | ||
msg.ranges = self.ranges | ||
msg.intensities = self.intensities | ||
|
||
self.scan_publisher.publish(msg) | ||
break_in_packet = True | ||
|
||
self.ranges = [0.0] * 720 | ||
self.intensities = [0.0] * 720 | ||
|
||
msg.header.stamp.set(packet.time.sec, packet.time.nanosec) | ||
# msg.header.stamp = self.get_clock().now().to_msg() | ||
|
||
self.ranges[index] = packet.ranges[i] | ||
self.intensities[index] = packet.intensities[i] | ||
|
||
if ( | ||
self.ranges[index] > 8.0 | ||
or self.ranges[index] < 0.1 | ||
or self.intensities[index] == 0 | ||
): | ||
self.ranges[index] = inf | ||
self.intensities[index] = 0.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.