Keyboard Control Example
Description
-
Use the keyboard to control the robot's movement
-
Use the keyboard to control the robot arm's movement
Example Code
-
Example Code 1: Use the keyboard to control robot movement and arm movement (freely switchable):
/home/lab/kuavo-ros-opensource/src/kuavo_sdk/scripts/keyboard_control/robot_keyboard_control.py
-
Example Code 2: Use only the keyboard to control robot movement:
/home/lab/kuavo-ros-opensource/src/kuavo_sdk/scripts/keyboard_control/move_keyboard_control.py
-
Example Code 3: Use only the keyboard to control robot arm movement:
/home/lab/kuavo-ros-opensource/src/kuavo_sdk/scripts/keyboard_control/arm_keyboard_control.py
-
The following description applies to
Example Code 1. If you useExample Code 2/3, refer only to the corresponding parts.
Program Logic
-
Program initialization
-
At program startup, initialize the ROS node; the node name is simulated_joystick
-
Create an instance of the SimulatedJoystick class
-
Subscribe to the /stop_robot topic to receive signals to stop the robot
-
Use the termios library to set keyboard input to non-blocking mode so that keyboard input can be read in real time
-
Arm movement control
-
Read sensor data to obtain the current joint angles, then use FK (forward kinematics) to obtain the initial end-effector pose
-
Initialize the program parameters and store the desired end-effector pose information of the robot
-
Call the termios library to set the keyboard input mode and read keyboard input in real time
-
Switch the arm to control mode 2 (external control)
-
Publish to the /kuavo_arm_target_poses topic to control the target arm pose
-
-
Whole-body motion control
- Publish to the /joy topic to send simulated remote controller signals
-
-
Keyboard input handling
-
getKey function:
Use the select library to monitor keyboard input, with a timeout of 0.1 seconds
If there is keyboard input, read a single character and return it; otherwise return an empty string
If the keyboard input is Ctrl-C, restore the original keyboard settings and exit the program
-
update_joy function:
-
Arm movement control
Update the parameter values of the end effector's
position control (XYZ)andorientation control (RPY)according to keyboard input -
Whole-body motion control
Update the state of the simulated remote controller (button and stick signals) according to keyboard input
-
-
-
Control command publishing
-
Main loop:
In the run function, the program enters the main loop and continuously monitors keyboard input.
-
Arm movement control
-
Adaptive processing:
Based on the set end-effector position, compute a relatively reasonable end-effector orientation
Adaptive processing ensures the maximum reachable workspace in
automatic end-effector orientation control mode. -
Manual modification:
In
manual end-effector orientation control mode, the input angle parameters are enabled.Using the end-effector orientation obtained from
adaptive processingas a reference, apply a secondary rotation.Send the final result as the final parameters to IK for solving
-
-
Whole-body motion control
If there is keyboard input, call the update_joy function to update the remote controller state.
Publish the updated remote controller signal (joy_msg) to the /joy topic.
-
Exit conditions:
If Ctrl-C is pressed, the program exits the main loop.
If a message on the /stop_robot topic is received, the program calls rospy.signal_shutdown to shut down the ROS node.
-
-
Program exit
-
Resource cleanup:
When the program exits, restore the original keyboard settings to avoid abnormal keyboard input.
-
Parameter Description
-
joystick_sensitivity: sensitivity, unitless. -
Modification path:
/home/lab/kuavo-ros-opensource/src/humanoid-control/humanoid_controllers/launch/joy/joy_control_sim.launch -
Input mode parameter:
sim_input_mode, defaults tokeyboard; for the GUI virtual joystick, you must explicitly passsim_input_mode:=gui.
Build
cd /home/lab/kuavo-ros-opensource
sudo su
catkin build kuavo_sdk motion_capture_ik kuavo_msgs humanoid_controllersRun
-
Start the robot
cd kuavo-ros-opensource # 进入下位机工作空间 sudo su source devel/setup.bash # 仿真环境运行(默认键盘输入) roslaunch humanoid_controllers load_kuavo_mujoco_sim.launch joystick_type:=sim # 仿真环境运行(GUI 虚拟手柄输入) # roslaunch humanoid_controllers load_kuavo_mujoco_sim.launch joystick_type:=sim sim_input_mode:=gui # 实物运行 roslaunch humanoid_controllers load_kuavo_real.launch -
Start the IK inverse kinematics server
- Note: In some versions, the IK inverse kinematics service starts automatically when the robot is started in the previous step; be careful not to start it twice
- How to check: enter
rosnode list | grep ikin the terminal - If
/arms_ik_nodealready exists, skip this step - If
/arms_ik_nodedoes not exist, run:cd kuavo-ros-opensource # 进入下位机工作空间 sudo su source devel/setup.bash roslaunch motion_capture_ik ik_node.launch
-
Start the keyboard control example
cd kuavo-ros-opensource # 进入下位机工作空间 sudo su source devel/setup.bash python3 src/kuavo_sdk/scripts/keyboard_control/robot_keyboard_control.py -
Operating instructions
-
Initially in
keyboard arm control mode -
Press
vto switch betweenkeyboard arm control modeandkeyboard robot movement control mode-
Keyboard robot movement control mode:
- The
WASDkeys are the left joystick, controlling forward/backward and left/right lateral movement; each press increases or decreases the value by 10%; - The
IKandJL/QEkeys are the right joystick, controlling up/down and left/right turning; each press increases or decreases the value by 10%; - The
Rkey is walk (stepping in place);Cis stance (standing in place); - The spacebar resets the left and right joystick inputs to zero.
- The
-
Keyboard arm control mode:
Update the parameter values of the end effector's
position control (XYZ)andorientation control (RPY)according to keyboard input:Control X Y Z Roll Pitch Yaw Increase wdquijDecrease saeokl-
Press
Gto switch the control mode betweenmanual controlandautomatic controlChanges in
position control (XYZ)modify the value ofself.eef_target_xyzaccordinglyChanges in
orientation control (RPY)modify the value ofself.eef_angle_manualaccordingly
Note:
- The value of
self.eef_target_ypris generated automatically; do not modify it manually - The recommended range for
Yawis-90 degrees to +90 degrees - The recommended range for
PitchandRollis-30 degrees to +30 degrees
-
-
-
Results
-
Keyboard control of robot movement:

-
Keyboard control of the arm moving to a specified position:

-
Press
Gon the keyboard to switch tomanual control mode, then control the desired end-effector orientation to rotate 45 degrees about the new x-axis:
-
Keyboard control of the desired end-effector orientation rotating 90 degrees about the new x-axis:
