RoArm-M2-S ESP-NOW Control

From Waveshare Wiki
Jump to: navigation, search

ESP-NOW SETTINGS

This tutorial primarily focuses on configuring ESP-NOW for the robotic arm. After configuration, the robotic arm can achieve one-to-one and one-to-many ESP-NOW communication control using JSON commands.

ESP-NOW Communication Protocol

ESP-NOW, originally used on Espressif WiFi chips such as ESP8266 and ESP32, is a high-speed, efficient wireless communication protocol developed by Espressif Systems, aiming to provide a low-power, high-performance LAN (Local Area Network) communication solution.

1. Quick Response: As the ESP-NOW protocol does not require TCP/IP protocol stack to address, it is low-delay and is suitable for application scenarios with high real-time requirements.
2. High-throughout: ESP-NOW protocol uses frame broadcasting to transmit data with high transmission efficiency, enabling simultaneous transmission of the same information to multiple nodes.
3. Low-power: ESP-NOW protocol uses very low power in data transmission, and is suitable for battery power supply.
4. User-friendly: The configuration of ESP-NOW protocol is easy, only by defining the basic parameters such as the channels, encryption key, and data structure can it realize the wireless communication.
5. Multi-cast Support: ESP-NOW protocol supports one-to-many and one-to-many communication, improving the network flexibility.
6. Expandability: ESP-NOW protocol supports multi-data transmission and can realize more complicated communication functions by defining the data structure.

ESP-NOW Functionalities

RoArm-M2-S comes with ESP-NOW functionality, and by default, is in Follower mode upon factory startup. In this mode, RoArm-M2-S can be controlled by other devices or can send ESP-NOW messages to other devices to control them.

ESP-NOW Communication Data Structure

Before explaining the meanings of various functional commands, let's first introduce the ESP-NOW communication data structure of RoArm-M2-S, as follows:

typedef struct struct_message {
  byte devCode;
  float base;
  float shoulder;
  float elbow;
  float hand;
  byte cmd;
  char message[210];
} struct_message;

In this part, the specific value for variables will be set in the JSON commands. Here, we introduce the meaning of variables:

  • devCode: this device code. We do not use this variable in the demo we use, so you can choose not to set this code (set it as 0). You can set this variable to differentiate the devices controlled by JSON commands when there are many devices that should be controlled through broadcast rather than multicast. This interface is reserved for you to modify the OnDataRecv() function of esp_now_ctrl.h in the slave demo.
  • base: BASE joint angle (in radian)
  • shoulder: SHOULDER joint angle (in radian)
  • elbow: ELBOW joint angle (in radian)
  • hand: GRIPPER/WRIST joint angle (in radian)
  • cmd: ESP-NOW control message types. For specific valuing, you can refer to the following JSON command introduction.
  • message: A series of strings. When the ESP-NOW control message type is 1, this character string can be used in JSON format, but the command will not generate the long-time blocking command.

Next, let's explore the specific meaning of the related JSON command for ESP-NOW configuration.

Set the Broadcast Follower Mode

{"T":300,"mode":1}
{"T":300,"mode":0,"mac":"00:00:00:00:00:00"}
  • 300: This command is CMD_BROADCAST_FOLLOWER for setting the broadcast follower mode of this device.
  • mode:
    • 1 - [factory default] This device can be controlled by the broadcast signal from other devices.
    • 0 - This device can not be controlled by the broadcast signal from other devices.
  • mac: the MAC address of the only Leader controlling the device when the mode =0.
    • The device can be controlled only by the ESP-NOW messages sent by the devices with a given MAC address.
    • If the device does not need to be controlled by other devices, you can randomly input a MAC address such as 00:00:00:00:00:00.

Set the ESP-NOW Operation Mode

