-
Notifications
You must be signed in to change notification settings - Fork 0
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
Velocity port done #20
base: main
Are you sure you want to change the base?
Changes from 9 commits
fc57679
441020c
911509d
9701efe
73dbccb
2b6ab24
8923009
d32f012
765f2b9
3060cce
08b2e29
283f95b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<launch> | ||
|
||
<node pkg="foxglove_bridge" exec="foxglove_bridge" name="foxglove" namespace="SC"/> | ||
<node pkg="buggy" exec="engine.py" name="SC_sim_single" namespace="SC"> | ||
<param name="velocity" value="12"/> | ||
<param name="pose" value="Hill1_SC"/> | ||
</node> | ||
<node pkg="buggy" exec="buggy_state_converter.py" name="SC_state_converter" namespace="SC"/> | ||
<node pkg="buggy" exec="controller_node.py" name="SC_controller" namespace="SC"> | ||
<param name="dist" value="0.0"/> | ||
<param name="traj_name" value="buggycourse_safe.json"/> | ||
<param name="controller" value="stanley"/> | ||
</node> | ||
<node pkg="buggy" exec="velocity_updater.py" name="SC_velocity_updater" namespace="SC"> | ||
<param name="init_vel" value="15"/> | ||
<param name="buggy_name" value="SC"/> | ||
</node> | ||
</launch> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{ | ||
"checkpoints": | ||
[ | ||
{ | ||
"x-pos": 589700, | ||
"y-pos": 4477159, | ||
"radius": 20, | ||
"velocity": 12.345 | ||
}, | ||
{ | ||
"x-pos": 589685, | ||
"y-pos": 4477153, | ||
"radius": 20, | ||
"velocity": 17.234 | ||
} | ||
] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
#! /usr/bin/env python3 | ||
import math | ||
import threading | ||
import json | ||
import rclpy | ||
from rclpy.node import Node | ||
from geometry_msgs.msg import Pose | ||
from geometry_msgs.msg import Point | ||
from std_msgs.msg import Float64 | ||
|
||
class VelocityUpdater(Node): | ||
RATE = 100 | ||
|
||
# Bubbles for updating acceleration based on position | ||
# represented as {x-pos, y-pos, radius, velocity} | ||
# imported from checkpoints.json | ||
checkpoints_path = "/rb_ws/src/buggy/scripts/simulator/checkpoints.json" | ||
with open(checkpoints_path, 'r') as checkpoints_file: | ||
CHECKPOINTS = (json.load(checkpoints_file))["checkpoints"] | ||
|
||
def __init__(self): | ||
super().__init__('velocity_updater') | ||
self.get_logger().info('INITIALIZED.') | ||
|
||
# Declare parameters with default values | ||
self.declare_parameter('init_vel', 12) | ||
self.declare_parameter('buggy_name', 'SC') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we want to be able to do this for NAND too, right? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's the default variable, it can change based on the parameter itself There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can change the variable "buggy_name" in the xml launch file to make it run for NAND. Maybe there is a way to run two velocity_updater nodes to make it change velocity of SC and NAND at the same time, but I am not sure. |
||
# Get the parameter values | ||
self.init_vel = self.get_parameter("init_vel").value | ||
self.buggy_name = self.get_parameter("buggy_name").value | ||
|
||
# initialize variables | ||
self.buggy_vel = self.init_vel | ||
self.debug_dist = 0 | ||
|
||
self.position = Point() | ||
self.lock = threading.Lock() | ||
|
||
# subscribe sim_2d/utm for position values | ||
self.pose_subscriber = self.create_subscription( | ||
Pose, | ||
"sim_2d/utm", | ||
self.update_position, | ||
1 | ||
) | ||
|
||
# publish velocity to "sim/velocity" | ||
self.velocity_publisher = self.create_publisher(Float64, "sim/velocity", 1) | ||
|
||
# ROS2 timer for stepping | ||
self.timer = self.create_timer(1.0 / self.RATE, self.step) | ||
|
||
def update_position(self, new_pose: Pose): | ||
'''Callback function to update internal position variable when new | ||
buggy position is published | ||
|
||
Args: | ||
new_pose (Pose): Pose object from topic | ||
''' | ||
with self.lock: | ||
self.position = new_pose.position | ||
|
||
def check_velocity(self): | ||
'''Check if the position of the buggy is in any of the checkpoints set | ||
in self.CHECKPOINTS, and update velocity of buggy accordingly | ||
''' | ||
for checkpoint in self.CHECKPOINTS: | ||
x = checkpoint["x-pos"] | ||
y = checkpoint["y-pos"] | ||
r = checkpoint["radius"] | ||
v = checkpoint["velocity"] | ||
dist = math.sqrt((x-self.position.x)**2 + (y-self.position.y)**2) | ||
self.debug_dist = dist | ||
if dist < r: | ||
self.buggy_vel = v | ||
break | ||
|
||
def step(self): | ||
'''Update velocity of buggy for one timestep | ||
and publish it so that the simulator engine can subscribe and use it''' | ||
|
||
# update velocity | ||
self.check_velocity() | ||
|
||
# publish velocity | ||
float_64_velocity = Float64() | ||
float_64_velocity.data = float(self.buggy_vel) | ||
self.velocity_publisher.publish(float_64_velocity) | ||
|
||
|
||
def main(args=None): | ||
rclpy.init(args=args) | ||
vel_updater = VelocityUpdater() | ||
rclpy.spin(vel_updater) | ||
rclpy.shutdown() | ||
|
||
if __name__ == "__main__": | ||
main() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
needs to be environment-variable-ized. do we have a process for that now? @mehulgoel873 or @TiaSinghania
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, let me quickly add that in.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The path is now environment-variable-ized!