Welcome to ROSMASTER R2 repository
3. Robot control
3、Robot control
1、Program function description
Open the chassis, run the handle/keyboard control program, you can control the trolley movement through the handle or keyboard, and the handle also controls the buzzer, controls the light strip and other functions.
2、Program code reference path
For Raspberry Pi PI5 master control, you need to enter the docker container first, but for Orin motherboard, you do not need to enter.
the location of the source code of the controller control function is located, taking the X3 model as an example
~/yahboomcar_ros2_ws/yahboomcar_ws/src/yahboomcar_ctrl/yahboomcar_ctrl/yahboom_joy_R2.pythe location of the source code of the keyboard control function is located at,
xxxxxxxxxx~/yahboomcar_ros2_ws/yahboomcar_ws/src/yahboomcar_ctrl/yahboomcar_ctrl/yahboom_keyboard.py3、The program starts
xxxxxxxxxx#The underlying driverros2 run yahboomcar_bringup Ackman_driver_R2#Handle controlros2 run yahboomcar_ctrl yahboom_joy_R2ros2 run joy joy_node#Keyboard controlros2 run yahboomcar_ctrl yahboom_keyboardIt should be noted here that the controller and the keyboard cannot run at the same time, because after the keyboard control is started, when the keyboard is not pressed, the default is to send a message with 0 data of speed.
3.1、Controller control
After turning on, press the "START" button, hear the buzzer sound, and you can start the remote control. The remote control will enter sleep mode after being turned on for a period of time, and you need to press the "START" button to end sleep. If you want to control the trolley running, you also need to press the R2 key and release the motion control lock before you can use the joystick to control the trolley movement.
Remote control effect description,
| handle | effect |
|---|---|
| Left stick up/down | Go straight forward/backward |
| Left stick left/right | Go straight left/right |
| Right stick left/right | Rotate left/rotate right |
| The right "1" key | Control the strip light effect |
| The right "2" key | Unlock/lock motion control |
| "START" button | Control the buzzer/end hibernation |
| The left stick is pressed | Adjust the speed of the X/Y axis |
| The right stick is pressed | Adjust the angular velocity magnitude |
3.2、keyboard control
Key description,
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】 |
Speed control
| Key | Speed changes | Key | Speed changes |
|---|---|---|---|
| 【q】 | 10% increase in both linear and angular velocities | 【z】 | 10% reduction in both linear and angular velocities |
| 【w】 | Only 10% increase in line speed | 【x】 | Only 10% reduction in line speed |
| 【e】 | Only 10% increase in angular velocity | 【c】 | Only 10% reduction in angular velocity |
| 【t】 | Linear speed X/Y direction switching | 【s】 | Stop keyboard control |
3.3、node communication
Handle control trolley node communication diagram,

Keyboard control trolley node communication diagram,

4、core code parsing
4.1、Controller control code
Taking the X3 model as an example, we said in the previous lesson that the chassis control program is the Mcnamu_driver_X3.py, defining three subscribers: speed (/cmd_vel), light strip effect (/RGBLight), and buzzer (/buzzer), so we can control speed, light strip and buzzer as long as we publish this type of topic data yahboom_joy_X3.py the controller control code program.
xxxxxxxxxx#create pubself.pub_goal = self.create_publisher(GoalID,"move_base/cancel",10)self.pub_cmdVel = self.create_publisher(Twist,'cmd_vel', 10)self.pub_Buzzer = self.create_publisher(Bool,"Buzzer", 1)self.pub_RGBLight = self.create_publisher(Int32,"RGBLight" , 10)self.pub_JoyState = self.create_publisher(Bool,"JoyState", 10)
In addition, we need to subscribe to the "joy" topic data, which tells us that the keys (joysticks and keys) have changed, that is,
xxxxxxxxxx#create subself.sub_Joy = self.create_subscription(Joy,'joy', self.buttonCallback,10)
The main look is the callback function of this joy topic, which parses the received value, then assigns the value to the publisher's variable, and finally publishes it.
xxxxxxxxxxdef buttonCallback(self,joy_data):if not isinstance(joy_data, Joy): returnif self.user_name == "root": self.user_jetson(joy_data)else: self.user_pc(joy_data)
The function jumps here are self.user_jetson, and the parameter variable passed in is the topic received,
xxxxxxxxxxdef user_jetson(self, joy_data):
Take the control buzzer as an example for analysis,
xxxxxxxxxxif joy_data.buttons[11] == 1:Buzzer_ctrl = Bool()self.Buzzer_active=not self.Buzzer_activeBuzzer_ctrl.data =self.Buzzer_activefor i in range(3): self.pub_Buzzer.publish(Buzzer_ctrl)<