{"T":301,"mode":3}
  • 301: This command is CMD_ESP_NOW_CONFIG for setting the operation mode of ESP-NOW for this device.
  • mode (byte): The code of the ESP-NOW operation mode.
    • 0 - Turn off.
    • 1 - Stream multicast Leader continuously sends its joint position information to devices in the peerList.
    • 2 - Stream unicast/Stream broadcast Leader, continuously sends its joint position messages to a Mac address. When the Mac address is set to FF:FF:FF:FF:FF:FF, it is in stream broadcast control.
    • 3 - [factory default] Follower, when the robotic arm is in this mode, it can receive the ESP-NOW message from other Leader devices, or can send the ESP-NOW message to other devices as the Leader device.

Note: Only in stream multicast, stream unicast/stream broadcast modes, the master control device continuously sends its joint position information to the controlled devices. The controlled devices will mimic the same movement when the master control device is manually rotated.

Get MAC Address of This Device

{"T":302}
  • 302: This command is CMD_GET_MAC_ADDRESS for obtaining the MAC address of this device.

After inputting this command, the return value is shown below:

44:17:93:EE:FD:70

Each device has a unique MAC address. When using ESP-NOW features, except for broadcast control, you need to obtain the MAC address of the controlled device. By default, the OLED screen of each robotic arm displays its own MAC address.

Add the MAC Address of the Controlled Device to PeerList

{"T":303,"mac":"44:17:93:EE:FD:70"}
  • 303: This command is CMD_ESP_NOW_ADD_FOLLOWER for adding the MAC address of the controlled device to peerList, and peerList is for storing the MAC address.
  • mac: The MAC address of the controlled device to be added.
  • You can add dozens of MAC addresses to the peerList for multicast control. However, when using multicast control, the peerList should not include the MAC address for broadcast control: FF:FF:FF:FF:FF:FF.

Remove the MAC Address of the Controlled Device from the peerList

{"T":304,"mac":"44:17:93:EE:FD:70"}
  • 304: This command is CMD_ESP_NOW_REMOVE_FOLLOWER for deleting the MAC address of the controlled device from peerList.
  • mac: The MAC address of the controlled device to be deleted.

Multi-cast Control

# Control the device of peerList to rotate the joints to the specified angle in the multi-cast device.
{"T":305,"dev":0,"b":0,"s":0,"e":1.57,"h":1.57,"cmd":0,"megs":"hello!"}

# Multi-cast control, send the JSON command to turn on the LED to the devices in peerlist.
{"T":305,"dev":0,"b":0,"s":0,"e":0,"h":0,"cmd":1,"megs":"{\"T\":114,\"led\":255}"}
  • 305: This command is CMD_ESP_NOW_GROUP_CTRL. Use this command to send a message via ESP-NOW multicast to all devices in the peerList.

Please refer to the content of #ESP-NOW Communication Data Structure. The various keys in this JSON command correspond to the structure variables as follows, with additional supplementary parts:

  • dev: the corresponding variable is devCode.
  • b, s, e, h: respectively corresponds to the variable base, shoulder, elbow, and hand.
  • cmd: ESP-NOW control message types.
    • When cmd is 0, the controlled robotic arm will rotate to the specified angle of all joints.
    • When cmd is 1, b, s, e, and h are invalid. When a non-blocking JSON command is input into the message (megs), the robotic arm will execute the functionality corresponding to the JSON command.
  • megs: the corresponding variable is the message.

Unicast/Broadcast Cpntrol

# Unicast control, sends the command of controlling the joint angle to the specific device. 
{"T":306,"mac":"44:17:93:EE:FD:70","dev":0,"b":0,"s":0,"e":1.57,"h":1.57,"cmd":0,"megs":"hello!"}

# Broadcast control, sends the command of controlling the joint angle to all devices  
{"T":306,"mac":"FF:FF:FF:FF:FF:FF","dev":0,"b":0,"s":0,"e":1.57,"h":1.57,"cmd":0,"megs":"hello!"}
  • 306: This command is CMD_ESP_NOW_SINGLE for controlling the device of peerList in unicast or broadcast control.
  • mac: The MAC address of the controlled device. When the MAC address of the controlled device is FF:FF:FF:FF:FF:FF:FF:FF, the command is a broadcast signal and will be sent to all devices.

