Controlling the RoboMaster with an External Microcontroller - HU-ICT-LAB/RobotWars GitHub Wiki

This page contains info and a HowTo on controlling and communicating with the RoboMaster S1 using a microcontroller.

Sections

Hardware Interfaces

There are three methods of communication between a microcontroller and a RoboMaster:

USB

Communication using USB is relatively straightforward, allowing for quick, fast, and easy communication. Requirements: The external microcontroller must have a type-A USB interface and support the RNDIS function.

HowTo:

  1. Turn on the S1
  2. Connect the microcontroller to the smart central control of the S1 via the USB input shown above.
  3. Use the plaintext SDK to control the RoboMaster directly.

Download the Test USB/WiFi Connection Script found in this page's "Test Script" section and uncomment the IP address corresponding to the USB connection method. Run the script to start executing SDK commands.

WiFi

Communication using WiFi allows for wireless control of the S1, with the added benefit of not requiring an external power supply to power the PI as it's not mounted on the RoboMaster.

There are two WiFi connection modes, namely direct connection mode and router mode.

Direct connection mode

The external controller will connect directly to the S1's hotspot.

Requirements: The external microcontroller must have a WiFi function. (For example, the Raspberry PI zero W

HowTo:

  1. Turn on the S1 and set the connection mode switch of the smart central control to "direct connection mode"
  2. Turn on the wireless network of the microcontroller and then scan and connect to the S1 hotspot
  3. Use the plaintext SDK to control the RoboMaster directly.

Download the Test USB/WiFi Connection Script found in this page's "Test Script" section and uncomment the IP address corresponding to the direct connection method. Run the script to start executing SDK commands.

Router connection mode

The external controller will connect to the S1 using a shared LAN connection

Requirements: The external microcontroller must have a WiFi function or wired network function.

HowTo:

  1. Turn on the S1 and set the connection mode switch of the smart central control to "router mode"
  2. Connect the S1 to the router by scanning the QR code in the official app.
  3. Connect the microcontroller to the same router via WiFi or a wired connection.
  4. Obtain the S1's IP address using either the official app (connect the robot -> 'Settings' -> 'Connection') or using the 'Monitor Robot Address Broadcasting Script' found in the "Test Scripts" section of this page.

Download the Test USB/WiFi Connection Script found in this page's "Test Script" section and uncomment the IP address corresponding to the router connection method. Run the script to start executing SDK commands.

UART

Communication using UART allows for a microcontroller to control the S1 using the UART port of the S1 motion controller.

Requirements: The external microcontroller must have a UART interface or a serial-to-USB function

HowTo

  1. Turn on the S1
  2. Connect the TX/RX and GND terminals of the UART module of the microcontroller to the RX/TX and GND terminals of the UART module of thee S1's motion controller as shown above
  3. Determine which port the S1 is connected to

Download the Test UART Connection Script found in this page's "Test Script" section and uncomment the IP address corresponding to the USB connection method. Run the script to start executing SDK commands.

Control using Raspberry PI via USB connection

This section contains a step by step process on connecting a Raspberry PI to a RoboMaster S1 using USB. Other methods can also be used by following the steps outlined in the "Hardware Interfaces" section.

Components:

  • 1X Raspberry PI (version does not matter as long as a USB can be connected)
  • 1X Raspberry PI power cable (Or another method of powering the RPI)
  • 1X Micro-SD card for Raspberry PI OS
  • 1X Type-A USB-cable
  • 1X RoboMaster S1

steps:

  1. Install the official Raspberry Pi Imager from their website (works on Ubuntu, Windows and macOS)
  2. Connect the Micro-SD card to a PC and install the Raspberry OS_ on it using the imager
  3. Connect the raspberry PI to a monitor using the HDMI interface (Or use a headless connection, as long as the RPI can be accessed)
  4. Start the S1
  5. Connect the microcontroller to the smart central control of the S1 via the USB input
  6. Download the Test USB/WiFi Connection Script found in this page's "Test Script" section and uncomment the IP address corresponding to the USB connection method.
  7. Finally, run the test script to start executing SDK commands.

It's that simple

Test scripts

Monitor Robot Address Broadcasting script

# -*- encoding: utf-8 -*-
import socket

ip_sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

# Bind with the IP broadcasting port.
ip_sock.bind(('0.0.0.0', 40926))

# Wait to receive data.
ip_str = ip_sock.recvfrom(1024)

# Output the data.
print(ip_str)

Test USB/WiFi Connection Script

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

import socket
import sys

#The following commented variables should be commented out depending on the communication method used.
#host = "192.168.2.1"   #Direct Connection, standard for all RoboMasters in direct connection mode 
#host = "192.168.0.115" #Router Connection, replace for the actual IP of RoboMaster
#host = "192.168.42.2"  #USB Connection, IP is default for all RoboMasters
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()

Test UART Connection Script

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

ser = serial.Serial()

# Set the baud rate to 115200, 8 data bits, 1 stop bit, no parity bit, and set the timeout period to 0.2 seconds for the serial port.

ser.port = 'COM3' #Change to port S1 is connected to
ser.baudrate = 115200
ser.bytesize = serial.EIGHTBITS
ser.stopbits = serial.STOPBITS_ONE
ser.parity = serial.PARITY_NONE
ser.timeout = 0.2

# Open the serial port.
ser.open()

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 += ';'

        ser.write(msg.encode('utf-8'))

        recv = ser.readall()

        print(recv.decode('utf-8'))

# Close the serial port.
ser.close()

Sources

  1. DJI. (n.d.). Third-party Platform Communication — RoboMaster Developer Guide documentation. RoboMaster Developer Documentation. Retrieved November 12, 2021, from https://robomaster-dev.readthedocs.io/en/latest/third_part_comm.html

Related issues

Issues: #9 & #17