This post was last edited by lb8820265 on 2022-11-8 23:14
Previously, we used ROSBridge to control the little turtle through the web page . Here we will introduce how to control the little turtle using an Android phone.
1. Create a new ROSBridge project from scratch
Learning from Android source code is not the best method. Due to different environments, direct compilation may not be successful, and porting to your own project may not be successful. Therefore, here we introduce creating a new ROSBridge project from scratch.
0. Download source code
Download link at the end of the post.
1. Create a new AS project
This post uses Android Studio 4.1. Create a blank project with the package name "Lb_ROSBridge_Turtle".
2. Copy the sample files to the new project
Copy the files in the "libs" folder to the corresponding location of the new project.
Copy the "jilk" folder to the corresponding location of the new project.
Replace the "activity_main.xml" layout file to the corresponding location of the new project. If the package name is different, remember to modify the package path of tools:context in the file.
Replace the "MainActivity.java" and copy the "RCApplication.java" files to the corresponding locations of the new project. If the package names are different, remember to modify the package name in the first line of the file.
3. Modify the "AndroidManifest.xml" file
Add permissions
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
Associate Application
android:name="com.example.lb_rosbridge_turtle.RCApplication"
Allow external components to start (required on Android 12 and above phones)
android:exported="true"
4. Modify the "build.gradle" file
Add libs folder library path
implementation fileTree(include: ['*.jar'], dir: 'libs')
2. ROSBridge Workflow
All the writing is in the "MainActivity.java" file.
1. Register EventBus to receive messages
EventBus.getDefault().register(this);
2. Call connect to connect to the Raspberry Pi server
connect(String ip, String port)
3. Get the connected Rosbridgeclient class instance
client = ((RCApplication)getApplication()).getRosClient();
4. Send data to the server
client.send(msg);
Subscribing and publishing information are the same command.
5. Get server data
onEvent(final PublishEvent event)
The above is just the main process, for detailed code, see the source code.
3. Operation and Demonstration
Raspberry Pi Server Operation
1. Start the kernel
roscore
2. Start the turtle
rosrun turtlesim turtlesim_node
3. Start the rosbridgea service
roslaunch rosbridge_server rosbridge_websocket.launch
Mobile operation
0. Run the app
You can use online simulation or directly install the APP.
1. Enter the Raspberry Pi IP
The mobile phone and Raspberry Pi need to be in the same LAN.
2. Click "Connect"
If the connection is normal, the connection status will show "Connected"
3. Click "Subscribe to Location"
That is, send a subscription command, and then the Raspberry Pi will keep sending location information to the mobile phone.
4. Input control parameters
Slide the slider to select the control parameters: straight speed, lateral speed and turning speed.
5. Click Publish
After the control parameters are released, the Raspberry Pi turtle will start moving according to the motion parameters.
4. Source Code
GitHub:https://github.com/wanli-car/Examples/tree/master/Android/Lb_ROSBridge_Turtle
Gitee:https://gitee.com/wanli-car/Examples/tree/master/Android/Lb_ROSBridge_Turtle
The APK installation package is attached in the post.