Package: rtcbot
RTCBotRTCBot's purpose is to provide a set of simple modules that help in developing remote-controlled robots in Python, with a focus on the Raspberry Pi.The documentation includes tutorials that guide in developing your robot, starting from a basic connection between a Raspberry Pi and Browser, and encompass
creating a video-streaming robot controlled entirely over a 4G mobile connection, all the way to a powerful system that offloads complex computation to a desktop PC in real-time.All communication happens throughWebRTC,
using Python 3's asyncio and the wonderfulaiortclibrary,
meaning that your robot can be controlled with low latency both from the browser and through Python,
even when it is not connected to your local network.The library is explained piece by piece inthe documentation.See Documentation & TutorialsInstallingRTCBot relies on some Python libraries that will need to be compiled by pip, so
you need to install their dependencies. The following commands will work on Ubuntu and Raspbian Buster:sudoapt-getinstallbuild-essentialpython3-numpypython3-cffipython3-aiohttp\libavformat-devlibavcodec-devlibavdevice-devlibavutil-dev\libswscale-devlibswresample-devlibavfilter-devlibopus-dev\libvpx-devpkg-configlibsrtp2-devpython3-opencvpulseaudioAnd then install rtcbot:sudopip3installrtcbotFor installation instructions for Windows and Mac,refer to the documentationExampleThis example uses RTCBot to live stream a webcam to the browser. For details, please look atthe tutorials.Python code that streams video to the browser:fromaiohttpimportwebroutes=web.RouteTableDef()fromrtcbotimportRTCConnection,getRTCBotJS,CVCameracamera=CVCamera()# For this example, we use just one global connectionconn=RTCConnection()conn.video.putSubscription(camera)# Serve the RTCBot javascript library at /rtcbot.js@routes.get("/rtcbot.js")asyncdefrtcbotjs(request):returnweb.Response(content_type="application/javascript",text=getRTCBotJS())# This sets up the connection@routes.post("/connect")asyncdefconnect(request):clientOffer=awaitrequest.json()serverResponse=awaitconn.getLocalDescription(clientOffer)returnweb.json_response(serverResponse)@routes.get("/")asyncdefindex(request):withopen("index.html","r")asf:returnweb.Response(content_type="text/html",text=f.read())asyncdefcleanup(app=None):awaitconn.close()camera.close()app=web.Application()app.add_routes(routes)app.on_shutdown.append(cleanup)web.run_app(app)Browser code (index.html) that displays the video stream:<html><head><title>RTCBot: Video</title><scriptsrc="/rtcbot.js"></script></head><bodystyle="text-align: center;padding-top: 30px;"><videoautoplayplaysinline></video><audioautoplay></audio><p>Open the browser's developer tools to see console messages (CTRL+SHIFT+C)</p><script>varconn=newrtcbot.RTCConnection();conn.video.subscribe(function(stream){document.querySelector("video").srcObject=stream;});asyncfunctionconnect(){letoffer=awaitconn.getLocalDescription();// POST the information to /connectletresponse=awaitfetch("/connect",{method:"POST",cache:"no-cache",body:JSON.stringify(offer),});awaitconn.setRemoteDescription(awaitresponse.json());console.log("Ready!");}connect();</script></body></html>DevelopmentTo usertcbotcode directly, clone the repository, and install the requirements:pip install -r requirements.txtThen, you will need to perform the javascript build step, to prepare the browser code. This step requires bothmakeandnpmto be installed on your machine. To build the javascript, type in:make jsThis createsrtcbot/rtcbot.js, which is returned byrtcbot.getRTCBotJS().After these two steps, you should be able to successfully import and use the library.