Referring to #ESP-NOW Communication Data Structure, each key in this JSOB command is referenced to the following structural variables with supplements:

  • dev: the corresponding variable devCode.
  • b, s, e, h: respectively corresponds to base, shoulder, elbow, and hand.
  • cmd: ESP-NOW control information type
    • When cmd is 0, the controlled robotic arm will rotate to the given angle of each joint.
    • When cmd is 1, b, s, e, and h are invalid, input the JSON command that will not cause blocking, and the robotic arm will execute the corresponding function of JSON commands.
  • megs: the corresponding variable message.

Note: Whether it is multicast, or unicast/broadcast, the MAC address of the controlled device should be added to peerList first.

ESP-NOW Usage

After learning the specific meaning and usage of JSON commands, you can learn the following introduces the usage of broadcast control, unicast control, and multicast control. Here we take four robotic arms as an example, and these four robotic arms are respectively labeled as A, B, C, and D.

Broadcast Control

In this part, we use the robotic arm A to control B, C, and D to swing. It is not necessary to obtain the MAC address of robotic arms B, C, and D for broadcast control, and it is sufficient to use the broadcast MAC address, and the example is as follows: Connect the robotic arm A to the PC with a USB cable through the Type-C connector, and communicate with the robotic arm A on the computer through the serial port. We can use the serial port debugging assistant or the UART monitoring of the Arduino IDE to communicate with the robotic arm A or use the Python serial communication tutorial to communicate with the robotic arm A.
First, add the broadcast MAC address first. Use the following command to add the broadcast MAC address to the peerList of robot arm A:

{"T":303,"mac":"FF:FF:FF:FF:FF:FF"}

Once added, you can send JSON command messages to robotic arms B, C, and D using the commands for broadcast control.
For example, use the following command to send the command to turn on the LED to robotic arms B, C, and D via robotic arm A.

{"T":306,"mac":"FF:FF:FF:FF:FF:FF","dev":0,"b":0,"s":0,"e":0,"h":0,"cmd":1,"megs":"{\"T\":114,\"led\":255}"}

After the command is successfully sent, the LEDs of robotic arms B, C, and D will be on.
The above is just an example of sending the signal for broadcasting control, you can also send other JSON commands to robotic arms B, C, and D. Please refer to the meaning of broadcasting control commands of ESP-NOW function for specific modification.
Next, use the following command to turn off the torque lock on the robotic arm A, then you can manually swing it.

{"T":210,"cmd":0}

You can turn on [stream broadcast Leader] mode by setting the "ESP-NOW working method" of the robotic arm A. (When there is only the broadcast MAC address on the peerlist, the original peerlist mode is the broadcast mode.)

{"T":301,"mode":3}

Note: All of the above JSON commands are communicating with Robot Arm A.

Unicast Control

In this part, we introduce how the robotic arm A controls the robotic arm B only. In the unicast control mode, it requires the MAC address of the controlled device, that is, the MAC address of the robotic arm B. The MAC address can be obtained from the OLED screen of the robotic arm B, such as BB:BB:BB:BB:BB:BB.
Connect the robotic arm A to the PC with a USB cable through the Type-C connector, and communicate with the robotic arm A on the computer through the serial port. We can use the serial port debugging assistant or the Arduino IDE's serial port monitor to communicate with robotic arm A. We can also use the Python serial communication tutorial to communicate with robotic arm A.
First, add the MAC address of robotic arm B. Use the following command to add the MAC address of B to the robotic arm A's peerList:

{"T":303,"mac":"BB:BB:BB:BB:BB:BB"}
#The Mac address is specifically changed to the MAC address of the device you want to control.

After adding successfully, use the command of unicast control to send a JSON command message to the robotic arm. Note: If the MAC address of robotic arm B has been added before, adding it again will show Add Failure due to a duplicate MAC address, but as long as the MAC address of robotic arm B is correct, it will still be determined as the designated controlled device for unicast control. When the [Stream Unicast Leader] mode is turned on, only the robotic arm B will follow the joint position information from robotic arm A.
For example, using the following command of turning on the LED to the robotic arm B.

