Kuavo Docs
Feature ExamplesGeneral Examples

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 use Example Code 2/3, refer only to the corresponding parts.

Program Logic

  1. 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
  2. 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) and orientation 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

  3. 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 processing as 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.

  4. 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 to keyboard; for the GUI virtual joystick, you must explicitly pass sim_input_mode:=gui.

Build

cd /home/lab/kuavo-ros-opensource
sudo su
catkin build kuavo_sdk motion_capture_ik kuavo_msgs humanoid_controllers

Run

  • 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 ik in the terminal
    • If /arms_ik_node already exists, skip this step
    • If /arms_ik_node does 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 v to switch between keyboard arm control mode and keyboard robot movement control mode

      • Keyboard robot movement control mode:

        • The WASD keys are the left joystick, controlling forward/backward and left/right lateral movement; each press increases or decreases the value by 10%;
        • The IK and JL/QE keys are the right joystick, controlling up/down and left/right turning; each press increases or decreases the value by 10%;
        • The R key is walk (stepping in place); C is stance (standing in place);
        • The spacebar resets the left and right joystick inputs to zero.
      • Keyboard arm control mode:

        Update the parameter values of the end effector's position control (XYZ) and orientation control (RPY) according to keyboard input:

        ControlXYZRollPitchYaw
        Increasewdquij
        Decreasesaeokl
        • Press G to switch the control mode between manual control and automatic control

          Changes in position control (XYZ) modify the value of self.eef_target_xyz accordingly

          Changes in orientation control (RPY) modify the value of self.eef_angle_manual accordingly

        Note:

        • The value of self.eef_target_ypr is generated automatically; do not modify it manually
        • The recommended range for Yaw is -90 degrees to +90 degrees
        • The recommended range for Pitch and Roll is -30 degrees to +30 degrees

Results

  • Keyboard control of robot movement:

    Keyboard control

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

    Keyboard control arm movement default angles

  • Press G on the keyboard to switch to manual control mode, then control the desired end-effector orientation to rotate 45 degrees about the new x-axis:

    Keyboard control arm movement 45 degrees

  • Keyboard control of the desired end-effector orientation rotating 90 degrees about the new x-axis:

    Keyboard control arm movement 90 degrees