Setting Up and Controlling Husky Robot in Gazebo with ROS Noetic
In this tutorial, we will work with the Husky robot using Gazebo and ROS Noetic. Gazebo is a powerful robot simulation tool, and ROS Noetic is the latest long-term support release of ROS 1. Together, they provide a great platform for robotics development and testing.
First, let’s open Gazebo:
1
gazebo
Next, we need to download the related Husky simulation files from the Husky GitHub page. Make sure to choose the noetic-devel
branch, which is compatible with ROS Noetic. After downloading, extract all the files (such as husky_base
, husky_simulator
, husky_launch
, etc.) to the robotics_ws/src/robotics
folder where our scripts
folder is located.
Navigate to the workspace and compile the workspace using catkin_make
:
1
2
cd ~/robotics_ws
catkin_make
Now, we can start the Husky simulation in an empty world with the following command:
1
roslaunch husky_gazebo empty_world.launch
You may encounter some dependency issues as the Husky robot simulator does not fully support ROS Noetic. To resolve these, install the following packages:
1
2
3
4
5
6
7
8
9
10
11
12
sudo apt-get install ros-noetic-fath-pivot-mount-description
sudo apt-get install ros-noetic-flir-camera-description
sudo apt-get install ros-noetic-lms1xx
sudo apt-get install ros-noetic-velodyne-description
sudo apt install ros-noetic-realsense2-description
sudo apt-get install ros-noetic-robot-localization
sudo apt-get install ros-noetic-interactive-marker-twist-server
sudo apt-get install ros-noetic-twist-mux
sudo apt-get install ros-noetic-teleop-twist-keyboard
sudo apt-get install ros-noetic-teleop-twist-joy
sudo apt-get install ros-noetic-rviz-imu-plugin
sudo apt-get install ros-noetic-gmapping
After installing these dependencies, you can launch Gazebo with the Husky robot using the previous command.
Next, we’ll write a Python program to drive our robot to a desired location. Create a new Python script in the ~/robotics_ws/src/robotics/scripts/src
directory with the following code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/usr/bin/env python
import rospy
from math import atan2
from nav_msgs.msg import Odometry
from tf.transformations import euler_from_quaternion
from geometry_msgs.msg import Point, Twist
class Movement:
def __init__(self):
rospy.init_node("speed_controller")
self.x = 0.0
self.y = 0.0
self.theta = 0 # yaw
self.publisher = rospy.Publisher("/cmd_vel", Twist, queue_size=10)
rospy.Subscriber("/odometry/filtered", Odometry, self.newOdom)
self.twist = Twist()
self.r = rospy.Rate(4)
xgoal = input("Enter goal for x: ")
ygoal = input("Enter goal for y: ")
self.goal = Point()
self.goal.x = float(xgoal)
self.goal.y = float(ygoal)
self.move()
def newOdom(self, msg):
self.x = msg.pose.pose.position.x
self.y = msg.pose.pose.position.y
self.q_rotation = msg.pose.pose.orientation
(self.roll, self.pitch, self.theta) = euler_from_quaternion(
[self.q_rotation.x, self.q_rotation.y, self.q_rotation.z, self.q_rotation.w]
)
def move(self):
while not rospy.is_shutdown():
self.dist_x = self.goal.x - self.x
self.dist_y = self.goal.y - self.y
self.measured_angle = atan2(self.dist_y, self.dist_x)
if abs(self.measured_angle - self.theta) > 0.1:
self.twist.linear.x = 0.0
self.twist.angular.z = 0.2
else:
self.twist.linear.x = 0.5
self.twist.angular.z = 0.0
self.publisher.publish(self.twist)
self.r.sleep()
if __name__ == "__main__":
Movement()
Don’t forget to make the Python script executable:
1
chmod +x control.py
To run the complete setup, follow these steps in separate terminal windows:
- Launch the robot localization:
1
roslaunch robot_localization ekf_template.launch
- Echo the odometry data to verify the robot’s position:
1
rostopic echo /odometry/filtered
- Run the control script:
1
rosrun scripts control.py
This setup will allow you to control the Husky robot in the Gazebo simulation environment using ROS Noetic. Happy coding!