{"T":306,"mac":"BB:BB:BB:BB:BB:BB","dev":0,"b":0,"s":0,"e":0,"h":0,"cmd":1,"megs":"{\"T\":114,\"led\":255}"}

If you have operated the broadcast control, the LED of the robotic arm B is on, and you can use the following command to turn off the LED of the robotic arm B.

{"T":306,"mac":"BB:BB:BB:BB:BB:BB","dev":0,"b":0,"s":0,"e":0,"h":0,"cmd":1,"megs":"{\"T\":114,\"led\":0}"}

Next, you can use the following command to turn off the torque lock of the robotic am A, then you can manually swing the robotic arm A.

{"T":210,"cmd":0}

After setting the "ESP-NOW working method" of the robotic arm A to enable [stream unicast Leader] mode. If you operate the broadcast control, the new MAC address added is the robotic arm B's. Hence, the [stream unicast Leader] mode only sends the joint angle information to the robotic arm B.

{"T":301,"mode":2}

Now, the robotic arm B will swing to the same position as the robotic arm A. When rotating the robotic arm A, the robotic arm B will follow while the robotic arm C and D will not move.
NoteL the above JSON commands all are the communication with the robotic arm A.

Broadcast Control

In this part, we introduce how the robotic arm A controls the robotic arm B and C to swing. In the multicast control, it requires the MAC address of the controlled device, that is, the MAC address of the robotic arms B and C. The MAC address can be obtained from the OLED screen of the robotic arm B and robotic arm C, such as BB:BB:BB:BB:BB:BB and CC:CC:CC:CC:CC:CC.

Connect the robotic arm A to the PC with a USB cable through the Type-C connector, and communicate with the robotic arm A on the computer through the serial port. We can use the serial port debugging assistant or the Arduino IDE's serial port monitor to communicate with robotic arm A. We can also use the Python serial communication tutorial to communicate with robotic arm A.

First, add the MAC addresses of the robotic arm B and C. Use the following commands to add the MAC addresses of the robotic arm B and C to the peerList of the robotic arm A:

{"T":303,"mac":"BB:BB:BB:BB:BB:BB"}
{"T":303,"mac":"CC:CC:CC:CC:CC:CC"}
#Modify the mac to the MAC addresses of the two devices to be controlled

If you have operated the unicast control, you have added the MAC address of B to the peerList, then executing the JSON command to add the MAC address of robotic arm B may report an error, so just ignore it.
If you have added a broadcast address, you need to remove the broadcast address. Use the following command to remove the broadcast address from the peerList of robotic arm A:

{"T":304,"mac":"FF:FF:FF:FF:FF:FF"}

You can send the following commands of turn on the LED to the robotic arms B and C.

{"T":305,"dev":0,"b":0,"s":0,"e":0,"h":0,"cmd":1,"megs":"{\"T\":114,\"led\":255}"}

If the LED is on, you can use the following commands to turn off the LED of the robotics B and C.

{"T":305,"dev":0,"b":0,"s":0,"e":0,"h":0,"cmd":1,"megs":"{\"T\":114,\"led\":0}"}

Next, you can use the following command to turn off the torque lock of the robotic arm A, and then you can manually swing the robotic arm A.

{"T":210,"cmd":0}

Turn on [stream multicast Leader] mode through setting the ESPP-NOW operation mode of the robotic arm A, and then the angle information fo the robotic arm A will be sent to the robotic arms B and C.

{"T":301,"mode":1}

Now, the robotic arms B and C will be in the same position as the robotic arm A's. If you rotate the joint of the robotic arm A, the robotic arms B and C will follow.
Note: the above JSON commands all communicate with the robotic arm A.

RoArm-M2-S Tutorial Directory

RoArm-M2-S User Tutorial

RoArm-M2-S ROS2 Basic Tutorial