Connect the RoboMaster with the app or Python Script. - HU-ICT-LAB/RobotWars GitHub Wiki

Connecting with the app

Naturaly in order to connect to the app, you need to download the app.

Google Play: https://play.google.com/store/apps/details?id=com.dji.robomaster&gl=NL

PC (Not linux): https://www.dji.com/nl/downloads/softwares/robomaster-win

After downloading the app you need to create an account or login to your account. Now click on connect to see the 2 options for connecting:

  1. Connection via Wi-Fi
  2. Connection via router

Connection via Wi-Fi on the app

After clicking on the connect button, you'll have to click on Connection via Wi-Fi. Then you need to follow the instructions that are given.

wifi connect

Connection via router on the app

After clicking on the connect button, you'll have to click on Connection via router. Then you need to follow the instructions that are given.

router connect no qr code

Once you have entered the password of the router you will see a QR-code. Router connect with qr code

Connect RoboMaster to python code

You can, just like the app, connect the RoboMaster to python with the 2 methods: Wi-Fi and router connection.

Connect via Wi-Fi with python for plaintext SDK

For the Wi-Fi method you first need to turn on the RoboMaster en switch the RoboMaster to Wi-Fi mode like seen on the image under Connection via Wi-Fi on the app. Next u need to connect to the Wi-Fi network of the RoboMaster. After your connect to the RoboMaster Wi-Fi network run this code:

# -*- encoding: utf-8 -*-
# Test environment: Python 3.6

import socket
import sys

# In direct connection mode, the default IP address of the robot is 192.168.2.1 and the control command port is port 40923.
host = "192.168.2.1"
port = 40923

def main():

        address = (host, int(port))

        # Establish a TCP connection with the control command port of the robot.
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

        print("Connecting...")

        s.connect(address)

        print("Connected!")

        while True:

                # Wait for the user to enter control commands.
                msg = input(">>> please input SDK cmd: ")

                # When the user enters Q or q, exit the current program.
                if msg.upper() == 'Q':
                        break

                # Add the ending character.
                msg += ';'

                # Send control commands to the robot.
                s.send(msg.encode('utf-8'))

                try:
                        # Wait for the robot to return the execution result.
                        buf = s.recv(1024)

                        print(buf.decode('utf-8'))
                except socket.error as e:
                        print("Error receiving :", e)
                        sys.exit(1)
                if not len(buf):
                        break

        # Disconnect the port connection.
        s.shutdown(socket.SHUT_WR)
        s.close()

if __name__ == '__main__':
        main()

After you have made the connection like this, you need to enter the line 'command' in the terminal to enter plaintext mode. When in this mode you can enter a plaintext command to make the RoboMaster perform actions. These commands are documented here.

Connection via router with the RoboMaster Python library

The first step to make the RoboMaster perform action via Python is to connect the RoboMaster to the same network as your computer. You need a QR-code to connect the RoboMaster to the network. This can be done with the app like stated in Connection via router on the app. To create the needed QR-code without the app, you can run the script under this paragraph. Switch the RoboMaster on network mode, press the red button next to the switch and scan the QR-code.

if __name__ == '__main__':

    helper = conn.ConnectionHelper()
    info = helper.build_qrcode_string(ssid="RobotWars", password="39971388")
    myqr.run(words=info)
    time.sleep(1)
    img = Image.open(QRCODE_NAME)
    img.show()
    if helper.wait_for_connection():
        print("Connected!")
    else:
        print("Connect failed!")

source: https://robomaster-dev.readthedocs.io/en/latest/python_sdk/connection.html

The RoboMaster should be connect with the network now. Now to make the RoboMaster perform actions, you need a few code lines to connect the RoboMaster to python. These lines are:

from robomaster import robot

ep_robot = robot.Robot()
ep_robot.initialize(conn_type="sta") #, sn="insert sn code of the robot") to connect to a certain robot

# enter code to make the RoboMaster perform action HERE!
# Example(RoboMaster drives 0.5m forward):
# ep_chassis = ep_robot.chassis
# ep_chassis.move(x=0.5, y=0, z=0, xy_speed=1)

ep_robot.close() # closes the connect toy stop the program after the RoboMaster has performed the action note in the code

Sources:

Extra example sources

For more info over code to perform actions, click

here (half chinese) or

here (incomplete but can still be useful).

Info sources

  1. RoboMaster. (2020). [Computer/mobile app]. RoboMaster DJI TECHNOLOGY CO.
  2. Introduction — RoboMaster Developer Guide documentation. (n.d.). RoboMaster Developer Guide. Retrieved October 15, 2021, from https://robomaster-dev.readthedocs.io/en/latest/introduction.html

Related issues

Issues: #6 & #7