This repository contains a ROS 2 package designed to simulate a DC motor, generate setpoints, and control the motor using a PID controller.
Ensure you have ROS 2 installed and sourced on your system, along with colcon for building packages.
- Navigate to the root of your ROS 2 workspace (e.g.,
~/ros2_ws/). - Build the packages using
colcon:
colcon build --packages-select custom_interfaces motor_control- Source the setup file to overlay the new packages onto your environment:
source install/setup.bashYou can launch the entire system (the DC motor simulation, the setpoint generator, and the PID controller) using the provided launch file.
Run the following command in your terminal:
ros2 launch motor_control motor_launch.pyThis will start three nodes simultaneously:
/motor_sys(fromdc_motor.py)/sp_gen(fromset_point.py)/ctrl(fromcontroller.py)
To verify that your nodes are communicating correctly over their respective topics, you can use rqt_graph.
- Open a new terminal.
- Source your ROS 2 installation (and workspace if needed).
- Run the following command:
rqt_graphThis will open a GUI window displaying the nodes and the topics connecting them (e.g., /set_point, /motor_input_u).
The nodes are designed to accept parameter updates at runtime. You can adjust the PID gains, the setpoint signal types, and the DC motor constants on the fly.
Open a new terminal and run:
rqtIn the top menu bar, go to:
Plugins → Configuration → Parameter Reconfigure
In the left pane, click on the node you want to tune:
- Select
ctrlto adjustkp,ki,kd, andts(sampling time). - Select
sp_gento change the signal type (e.g.,"constant","sine","square") or the amplitude. - Select
motor_systo adjustsys_gain_Korsys_tau_T.
Modify the values in the right pane. The changes will be applied to the running system immediately.
You can also set parameters directly from the terminal using the ros2 param command.
View all available parameters:
ros2 param listSet specific parameters (examples):
To change the Proportional gain (kp) of the controller to 1.5:
ros2 param set /ctrl kp 1.5To change the setpoint signal type to a square wave:
ros2 param set /sp_gen type "square"To change the amplitude of the setpoint signal to 10.0:
ros2 param set /sp_gen amplitude 10.0To see the motor's performance and track how well the output follows the setpoint, you can plot the topic data in real-time.
Run rqt_plot:
rqt_plotIn the Topic text box:
- Type
/set_point/dataand click the + button. - Type the motor output topic (e.g.,
/motor_speed_y/dataor/motor_output_y/datadepending on your current node connections) and click the + button to overlay the system response.