Welcome to ROSMASTER X3 repository
10.4 Robot keyboard control
4. Robot keyboard control
According to different models, just set the purchased model in [.bashrc], X1 (normal four-wheel drive) X3 (Mailun) X3plus (Mailun robotic arm) R2 (Ackerman differential) etc. , this section takes X3 as an example
#Raspberry Pi 5 master needs to enter docker first, please perform this step#If running the script into docker fails, please refer to ROS/07, Docker tutorial~/run_docker.shOpen the [.bashrc] file
xxxxxxxxxxsudo vim .bashrc
Find the [ROBOT_TYPE] parameter and modify the corresponding model
xxxxxxxxxxexport ROBOT_TYPE=X3 # ROBOT_TYPE: X1 X3 X3plus R2 X7
4.1. Button description
4.1.1. Direction control
| [i] or [I] | [linear, 0] | [u] or [U] | [linear, angular] |
|---|---|---|---|
| [,] | [-linear, 0] | [o] or [O] | [linear, - angular] |
| [j] or [J] | [0, angular] | [m] or [M] | [- linear, - angular] |
| [l] or [L] | [0, - angular] | [.] | [- linear, angular] |
4.1.2. Speed control
| Button | Speed change | Button | Speed change |
|---|---|---|---|
| [q] | Linear speed and angular speed are both increased by 10% | [z] | Linear speed and angular speed are both reduced by 10% |
| [w] | Only the linear speed increases by 10% | [x] | Only the linear speed decreases by 10% |
| [e] | Only the angular velocity increases by 10% | [c] | Only the angular velocity decreases by 10% |
| [t] | Line speed X-axis/Y-axis direction switching | [s] | Stop keyboard control |
4.2. Run the program
4.2.1. Keyboard control code yahboom_keyboard.py path:
xxxxxxxxxx~/yahboomcar_ws/src/yahboom_ctrl/scripts
4.2.2. Run
xxxxxxxxxxroslaunch yahboomcar_bringup bringup.launch #robotchassisstart<PI5 needs to open another terminal to enter the same docker container

xxxxxxxxxxroslaunch yahboomcar_ctrl yahboom_keyboard.launch #Keyboard control node4.2.3. Analysis of yahboom_keyboard.py
1). Posted topic: cmd_vel
xxxxxxxxxxpub = rospy.Publisher('cmd_vel', Twist, queue_size=1)Therefore, you only need to package the speed and publish it through pub.publish(twist). The speed subscriber of the chassis can receive the speed data and then drive the car.
- Mainly used modules
- The select module is mainly used for socket communication, seeing changes in file descriptions, and completing work in a non-blocking manner.
- The termios module provides an interface for IO controlled POSIX calls for tty
- The tty module is mainly used to change the mode of the file descriptor fd
3), mobile dictionary and speed dictionary
The mobile dictionary mainly stores characters related to direction control
xxxxxxxxxxmoveBindings = {'i': (1, 0),'o': (1, -1),'j': (0, 1),'l': (0, -1),'u': (1, 1),',': (-1, 0),'.': (-1, 1),'m': (-1, -1),'I': (1, 0),'O': (1, -1),'J': (0, 1),'L': (0, -1),'U': (1, 1),'M': (-1, -1),}The speed dictionary mainly stores characters related to speed control
xxxxxxxxxxspeedBindings = {'Q': (1.1, 1.1),'Z': (.9, .9),'W': (1.1, 1),'X': (.9, 1),'E': (1, 1.1),'C': (1, .9),'q': (1.1, 1.1),'z': (.9, .9),'w': (1.1, 1),'x': (.9, 1),'e': (1, 1.1),'c': (1, .9),}- Get the current key information
xxxxxxxxxxdef getKey():#tty.setraw(): Change the file descriptor fd mode to raw; fileno(): Return an integer file descriptor (fd)tty.setraw(sys.stdin.fileno())# select(): Directly call the IO interface of the operating system; monitor all file handles with the fileno() methodrlist, _, _ = select.select([sys.stdin], [], [], 0.1)# Read a byte input streamif rlist: key = sys.stdin.read(1)else: key = ''#tcsetattr sets the tty attribute of the file descriptor fd from the attributetermios.tcsetattr(sys.stdin, termios.TCSADRAIN, settings)return key- Determine whether t/T or s/S is pressed
xxxxxxxxxxif key=="t" or key == "T": xspeed_switch = not xspeed_switchelif key == "s" or key == "S":print ("stop keyboard control: {}".format(not stop))stop = not stop- Determine whether the string is in the dictionary
xxxxxxxxxx#Whether the key string is in the mobile dictionaryif key in moveBindings.keys():x = moveBindings[key][0]th = moveBindings[key][1]count = 0#Whether the key character is in the speed dictionaryelif key in speedBindings.keys():speed = speed * speedBindings[key][0]turn = turn * speedBindings[key][1]count = 06), speed limit
Both angular speed and linear speed have a limit value, and it is impossible to increase forever. When starting, the program will first obtain the speed limit value, and when increasing the speed, the increased value will be judged and processed.
xxxxxxxxxxlinear_limit = rospy.get_param('~linear_speed_limit', 1.0)angular_limit = rospy.get_param('~angular_speed_limit', 5.0)7), program flow chart

Precautions for using the handle
When connecting the USB handle receiver, it is recommended to connect it to the outermost USB-HUB expansion board instead of directly connecting to the motherboard or the middle USB-HUB expansion board (X3plus). If it is directly connected to the motherboard or the middle USB-HUB expansion board (X3plus), due to the aluminum alloy blocks on the top and bottom, it will seriously interfere with the handle signal reception.
After plugging and unplugging the handle receiver, you need to restart the handle program, otherwise you will not be able to control the car.
After starting the handle control program, if the handle cannot control the car, it may be caused by the wrong handle control mode. You can press and hold the handle mode button for about 15 seconds to switch modes. After the green indicator light is always on, press the start button again, such as If a buzzer sounds, the switch is successful. If there is no response, you can press and hold the mode button on the handle again for 15 seconds.
Jetson series support mode: PC/PCS mode, the POWER MODE indicator light is red by default in PC mode. You can connect the handle receiver to the USB port of the computer to connect the wireless handle. Enter the URL in the browser: https://gamepad -tester.com/ . Pressing the button URL will display the change of the button value, as shown in the figure below:

Raspberry Pi series supported modes: X-BOX mode. In X-BOX mode, the POWER MODE indicator light is green by default. You can connect the handle receiver to the USB port of the computer to connect the wireless handle. Enter the URL in the browser: https: //gamepad-tester.com/ . Pressing the button URL will display the change of the button value, as shown in the figure below:


- After re-plugging or unplugging the handle receiver or restarting the motherboard, the handle will be reset to factory mode. If it cannot be controlled, the mode will need to be switched again every time it is plugged in, unplugged or restarted.
- If matching is not successful, the POWER MODE indicator light will flash in red and green for a few seconds and then go into sleep mode.