Welcome to ROSMASTER A1 repository
10. AI large model offline voice assistant
10. AI Large Model Offline Voice Assistant
10. AI Large Model Offline Voice Assistant1. Offline Voice Configuration2. Create a Systemd Service2.1 Raspberry Pi 52.1.1 Creating a Docker Container Startup Script2.1.2 If you are using the factory image of Yabo's AI large model, you do not need to recreate the script. Just run the following command to start the automatic startup service:2.1.3 Related commands:2.1.4 CSI Camera Auto-Start ServiceIf you are using the factory image of Yabo's AI large model, you do not need to recreate the script. Just run the following command to restart the auto-start service:2.2 RDK X52.2.1 Creating a Startup Script2.2.2 Create Systemd service file2.2.3 Managing and Debugging the Service2.3 ORIN2.3.1 Creating a Startup Service (Systemd)2.3.2 Creating a Startup Script2.3.3 Creating the Systemd Service File2.3.4 Managing and Debugging the Service
Note: Raspberry Pi 5 2GB/4GB, RDK X5 4GB, and Jetson Orin Nano 4GB versions cannot run offline due to performance limitations. Please refer to the online large model tutorial.
1. Offline Voice Configuration
Before setting up auto-start, we must ensure that the program itself can work independently offline. This requires modifying the configuration file.
- Locate the configuration file:
In your project code, find and open the configuration file:
xxxxxxxxxxvim ~/yahboom_ws/src/largemodel/config/yahboom.yaml
- Modify configuration parameters:
Please check the following parameters in the file and ensure that their values are consistent with those shown below. If the parameters are not present, please add them.
xxxxxxxxxx asr#Voice node parameters ros__parameters VAD_MODE2 #vad sensitivity sample_rate16000 #asr recording audio sampling rate frame_duration_ms30 #vad frame size in ms use_oline_asrFalse #Whether to use online ASR recognition (True uses online, False uses offline) mic_serial_port"/dev/ttyUSB0" #Microphone serial port alias mic_index0 #Microphone Index language'zh' #asr language regional_setting "China" #international:International version China:Domestic version model_service#Model server node parameters ros__parameters language'zh' #Large Model Interface Language useolinettsFalse #Whether to use online speech synthesis (True uses online, False uses offline) # Large model configuration # llm_platform: 'ollama' # Optional platform: 'ollama', 'tongyi', 'spark', 'qianfan', 'openrouter' llm_platform'ollama' # Currently selected large model platform regional_setting "China" use_oline_asranduseolinettsmust be set to False.llm_platformmust be set toollama.This setting will make everything offline.
Save the file and recompile the project to apply the changes:
xxxxxxxxxxcd ~/yahboom_wscolcon buildsource install/setup.bash
After completing this step, the program is now a purely offline voice service.
2. Create a Systemd Service
Now, we will create a systemd service to automatically run largemodel_control.launch.py at system startup.
Note: If you are using the Yabo AI large model factory image, these scripts are already created; you only need to restart the auto-start container.
2.1 Raspberry Pi 5
2.1.1 Creating a Docker Container Startup Script
Creating a Script File:
In your home directory (
~), create a file namedros2_docker_autostart.sh.xxxxxxxxxxvim ~/ros2_docker_autostart.shWrite the Script Content: Copy and paste the following content into the script file.
xxxxxxxxxxset -eCN=${CONTAINER_NAME:-ros_humble_ai_service}IMG=${IMAGE_NAME:-ros_humble_ai:v1.0}WS=${HOST_WS_DIR:-"$HOME/yahboom_ws"}ROS=${ROS_DISTRO:-humble}CMD=${LAUNCH_CMD:-"ros2 launch largemodel largemodel_control.launch.py"}AUID=${AUDIO_UID:-$(id -u)}XAUTH=${XAUTHORITY:-"$HOME/.Xauthority"}mkdir -p "$WS"x=()if [ -d /tmp/.X11-unix ]; thencommand -v xhost >/dev/null 2>&1 && xhost +local:root || truex+=( -e DISPLAY=${DISPLAY:-:0} -e QT_X11_NO_MITSHM=1 -v /tmp/.X11-unix:/tmp/.X11-unix )[ -f "$XAUTH" ] && x+=( -v "$XAUTH:/root/.Xauthority" -e XAUTHORITY=/root/.Xauthority )fia=()if [ -S "/run/user/$AUID/pulse/native" ]; thena+=( -e PULSE_SERVER=unix:/run/user/$AUID/pulse/native -v "/run/user/$AUID/pulse:/run/user/$AUID/pulse:ro" )[ -d "$HOME/.config/pulse" ] && a+=( -v "$HOME/.config/pulse:/root/.config/pulse:ro" )fid=()[ -e /dev/video0 ] && d+=( --device=/dev/video0 )[ -d /dev/bus/usb ] && d+=( --device=/dev/bus/usb )[ -d /dev/snd ] && d+=( --device=/dev/snd )[ -e /dev/mic ] && d+=( -v /dev/mic:/dev/mic )if docker ps -a --format '{{.Names}}' | grep -qx "$CN"; thendocker ps --format '{{.Names}}' | grep -qx "$CN" || docker start "$CN" >/dev/nullelsedocker run -d \--name "$CN" --net=host --privileged --restart unless-stopped \--security-opt apparmor:unconfined \-v "$WS:/root/yahboom_ws:rw" \"${d[@]}" "${x[@]}" "${a[@]}" \"$IMG" \bash -lc "source /opt/ros/$ROS/setup.bash; [ -f /root/yahboom_ws/install/setup.bash ] && source /root/yahboom_ws/install/setup.bash; $CMD"fiecho "OK: $CN"Save and exit
Confirm that Docker starts at boot:
xxxxxxxxxxsudo systemctl enable --now dockerGive the script execution permissions, then run:
xxxxxxxxxxchmod +x ~/ros2_docker_autostart.sh./ros2_docker_autostart.shVerification:
xxxxxxxxxxdocker inspect -f '{{.HostConfig.RestartPolicy.Name}}' ros_humble_ai_serviceThe output should be "unless-stopped". Docker will automatically start the container each time you boot the computer, enabling the voice assistant to start automatically.
2.1.2 If you are using the factory image of Yabo's AI large model, you do not need to recreate the script. Just run the following command to start the automatic startup service:
xxxxxxxxxx~/ros2_docker_autostart.sh
2.1.3 Related commands:
Stop the container immediately, and also stop it from automatically starting:
xxxxxxxxxxdocker stop ros_humble_ai_service
View the running log of the docker container:
xxxxxxxxxxdocker logs -f ros_humble_ai_service
2.1.4 CSI Camera Auto-Start Service
To access the CSI camera feed in Docker, you need to start host_stream.py on the host machine. Therefore, if you need to access the CSI camera feed during auto-start, you must also add this file to the Systemd startup service.
Note: If you are using the Yabo AI large model factory image, these scripts are already created; you only need to restart the auto-start service.
1.Enter the command to write the file
xxxxxxxxxxsudo vim /etc/systemd/system/host-stream.service
2.Copy the following content into the file
xxxxxxxxxx[Unit]Description=Host stream (runs ~/host_stream.py)After=network.target[Service]Type=simpleUser=piWorkingDirectory=/home/piExecStart=/usr/bin/python3 /home/pi/host_stream.pyRestart=on-failureRestartSec=5StandardOutput=append:/home/pi/host_stream.logStandardError=append:/home/pi/host_stream.log[Install]WantedBy=multi-user.target
3.Save and exit.
4.Apply and start the service:
xxxxxxxxxxsudo systemctl daemon-reloadsudo systemctl enable host-stream.servicesudo systemctl start host-stream.service
5.Check the service status:
xxxxxxxxxxsudo systemctl status host-stream.service如果看到 `Active: active (running)`,the service has started successfully!6.If you do not need the CSI camera self-start service, run the following command to stop it:
xxxxxxxxxxsudo systemctl stop host-stream.servicesudo systemctl disable host-stream.service
If you are using the factory image of Yabo's AI large model, you do not need to recreate the script. Just run the following command to restart the auto-start service:
xxxxxxxxxxsudo systemctl enable host-stream.servicesudo systemctl start host-stream.service
2.2 RDK X5
2.2.1 Creating a Startup Script
To ensure that systemd can correctly load the ROS2 environment, the best practice is to create a simple bash script to encapsulate our startup command.
Creating a script file: In the directory (
~/yahboom_ws/src/largemodel/), create a file namedstart_largemodel.sh.xxxxxxxxxxvim ~/yahboom_ws/src/largemodel/start_largemodel.shWrite script content: Copy and paste the following content into the script file.
xxxxxxxxxx# Source ROS2 Humble environmentsource /opt/ros/humble/setup.bash# Source Yahboom Workspace Environmentsource /home/sunrise/yahboom_ws/install/setup.bash# Start the largemodel control scriptros2 launch largemodel largemodel_control.launch.pyImportant Note: Please ensure that
/home/sunrise/in the script is replaced with your own user home directory path.Save and Exit
Grant script execution permissions:
xxxxxxxxxxchmod +x ~/yahboom_ws/src/largemodel/start_largemodel.sh
2.2.2 Create Systemd service file
Create service file: You need to use
sudoprivileges to create this file.xxxxxxxxxxsudo vim /etc/systemd/system/largemodel.serviceWrite service configuration: Copy and paste the following content into the service file.
xxxxxxxxxx[Unit]Description=Robot ServiceAfter=network.target sound.target graphical.target multi-user.targetWants=network.target sound.target graphical.target multi-user.target[Service]Type=simpleUser=sunriseGroup=sunriseEnvironment=DISPLAY=:0Environment=XDG_RUNTIME_DIR=/run/user/1000Environment=PULSE_SERVER=unix:/run/user/1000/pulse/nativeSupplementaryGroups=audio videoExecStartPre=/bin/sleep 10ExecStart=/home/sunrise/yahboom_ws/src/largemodel/start_largemodel.shRestart=on-failureStandardOutput=journalStandardError=journal[Install]WantedBy=multi-user.targetEnvironment=DISPLAY=:0will display a GUI pop-up on the desktop. Users connecting via SSH can change this toEnvironment=DISPLAY=:10. You can find the specific parameters by typingecho $DISPLAYin the terminal.- The path in
ExecStartmust be exactly the same as the path to the startup script to be executed.
Save and exit.
2.2.3 Managing and Debugging the Service
Now that your service has been created, we need to have systemd load it and set it to start automatically on boot.
- Reload the
systemddaemon so it reads the service file we just created:
xxxxxxxxxxsudo systemctl daemon-reload- Set the service to start automatically on boot:
xxxxxxxxxxsudo systemctl enable largemodel.service- Start the service immediately:
xxxxxxxxxxsudo systemctl start largemodel.service- Check the service status:
This is the most important command to verify that the service is running successfully.
xxxxxxxxxxsudo systemctl status largemodel.service* If you see `Active: active (running)`, congratulations, the service has started successfully!* If the status is `failed` or something else, please continue to the next step for debugging.- View Service Logs (Essential for Debugging):
If the service fails to start, you can view all real-time logs generated by the ros2 launch command using the following command. This is crucial for troubleshooting.
xxxxxxxxxxjournalctl -u largemodel.service -fAfter completing all the above steps, the purely offline largemodel voice service will now start automatically every time you boot up.
To disable the automatic startup service, you can run the following command:
xxxxxxxxxxsudo systemctl start largemodel.servicesudo systemctl disable largemodel.service
2.3 ORIN
2.3.1 Creating a Startup Service (Systemd)
Now, we will create a systemd service so that largemodel_control.launch.py runs automatically at system startup.
2.3.2 Creating a Startup Script
To ensure that systemd can correctly load the ROS2 environment, the best practice is to create a simple bash script to encapsulate our startup command.
Create the script file:
In the directory (
~/yahboom_ws/src/largemodel/), create a file namedstart_largemodel.sh.
xxxxxxxxxxvim ~/yahboom_ws/src/largemodel/start_largemodel.shWrite the script content:
Copy and paste the following content into the script file.
xxxxxxxxxx# Source the ROS2 Humble environmentsource /opt/ros/humble/setup.bash# Source the Yahboom workspace environmentsource /home/jetson/yahboom_ws/install/setup.bash# Launch the largemodel control scriptros2 launch largemodel largemodel_control.launch.pyImportant Note: Please ensure that /home/sunrise/ in the script is replaced with your own user home directory path.
- Save and Exit
- Grant Script Execution Permissions:
xxxxxxxxxxchmod +x ~/yahboom_ws/src/largemodel/start_largemodel.sh2.3.3 Creating the Systemd Service File
This is the most crucial step. We will tell the system that we have a new service to manage.
Create the Service File:
You need to use
sudoprivileges to create this file.
xxxxxxxxxxsudo vim /etc/systemd/system/largemodel.serviceWrite the Service Configuration:
Copy and paste the following content completely into the service file.
xxxxxxxxxx[Unit] Description=Robot Service After=network.target sound.target graphical.target multi-user.target Wants=network.target sound.target graphical.target multi-user.target [Service] Type=simple User=sunrise Group=sunrise Environment=DISPLAY=:0 Environment=XDG_RUNTIME_DIR=/run/user/1000 Environment=PULSE_SERVER=unix:/run/user/1000/pulse/native SupplementaryGroups=audio video ExecStartPre=/bin/sleep 10 ExecStart=/home/sunrise/yahboom_ws/src/largemodel/start_largemodel.sh Restart=on-failure StandardOutput=journal StandardError=journal [Install] WantedBy=multi-user.target ExecStartThe path in the command must be exactly the same as the path to the startup script to be executed.
- Save and exit.
2.3.4 Managing and Debugging the Service
Now that your service has been created, we need to get systemd to load it and set it to start automatically on boot.
- Reload the
systemddaemon so that it reads the service file we just created:
xxxxxxxxxxsudo systemctl daemon-reload- Set the service to start automatically on boot:
xxxxxxxxxxsudo systemctl enable largemodel.service- Start the service immediately:
xxxxxxxxxxsudo systemctl start largemodel.service- Check the service status:
This is the most important command to verify that the service is running successfully.
xxxxxxxxxxsudo systemctl status largemodel.service* If you see `Active: active (running)`, congratulations, the service has started successfully!* If the status is `failed` or something else, please continue to the next step for debugging.- View service logs (essential for debugging):
If the service fails to start, you can view all real-time logs generated by the ros2 launch command using the following command. This is crucial for locating the problem.
xxxxxxxxxxjournalctl -u largemodel.service -fAfter completing all the above steps, the purely offline largemodel voice service will now start automatically every time you boot up.